forked from josdejong/typed-function
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyped-function.js
1129 lines (1000 loc) · 34.7 KB
/
typed-function.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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* typed-function
*
* Type checking for JavaScript functions
*
* https://github.com/josdejong/typed-function
*/
'use strict';
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// OldNode. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like OldNode.
module.exports = factory();
} else {
// Browser globals (root is window)
window.typed = factory();
}
}(function () {
// factory function to create a new instance of typed-function
// TODO: allow passing configuration, types, tests via the factory function
function create() {
/**
* Get a type test function for a specific data type
* @param {string} type A data type like 'number' or 'string'
* @returns {function(obj: *) : boolean} Returns a type testing function.
* Throws an error for an unknown type.
*/
function getTypeTest(type) {
var test = typed.types[type];
if (!test) {
var hint;
for (var name in typed.types) {
if (typed.types.hasOwnProperty(name)) {
if (name.toLowerCase() == type.toLowerCase()) {
hint = name;
break;
}
}
}
throw new Error('Unknown type "' + type + '"' +
(hint ? ('. Did you mean "' + hint + '"?') : ''));
}
return test;
}
/**
* Create an ArgumentsError. Creates messages like:
*
* Unexpected type of argument (expected: ..., actual: ..., index: ...)
* Too few arguments (expected: ..., index: ...)
* Too many arguments (expected: ..., actual: ...)
*
* @param {String} fn Function name
* @param {number} argCount Number of arguments
* @param {Number} index Current argument index
* @param {*} actual Current argument
* @param {string} [expected] An optional, comma separated string with
* expected types on given index
* @extends Error
*/
function createError(fn, argCount, index, actual, expected) {
var actualType = getTypeOf(actual);
var _expected = expected ? expected.split(',') : null;
var anyType = _expected && contains(_expected, 'any');
var message;
var data = {
fn: fn,
index: index,
actual: actual,
expected: _expected
};
//console.log('createError', fn, args, index, expected) // TODO: cleanup
// TODO: add function name to the message
if (_expected) {
if (argCount > index && !anyType) {
// unexpected type
message = 'Unexpected type of argument' +
' (expected: ' + _expected.join(' or ') + ', actual: ' + actualType + ', index: ' + index + ')';
}
else {
// too few arguments
message = 'Too few arguments' +
' (expected: ' + _expected.join(' or ') + ', index: ' + index + ')';
}
}
else {
// too many arguments
message = 'Too many arguments' +
' (expected: ' + index + ', actual: ' + argCount + ')'
}
var err = new TypeError(message);
err.data = data;
return err;
}
/**
* Collection with function references (local shortcuts to functions)
* @constructor
* @param {string} [name='refs'] Optional name for the refs, used to generate
* JavaScript code
*/
function Refs(name) {
this.name = name || 'refs';
this.categories = {};
}
/**
* Add a function reference.
* @param {function} fn
* @param {string} [category='fn'] A function category, like 'fn' or 'signature'
* @returns {string} Returns the function name, for example 'fn0' or 'signature2'
*/
Refs.prototype.add = function (fn, category) {
var cat = category || 'fn';
if (!this.categories[cat]) this.categories[cat] = [];
var index = this.categories[cat].indexOf(fn);
if (index == -1) {
index = this.categories[cat].length;
this.categories[cat].push(fn);
}
return cat + index;
};
/**
* Create code lines for all function references
* @returns {string} Returns the code containing all function references
*/
Refs.prototype.toCode = function () {
var code = [];
var path = this.name + '.categories';
var categories = this.categories;
for (var cat in categories) {
if (categories.hasOwnProperty(cat)) {
var category = categories[cat];
for (var i = 0; i < category.length; i++) {
code.push('var ' + cat + i + ' = ' + path + '[\'' + cat + '\'][' + i + '];');
}
}
}
return code.join('\n');
};
/**
* A function parameter
* @param {string | string[] | Param} types A parameter type like 'string',
* 'number | boolean'
* @param {boolean} [varArgs=false] Variable arguments if true
* @constructor
*/
function Param(types, varArgs) {
// parse the types, can be a string with types separated by pipe characters |
if (typeof types === 'string') {
// parse variable arguments operator (ellipses '...number')
var _types = types.trim();
var _varArgs = _types.substr(0, 3) === '...';
if (_varArgs) {
_types = _types.substr(3);
}
if (_types === '') {
this.types = ['any'];
}
else {
this.types = _types.split('|');
for (var i = 0; i < this.types.length; i++) {
this.types[i] = this.types[i].trim();
}
}
}
else if (Array.isArray(types)) {
this.types = types;
}
else if (types instanceof Param) {
return types.clone();
}
else {
throw new Error('String or Array expected');
}
// can hold a type to which to convert when handling this parameter
this.conversions = [];
// TODO: better implement support for conversions, be able to add conversions via constructor (support a new type Object?)
// variable arguments
this.varArgs = _varArgs || varArgs || false;
// check for any type arguments
this.anyType = this.types.indexOf('any') !== -1;
}
/**
* Order Params
* any type ('any') will be ordered last, and object as second last (as other
* types may be an object as well, like Array).
*
* @param {Param} a
* @param {Param} b
* @returns {number} Returns 1 if a > b, -1 if a < b, and else 0.
*/
Param.compare = function (a, b) {
if (a.anyType) return 1;
if (b.anyType) return -1;
if (contains(a.types, 'Object')) return 1;
if (contains(b.types, 'Object')) return -1;
if (a.hasConversions()) {
if (b.hasConversions()) {
var i, ac, bc;
for (i = 0; i < a.conversions.length; i++) {
if (a.conversions[i] !== undefined) {
ac = a.conversions[i];
break;
}
}
for (i = 0; i < b.conversions.length; i++) {
if (b.conversions[i] !== undefined) {
bc = b.conversions[i];
break;
}
}
return typed.conversions.indexOf(ac) - typed.conversions.indexOf(bc);
}
else {
return 1;
}
}
else {
if (b.hasConversions()) {
return -1;
}
}
return 0;
};
/**
* Test whether this parameters types overlap an other parameters types.
* @param {Param} other
* @return {boolean} Returns true when there are conflicting types
*/
Param.prototype.overlapping = function (other) {
for (var i = 0; i < this.types.length; i++) {
if (contains(other.types, this.types[i])) {
return true;
}
}
return false;
};
/**
* Create a clone of this param
* @returns {Param} Returns a cloned version of this param
*/
Param.prototype.clone = function () {
var param = new Param(this.types.slice(), this.varArgs);
param.conversions = this.conversions.slice();
return param;
};
/**
* Test whether this parameter contains conversions
* @returns {boolean} Returns true if the parameter contains one or
* multiple conversions.
*/
Param.prototype.hasConversions = function () {
return this.conversions.length > 0;
};
/**
* Return a string representation of this params types, like 'string' or
* 'number | boolean' or '...number'
* @param {boolean} [toConversion] If true, the returned types string
* contains the types where the parameter
* will convert to. If false (default)
* the "from" types are returned
* @returns {string}
*/
Param.prototype.toString = function (toConversion) {
var types = [];
var keys = {};
for (var i = 0; i < this.types.length; i++) {
var conversion = this.conversions[i];
var type = toConversion && conversion ? conversion.to : this.types[i];
if (!(type in keys)) {
keys[type] = true;
types.push(type);
}
}
return (this.varArgs ? '...' : '') + types.join('|');
};
/**
* A function signature
* @param {string | string[] | Param[]} params
* Array with the type(s) of each parameter,
* or a comma separated string with types
* @param {function} fn The actual function
* @constructor
*/
function Signature(params, fn) {
var _params;
if (typeof params === 'string') {
_params = (params !== '') ? params.split(',') : [];
}
else if (Array.isArray(params)) {
_params = params;
}
else {
throw new Error('string or Array expected');
}
this.params = new Array(_params.length);
for (var i = 0; i < _params.length; i++) {
var param = new Param(_params[i]);
this.params[i] = param;
if (i === _params.length - 1) {
// the last argument
this.varArgs = param.varArgs;
}
else {
// non-last argument
if (param.varArgs) {
throw new SyntaxError('Unexpected variable arguments operator "..."');
}
}
}
this.fn = fn;
}
/**
* Create a clone of this signature
* @returns {Signature} Returns a cloned version of this signature
*/
Signature.prototype.clone = function () {
return new Signature(this.params.slice(), this.fn);
};
/**
* Expand a signature: split params with union types in separate signatures
* For example split a Signature "string | number" into two signatures.
* @return {Signature[]} Returns an array with signatures (at least one)
*/
Signature.prototype.expand = function () {
var signatures = [];
function recurse(signature, path) {
if (path.length < signature.params.length) {
var i, newParam, conversion;
var param = signature.params[path.length];
if (param.varArgs) {
// a variable argument. do not split the types in the parameter
newParam = param.clone();
// add conversions to the parameter
// recurse for all conversions
for (i = 0; i < typed.conversions.length; i++) {
conversion = typed.conversions[i];
if (!contains(param.types, conversion.from) && contains(param.types, conversion.to)) {
var j = newParam.types.length;
newParam.types[j] = conversion.from;
newParam.conversions[j] = conversion;
}
}
recurse(signature, path.concat(newParam));
}
else {
// split each type in the parameter
for (i = 0; i < param.types.length; i++) {
recurse(signature, path.concat(new Param(param.types[i])));
}
// recurse for all conversions
for (i = 0; i < typed.conversions.length; i++) {
conversion = typed.conversions[i];
if (!contains(param.types, conversion.from) && contains(param.types, conversion.to)) {
newParam = new Param(conversion.from);
newParam.conversions[0] = conversion;
recurse(signature, path.concat(newParam));
}
}
}
}
else {
signatures.push(new Signature(path, signature.fn));
}
}
recurse(this, []);
return signatures;
};
/**
* Compare two signatures.
*
* When two params are equal and contain conversions, they will be sorted
* by lowest index of the first conversions.
*
* @param {Signature} a
* @param {Signature} b
* @returns {number} Returns 1 if a > b, -1 if a < b, and else 0.
*/
Signature.compare = function (a, b) {
if (a.params.length > b.params.length) return 1;
if (a.params.length < b.params.length) return -1;
// count the number of conversions
var i;
var len = a.params.length; // a and b have equal amount of params
var ac = 0;
var bc = 0;
for (i = 0; i < len; i++) {
if (a.params[i].hasConversions()) ac++;
if (b.params[i].hasConversions()) bc++;
}
if (ac > bc) return 1;
if (ac < bc) return -1;
// compare the conversion index per parameter
for (i = 0; i < a.params.length; i++) {
var cmp = Param.compare(a.params[i], b.params[i]);
if (cmp !== 0) {
return cmp;
}
}
return 0;
};
/**
* Test whether any of the signatures parameters has conversions
* @return {boolean} Returns true when any of the parameters contains
* conversions.
*/
Signature.prototype.hasConversions = function () {
for (var i = 0; i < this.params.length; i++) {
if (this.params[i].hasConversions()) {
return true;
}
}
return false;
};
/**
* Generate the code to invoke this signature
* @param {Refs} refs
* @param {string} prefix
* @returns {string} Returns code
*/
Signature.prototype.toCode = function (refs, prefix) {
var code = [];
var args = new Array(this.params.length);
for (var i = 0; i < this.params.length; i++) {
var param = this.params[i];
var conversion = param.conversions[0];
if (param.varArgs) {
args[i] = 'varArgs';
}
else if (conversion) {
args[i] = refs.add(conversion.convert, 'convert') + '(arg' + i + ')';
}
else {
args[i] = 'arg' + i;
}
}
var ref = this.fn ? refs.add(this.fn, 'signature') : undefined;
if (ref) {
return prefix + 'return ' + ref + '(' + args.join(', ') + '); // signature: ' + this.params.join(', ');
}
return code.join('\n');
};
/**
* Return a string representation of the signature
* @returns {string}
*/
Signature.prototype.toString = function () {
return this.params.join(', ');
};
/**
* A group of signatures with the same parameter on given index
* @param {Param[]} path
* @param {Signature} [signature]
* @param {Node[]} childs
* @constructor
*/
function Node(path, signature, childs) {
this.path = path || [];
this.param = path[path.length - 1] || null;
this.signature = signature || null;
this.childs = childs || [];
}
/**
* Generate code for this group of signatures
* @param {Refs} refs
* @param {string} prefix
* @param {Node | undefined} [anyType] Sibling of this node with any type parameter
* @returns {string} Returns the code as string
*/
Node.prototype.toCode = function (refs, prefix, anyType) {
// TODO: split this function in multiple functions, it's too large
var code = [];
if (this.param) {
var index = this.path.length - 1;
var conversion = this.param.conversions[0];
var comment = '// type: ' + (conversion ?
(conversion.from + ' (convert to ' + conversion.to + ')') :
this.param);
// non-root node (path is non-empty)
if (this.param.varArgs) {
if (this.param.anyType) {
// variable arguments with any type
code.push(prefix + 'if (arguments.length > ' + index + ') {');
code.push(prefix + ' var varArgs = [];');
code.push(prefix + ' for (var i = ' + index + '; i < arguments.length; i++) {');
code.push(prefix + ' varArgs.push(arguments[i]);');
code.push(prefix + ' }');
code.push(this.signature.toCode(refs, prefix + ' '));
code.push(prefix + '}');
}
else {
// variable arguments with a fixed type
var getTests = function (types, arg) {
var tests = new Array(types.length);
for (var i = 0; i < types.length; i++) {
tests[i] = refs.add(getTypeTest(types[i]), 'test') + '(' + arg + ')';
}
return tests.join(' || ');
}.bind(this);
var allTypes = this.param.types;
var exactTypes = [];
for (var i = 0; i < allTypes.length; i++) {
if (this.param.conversions[i] === undefined) {
exactTypes.push(allTypes[i]);
}
}
code.push(prefix + 'if (' + getTests(allTypes, 'arg' + index) + ') { ' + comment);
code.push(prefix + ' var varArgs = [arg' + index + '];');
code.push(prefix + ' for (var i = ' + (index + 1) + '; i < arguments.length; i++) {');
code.push(prefix + ' if (' + getTests(exactTypes, 'arguments[i]') + ') {');
code.push(prefix + ' varArgs.push(arguments[i]);');
for (var i = 0; i < allTypes.length; i++) {
var conversion_i = this.param.conversions[i];
if (conversion_i) {
var test = refs.add(getTypeTest(allTypes[i]), 'test');
var convert = refs.add(conversion_i.convert, 'convert');
code.push(prefix + ' }');
code.push(prefix + ' else if (' + test + '(arguments[i])) {');
code.push(prefix + ' varArgs.push(' + convert + '(arguments[i]));');
}
}
code.push(prefix + ' } else {');
code.push(prefix + ' throw createError(\'\', arguments.length, i, arguments[i], \'' + allTypes.join(',') + '\');');
code.push(prefix + ' }');
code.push(prefix + ' }');
code.push(this.signature.toCode(refs, prefix + ' '));
code.push(prefix + '}');
}
}
else {
if (this.param.anyType) {
// any type
code.push(prefix + '// type: any');
code.push(this._innerCode(refs, prefix, anyType));
}
else {
// regular type
var type = this.param.types[0];
var test = type !== 'any' ? refs.add(getTypeTest(type), 'test') : null;
code.push(prefix + 'if (' + test + '(arg' + index + ')) { ' + comment);
code.push(this._innerCode(refs, prefix + ' ', anyType));
code.push(prefix + '}');
}
}
}
else {
// root node (path is empty)
code.push(this._innerCode(refs, prefix, anyType));
}
return code.join('\n');
};
/**
* Generate inner code for this group of signatures.
* This is a helper function of Node.prototype.toCode
* @param {Refs} refs
* @param {string} prefix
* @param {Node | undefined} [anyType] Sibling of this node with any type parameter
* @returns {string} Returns the inner code as string
* @private
*/
Node.prototype._innerCode = function (refs, prefix, anyType) {
var code = [];
var i;
if (this.signature) {
code.push(prefix + 'if (arguments.length === ' + this.path.length + ') {');
code.push(this.signature.toCode(refs, prefix + ' '));
code.push(prefix + '}');
}
var nextAnyType;
for (i = 0; i < this.childs.length; i++) {
if (this.childs[i].param.anyType) {
nextAnyType = this.childs[i];
break;
}
}
for (i = 0; i < this.childs.length; i++) {
code.push(this.childs[i].toCode(refs, prefix, nextAnyType));
}
if (anyType && !this.param.anyType) {
code.push(anyType.toCode(refs, prefix, nextAnyType));
}
var exceptions = this._exceptions(refs, prefix);
if (exceptions) {
code.push(exceptions);
}
return code.join('\n');
};
/**
* Generate code to throw exceptions
* @param {Refs} refs
* @param {string} prefix
* @returns {string} Returns the inner code as string
* @private
*/
Node.prototype._exceptions = function (refs, prefix) {
var index = this.path.length;
// TODO: add function name to exceptions
if (this.childs.length === 0) {
// TODO: can this condition be simplified? (we have a fall-through here)
return [
prefix + 'if (arguments.length > ' + index + ') {',
prefix + ' throw createError(\'\', arguments.length, ' + index + ', arguments[' + index + ']);',
prefix + '}'
].join('\n');
}
else {
var keys = {};
var types = [];
for (var i = 0; i < this.childs.length; i++) {
var node = this.childs[i];
if (node.param) {
for (var j = 0; j < node.param.types.length; j++) {
var type = node.param.types[j];
if (!(type in keys)) {
keys[type] = true;
types.push(type);
}
}
}
}
return prefix + 'throw createError(\'\', arguments.length, ' + index + ', arguments[' + index + '], \'' + types.join(',') + '\');';
}
};
/**
* Split all raw signatures into an array with expanded Signatures
* @param {Object.<string, function>} rawSignatures
* @return {Signature[]} Returns an array with expanded signatures
*/
function parseSignatures(rawSignatures) {
// FIXME: need to have deterministic ordering of signatures, do not create via object
var signature;
var keys = {};
var signatures = [];
var i;
for (var types in rawSignatures) {
if (rawSignatures.hasOwnProperty(types)) {
var fn = rawSignatures[types];
signature = new Signature(types, fn);
var expanded = signature.expand();
for (i = 0; i < expanded.length; i++) {
var signature_i = expanded[i];
var key = signature_i.toString();
var existing = keys[key];
if (!existing) {
keys[key] = signature_i;
}
else {
var cmp = Signature.compare(signature_i, existing);
if (cmp < 0) {
// override if sorted first
keys[key] = signature_i;
}
else if (cmp === 0) {
throw new Error('Signature "' + key + '" is defined twice');
}
// else: just ignore
}
}
}
}
// convert from map to array
for (key in keys) {
if (keys.hasOwnProperty(key)) {
signatures.push(keys[key]);
}
}
// filter redundant conversions from signatures with varArgs
// TODO: simplify this loop or move it to a separate function
for (i = 0; i < signatures.length; i++) {
signature = signatures[i];
if (signature.varArgs) {
var index = signature.params.length - 1;
var param = signature.params[index];
var t = 0;
while (t < param.types.length) {
if (param.conversions[t]) {
var type = param.types[t];
for (var j = 0; j < signatures.length; j++) {
var other = signatures[j];
var p = other.params[index];
if (other !== signature &&
p &&
contains(p.types, type) && !p.conversions[index]) {
// this (conversion) type already exists, remove it
param.types.splice(t, 1);
param.conversions.splice(t, 1);
t--;
break;
}
}
}
t++;
}
}
}
return signatures;
}
/**
* create a map with normalized signatures as key and the function as value
* @param {Signature[]} signatures An array with split signatures
* @return {Object.<string, function>} Returns a map with normalized
* signatures as key, and the function
* as value.
*/
function mapSignatures(signatures) {
var normalized = {};
for (var i = 0; i < signatures.length; i++) {
var signature = signatures[i];
if (signature.fn) {
var params = signature.params.join(',');
normalized[params] = signature.fn;
}
}
return normalized;
}
/**
* Parse signatures recursively in a node tree.
* @param {Signature[]} signatures Array with expanded signatures
* @param {Param[]} path Traversed path of parameter types
* @return {Node} Returns a node tree
*/
function parseTree(signatures, path) {
var i, signature;
var index = path.length;
var nodeSignature;
var filtered = [];
for (i = 0; i < signatures.length; i++) {
signature = signatures[i];
// filter the first signature with the correct number of params
if (signature.params.length === index && !nodeSignature) {
nodeSignature = signature;
}
if (signature.params[index] != undefined) {
filtered.push(signature);
}
}
// sort the filtered signatures by param
filtered.sort(function (a, b) {
return Param.compare(a.params[index], b.params[index]);
});
// recurse over the signatures
var entries = [];
for (i = 0; i < filtered.length; i++) {
signature = filtered[i];
// group signatures with the same param at current index
var param = signature.params[index];
// TODO: replace the next filter loop
var existing = entries.filter(function (entry) {
return entry.param.overlapping(param);
})[0];
//var existing;
//for (var j = 0; j < entries.length; j++) {
// if (entries[j].param.overlapping(param)) {
// existing = entries[j];
// break;
// }
//}
if (existing) {
if (existing.param.varArgs) {
throw new Error('Conflicting types "' + existing.param + '" and "' + param + '"');
}
existing.signatures.push(signature);
}
else {
entries.push({
param: param,
signatures: [signature]
});
}
}
// parse the childs
var childs = new Array(entries.length);
for (i = 0; i < entries.length; i++) {
var entry = entries[i];
childs[i] = parseTree(entry.signatures, path.concat(entry.param))
}
return new Node(path, nodeSignature, childs);
}
/**
* Generate an array like ['arg0', 'arg1', 'arg2']
* @param {number} count Number of arguments to generate
* @returns {Array} Returns an array with argument names
*/
function getArgs(count) {
// create an array with all argument names
var args = [];
for (var i = 0; i < count; i++) {
args[i] = 'arg' + i;
}
return args;
}
/**
* Compose a function from sub-functions each handling a single type signature.
* Signatures:
* typed(signature: string, fn: function)
* typed(name: string, signature: string, fn: function)
* typed(signatures: Object.<string, function>)
* typed(name: string, signatures: Object.<string, function>)
*
* @param {string | null} name
* @param {Object.<string, function>} signatures
* @return {function} Returns the typed function
* @private
*/
function _typed(name, signatures) {
var refs = new Refs();
// parse signatures, expand them
var _signatures = parseSignatures(signatures);
if (_signatures.length == 0) {
throw new Error('No signatures provided');
}
// parse signatures into a node tree
var node = parseTree(_signatures, []);
//var util = require('util');
//console.log('ROOT');
//console.log(util.inspect(node, { depth: null }));
// generate code for the typed function
var code = [];
var _name = name || '';
var _args = getArgs(maxParams(_signatures));
code.push('function ' + _name + '(' + _args.join(', ') + ') {');
code.push(' "use strict";');
code.push(node.toCode(refs, ' '));
code.push('}');
// generate body for the factory function
var body = [
refs.toCode(),
'return ' + code.join('\n')
].join('\n');
// evaluate the JavaScript code and attach function references
var factory = (new Function(refs.name, 'createError', body));
var fn = factory(refs, createError);
//console.log('FN\n' + fn.toString()); // TODO: cleanup
// attach the signatures with sub-functions to the constructed function
fn.signatures = mapSignatures(_signatures);
return fn;
}
/**
* Calculate the maximum number of parameters in givens signatures
* @param {Signature[]} signatures
* @returns {number} The maximum number of parameters
*/
function maxParams(signatures) {
var max = 0;
for (var i = 0; i < signatures.length; i++) {
var len = signatures[i].params.length;
if (len > max) {
max = len;
}
}
return max;
}
/**
* Get the type of a value
* @param {*} x
* @returns {string} Returns a string with the type of value
*/
function getTypeOf(x) {
for (var type in types) {
if (types.hasOwnProperty(type) && type !== 'Object') {
// Array and Date are also Object, so test for Object afterwards
if (types[type](x)) return type;
}
}
if (types['Object'](x)) return type;
return 'unknown';
}
/**
* Test whether an array contains some entry
* @param {Array} array
* @param {*} entry
* @return {boolean} Returns true if array contains entry, false if not.
*/
function contains(array, entry) {
return array.indexOf(entry) !== -1;
}
// data type tests
var types = {
'null': function (x) {
return x === null
},