@makano/rew 1.4.6 → 1.4.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.4.6",
3
+ "version": "1.4.7",
4
4
  "description": "A simple coffescript runtime and app manager",
5
5
  "main": "main.js",
6
6
  "directories": {