-
Notifications
You must be signed in to change notification settings - Fork 0
/
parametrize.js
124 lines (102 loc) · 2.98 KB
/
parametrize.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
'use strict';
(function (globalObj, isModuleGlobal) {
var noArguments = '\\';
var strict = false;
var argsTree = {};
var funcs = {};
var Func = {
new: addFunction,
get: addFunction,
set: addFunction,
strict: function (bool) {
strict = bool;
console.warn('parametrize strict mode is set to ' + strict);
}
};
if (isModuleGlobal) {
globalObj.exports = Func;
} else {
globalObj.Func = Func;
}
function addFunction(name, params, func) {
if (!funcs[name]) {
var newFunc = setupNewFunction(name);
var initialFunction = (strict) ? errorFunction.bind(null, name) : function () {};
setThroughPath(argsTree, [name, noArguments], initialFunction);
newFunc.parametrize = newFunc.overload = function (params, func) {
return addFunction(name, params, func);
};
funcs[name] = newFunc;
}
if (!func) {
if (params) {
setThroughPath(argsTree, [name, noArguments], params);
}
return funcs[name];
}
var constructorsNames = getConstructorNames(params);
setThroughPath(argsTree, [name].concat(constructorsNames), func);
return funcs[name];
};
function setupNewFunction(funcName) {
return function () {
var func = getFunctonBasedOnArguments(funcName, arguments);
return func.apply(this, arguments);
};
};
function getFunctonBasedOnArguments(funcName, args) {
var argsTypes = [].map.call(args, function (arg) {
return arg.constructor.name;
});
var func = void 0;
try {
if (!argsTypes.length) {
return argsTree[funcName][noArguments];
}
func = getThroughPath(argsTree, [funcName].concat(argsTypes));
} catch (e) {
if (strict) {
throw TypeError (
"Calling '" + funcName + "' with argument types [" + argsTypes.join(', ') + "] doesn't match any defined signature."
);
}
func = argsTree[funcName][noArguments];
}
return func;
}
function getConstructorNames(constructorsArr) {
return constructorsArr.map(function (c) {
return c.name;
});
}
function getThroughPath(obj, path) {
var value = obj;
for (var i = 0; i < path.length; i++) {
value = value[path[i]];
}
return value;
}
function setThroughPath(obj, path, value) {
var dest = obj;
for (var i = 0; i < path.length - 1; i++) {
if (!dest[path[i]]) {
dest = dest[path[i]] = {};
} else {
dest = dest[path[i]];
}
}
if (isObject(value)) {
dest[path[i]] = dest[path[i]] || {};
Object.assign(dest[path[i]], value);
} else {
dest[path[i]] = value;
}
return obj;
}
function errorFunction (name) {
throw TypeError ("Calling '" + name + "' with argument types [] doesn't match any defined signature.");
}
function isObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
})(typeof module !== 'undefined' ? module : window, typeof module !== 'undefined');