Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80690 views
1
// Generated by LiveScript 1.2.0
2
(function(){
3
var parsedTypeCheck, types, toString$ = {}.toString;
4
parsedTypeCheck = require('type-check').parsedTypeCheck;
5
types = {
6
'*': function(value, options){
7
switch (toString$.call(value).slice(8, -1)) {
8
case 'Array':
9
return typeCast(value, {
10
type: 'Array'
11
}, options);
12
case 'Object':
13
return typeCast(value, {
14
type: 'Object'
15
}, options);
16
default:
17
return {
18
type: 'Just',
19
value: typesCast(value, [
20
{
21
type: 'Undefined'
22
}, {
23
type: 'Null'
24
}, {
25
type: 'NaN'
26
}, {
27
type: 'Boolean'
28
}, {
29
type: 'Number'
30
}, {
31
type: 'Date'
32
}, {
33
type: 'RegExp'
34
}, {
35
type: 'Array'
36
}, {
37
type: 'Object'
38
}, {
39
type: 'String'
40
}
41
], (options.explicit = true, options))
42
};
43
}
44
},
45
Undefined: function(it){
46
if (it === 'undefined' || it === void 8) {
47
return {
48
type: 'Just',
49
value: void 8
50
};
51
} else {
52
return {
53
type: 'Nothing'
54
};
55
}
56
},
57
Null: function(it){
58
if (it === 'null') {
59
return {
60
type: 'Just',
61
value: null
62
};
63
} else {
64
return {
65
type: 'Nothing'
66
};
67
}
68
},
69
NaN: function(it){
70
if (it === 'NaN') {
71
return {
72
type: 'Just',
73
value: NaN
74
};
75
} else {
76
return {
77
type: 'Nothing'
78
};
79
}
80
},
81
Boolean: function(it){
82
if (it === 'true') {
83
return {
84
type: 'Just',
85
value: true
86
};
87
} else if (it === 'false') {
88
return {
89
type: 'Just',
90
value: false
91
};
92
} else {
93
return {
94
type: 'Nothing'
95
};
96
}
97
},
98
Number: function(it){
99
return {
100
type: 'Just',
101
value: +it
102
};
103
},
104
Int: function(it){
105
return {
106
type: 'Just',
107
value: parseInt(it)
108
};
109
},
110
Float: function(it){
111
return {
112
type: 'Just',
113
value: parseFloat(it)
114
};
115
},
116
Date: function(value, options){
117
var that;
118
if (that = /^\#([\s\S]*)\#$/.exec(value)) {
119
return {
120
type: 'Just',
121
value: new Date(+that[1] || that[1])
122
};
123
} else if (options.explicit) {
124
return {
125
type: 'Nothing'
126
};
127
} else {
128
return {
129
type: 'Just',
130
value: new Date(+value || value)
131
};
132
}
133
},
134
RegExp: function(value, options){
135
var that;
136
if (that = /^\/([\s\S]*)\/([gimy]*)$/.exec(value)) {
137
return {
138
type: 'Just',
139
value: new RegExp(that[1], that[2])
140
};
141
} else if (options.explicit) {
142
return {
143
type: 'Nothing'
144
};
145
} else {
146
return {
147
type: 'Just',
148
value: new RegExp(value)
149
};
150
}
151
},
152
Array: function(value, options){
153
return castArray(value, {
154
of: [{
155
type: '*'
156
}]
157
}, options);
158
},
159
Object: function(value, options){
160
return castFields(value, {
161
of: {}
162
}, options);
163
},
164
String: function(it){
165
var that;
166
if (toString$.call(it).slice(8, -1) !== 'String') {
167
return {
168
type: 'Nothing'
169
};
170
}
171
if (that = it.match(/^'([\s\S]*)'$/)) {
172
return {
173
type: 'Just',
174
value: that[1].replace(/\\'/g, "'")
175
};
176
} else if (that = it.match(/^"([\s\S]*)"$/)) {
177
return {
178
type: 'Just',
179
value: that[1].replace(/\\"/g, '"')
180
};
181
} else {
182
return {
183
type: 'Just',
184
value: it
185
};
186
}
187
}
188
};
189
function castArray(node, type, options){
190
var typeOf, element;
191
if (toString$.call(node).slice(8, -1) !== 'Array') {
192
return {
193
type: 'Nothing'
194
};
195
}
196
typeOf = type.of;
197
return {
198
type: 'Just',
199
value: (function(){
200
var i$, ref$, len$, results$ = [];
201
for (i$ = 0, len$ = (ref$ = node).length; i$ < len$; ++i$) {
202
element = ref$[i$];
203
results$.push(typesCast(element, typeOf, options));
204
}
205
return results$;
206
}())
207
};
208
}
209
function castTuple(node, type, options){
210
var result, i, i$, ref$, len$, types, cast;
211
if (toString$.call(node).slice(8, -1) !== 'Array') {
212
return {
213
type: 'Nothing'
214
};
215
}
216
result = [];
217
i = 0;
218
for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {
219
types = ref$[i$];
220
cast = typesCast(node[i], types, options);
221
if (toString$.call(cast).slice(8, -1) !== 'Undefined') {
222
result.push(cast);
223
}
224
i++;
225
}
226
if (node.length <= i) {
227
return {
228
type: 'Just',
229
value: result
230
};
231
} else {
232
return {
233
type: 'Nothing'
234
};
235
}
236
}
237
function castFields(node, type, options){
238
var typeOf, key, value;
239
if (toString$.call(node).slice(8, -1) !== 'Object') {
240
return {
241
type: 'Nothing'
242
};
243
}
244
typeOf = type.of;
245
return {
246
type: 'Just',
247
value: (function(){
248
var ref$, results$ = {};
249
for (key in ref$ = node) {
250
value = ref$[key];
251
results$[typesCast(key, [{
252
type: 'String'
253
}], options)] = typesCast(value, typeOf[key] || [{
254
type: '*'
255
}], options);
256
}
257
return results$;
258
}())
259
};
260
}
261
function typeCast(node, typeObj, options){
262
var type, structure, castFunc, ref$;
263
type = typeObj.type, structure = typeObj.structure;
264
if (type) {
265
castFunc = ((ref$ = options.customTypes[type]) != null ? ref$.cast : void 8) || types[type];
266
if (!castFunc) {
267
throw new Error("Type not defined: " + type + ".");
268
}
269
return castFunc(node, options, typesCast);
270
} else {
271
switch (structure) {
272
case 'array':
273
return castArray(node, typeObj, options);
274
case 'tuple':
275
return castTuple(node, typeObj, options);
276
case 'fields':
277
return castFields(node, typeObj, options);
278
}
279
}
280
}
281
function typesCast(node, types, options){
282
var i$, len$, type, ref$, valueType, value;
283
for (i$ = 0, len$ = types.length; i$ < len$; ++i$) {
284
type = types[i$];
285
ref$ = typeCast(node, type, options), valueType = ref$.type, value = ref$.value;
286
if (valueType === 'Nothing') {
287
continue;
288
}
289
if (parsedTypeCheck([type], value, {
290
customTypes: options.customTypes
291
})) {
292
return value;
293
}
294
}
295
throw new Error("Value " + JSON.stringify(node) + " does not type check against " + JSON.stringify(types) + ".");
296
}
297
module.exports = typesCast;
298
}).call(this);
299
300