-
Notifications
You must be signed in to change notification settings - Fork 53
/
coretypes.js
295 lines (240 loc) · 9.4 KB
/
coretypes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// I had made this a function (constructor) but there wasn't really any reason.
Rx.CoreType = {};
// Simple types
Rx.CoreType.allType = function (opt, rx) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true, of: true }))
throw new Rx.Error('unknown argument for all type');
if (! opt.of) throw new Rx.Error('no of given for //all');
if (opt.of.length == 0)
throw new Rx.Error('no alternatives given for //all of');
this.alts = [ ];
for (i in opt.of) this.alts.push( rx.makeSchema(opt.of[i]) )
};
Rx.CoreType.allType.uri = 'tag:codesimply.com,2008:rx/core/all';
Rx.CoreType.allType.prototype.check = function (v) {
for (i in this.alts) if (! this.alts[i].check(v)) return false;
return true;
};
Rx.CoreType.anyType = function (opt, rx) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true, of: true }))
throw new Rx.Error('unknown argument for any type');
this.alts = null;
if (opt.of) {
if (opt.of.length == 0)
throw new Rx.Error('no alternatives given for //any of');
this.alts = [ ];
for (i in opt.of) this.alts.push( rx.makeSchema(opt.of[i]) )
}
};
Rx.CoreType.anyType.uri = 'tag:codesimply.com,2008:rx/core/any';
Rx.CoreType.anyType.prototype.check = function (v) {
if (! this.alts) return true;
for (i in this.alts) if (this.alts[i].check(v)) return true;
return false;
};
Rx.CoreType.boolType = function (opt) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
throw new Rx.Error('unknown argument for bool type');
};
Rx.CoreType.boolType.uri = 'tag:codesimply.com,2008:rx/core/bool';
Rx.CoreType.boolType.prototype.check = function (v) {
return((typeof(v) == 'boolean') || (v instanceof Boolean));
};
Rx.CoreType.defType = function (opt) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
throw new Rx.Error('unknown argument for def type');
};
Rx.CoreType.defType.uri = 'tag:codesimply.com,2008:rx/core/def';
Rx.CoreType.defType.prototype.check = function (v) { return v != null; };
Rx.CoreType.failType = function (opt) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
throw new Rx.Error('unknown argument for fail type');
};
Rx.CoreType.failType.uri = 'tag:codesimply.com,2008:rx/core/fail';
Rx.CoreType.failType.prototype.check = function (v) { false; };
Rx.CoreType.intType = function (opt) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true, range: true, value: true }))
throw new Rx.Error('unknown argument for int type');
if (typeof(opt.value) != "undefined")
if (opt.value.constructor != Number || opt.value % 1 != 0)
throw new Rx.Error('invalid value parameter for int type');
this.value = opt.value;
if (opt.range) {
this.range_check = new Rx.Util.RangeChecker( opt.range );
}
};
Rx.CoreType.intType.uri = 'tag:codesimply.com,2008:rx/core/int';
Rx.CoreType.intType.prototype.check = function (v) {
if (v == null) return false;
if (v.constructor != Number) return false;
if (Math.floor(v) != v) return false;
if (this.value != null && v != this.value) return false;
if (this.range_check && ! this.range_check.check(v)) return false;
return true;
};
Rx.CoreType.nilType = function (opt) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
throw new Rx.Error('unknown argument for nil type');
};
Rx.CoreType.nilType.uri = 'tag:codesimply.com,2008:rx/core/nil';
Rx.CoreType.nilType.prototype.check = function (v) { return v === null };
Rx.CoreType.numType = function (opt) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true, range: true, value: true }))
throw new Rx.Error('unknown argument for num type');
if (typeof(opt.value) != "undefined")
if (opt.value.constructor != Number)
throw new Rx.Error('invalid value parameter for str type');
this.value = opt.value;
if (opt.range) {
this.range_check = new Rx.Util.RangeChecker( opt.range );
}
};
Rx.CoreType.numType.uri = 'tag:codesimply.com,2008:rx/core/num';
Rx.CoreType.numType.prototype.check = function (v) {
if (v == null) return false;
if (v.constructor != Number) return false;
if (this.range_check && ! this.range_check.check(v)) return false;
if (this.value != null && v != this.value) return false;
return true;
};
Rx.CoreType.strType = function (opt) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true, value: true, length: true }))
throw new Rx.Error('unknown argument for str type');
if (typeof(opt.value) != "undefined")
if (opt.value.constructor != String)
throw new Rx.Error('invalid value parameter for str type');
this.value = opt.value;
if (opt.length) {
this.length_check = new Rx.Util.RangeChecker( opt.length );
}
};
Rx.CoreType.strType.uri = 'tag:codesimply.com,2008:rx/core/str';
Rx.CoreType.strType.prototype.check = function (v) {
if (! ((typeof(v) == 'string') || (v instanceof String))) return false;
if (this.value != null && v != this.value) return false;
if (this.length_check && ! this.length_check.check(v.length)) {
return false;
}
return true;
};
Rx.CoreType.oneType = function (opt) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true }))
throw new Rx.Error('unknown argument for one type');
};
Rx.CoreType.oneType.uri = 'tag:codesimply.com,2008:rx/core/one';
Rx.CoreType.oneType.prototype.check = function (v) {
// for some reason this was false: (false instanceof Boolean)
if (v == null) return false;
return (
(v.constructor == String)
|| (v.constructor == Boolean)
|| (v.constructor == Number)
);
};
// Complex types
Rx.CoreType.arrType = function (opt, rx) {
if (! Rx.Util._x_subset_keys_y(opt, { type: 1, contents: 1, length: 1 }))
throw new Rx.Error('unknown argument for arr type');
if (! opt.contents) throw new Rx.Error('no contents argument for arr type');
if ((opt.contents.constructor != String) && (! opt.contents.type))
throw new Rx.Error('contents arg for arr type must declare a type');
this.content_check = rx.makeSchema(opt.contents);
if (opt.length) {
this.length_check = new Rx.Util.RangeChecker( opt.length );
}
};
Rx.CoreType.arrType.uri = 'tag:codesimply.com,2008:rx/core/arr';
Rx.CoreType.arrType.prototype.check = function (v) {
if (! (v instanceof Array)) return false;
for (i in v) if (! this.content_check.check(v[i])) return false;
if (this.length_check && ! this.length_check.check(v.length)) {
return false;
}
return true;
}
Rx.CoreType.recType = function (opt, rx) {
if (! Rx.Util._x_subset_keys_y(opt, {type: true, rest: true,
required: true, optional: true }))
throw new Rx.Error('unknown argument for rec type');
this.known = {};
if (opt.required) {
this.required = {};
for (prop in opt.required) {
this.known[prop] = true;
if (opt.optional && opt.optional[prop])
throw new Rx.Error(prop + ' appears in both optional and required');
this.required[prop] = rx.makeSchema(opt.required[prop]);
}
}
if (opt.optional) {
this.optional = {};
for (prop in opt.optional) {
this.known[prop] = true;
this.optional[prop] = rx.makeSchema(opt.optional[prop]);
}
}
if (opt.rest) this.restSchema = rx.makeSchema(opt.rest);
};
Rx.CoreType.recType.uri = 'tag:codesimply.com,2008:rx/core/rec';
Rx.CoreType.recType.prototype.check = function (v) {
if (!(((v != null) && (typeof(v) == 'object')) && ! (v instanceof Array)))
return false;
var rest = {};
var have_rest = false;
for (prop in v) if (! this.known[prop]) {
have_rest = true;
rest[ prop ] = v[ prop ];
}
if (have_rest && ! this.restSchema) return false
for (prop in this.required) {
if (v[prop] == null) return false;
if (! this.required[prop].check( v[prop] ) ) return false;
}
for (prop in this.optional) {
if (v[prop] == null) continue;
if (! this.optional[prop].check( v[prop] ) ) return false;
}
if (have_rest && ! this.restSchema.check(rest)) return false;
return true;
};
Rx.CoreType.mapType = function (opt, rx) {
if (! Rx.Util._x_subset_keys_y(opt, { type: true, values: true }))
throw new Rx.Error('unknown argument for map type');
this.valueSchema = rx.makeSchema(opt.values);
};
Rx.CoreType.mapType.uri = 'tag:codesimply.com,2008:rx/core/map';
Rx.CoreType.mapType.prototype.check = function (v) {
if (!(((v != null) && (typeof(v) == 'object')) && ! (v instanceof Array)))
return false;
for (prop in v) if (! this.valueSchema.check(v[prop])) return false;
return true;
};
Rx.CoreType.seqType = function (opt, rx) {
if (! Rx.Util._x_subset_keys_y(opt, { type: 1, contents: 1, tail: 1 }))
throw new Rx.Error('unknown argument for seq type');
if (! opt.contents) throw new Rx.Error('no contents argument for seq type');
if (! (opt.contents instanceof Array))
throw new Rx.Error('contents argument for seq type must be an Array');
this.content_checks = [];
for (i in opt.contents)
this.content_checks[i] = rx.makeSchema(opt.contents[i]);
if (opt.tail) {
this.tail_check = rx.makeSchema(opt.tail);
}
};
Rx.CoreType.seqType.uri = 'tag:codesimply.com,2008:rx/core/seq';
Rx.CoreType.seqType.prototype.check = function (v) {
if (!(v instanceof Array)) return false;
if (v.length < this.content_checks.length) return false;
if (v.length > this.content_checks.length) {
if (this.tail_check) {
var tail = v.slice(this.content_checks.length, v.length);
if (! this.tail_check.check( tail )) return false;
} else {
return false;
}
}
for (i in this.content_checks)
if (! this.content_checks[i].check(v[i])) return false;
return true;
};