Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80621 views
1
// Generated by LiveScript 1.2.0
2
(function(){
3
var ref$, id, reject, parsedTypeCheck, types, tokenRegex, toString$ = {}.toString;
4
ref$ = require('prelude-ls'), id = ref$.id, reject = ref$.reject;
5
parsedTypeCheck = require('type-check').parsedTypeCheck;
6
types = {
7
'*': function(it){
8
switch (toString$.call(it).slice(8, -1)) {
9
case 'Array':
10
return coerceType(it, {
11
type: 'Array'
12
});
13
case 'Object':
14
return coerceType(it, {
15
type: 'Object'
16
});
17
default:
18
return {
19
type: 'Just',
20
value: coerceTypes(it, [
21
{
22
type: 'Undefined'
23
}, {
24
type: 'Null'
25
}, {
26
type: 'Boolean'
27
}, {
28
type: 'Number'
29
}, {
30
type: 'Date'
31
}, {
32
type: 'RegExp'
33
}, {
34
type: 'Array'
35
}, {
36
type: 'Object'
37
}, {
38
type: 'String'
39
}
40
], {
41
explicit: true
42
})
43
};
44
}
45
},
46
Undefined: function(it){
47
if (it === 'undefined') {
48
return {
49
type: 'Just',
50
value: void 8
51
};
52
} else {
53
return {
54
type: 'Nothing'
55
};
56
}
57
},
58
Null: function(it){
59
if (it === 'null') {
60
return {
61
type: 'Just',
62
value: null
63
};
64
} else {
65
return {
66
type: 'Nothing'
67
};
68
}
69
},
70
Boolean: function(it){
71
if (it === 'true') {
72
return {
73
type: 'Just',
74
value: true
75
};
76
} else if (it === 'false') {
77
return {
78
type: 'Just',
79
value: false
80
};
81
} else {
82
return {
83
type: 'Nothing'
84
};
85
}
86
},
87
Number: function(it){
88
return {
89
type: 'Just',
90
value: +it
91
};
92
},
93
Int: function(it){
94
return {
95
type: 'Just',
96
value: parseInt(it)
97
};
98
},
99
Float: function(it){
100
return {
101
type: 'Just',
102
value: parseFloat(it)
103
};
104
},
105
Date: function(value, options){
106
var that;
107
if (that = /^\#(.*)\#$/.exec(value)) {
108
return {
109
type: 'Just',
110
value: new Date(+that[1] || that[1])
111
};
112
} else if (options.explicit) {
113
return {
114
type: 'Nothing'
115
};
116
} else {
117
return {
118
type: 'Just',
119
value: new Date(+value || value)
120
};
121
}
122
},
123
RegExp: function(value, options){
124
var that;
125
if (that = /^\/(.*)\/([gimy]*)$/.exec(value)) {
126
return {
127
type: 'Just',
128
value: new RegExp(that[1], that[2])
129
};
130
} else if (options.explicit) {
131
return {
132
type: 'Nothing'
133
};
134
} else {
135
return {
136
type: 'Just',
137
value: new RegExp(value)
138
};
139
}
140
},
141
Array: function(it){
142
return coerceArray(it, {
143
of: [{
144
type: '*'
145
}]
146
});
147
},
148
Object: function(it){
149
return coerceFields(it, {
150
of: {}
151
});
152
},
153
String: function(it){
154
var that;
155
if (that = it.match(/^'(.*)'$/)) {
156
return {
157
type: 'Just',
158
value: that[1]
159
};
160
} else if (that = it.match(/^"(.*)"$/)) {
161
return {
162
type: 'Just',
163
value: that[1]
164
};
165
} else {
166
return {
167
type: 'Just',
168
value: it
169
};
170
}
171
}
172
};
173
function coerceArray(node, type){
174
var typeOf, element;
175
if (toString$.call(node).slice(8, -1) !== 'Array') {
176
return {
177
type: 'Nothing'
178
};
179
}
180
typeOf = type.of;
181
return {
182
type: 'Just',
183
value: (function(){
184
var i$, ref$, len$, results$ = [];
185
for (i$ = 0, len$ = (ref$ = node).length; i$ < len$; ++i$) {
186
element = ref$[i$];
187
results$.push(coerceTypes(element, typeOf));
188
}
189
return results$;
190
}())
191
};
192
}
193
function coerceTuple(node, type){
194
var i, types;
195
if (toString$.call(node).slice(8, -1) !== 'Array') {
196
return {
197
type: 'Nothing'
198
};
199
}
200
return {
201
type: 'Just',
202
value: (function(){
203
var i$, ref$, len$, results$ = [];
204
for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {
205
i = i$;
206
types = ref$[i$];
207
results$.push(coerceTypes(node[i], types));
208
}
209
return results$;
210
}())
211
};
212
}
213
function coerceFields(node, type){
214
var typeOf, key, value;
215
if (toString$.call(node).slice(8, -1) !== 'Object') {
216
return {
217
type: 'Nothing'
218
};
219
}
220
typeOf = type.of;
221
return {
222
type: 'Just',
223
value: (function(){
224
var ref$, results$ = {};
225
for (key in ref$ = node) {
226
value = ref$[key];
227
results$[key] = coerceTypes(value, typeOf[key] || [{
228
type: '*'
229
}]);
230
}
231
return results$;
232
}())
233
};
234
}
235
function coerceType(node, typeObj, options){
236
var type, structure, coerceFunc;
237
type = typeObj.type, structure = typeObj.structure;
238
if (type) {
239
coerceFunc = types[type];
240
return coerceFunc(node, options);
241
} else {
242
switch (structure) {
243
case 'array':
244
return coerceArray(node, typeObj);
245
case 'tuple':
246
return coerceTuple(node, typeObj);
247
case 'fields':
248
return coerceFields(node, typeObj);
249
}
250
}
251
}
252
function coerceTypes(node, types, options){
253
var i$, len$, type, ref$, valueType, value;
254
options == null && (options = {});
255
for (i$ = 0, len$ = types.length; i$ < len$; ++i$) {
256
type = types[i$];
257
ref$ = coerceType(node, type, options), valueType = ref$.type, value = ref$.value;
258
if (valueType === 'Nothing') {
259
continue;
260
}
261
if (parsedTypeCheck([type], value)) {
262
return value;
263
}
264
}
265
throw new Error("Value '" + node + "' does not type check against " + JSON.stringify(types) + ".");
266
}
267
function consumeOp(tokens, op){
268
if (tokens[0] === op) {
269
return tokens.shift();
270
} else {
271
throw new Error("Expected '" + op + "', but got " + tokens[0] + " instead.");
272
}
273
}
274
function maybeConsumeOp(tokens, op){
275
if (tokens[0] === op) {
276
return tokens.shift();
277
}
278
}
279
function consumeList(tokens, delimiters, hasDelimiters){
280
var result;
281
if (hasDelimiters) {
282
consumeOp(tokens, delimiters[0]);
283
}
284
result = [];
285
while (tokens.length && tokens[0] !== delimiters[1]) {
286
result.push(consumeElement(tokens));
287
maybeConsumeOp(tokens, ',');
288
}
289
if (hasDelimiters) {
290
consumeOp(tokens, delimiters[1]);
291
}
292
return result;
293
}
294
function consumeArray(tokens, hasDelimiters){
295
return consumeList(tokens, ['[', ']'], hasDelimiters);
296
}
297
function consumeTuple(tokens, hasDelimiters){
298
return consumeList(tokens, ['(', ')'], hasDelimiters);
299
}
300
function consumeFields(tokens, hasDelimiters){
301
var result, key;
302
if (hasDelimiters) {
303
consumeOp(tokens, '{');
304
}
305
result = {};
306
while (tokens.length && (!hasDelimiters || tokens[0] !== '}')) {
307
key = tokens.shift();
308
consumeOp(tokens, ':');
309
result[key] = consumeElement(tokens);
310
maybeConsumeOp(tokens, ',');
311
}
312
if (hasDelimiters) {
313
consumeOp(tokens, '}');
314
}
315
return result;
316
}
317
function consumeElement(tokens){
318
switch (tokens[0]) {
319
case '[':
320
return consumeArray(tokens, true);
321
case '(':
322
return consumeTuple(tokens, true);
323
case '{':
324
return consumeFields(tokens, true);
325
default:
326
return tokens.shift();
327
}
328
}
329
function consumeTopLevel(tokens, types){
330
var structure, origTokens, result;
331
structure = types[0].structure;
332
if (types.length === 1 && structure) {
333
origTokens = tokens.slice();
334
result = structure === 'array'
335
? consumeArray(tokens, tokens[0] === '[')
336
: structure === 'tuple'
337
? consumeTuple(tokens, tokens[0] === '(')
338
: consumeFields(tokens, tokens[0] === '{');
339
if (tokens.length) {
340
return consumeElement(structure === 'array'
341
? ['['].concat(origTokens, [']'])
342
: ['('].concat(origTokens, [')']));
343
} else {
344
return result;
345
}
346
} else {
347
return consumeElement(tokens);
348
}
349
}
350
tokenRegex = /("(?:[^"]|\\")*")|('(?:[^']|\\')*')|(#.*#)|(\/(?:\\\/|[^\/])*\/[gimy]*)|([\[\]\(\)}{:,])|([-\.\$\w]+)|\s*/;
351
function coerce(types, string){
352
var tokens, node;
353
tokens = reject(function(it){
354
return !it || /^\s+$/.test(it);
355
}, string.split(tokenRegex));
356
node = consumeTopLevel(tokens, types);
357
if (!node) {
358
throw new Error("Error parsing " + string);
359
}
360
return coerceTypes(node, types);
361
}
362
module.exports = coerce;
363
/*
364
function log
365
console.log it; it
366
*/
367
}).call(this);
368
369