-
Notifications
You must be signed in to change notification settings - Fork 10
/
ducktype.js
702 lines (627 loc) · 17.6 KB
/
ducktype.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/**
* ducktype
*
* Data validation using a ducktype interface. For JavaScript and Node.js.
*
* Load
* var ducktype = require('ducktype');
*
* Syntax:
* ducktype(type)
* ducktype(type, options)
* ducktype(type1, type2, ...)
* ducktype(type1, type2, ..., options)
*
* Where:
* type is a type description
* options is an object with properties:
* name: String (optional)
* optional: Boolean (optional)
* nullable: Boolean (optional)
*/
(function () {
'use strict';
/**
* Duck type constructor
* @param {{name: String, test: Function}} options
* @constructor DuckType
*/
function DuckType(options) {
this.name = options.name;
this.test = options.test;
}
/**
* Test whether an object matches this DuckType.
* This function should be overwritten by the DuckType implementation
* @param {*} object
* @returns {boolean} match
*/
DuckType.prototype.test = function (object) {
return false;
};
/**
* Test whether an object matches this DuckType.
* Throws a TypeError when the object does not match.
* @param {*} object
*/
DuckType.prototype.validate = function (object) {
if (!this.test(object)) {
throw new TypeError(object + ' is not a valid ' + (this.name || 'type'));
}
};
/**
* Create a wrapper around the provided function. The wrapper first validates
* the function arguments, and throws a TypeError if not correct.
* When correct, the function will be executed.
* @param {Function} fn
* @returns {Function} wrapper
*/
DuckType.prototype.wrap = function (fn) {
var ducktype = this
// TODO: test whether this DuckType is an Array
// Alter the behavior of the ducktype in case of a test with zero or one arguments
return function ducktypeWrapper() {
ducktype.validate(arguments);
return fn.apply(fn, arguments);
};
};
// The object basic contains all basic types
var basic = {};
// type Array
basic.array = new DuckType({
name: 'Array',
test: function isArray(object) {
return (Array.isArray(object) ||
((object != null) && (object.toString() === '[object Arguments]')));
}
});
// type Boolean
basic.boolean = new DuckType({
name: 'Boolean',
test: function isBoolean(object) {
return ((object instanceof Boolean) || (typeof object === 'boolean'));
}
});
// type Date
basic.date = new DuckType({
name: 'Date',
test: function isDate(object) {
return (object instanceof Date);
}
});
// type Function
basic.function = new DuckType({
name: 'Function',
test: function isFunction(object) {
return ((object instanceof Function) || (typeof object === 'function'));
}
});
// type Number
basic.number = new DuckType({
name: 'Number',
test: function isNumber(object) {
return ((object instanceof Number) || (typeof object === 'number'));
}
});
// type Object
basic.object = new DuckType({
name: 'Object',
test: function isObject(object) {
return ((object instanceof Object) && (object.constructor === Object));
}
});
// type RegExp
basic.regexp = new DuckType({
name: 'RegExp',
test: function isRegExp(object) {
return (object instanceof RegExp);
}
});
// type String
basic.string = new DuckType({
name: 'String',
test: function isString(object) {
return ((object instanceof String) || (typeof object === 'string'));
}
});
// type null
basic['null'] = new DuckType({
name: 'null',
test: function isNull(object) {
return (object === null);
}
});
// type undefined
basic['undefined'] = new DuckType({
name: 'undefined',
test: function isUndefined(object) {
return (object === undefined);
}
});
// type url
basic.url = new DuckType({
name: 'url',
test: isUrl
});
// type email
// Be careful: when changing the regexp, double check whether it is still secure.
// test with https://www.npmjs.com/package/vuln-regex-detector
var emailRegExp = /^[a-zA-Z][\w.-]*[a-zA-Z0-9]@\w+\.[a-zA-Z]+$/;
basic.email = new DuckType({
name: 'email',
test: function (object) {
return emailRegExp.test(object);
}
});
// type integer
basic['integer'] = new DuckType({
name: 'integer',
test: function isInteger (object) {
return ((object instanceof Number) || (typeof object === 'number')) &&
(object === parseInt(object));
}
});
// TODO: add types like phone number, postcode, ...
/**
* Create a ducktype handling an object
* @param {Object} type
* @param {{name: String}} [options]
* @returns {*}
*/
function createObject (type, options) {
// retrieve the test functions for each of the objects properties
var tests = {},
prop;
for (prop in type) {
if (type.hasOwnProperty(prop)) {
tests[prop] = ducktype(type[prop]).test;
}
}
// non-empty object
var isObject = basic.object.test;
return new DuckType({
name: options && options.name || null,
test: function test (object) {
var prop;
// test whether we have an object
if (!isObject(object)) {
return false;
}
// test each of the defined properties
for (prop in tests) {
if (tests.hasOwnProperty(prop)) {
if (!tests[prop](object[prop])) {
return false;
}
}
}
return true;
}
});
}
/**
* Create a ducktype handling an array
* @param {Array} type An array with multiple elements
* @param {{name: String}} [options]
* @returns {*}
*/
function createArray (type, options) {
// multiple childs, fixed length
var tests = [];
var isArray = basic.array.test;
for (var i = 0, ii = type.length; i < ii; i++) {
tests[i] = ducktype(type[i]).test;
}
// create the ducktype
return new DuckType({
name: options && options.name || null,
test: function test (object) {
// test whether object is an array
if (!isArray(object)) {
return false;
}
// test for correct length
if (object.length !== tests.length) {
return false;
}
// test all childs of the array
for (var i = 0, ii = object.length; i < ii; i++) {
if (!tests[i](object[i])) {
return false;
}
}
return true;
}
});
// TODO: create an option length, length.min, length.max for the array.
// length can be an integer or a function
}
/**
* Create a ducktype handling an array
* @param {Array} type An array containing one element
* @param {{name: String}} [options]
* @returns {*}
*/
function createArrayRepeat (type, options) {
// a single child, repeat for each child
var childTest = ducktype(type[0]).test;
// create the ducktype
return new DuckType({
name: options && options.name || null,
test: function test (object) {
// test whether object is an array
if (!Array.isArray(object)) {
return false;
}
// test all childs of the array
for (var i = 0, ii = object.length; i < ii; i++) {
if (!childTest(object[i])) {
return false;
}
}
return true;
}
});
// TODO: create an option length, length.min, length.max for the array.
// length can be an integer or a function
}
/**
* Create a ducktype handling a prototype
* @param {Object} type A prototype function
* @param {{name: String}} [options]
* @returns {*}
*/
function createPrototype (type, options) {
return new DuckType({
name: options && options.name || null,
test: function test (object) {
return (object instanceof type);
}
});
}
/**
* Create a ducktype handling a combination of types
* @param {Array} types
* @param {{name: String}} [options]
* @returns {*}
*/
function createCombi (types, options) {
var tests = types.map(function (type) {
return ducktype(type).test;
});
return new DuckType({
name: options && options.name || null,
test: function test (object) {
for (var i = 0, ii = tests.length; i < ii; i++) {
if (tests[i](object)) {
return true;
}
}
return false;
}
});
}
/**
* Create a ducktype from a test function
* @param {Function} test A test function, returning true when a provided
* object matches, or else returns false.
* @param {{name: String}} [options]
* @returns {*}
*/
function createFunction (test, options) {
return new DuckType({
name: options && options.name || null,
test: test
});
}
/**
* Create a ducktype from a regular expression. The created ducktype
* will check whether the provided object is a String,
* and matches with the regular expression
* @param {RegExp} regexp A regular expression
* @param {{name: String}} [options]
* @returns {*}
*/
function createRegExp (regexp, options) {
return new DuckType({
name: options && options.name || null,
test: function (object) {
return ((object instanceof String) || typeof(object) === 'string') && regexp.test(object);
}
});
}
// TODO: document ducktype.construct(function) and ducktype.construct(regexp)
/**
* Create a new duck type. Syntax:
* ducktype(type)
* ducktype(type, options)
* ducktype(type1, type2, ...)
* ducktype(type1, type2, ..., options)
*
* Where:
* type is a type description
* options is an object with properties:
* name: String (optional)
* optional: Boolean (optional)
* nullable: Boolean (optional)
*
* @param {*...} args
* @return {DuckType} ducktype
*/
function ducktype (args) {
// TODO: implement support for ducktype(test: Function) to create a custom type
// TODO: implement support for ducktype(test: RegExp) to create a custom type
var i, ii;
var newDucktype;
var type = null;
var types = null;
var options = null;
var test;
// process arguments
if (arguments.length === 0) {
throw new SyntaxError('Parameter type missing');
}
else if (arguments.length === 1) {
type = arguments[0];
}
else {
types = [];
for (i = 0, ii = arguments.length; i < ii; i++) {
if ((i === ii - 1) && arguments[i].constructor === Object) {
options = arguments[i];
}
else {
types[i] = arguments[i];
}
}
if (types.length === 1) {
type = types[0];
types = null;
}
}
// create a duck type
if (types) {
newDucktype = createCombi(types, options);
}
else if (type === Array) {
newDucktype = basic.array;
}
else if (type === Boolean) {
newDucktype = basic.boolean;
}
else if (type === Date) {
newDucktype = basic.date;
}
else if (type === Function) {
newDucktype = basic.function;
}
else if (type === Number) {
newDucktype = basic.number;
}
else if (type === Object) {
newDucktype = basic.object;
}
else if (type === String) {
newDucktype = basic.string;
}
else if (type === RegExp) {
newDucktype = basic.regexp;
}
else if (type === null) {
newDucktype = basic['null'];
}
else if (type === undefined) {
newDucktype = basic['undefined'];
}
else if (type instanceof DuckType) {
newDucktype = type; // already a duck type
}
else if (Array.isArray(type)) {
if (type.length === 0) {
newDucktype = basic.array;
}
else if (type.length === 1) {
newDucktype = createArrayRepeat(type, options);
}
else {
newDucktype = createArray(type, options);
}
}
else if ((type instanceof Object) && (type.constructor === Object)) {
if (Object.keys(type).length === 0) {
newDucktype = basic.object;
}
else {
newDucktype = createObject(type, options);
}
}
else {
newDucktype = createPrototype(type, options);
}
// process options
if (options) {
test = newDucktype.test;
var constructedTest = null;
var tests = [];
var name = options.name || newDucktype.name || null;
if ((options.optional !== undefined) || (options.nullable !== undefined)) {
var optional = (options.optional !== undefined) ? options.optional : false;
var nullable = (options.nullable !== undefined) ? options.nullable : false;
constructedTest = function (object) {
return test(object) ||
(nullable && (object === null)) ||
(optional && (object === undefined));
};
tests.push(constructedTest);
}
else {
tests.push(test);
}
if (options.integer === true) {
tests.push(function test_integer (object) {
return (object === parseInt(object));
});
}
if (options.min !== undefined) {
tests.push(function test_min (object) {
return (object >= options.min);
});
}
if (options.max !== undefined) {
tests.push(function test_max (object) {
return (object <= options.max);
});
}
if (tests.length === 1) {
// a single test
newDucktype = new DuckType({
name: name,
test: tests[0]
});
}
else {
// multiple tests
newDucktype = new DuckType({
name: name,
test: function (object) {
for (var i = 0, ii = tests.length; i < ii; i++) {
if (!tests[i](object)) {
return false;
}
}
return true;
}
});
}
}
// return the created ducktype
return newDucktype;
}
/**
* Create a DuckType from a test function or regular expression
* @param {String} [name]
* @param {Function | RegExp} test
* @return {DuckType} ducktype
*/
// TODO: document ducktype.construct
ducktype.construct = function construct(name, test) {
if (arguments.length === 1) {
test = arguments[0];
name = null;
}
if (basic.function.test(test)) {
// function
return createFunction(test, {
name: name
});
}
else if (basic.regexp.test(test)) {
// regexp
return createRegExp(test, {
name: name
});
}
else {
throw new TypeError('Function or RegExp expected');
}
};
// attach each of the basic types to the ducktype function
for (var type in basic) {
if (basic.hasOwnProperty(type)) {
ducktype[type] = basic[type];
}
}
/**
* RegExps.
* A URL must match #1 and then at least one of #2/#3.
* Use two levels of REs to avoid REDOS.
*/
var protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/;
var localhostDomainRE = /^localhost[:?\d]*(?:[^:?\d]\S*)?$/
var nonLocalhostDomainRE = /^[^\s.]+\.\S{2,}$/;
/**
* Loosely validate a URL `string`.
*
* Source: https://github.com/segmentio/is-url
*
* @param {String} string
* @return {Boolean}
*/
function isUrl(string){
if (typeof string !== 'string') {
return false;
}
var match = string.match(protocolAndDomainRE);
if (!match) {
return false;
}
var everythingAfterProtocol = match[1];
if (!everythingAfterProtocol) {
return false;
}
return localhostDomainRE.test(everythingAfterProtocol) ||
nonLocalhostDomainRE.test(everythingAfterProtocol);
}
// TODO: implement a parser implements js type annotations
// TODO: implement non-strict tests and an option strict
/**
* Shims for older JavaScript engines
*/
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
if(!Array.isArray) {
Array.isArray = function (vArg) {
return Object.prototype.toString.call(vArg) === '[object Array]';
};
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) result.push(prop);
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
}
}
return result;
};
})();
}
/**
* CommonJS module exports
*/
if ((typeof module !== 'undefined') && (typeof module.exports !== 'undefined')) {
module.exports = ducktype;
}
if (typeof exports !== 'undefined') {
exports = ducktype;
}
/**
* AMD module exports
*/
if (typeof(define) === 'function') {
define(ducktype);
}
/**
* Browser exports
*/
if (typeof(window) !== 'undefined') {
window['ducktype'] = ducktype;
}
})();