@makano/rew 1.4.5 → 1.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,7 +41,7 @@ function typedef(value, strict = false) {
|
|
41
41
|
});
|
42
42
|
}
|
43
43
|
|
44
|
-
function typef(fn, returnType) {
|
44
|
+
function typef(fn, returnType, argumentsTypes) {
|
45
45
|
if(typeof returnType == "function"){
|
46
46
|
const ref = fn;
|
47
47
|
fn = returnType;
|
@@ -51,7 +51,17 @@ function typef(fn, returnType) {
|
|
51
51
|
throw new Error('First argument must be a function');
|
52
52
|
}
|
53
53
|
if (typeof returnType == 'function' && returnType.type instanceof Type) returnType = returnType.type;
|
54
|
+
const requiredArguments = Array.isArray(argumentsTypes) ? argumentsTypes.filter(i => Array.isArray(i) ? !i.includes(null) : true) : [];
|
54
55
|
const wrappedFn = function(...args){
|
56
|
+
if(argumentsTypes && Array.isArray(argumentsTypes)){
|
57
|
+
if(args.length !== requiredArguments.length && args.length !== argumentsTypes.length){
|
58
|
+
throw new TypeError(`Function ${fn.name || '<anonymous>'} takes exactly ${requiredArguments.length} parameters`)
|
59
|
+
}
|
60
|
+
const argumentsTyped = typeAre(args, argumentsTypes);
|
61
|
+
if(argumentsTyped !== false){
|
62
|
+
throw new TypeError(`Function ${fn.name || '<anonymous>'} call error: Parameter at index ${argumentsTyped} is of the wrong type`);
|
63
|
+
}
|
64
|
+
}
|
55
65
|
const result = fn.call(this, ...args);
|
56
66
|
if(!typeis(result, wrappedFn.returnType)){
|
57
67
|
throw new TypeError(`Function ${fn.name || '<anonymous>'} does not return it's own return type.`);
|
@@ -60,13 +70,32 @@ function typef(fn, returnType) {
|
|
60
70
|
}
|
61
71
|
wrappedFn.returnType = returnType;
|
62
72
|
wrappedFn.type = returnType;
|
73
|
+
wrappedFn.argumentsTypes = argumentsTypes;
|
63
74
|
return wrappedFn;
|
64
75
|
}
|
65
|
-
typef.is = function(func, returnType){
|
76
|
+
typef.is = function(func, returnType, argumentsTypes){
|
66
77
|
return typeis(func.returnType.defaultValue, returnType);
|
67
78
|
}
|
68
79
|
|
80
|
+
const typeAre = (values, types) => {
|
81
|
+
const verified = values.map((t, i) => Array.isArray(types[i]) ? (types[i].map((t2) => typeis(t, t2)).includes(true)) : typeis(t, types[i]));
|
82
|
+
const hasWrong = verified.indexOf(false);
|
83
|
+
return hasWrong > -1 ? hasWrong : false;
|
84
|
+
}
|
85
|
+
|
69
86
|
function typeis(obj, typeDef, missingObjects = false) {
|
87
|
+
|
88
|
+
if(obj == null && typeDef === null) return true;
|
89
|
+
else if(obj == null) return false;
|
90
|
+
if(obj == undefined && typeDef === undefined) return true;
|
91
|
+
else if(obj == undefined) return false;
|
92
|
+
|
93
|
+
if(typeDef == null && obj === null) return true;
|
94
|
+
else if(typeDef == null) return false;
|
95
|
+
|
96
|
+
if(typeDef == undefined && obj === undefined) return true;
|
97
|
+
else if(typeDef == undefined) return false;
|
98
|
+
|
70
99
|
// Resolve Type
|
71
100
|
if (typeof typeDef == 'function' && typeDef.type instanceof Type) typeDef = typeDef.type;
|
72
101
|
|
@@ -123,6 +152,7 @@ function typeis(obj, typeDef, missingObjects = false) {
|
|
123
152
|
|
124
153
|
return missingObjects ? [true] : true;
|
125
154
|
}
|
155
|
+
typeis.multi = (values, types) => typeAre(values, types);
|
126
156
|
|
127
157
|
function typex(child, parent) {
|
128
158
|
return child.prototype instanceof parent || child === parent;
|
@@ -63,10 +63,6 @@ module.exports.prepareContext = function (
|
|
63
63
|
...custom_context,
|
64
64
|
Usage
|
65
65
|
};
|
66
|
-
globalEmitter.on('attach_name', (name, object) => {
|
67
|
-
context[name] = object;
|
68
|
-
});
|
69
|
-
globalEmitter.on('detach_name', (name) => delete context[name]);
|
70
66
|
std.prototype = { ns: (cb) => {
|
71
67
|
return new (class extends STDNS {
|
72
68
|
constructor(){
|
@@ -143,6 +139,10 @@ module.exports.prepareContext = function (
|
|
143
139
|
})
|
144
140
|
context.std = std;
|
145
141
|
}
|
142
|
+
globalEmitter.on('attach_name', (name, object) => {
|
143
|
+
context[name] = object;
|
144
|
+
});
|
145
|
+
globalEmitter.on('detach_name', (name) => delete context[name]);
|
146
146
|
if (!context.process)
|
147
147
|
context.process = {
|
148
148
|
argv: options.argv || process.argv,
|