-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtypology.js
600 lines (549 loc) · 16.5 KB
/
typology.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/**
* PRIVATE GLOBALS:
* ****************
*/
/**
* This object is a dictionnary that maps "[object Something]" strings to the
* typology form "something":
*/
const __class2type = {};
/**
* This array is the list of every types considered native by typology:
*/
const __nativeTypes = { '*': true };
(function() {
let k;
let className;
const classes = [
'Boolean',
'Number',
'String',
'Object',
'Array',
'Function',
'Arguments',
'RegExp',
'Date',
// ES2015
'Map',
'Set',
'WeakMap',
'WeakSet',
'Symbol'
];
// Fill types
for (k in classes) {
className = classes[k];
__nativeTypes[className.toLowerCase()] = true;
__class2type['[object ' + className + ']'] = className.toLowerCase();
}
})();
/**
* This function returns the string native type of anything. The type is either
* `"null"`, `"undefined"`, `"object"` or a value from `__class2type`.
*
* @param {*} value The value to get the type of.
* @return {string} Returns the string native type of the input value.
*/
function __getNativeType(value) {
return value === null || value === undefined
? String(value)
: __class2type[Object.prototype.toString.call(value)] || 'object';
}
/**
* CONSTRUCTOR:
* ************
*/
function Typology(defs) {
/**
* INSTANCE PRIVATES:
* ******************
*/
const _self = this;
/**
* This objects will contain every instance-specific custom types:
*/
const _customTypes = {};
/**
* INSTANCE METHODS:
* *****************
*/
/**
* This function will recursively scan an object to check wether or not it
* matches a given type. It will return null if it matches, and an Error
* object else.
*
* Examples:
* *********
* 1. When the type matches:
* > types.scan('string', 'abc');
* will return an object like the following :
* {
* expected: 'string',
* type: 'string',
* value: 'abc'
* }
*
* 2. When a top-level type does not match:
* > types.scan('number', 'abc');
* will return an object with like the following :
* {
* error: 'Expected a "number" but found a "string".',
* expected: 'number',
* type: 'string',
* value: 'abc'
* }
*
* 3. When a sub-object type does not its type:
* > types.scan({ a: 'number' }, { a: 'abc' });
* will return an object like the following :
* {
* error: 'Expected a "number" but found a "string".',
* expected: 'number',
* type: 'string',
* value: 'abc',
* path: [ 'a' ]
* }
*
* 4. When a deep sub-object type does not its type:
* > types.scan({ a: ['number'] }, { a: [ 123, 'abc' ] });
* will return an object like the following :
* {
* error: 'Expected a "number" but found a "string".',
* expected: 'number',
* type: 'string',
* value: 'abc',
* path: [ 'a', 1 ]
* }
*
* 5. When a required key is missing:
* > types.scan({ a: 'number' }, {});
* will return an object like the following :
* {
* error: 'Expected a "number" but found a "undefined".',
* expected: 'number',
* type: 'undefined',
* value: undefined,
* path: [ 'a' ]
* }
*
* 6. When an unexpected key is present:
* > types.scan({ a: 'number' }, { a: 123, b: 456 });
* will return an object like the following :
* {
* error: 'Unexpected key "b".',
* expected: { a: 'number' },
* type: 'object',
* value: { a: 123, b: 456 }
* }
*
* @param {*} obj The value to validate.
* @param {type} type The type.
* @return {?Error} Returns null or an Error object.
*/
this.scan = function(type, obj) {
let a;
let i;
let l;
let k;
let res;
let nbOpt;
let objKeys;
let typeKeys;
let hasStar;
let hasTypeOf;
let optional = false;
let exclusive = false;
const typeOf = __getNativeType(obj);
const requiredTypeOf = __getNativeType(type);
if (requiredTypeOf === 'string') {
a = type.replace(/^[\?\!]/, '').split(/\|/);
l = a.length;
for (i = 0; i < l; i++)
if (!__nativeTypes[a[i]] && typeof _customTypes[a[i]] === 'undefined')
throw new Error('Invalid type.');
if (type.charAt(0) === '?') optional = true;
else if (type.charAt(0) === '!') exclusive = true;
l = a.length;
for (i = 0; i < l; i++)
if (typeof _customTypes[a[i]] !== 'undefined')
if (
typeof _customTypes[a[i]].type === 'function'
? _customTypes[a[i]].type.call(_self, obj) === true
: !this.scan(_customTypes[a[i]].type, obj).error
) {
if (exclusive)
return {
error: 'Expected a "' + type + '" but found a ' + '"' + a[i] + '".',
expected: type,
type: a[i],
value: obj
};
else
return {
expected: type,
type: a[i],
value: obj
};
}
if (obj === null || obj === undefined) {
if (!exclusive && !optional)
return {
error: 'Expected a "' + type + '" but found a ' + '"' + typeOf + '".',
expected: type,
type: typeOf,
value: obj
};
else
return {
nully: true,
expected: type,
type: typeOf,
value: obj
};
} else {
hasStar = ~a.indexOf('*');
hasTypeOf = ~a.indexOf(typeOf);
if (exclusive && (hasStar || hasTypeOf))
return {
error:
'Expected a "' + type + '" but found a ' + '"' + (hasTypeOf ? typeOf : '*') + '".',
expected: type,
type: hasTypeOf ? typeOf : '*',
value: obj
};
else if (!exclusive && !(hasStar || hasTypeOf))
return {
error: 'Expected a "' + type + '" but found a ' + '"' + typeOf + '".',
expected: type,
type: typeOf,
value: obj
};
else
return {
expected: type,
type: typeOf,
value: obj
};
}
} else if (requiredTypeOf === 'object') {
if (typeOf !== 'object')
return {
error: 'Expected an object but found a "' + typeOf + '".',
expected: type,
type: typeOf,
value: obj
};
typeKeys = Object.keys(type);
l = typeKeys.length;
nbOpt = 0;
for (k = 0; k < l; k++) {
res = this.scan(type[typeKeys[k]], obj[typeKeys[k]]);
if (res.error) {
res.path = res.path ? [typeKeys[k]].concat(res.path) : [typeKeys[k]];
return res;
} else if (res.nully) nbOpt++;
}
objKeys = Object.keys(obj);
if (objKeys.length > l - nbOpt) {
l = objKeys.length;
for (k = 0; k < l; k++)
if (typeof type[objKeys[k]] === 'undefined')
return {
error: 'Unexpected key "' + objKeys[k] + '".',
expected: type,
type: typeOf,
value: obj
};
}
return {
expected: type,
type: typeOf,
value: obj
};
} else if (requiredTypeOf === 'function') {
const output = {
expected: type,
type: typeOf,
value: obj
};
// Just applying a function
if (!type(obj)) output.error = 'The target did not pass the given test (function).';
return output;
} else if (requiredTypeOf === 'array') {
if (type.length !== 1) throw new Error('Invalid type.');
if (typeOf !== 'array')
return {
error: 'Expected an array but found a "' + typeOf + '".',
expected: type,
type: typeOf,
value: obj
};
l = obj.length;
for (i = 0; i < l; i++) {
res = this.scan(type[0], obj[i]);
if (res.error) {
res.path = res.path ? [i].concat(res.path) : [i];
return res;
}
}
return {
expected: type,
type: typeOf,
value: obj
};
} else throw new Error('Invalid type.');
};
/**
* This method registers a custom type into the Typology instance. A type
* is registered under a unique name, and is described by an object (like
* classical C structures) or a function.
*
* Variant 1:
* **********
* > types.add('user', { id: 'string', name: '?string' });
*
* @param {string} id The unique id of the type.
* @param {object} type The corresponding structure.
* @return {Typology} Returns this.
*
* Variant 2:
* **********
* > types.add('integer', function(value) {
* > return typeof value === 'number' && value === value | 0;
* > });
*
* @param {string} id The unique id of the type.
* @param {function} type The function validating the type.
* @return {Typology} Returns this.
*
* Variant 3:
* **********
* > types.add({
* > id: 'user',
* > type: { id: 'string', name: '?string' }
* > });
*
* > types.add({
* > id: 'integer',
* > type: function(value) {
* > return typeof value === 'number' && value === value | 0;
* > }
* > });
*
* @param {object} specs An object describing fully the type.
* @return {Typology} Returns this.
*
* Recognized parameters:
* **********************
* Here is the exhaustive list of every accepted parameters in the specs
* object:
*
* {string} id The unique id of the type.
* {function|object} type The function or the structure object
* validating the type.
* {?[string]} proto Eventually an array of ids of types that are
* referenced in the structure but do not exist
* yet.
*/
this.add = function(a1, a2) {
let o;
let k;
let a;
let id;
let tmp;
let type;
// Polymorphism:
if (arguments.length === 1) {
if (this.get(a1) === 'object') {
o = a1;
id = o.id;
type = o.type;
} else
throw new Error(
'If types.add is called with one argument, ' + 'this one has to be an object.'
);
} else if (arguments.length === 2) {
if (typeof a1 !== 'string' || !a1)
throw new Error(
'If types.add is called with more than one ' +
'argument, the first one must be the string id.'
);
else id = a1;
type = a2;
} else throw new Error('types.add has to be called ' + 'with one or two arguments.');
if (this.get(id) !== 'string' || id.length === 0)
throw new Error('A type requires an string id.');
if (_customTypes[id] !== undefined && _customTypes[id] !== 'proto')
throw new Error('The type "' + id + '" already exists.');
if (__nativeTypes[id]) throw new Error('"' + id + '" is a reserved type name.');
_customTypes[id] = 1;
// Check given prototypes:
a = (o || {}).proto || [];
a = Array.isArray(a) ? a : [a];
tmp = {};
for (k in a)
if (_customTypes[a[k]] === undefined) {
_customTypes[a[k]] = 1;
tmp[a[k]] = 1;
}
if (this.get(type) !== 'function' && !this.isValid(type))
throw new Error(
'A type requires a valid definition. ' +
'This one can be a preexistant type or else ' +
'a function testing given objects.'
);
// Effectively add the type:
_customTypes[id] =
o === undefined
? {
id: id,
type: type
}
: {};
if (o !== undefined) for (k in o) _customTypes[id][k] = o[k];
// Delete prototypes:
for (k in tmp) if (k !== id) delete _customTypes[k];
return this;
};
/**
* This method returns true if a custom type is already registered in this
* instance under the given key.
*
* @param {string} key A type name.
* @return {boolean} Returns true if the key is registered.
*/
this.has = function(key) {
return !!_customTypes[key];
};
/**
* This method returns the native type of a given value.
*
* Examples:
* *********
* > types.get({ a: 1 }); // returns "object"
* > types.get('abcde'); // returns "string"
* > types.get(1234567); // returns "number"
* > types.get([1, 2]); // returns "array"
*
* @param {*} value Anything.
* @return {string} Returns the native type of the value.
*/
this.get = __getNativeType;
/**
* This method validates some value against a given type. It works exactly
* as the #scan method, but will return true if the value matches the given
* type and false else, instead of reporting objects.
*
* Examples:
* *********
* > types.check('object', { a: 1 }); // returns true
* > types.check({ a: 'string' }, { a: 1 }); // returns true
* > types.check({ a: 'string', b: '?number' }, { a: 1 }); // returns true
*
* > types.check({ a: 'string', b: 'number' }, { a: 1 }); // returns false
* > types.check({ a: 'number' }, { a: 1 }); // returns false
* > types.check('array', { a: 1 }); // returns false
*
* @param {type} type A valid type.
* @param {*} value Anything.
* @return {boolean} Returns true if the value matches the type, and
* not else.
*/
this.check = function(type, obj) {
return !this.scan(type, obj).error;
};
/**
* This method validates a type. If the type is not referenced or is not
* valid, it will return false.
*
* To know more about that function, don't hesitate to read the related
* unit tests.
*
* Examples:
* *********
* > types.isValid('string'); // returns true
* > types.isValid('?string'); // returns true
* > types.isValid('!string'); // returns true
* > types.isValid('string|number'); // returns true
* > types.isValid({ a: 'string' }); // returns true
* > types.isValid(['string']); // returns true
*
* > types.isValid('!?string'); // returns false
* > types.isValid('myNotDefinedType'); // returns false
* > types.isValid(['myNotDefinedType']); // returns false
* > types.isValid({ a: 'myNotDefinedType' }); // returns false
*
* > types.isValid('user'); // returns false
* > types.add('user', { id: 'string' }); // makes the type become valid
* > types.isValid('user'); // returns true
*
* @param {*} type The type to get checked.
* @return {boolean} Returns true if the type is valid, and false else.
*/
this.isValid = function(type) {
let a;
let k;
let i;
let l;
let aKeys;
let typeKeys;
const typeOf = __getNativeType(type);
if (typeOf === 'string') {
a = type.replace(/^[\?\!]/, '').split(/\|/);
aKeys = Object.keys(a);
l = aKeys.length;
for (i = 0; i < l; i++)
if (!__nativeTypes[a[aKeys[i]]] && typeof _customTypes[a[aKeys[i]]] === 'undefined')
return false;
return true;
} else if (typeOf === 'object') {
typeKeys = Object.keys(type);
l = typeKeys.length;
for (k = 0; k < l; k++) if (!this.isValid(type[typeKeys[k]])) return false;
return true;
} else if (typeOf === 'array') return type.length === 1 ? this.isValid(type[0]) : false;
else if (typeOf === 'function') return true;
else return false;
};
/**
* INSTANTIATION ROUTINE:
* **********************
*/
// Add a type "type" to shortcut the #isValid method:
this.add(
'type',
function(v) {
return this.isValid(v);
}.bind(this)
);
// Add a type "primitive" to match every primitive types (including null):
this.add('primitive', v => v !== Object(v));
// Adding custom types at instantiation:
defs = defs || {};
if (this.get(defs) !== 'object') throw Error('Invalid argument.');
for (let k in defs) this.add(k, defs[k]);
}
/**
* GLOBAL PUBLIC API:
* ******************
*/
// Creating a "main" typology instance to export:
const types = Typology;
Typology.call(types);
// Version:
Object.defineProperty(types, 'version', {
value: '1.3.0'
});
/**
* EXPORT:
* *******
*/
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) exports = module.exports = types;
exports.types = types;
} else if (typeof define === 'function' && define.amd)
define('typology', [], function() {
return types;
});
else scope.types = types;