-
Notifications
You must be signed in to change notification settings - Fork 0
/
is.js
384 lines (363 loc) · 11.2 KB
/
is.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
export const is = (function(){
// 'Types' is a dict of definitions, referenced by the 'is' library below
// build from an object with empty prototype
const Types = Object.assign( Object.create(null, {
// supporting methods for our type defs
size: {
get: function() {
return Object.keys(this).length
}
},
findBy: {
value: function(p,v) {
return Object.entries(this).find(
([,defObj])=> defObj[p]===v
)?.at(1) ?? null
}
},
validators: {
get: function() {
Object.entries(this).map(
([typename,{x,y,z,q,valdtr}])=> [typename, valdtr]
)
}
},
typeOf: {
get: function() {
return obj=> this.validators.find(
([ , val])=> val(obj) === true
).at(0)
}
},
descriptions:{
get: function() {
return Object.entries(this).map(
([typeName, { name, abbrev, char, constr, valdtr}])=>
[ typeName, name, abbrev, char ]
)
}
}
}),
// type definitions
// should be in the form
// [abbrev] : {
// name: string,
// abbrev: string,
// char: single character string,
// constr: reference,
// valdtr: function
// }
{
nul: {
name: 'null',
abbrev: 'nul',
char: '∅',
constr: null,
valdtr: _ => typeof _==='object' && !_
},
obj: {
name: 'object',
abbrev: 'obj',
char: 'O',
constr: Object,
valdtr: _ => typeof _==='object' && _?.constructor?.name === 'Object'
},
num: {
name: 'number',
abbrev: 'num',
char: '№',
constr: Number,
valdtr: _ => typeof _==='number' && Number.isFinite(_)
},
int: {
name: 'integer',
abbrev: 'int',
char: 'ℤ',
constr: Number,
valdtr: _ => Number.isInteger(_) && !(isNaN(_))
},
arr: {
name: 'Array',
abbrev: 'arr',
char: '₳',
constr: Array,
valdtr: _ => Array.isArray(_)
},
undf: {
name: 'undefined',
abbrev: 'undf',
char: '⁇',
constr: null,
valdtr: _ => typeof _ === 'undefined'
},
inf: {
name: 'Infinity',
abbrev: 'inf',
char: '∞',
constr: Object(Infinity),
valdtr: _ => _ === Infinity
},
bool: {
name: 'boolean',
abbrev: 'bool',
char: '◐',
constr: Boolean,
valdtr: _ => typeof _ === 'boolean'
},
bigN: {
name: 'bigint',
abbrev: 'bigN',
char: 'N',
constr: BigInt,
valdtr: _ => typeof _ === 'bigint'
},
str: {
name: 'string',
abbrev: 'str',
char: 's',
constr: String,
valdtr: _ => typeof _ === 'string'
},
symb: {
name: 'symbol',
abbrev: 'symb',
char: '§',
constr: Symbol,
valdtr: _ => typeof _ === 'symbol'
},
func: {
name: 'function',
abbrev: 'func',
char: 'ƒ',
constr: Function,
valdtr: _ => typeof _ === 'function'
},
rgx: {
name: 'RegExp',
abbrev: 'rgx',
char: 'ℝ',
constr: RegExp,
valdtr: _ => _ instanceof RegExp
},
evt: {
name: 'Event',
abbrev: 'evt',
char: 'ℯ',
constr: Event,
valdtr: _ => _ instanceof Event
},
err: {
name: 'Error',
abbrev: 'err',
char: '∃',
constr: Error,
valdtr: _ => _ instanceof Error
},
map: {
name: 'Map',
abbrev: 'map',
char: '∭',
constr: Map,
valdtr: _ => _ instanceof Map
},
wmp: {
name: 'WeakMap',
abbrev: 'wmp',
char: '∰',
constr: WeakMap,
valdtr: _ => _ instanceof WeakMap
},
set: {
name: 'Set',
abbrev: 'set',
char: '∬',
constr: Set,
valdtr: _ => _ instanceof Set
},
wst: {
name: 'WeakSet',
abbrev: 'wst',
char: '∯',
constr: WeakSet,
valdtr: _ => _ instanceof WeakSet
},
prm: {
name: 'Promise',
abbrev: 'prm',
char: '⅌',
constr: Promise,
valdtr: _ => _ instanceof Promise
},
prx: {
name: 'Proxy',
abbrev: 'prx',
char: 'ℙ',
constr: Proxy,
valdtr: _ => undefined
},
// need to determine a way to detect proxy objects
wrf: {
name: 'WeakRef',
abbrev: 'wrf',
char: '⍵',
constr: WeakRef,
valdtr: _ => _ instanceof WeakRef
},
wrk: {
name: 'Worker',
abbrev: 'wrk',
char: '⚒',
constr: Worker,
valdtr: _ => _ instanceof Worker
},
dom: {
name: 'DomNode',
abbrev: 'dom',
char: 'd',
constr: Node,
valdtr: _=> _ instanceof EventTarget || _ instanceof DocumentFragment
},
elem: {
name: 'Element',
abbrev: 'elem',
char: 'ℰ',
constr: Element,
valdtr: _=> _.constructor?.name.includes('lement')
&& _ instanceof Element
}
} )
// 'is' is the library containing the type checking methods
// so we can start from empty prototype object
return Object.assign(Object.create(null, Object.getOwnPropertyDescriptors({
// internal method used by 'argsLike' to check the type
// of an invidual list item
checkArgType(val, tStr) {
if (['*','?'].includes(tStr)) {
return true
}
if (tStr.indexOf('|') > 0) {
return tStr.replace(' ','').split('|').map(
(typ) => typ in Types && is[typ](val)
).includes(true)
}
else {
return is[tStr](val)
}
},
// this is the method used to check the type shape of a list of arguments
// can be partially applied to build a quick shortcut validator for a given
// object or function.
// args - the list of values/objects to check type of
// argTypes - an array/list of string type abbreviations
argsLike(args, argTypes) {
if (argTypes === undefined && is.arr(args)) {
return (args)=> is.argsLike(args, argTypes)
}
if (is.num(argTypes)) {
return argTypes === args.length
}
if (!(is.arr(args))) {
args = [args].flat()
}
if (!(is.arr(argTypes))) {
argTypes = is.str(argTypes) && argTypes.indexOf(',') > 0 ?
argTypes.replace(/\s/g,'').split(',')
: Array(args.length).fill().map(()=> argTypes);
}
return ( args.length >= ( argTypes.length - (
(arr, n)=> {
while (arr.pop()=='?')
n++;
return n
})([...argTypes], 0)
)
&& args.length <= argTypes.length
) && !(
args.map((val, i)=>
is.checkArgType(val, argTypes[i])
).includes(false)
)
},
// alias property for registerNewValidator method
get addNew() {
return this.registerNewValidator
},
// alias property for Types.typeOf method
get typeOf() {
return Types.typeOf
},
// add a new type validator named 'type' with validator func 'func'
// to the is object
registerNewValidator(type, func) {
if (!(type in is)) {
Object.defineProperty(is, type, {
value: func,
enumerable: true
})
}
},
// searches all the string fields of each type definition
// for a property equal to keyOrObj
hasType(keyOrObj) {
return this.descriptions.filter(
(strProps)=> strProps.includes(keyOrObj)
).length > 0
},
// method that determines type of items of array
// ex. is.arrOf.str(arr) - true if arr is array containing only strings
// is.arrOf.arr(arr) - true if arr is array containing only arrays
arrOf: new Proxy({}, {
get(_, typ) {
if (!is.arr(_)) {
return (()=> false)
}
return (typ in is) ?
((arr)=> !(arr.map(val=> is[typ](val))).includes(false))
: undefined
}
}),
// Checks the type of 'value' to see if it matches any of the types
// indicated by one or more string type abbreviations in '...types'
like(value, ...types) {
for (const tStr of types) {
if (this.checkArgType(value, tStr)) {
return true
}
}
return false
},
// takes a function as input and returns a version of the function that
// validates the arguments passed to it using the 'argsLike' function above
//
validateArgs(argTypes) {
return (func)=> function(...args) {
if ( is.arr(argTypes) &&
!(is.arr(argTypes[0]) || is.str(argTypes[0]))) {
argTypes = [ argTypes ]
}
const tests = argTypes.map((typeSet)=> is.argsLike(args, typeSet))
if (!(tests.includes(true))) {
return console.error('Arguments ', args,' failed validation')
}
return func.apply(this, args)
}
}
})),
// here we finally combine the Types properties above into the is object
// so that type methods and definitions are ultimately all contained in
// the same 'is' object
Object.entries(Types)
.concat(([
['htmlTag' , (s)=> REF.HTMLTAGS.has(s) ],
['svgTag' , (s)=> REF.SVGTAGS.has(s) ],
['eventName' , (s)=> REF.DOMEVENTNAMES.has(s) ],
['entries' , (s)=> is.arr(s) && !(
s.map((item)=> is.arr(item)).includes(false))
]
]).map(entry=> [ entry[0], { valdtr: entry[1] } ])
).reduce((acc, [abbrev, defObj])=> ({
...acc,
[abbrev] : defObj.valdtr
}) ,{})
)
})()