UNPKG

7.86 kBJavaScriptView Raw
1// Generated by LiveScript 1.6.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: +it
108 };
109 },
110 Float: function(it){
111 return {
112 type: 'Just',
113 value: +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 replace, that;
166 if (toString$.call(it).slice(8, -1) !== 'String') {
167 return {
168 type: 'Nothing'
169 };
170 }
171 replace = function(value, quote){
172 return value.replace(/\\([^u]|u[0-9a-fA-F]{4})/g, function(all, escaped){
173 switch (escaped[0]) {
174 case quote:
175 return quote;
176 case '\\':
177 return '\\';
178 case 'b':
179 return '\b';
180 case 'f':
181 return '\f';
182 case 'n':
183 return '\n';
184 case 'r':
185 return '\r';
186 case 't':
187 return '\t';
188 case 'u':
189 return JSON.parse("\"" + all + "\"");
190 default:
191 return escaped;
192 }
193 });
194 };
195 if (that = it.match(/^'([\s\S]*)'$/)) {
196 return {
197 type: 'Just',
198 value: replace(that[1], "'")
199 };
200 } else if (that = it.match(/^"([\s\S]*)"$/)) {
201 return {
202 type: 'Just',
203 value: replace(that[1], '"')
204 };
205 } else {
206 return {
207 type: 'Just',
208 value: it
209 };
210 }
211 }
212 };
213 function castArray(node, type, options){
214 var typeOf, element;
215 if (toString$.call(node).slice(8, -1) !== 'Array') {
216 return {
217 type: 'Nothing'
218 };
219 }
220 typeOf = type.of;
221 return {
222 type: 'Just',
223 value: (function(){
224 var i$, ref$, len$, results$ = [];
225 for (i$ = 0, len$ = (ref$ = node).length; i$ < len$; ++i$) {
226 element = ref$[i$];
227 results$.push(typesCast(element, typeOf, options));
228 }
229 return results$;
230 }())
231 };
232 }
233 function castTuple(node, type, options){
234 var result, i, i$, ref$, len$, types, cast;
235 if (toString$.call(node).slice(8, -1) !== 'Array') {
236 return {
237 type: 'Nothing'
238 };
239 }
240 result = [];
241 i = 0;
242 for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {
243 types = ref$[i$];
244 cast = typesCast(node[i], types, options);
245 if (toString$.call(cast).slice(8, -1) !== 'Undefined') {
246 result.push(cast);
247 }
248 i++;
249 }
250 if (node.length <= i) {
251 return {
252 type: 'Just',
253 value: result
254 };
255 } else {
256 return {
257 type: 'Nothing'
258 };
259 }
260 }
261 function castFields(node, type, options){
262 var typeOf, key, value;
263 if (toString$.call(node).slice(8, -1) !== 'Object') {
264 return {
265 type: 'Nothing'
266 };
267 }
268 typeOf = type.of;
269 return {
270 type: 'Just',
271 value: (function(){
272 var ref$, resultObj$ = {};
273 for (key in ref$ = node) {
274 value = ref$[key];
275 resultObj$[typesCast(key, [{
276 type: 'String'
277 }], options)] = typesCast(value, typeOf[key] || [{
278 type: '*'
279 }], options);
280 }
281 return resultObj$;
282 }())
283 };
284 }
285 function typeCast(node, typeObj, options){
286 var type, structure, castFunc, ref$;
287 type = typeObj.type, structure = typeObj.structure;
288 if (type) {
289 castFunc = ((ref$ = options.customTypes[type]) != null ? ref$.cast : void 8) || types[type];
290 if (!castFunc) {
291 throw new Error("Type not defined: " + type + ".");
292 }
293 return castFunc(node, options, typesCast);
294 } else {
295 switch (structure) {
296 case 'array':
297 return castArray(node, typeObj, options);
298 case 'tuple':
299 return castTuple(node, typeObj, options);
300 case 'fields':
301 return castFields(node, typeObj, options);
302 }
303 }
304 }
305 function typesCast(node, types, options){
306 var i$, len$, type, ref$, valueType, value;
307 for (i$ = 0, len$ = types.length; i$ < len$; ++i$) {
308 type = types[i$];
309 ref$ = typeCast(node, type, options), valueType = ref$.type, value = ref$.value;
310 if (valueType === 'Nothing') {
311 continue;
312 }
313 if (parsedTypeCheck([type], value, {
314 customTypes: options.customTypes
315 })) {
316 return value;
317 }
318 }
319 throw new Error("Value " + JSON.stringify(node) + " does not type check against " + JSON.stringify(types) + ".");
320 }
321 module.exports = function(node, types, options){
322 if (!options.explicit && types.length === 1 && types[0].type === 'String') {
323 return node;
324 }
325 return typesCast(node, types, options);
326 };
327}).call(this);