-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoverload.js
528 lines (461 loc) · 11.5 KB
/
overload.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
(function(TRUE, FALSE, NULL, undefined) {
var root = this;
// Variablizing the strings for consistency
// and to avoid harmful dot-notation look-ups with
// javascript keywords
var sNull = 'Null',
sUndefined = 'Undefined',
sInfinity = 'Infinity',
sDate = 'Date',
sNaN = 'NaN',
sNumber = 'Number',
sString = 'String',
sObject = 'Object',
sArray = 'Array',
sRegExp = 'RegExp',
sBoolean = 'Boolean',
sFunction = 'Function',
sElement = 'Element';
// Utilizing the non-standard (but available in modern browsers) Global Object names
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
// Provide a polyfill for items without names
(function() {
var globalObjects = [
sDate,
sNumber,
sString,
sObject,
sArray,
sRegExp,
sBoolean,
sFunction,
sElement
],
idx = globalObjects.length,
globalObject;
while (idx--) {
globalObject = globalObjects[idx];
if (root[globalObject] !== undefined) {
if (!root[globalObject].name) {
root[globalObject].name = globalObject;
}
}
}
}());
/**
* Possible values
* @type {Object}
*/
var _types = {};
_types[sNull] = 0;
_types[sUndefined] = 1;
_types[sInfinity] = 2;
_types[sDate] = 3;
_types[sNaN] = 4;
_types[sNumber] = 5;
_types[sString] = 6;
_types[sObject] = 7;
_types[sArray] = 8;
_types[sRegExp] = 9;
_types[sBoolean] = 10;
_types[sFunction] = 11;
_types[sElement] = 12;
/**
* Cached reference to Object.prototype.toString
* for type checking
* @type {Function}
*/
var _toString = (function(toString) {
return function(obj) {
return toString.call(obj);
};
}(({}).toString)),
_noopArr = [],
/**
* Type checks
*/
_checkMap = (function(map) {
var types = [
// Only mapping items that need to be mapped.
// Items not in this list are doing faster
// (non-string) checks
//
// 0 = key, 1 = value
[ sDate, _types[sDate] ],
[ sNumber, _types[sNumber] ],
[ sString, _types[sString] ],
[ sObject, _types[sObject] ],
[ sArray, _types[sArray] ],
[ sRegExp, _types[sRegExp] ],
[ sFunction, _types[sFunction] ]
],
idx = types.length;
while (idx--) {
map['[object ' + types[idx][0] + ']'] = types[idx][1];
}
return map;
}({})),
/**
* Mini extend
* @param {Function} base
* @param {Object} obj
* @return {Function} base
*/
extend = function(base, obj) {
var key;
for (key in obj) {
base[key] = obj[key];
}
return base;
};
var callLengths = {
0: function(fn, args, context) {
if (!context) { return fn(); }
return fn.call(context);
},
1: function(fn, args, context) {
return fn.call(context, args[0]);
},
2: function(fn, args, context) {
return fn.call(context, args[0], args[1]);
},
3: function(fn, args, context) {
return fn.call(context, args[0], args[1], args[2]);
}
};
var caller = function(fn, args, context) {
var call = callLengths[args.length];
if (call) { return call(fn, args, context); }
return fn.apply(context, args);
};
var _getConfigurationType = function(val) {
if (val === null) { return _types[sNull]; }
if (val === undefined) { return _types[sUndefined]; }
// we have something, but don't know what
if (!val.name) {
if (val === root[sElement]) { return _types[sElement]; } // Firefox doesn't allow setting the name of Element
if (val !== +val) { return _types[sNaN]; } // NaN check
return _types[sInfinity]; // Infinity check
}
return _types[val.name];
};
var _getParameterType = function(val) {
if (val === null) { return _types[sNull]; }
if (val === undefined) { return _types[sUndefined]; }
if (val === TRUE || val === FALSE) { return _types[sBoolean]; }
if (val && val.nodeType === 1) { return _types[sElement]; } // Element check from Underscore
var typeString = _toString(val);
if (_checkMap[typeString] === _types[sNumber]) {
if (val !== +val) { return _types[sNaN]; } // NaN check
if (!isFinite(val)) { return _types[sInfinity]; } // Finite check
return _types[sNumber]; // definitely a number
}
return _checkMap[typeString];
};
var _convertConfigurationTypes = function(args) {
var parameters = [],
idx = 0, length = args.length,
configItem;
for (; idx < length; idx++) {
configItem = args[idx];
parameters.push(
(configItem instanceof Custom) ? configItem : _getConfigurationType(configItem)
);
}
return parameters;
};
var _convertConfigurationMap = function(map) {
var parameters = {},
key, configItem;
for (key in map) {
configItem = map[key];
parameters[key] = (configItem instanceof Custom) ? configItem : _getConfigurationType(configItem);
}
return parameters;
};
var _convertParametersTypes = function(args) {
var parameters = [],
idx = 0, length = args.length;
for (; idx < length; idx++) {
parameters.push(_getParameterType(args[idx]));
}
return parameters;
};
var _doesMapMatchArgsTypes = function(map, argTypes, args) {
var mapLength = map.length,
argLength = argTypes.length;
if (mapLength === 0 && argLength === 0) { return TRUE; }
if (mapLength !== argLength) { return FALSE; }
var idx = 0,
mapItem;
for (; idx < argLength; idx++) {
mapItem = map[idx];
if (mapItem instanceof Custom) {
if (mapItem.check(args[idx])) {
continue;
}
return FALSE;
}
if (argTypes[idx] !== mapItem) {
return FALSE;
}
}
return TRUE;
};
var _getArgumentMatch = function(mappings, args) {
if (!mappings) { return; }
var argTypes = _convertParametersTypes(args),
idx = 0, length = mappings.length;
for (; idx < length; idx++) {
if (_doesMapMatchArgsTypes(mappings[idx].params, argTypes, args)) {
return mappings[idx];
}
}
};
var _getLengthMatch = function(mappings, args) {
if (!mappings) { return; }
var argLength = args.length,
idx = 0, length = mappings.length;
for (; idx < length; idx++) {
if (mappings[idx].length === argLength) {
return mappings[idx];
}
}
};
var _matchAny = function(args, val) {
var type = _getParameterType(val),
idx = args.length,
mapItem;
while (idx--) {
mapItem = args[idx];
if (mapItem instanceof Custom) {
if (mapItem.check(val)) {
return TRUE;
}
continue;
}
if (args[idx] === type) {
return TRUE;
}
}
return FALSE;
};
var _matchMap = function(config, map) {
var key, configItem, mapItem;
for (key in config) {
configItem = config[key];
mapItem = map[key];
if (configItem instanceof Custom) {
if (!configItem.check(mapItem)) {
return FALSE;
}
continue;
}
if (configItem !== _getParameterType(mapItem)) {
return FALSE;
}
}
return TRUE;
};
/**
* Custom type that validates a value
* @constructor
* @param {Function} check
*/
var Custom = function(check) {
this.check = check;
};
var o = {
wild: new Custom(function() {
return TRUE;
}),
truthy: new Custom(function(val) {
return !!val === TRUE;
}),
falsy: new Custom(function(val) {
return !!val === FALSE;
}),
any: function() {
var args = _convertConfigurationTypes(arguments);
return new Custom(function(val) {
return _matchAny(args, val);
});
},
except: function() {
var args = _convertConfigurationTypes(arguments);
return new Custom(function(val) {
return !_matchAny(args, val);
});
},
map: function(map) {
var mapConfig = _convertConfigurationMap(map);
return new Custom(function(map) {
return _matchMap(mapConfig, map);
});
}
};
var fn = {
/**
* Methods mapped to argument types
* Lazily instanciated
* @type {Array} argument mapping
*/
// this._m;
/**
* Methods mapped to argument lengths
* Lazily instanciated
* @type {Array} length mapping
*/
// this._l;
/**
* A fallback function if none
* of the criteria match on a call
* @type {Function}
*/
// this._f;
map: function(map) {
var self = this;
return {
use: function(method) {
var argMappings = self._m || (self._m = []);
argMappings.push({
params: [o.map(map)],
method: method
});
return self;
}
};
},
args: function() {
var self = this,
args = arguments;
return {
use: function(method) {
var argMappings = self._m || (self._m = []);
argMappings.push({
params: _convertConfigurationTypes(args),
method: method
});
return self;
}
};
},
len: function(num) {
var self = this;
return {
use: function(method) {
var lengthMappings = self._l || (self._l = []);
lengthMappings.push({
length: (num === undefined) ? method.length : num,
method: method
});
return self;
}
};
},
error: function(method) {
this._err = method;
return this;
},
fallback: function(method) {
this._f = method;
return this;
},
call: function() {
// prevent function deoptimation
var args = arguments, a = [];
for (var idx = 1, length = args.length; idx < length; idx++) {
a[idx] = args[idx];
}
return this._call(args[0], a);
},
apply: function(context, args) {
var a = args;
if (args && args.callee) {
// passed an arguments object,
// not an array.
// prevent function deoptimation
a = [];
for (var idx = 0, length = args.length; idx < length; idx++) {
a[idx] = args[idx];
}
}
return this._call(context, a);
},
bind: function(context) {
var self = this;
return function() {
// prevent function deoptimation
var args = arguments, a = [];
for (var idx = 0, length = args.length; idx < length; idx++) {
a[idx] = args[idx];
}
return self._call(context, a);
};
},
expose: function() {
var self = this;
return function() {
// prevent function deoptimation
var args = arguments, a = [];
for (var idx = 0, length = args.length; idx < length; idx++) {
a[idx] = args[idx];
}
return self._call(this, a);
};
},
_call: function(context, args) {
if (context === root) { context = null; }
args = args || _noopArr;
// Any argument match, of course, already matches
// the length match, so this should be done first
var argMatch = _getArgumentMatch(this._m, args);
if (argMatch) {
return caller(argMatch.method, args, context);
}
// Check for a length match
var lengthMatch = _getLengthMatch(this._l, args);
if (lengthMatch) {
return caller(lengthMatch.method, args, context);
}
// Check for a fallback
if (this._f) {
return caller(this._f, args, context);
}
// Error
return this._err ? this._err(args) : api.err;
}
};
fn.fail = fn.err = fn.error;
fn.count = fn.size = fn.len;
var api = function() {
var overload = function overload() {
return overload._call(overload, arguments);
};
return extend(overload, fn);
};
api.o = o;
api.fn = fn;
api.err = function() {
throw 'overload - exception: No methods matched';
};
api.define = api.defineType = function(name, check) {
var custom = new Custom(check);
return (o[name] = custom);
};
api.defineTypes = function(obj) {
var key;
for (key in obj) {
api.define(key, obj[key]);
}
return api;
};
if (typeof define === 'function') { // RequireJS
define(function() { return api; });
} else if (typeof module !== 'undefined' && module.exports) { // CommonJS
module.exports = api;
} else {
root.overload = api;
root.o = o;
}
}(true, false, null));