1 |
|
2 | (function(){
|
3 | var ref$, any, all, isItNaN, types, defaultType, toString$ = {}.toString;
|
4 | ref$ = require('prelude-ls'), any = ref$.any, all = ref$.all, isItNaN = ref$.isItNaN;
|
5 | types = {
|
6 | Number: {
|
7 | typeOf: 'Number',
|
8 | validate: function(it){
|
9 | return !isItNaN(it);
|
10 | }
|
11 | },
|
12 | NaN: {
|
13 | typeOf: 'Number',
|
14 | validate: isItNaN
|
15 | },
|
16 | Int: {
|
17 | typeOf: 'Number',
|
18 | validate: function(it){
|
19 | return !isItNaN(it) && it % 1 === 0;
|
20 | }
|
21 | },
|
22 | Float: {
|
23 | typeOf: 'Number',
|
24 | validate: function(it){
|
25 | return !isItNaN(it);
|
26 | }
|
27 | },
|
28 | Date: {
|
29 | typeOf: 'Date',
|
30 | validate: function(it){
|
31 | return !isItNaN(it.getTime());
|
32 | }
|
33 | }
|
34 | };
|
35 | defaultType = {
|
36 | array: 'Array',
|
37 | tuple: 'Array'
|
38 | };
|
39 | function checkArray(input, type, options){
|
40 | return all(function(it){
|
41 | return checkMultiple(it, type.of, options);
|
42 | }, input);
|
43 | }
|
44 | function checkTuple(input, type, options){
|
45 | var i, i$, ref$, len$, types;
|
46 | i = 0;
|
47 | for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {
|
48 | types = ref$[i$];
|
49 | if (!checkMultiple(input[i], types, options)) {
|
50 | return false;
|
51 | }
|
52 | i++;
|
53 | }
|
54 | return input.length <= i;
|
55 | }
|
56 | function checkFields(input, type, options){
|
57 | var inputKeys, numInputKeys, k, numOfKeys, key, ref$, types;
|
58 | inputKeys = {};
|
59 | numInputKeys = 0;
|
60 | for (k in input) {
|
61 | inputKeys[k] = true;
|
62 | numInputKeys++;
|
63 | }
|
64 | numOfKeys = 0;
|
65 | for (key in ref$ = type.of) {
|
66 | types = ref$[key];
|
67 | if (!checkMultiple(input[key], types, options)) {
|
68 | return false;
|
69 | }
|
70 | if (inputKeys[key]) {
|
71 | numOfKeys++;
|
72 | }
|
73 | }
|
74 | return type.subset || numInputKeys === numOfKeys;
|
75 | }
|
76 | function checkStructure(input, type, options){
|
77 | if (!(input instanceof Object)) {
|
78 | return false;
|
79 | }
|
80 | switch (type.structure) {
|
81 | case 'fields':
|
82 | return checkFields(input, type, options);
|
83 | case 'array':
|
84 | return checkArray(input, type, options);
|
85 | case 'tuple':
|
86 | return checkTuple(input, type, options);
|
87 | }
|
88 | }
|
89 | function check(input, typeObj, options){
|
90 | var type, structure, setting, that;
|
91 | type = typeObj.type, structure = typeObj.structure;
|
92 | if (type) {
|
93 | if (type === '*') {
|
94 | return true;
|
95 | }
|
96 | setting = options.customTypes[type] || types[type];
|
97 | if (setting) {
|
98 | return (setting.typeOf === void 8 || setting.typeOf === toString$.call(input).slice(8, -1)) && setting.validate(input);
|
99 | } else {
|
100 | return type === toString$.call(input).slice(8, -1) && (!structure || checkStructure(input, typeObj, options));
|
101 | }
|
102 | } else if (structure) {
|
103 | if (that = defaultType[structure]) {
|
104 | if (that !== toString$.call(input).slice(8, -1)) {
|
105 | return false;
|
106 | }
|
107 | }
|
108 | return checkStructure(input, typeObj, options);
|
109 | } else {
|
110 | throw new Error("No type defined. Input: " + input + ".");
|
111 | }
|
112 | }
|
113 | function checkMultiple(input, types, options){
|
114 | if (toString$.call(types).slice(8, -1) !== 'Array') {
|
115 | throw new Error("Types must be in an array. Input: " + input + ".");
|
116 | }
|
117 | return any(function(it){
|
118 | return check(input, it, options);
|
119 | }, types);
|
120 | }
|
121 | module.exports = function(parsedType, input, options){
|
122 | options == null && (options = {});
|
123 | if (options.customTypes == null) {
|
124 | options.customTypes = {};
|
125 | }
|
126 | return checkMultiple(input, parsedType, options);
|
127 | };
|
128 | }).call(this);
|