-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
378 lines (373 loc) · 11.9 KB
/
index.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
(function() {
"use strict";
const Generator = Object.getPrototypeOf((function*() {})()).constructor,
AsyncGenerator = Object.getPrototypeOf((async function*() {})()).constructor,
leftEqual = async (a,b) => {
if(a===b) return true;
if(typeof(a)!==typeof(b)) return false;
if((a && !b) || (!a && b)) return false;
if(a && typeof(a)==="object") {
if(a instanceof RegExp) {
return a.test(b);
}
if(Array.isArray(a)) {
if(!Array.isArray(b)) return false;
}
if(a instanceof Date) {
if(!(b instanceof Date)) return false;
return a.getTime()===b.getTime();
} else if(a instanceof Map) {
if(!(b instanceof Map)) return false;
if(a.size!==b.size) return false;
for(const [key,value] of a) {
if(!await leftEqual(value,b.get(key))) return false;
}
return true;
} else if(a instanceof Set) {
if(!(b instanceof Set)) return false;
if(a.size!==b.size) return false;
const results = new Set();
for(const avalue of a) {
if(b.has(avalue)) {
results.add(avalue);
} else {
for(const bvalue of b) {
if(await leftEqual(avalue,bvalue)) {
results.add(avalue);
}
}
}
}
if(results.size!=a.size) return false;
return true;
} else{
return await every(Object.keys(a),async (key) => {
const test = toTest(key,true);
if(typeof(test)==="function") {
let count = 0;
return await every(Object.keys(b),async (rkey) => {
if(await test.call(a,rkey)) {
count++;
return await leftEqual(a[key],b[rkey]);
}
return true;
}) && count > 0;
}
return await leftEqual(a[key],b[key]);
});
}
}
return false;
},
_reduceIterable = async (iterable,f,accum,{continuable,data,tree,nodes}) => {
let i = 0;
for await(const item of iterable) {
let [key,value] = data instanceof Map || tree ? item : [i,item];
if(accum===undefined && i===0) {
if(tree && typeof(value)==="object") {
accum = await reduce(value,f,accum,continuable,nodes,key)
} else {
accum = value;
}
} else {
if(tree && typeof(value)==="object") {
accum = await reduce(value,f,accum,continuable,nodes,key)
} else {
accum = await f(accum,value,key,data);
}
}
i++;
if(accum===undefined && !continuable) break;
}
return accum;
},
reduce = async (data,f,accum,continuable=true,nodes,recursing) => {
if(!data || typeof(data)!=="object") {
return f(accum,data);
}
if(nodes && recursing) await f(data,recursing,data);
let iterable = data, tree;
if(Symbol.iterator in data) {
iterable = (async function*(data) {
for(const item of data) {
yield item;
}
})(data);
} else if(!Array.isArray(data) && !(Symbol.asyncIterator in data)) {
iterable = Object.entries(data);
tree = true;
}
return _reduceIterable(iterable,f,accum,{continuable,data,tree,nodes});
},
_mapIterable = async (asyncIterable,f,accum,{skipUndefined,tree,data}) => {
let i = 0;
for await(const item of asyncIterable) {
const [key,value] = data instanceof Map || tree ? item : [i,item];
i++;
let newvalue;
if(tree && value && typeof(value)==="object") {
newvalue = await map(value,f,skipUndefined);
} else {
newvalue = await f(value,key,data);
}
if(newvalue===undefined && skipUndefined) {
continue;
}
if(accum instanceof Set) {
await accum.add(newvalue);
} else if(accum instanceof Map || exposes(accum,{del:true,get:true,set:true,})) {
await accum.set(key,newvalue);
} else if(exposes(accum,{getItem:true,removeItem:true,setItem:true})) {
await accum.setItem(key,newvalue);
} else {
accum[key] = newvalue;
}
}
return accum;
},
map = async (data,f,skipUndefined) => {
if(!data || typeof(data)!=="object") {
return f(data);
}
let tree, iterable = data, accum;
if(Symbol.iterator in data) {
iterable = (async function*(data) {
for(const item of data) {
yield item;
}
})(data);
} else if(!Array.isArray(data) && !(Symbol.asyncIterator in data)) {
iterable = Object.entries(data);
tree = true;
}
if(Array.isArray(data)) {
accum = [];
} else if(data instanceof Set) {
accum = new Set();
} else if(data instanceof Map) {
accum = new Map();
} else if(Symbol.iterator in data || Symbol.asyncIterator in data) {
accum = [];
} else {
accum = Object.create(Object.getPrototypeOf(data));
}
accum = await _mapIterable(iterable,f,accum,{skipUndefined,tree,data});
if(data instanceof Generator) {
if(Symbol.iterator in data) {
return (function*() { for(const item of accum) yield accum; })();
}
return (async function*() { for(const item of accum) yield accum; })();
}
return accum;
},
_forEachIterable = async (asyncIterable,f,{nodes,tree,data}) => {
let i = 0;
for await(const item of asyncIterable) {
const [key,value] = data instanceof Map || tree ? item : [i,item];
i++;
await forEach(value,f,nodes,key);
}
},
forEach = async (data,f,nodes,recursing) => {
if(!data || typeof(data)!=="object") {
return f(data);
}
if(nodes && recursing) await f(data,recursing,data);
let tree, iterable = data;
if(Symbol.iterator in data) {
iterable = (async function*(data) {
for(const item of data) {
yield item;
}
})(data);
} else if(!Array.isArray(data) && !(Symbol.asyncIterator in data)) {
iterable = Object.entries(data);
tree = true;
}
await _forEachIterable(iterable,f,{nodes,tree,data});
return true;
},
every = async (data,f,nodes) => {
let iterable = data, tree;
if(Symbol.iterator in data) {
iterable = (async function*(data) {
for(const item of data) {
yield item;
}
})(data);
} else if(!Array.isArray(data) && !(Symbol.asyncIterator in data)) {
iterable = Object.entries(data);
tree = true;
}
return await _reduceIterable(iterable,async (accum,value) => await f(value) ? accum : undefined,true,{continuable:false,nodes,tree,data}) ? true : false;
},
some = async (data,f,nodes) => {
let iterable = data, tree, found;
if(Symbol.iterator in data) {
iterable = (async function*(data) {
for(const item of data) {
yield item;
}
})(data);
} else if(!Array.isArray(data) && !(Symbol.asyncIterator in data)) {
iterable = Object.entries(data);
tree = true;
}
await _reduceIterable(iterable,async (accum,value) => {
if(await f(value)) {
found = true;
return undefined;
}
return true;
},
true,{continuable:false,nodes,tree,data});
return found;
},
exposes = (target,api) => Object.keys(api).every((key) => typeof(target[key])==="function"),
toTest = (data,property) => {
const type = typeof(data);
if(type==="function") {
return data;
}
if(property) {
if(type==="string") {
const key = data.replace(/\n/g,"");
try {
const i = key.lastIndexOf("/");
if(key[0]==="/" && i>0) {
const exp = key.substring(1,i),
flags = key.substring(i+1),
regexp = new RegExp(exp,flags);
return (key) => regexp.test(key);
} else if(key.startswith("function") || `/\(*\)*=>*`.test(key)) {
return Function("return " + key)();
}
} catch(e) {
;
}
}
}
if(data===null || ["boolean","number","string"].includes(type)) {
return arg => arg===data;
}
if(data && type==="object") {
if(data instanceof RegExp) {
return arg => data.test(arg);
}
}
return (arg) => leftEqual(data,arg);
},
// like pipe, but stops when accum is undefined
flow = (...values) => async (arg) => {
return await reduce(values,async (arg,value) => {
return typeof(value)==="function"
? await value(arg)
: value
},arg,false)
},
// passes an initial value through a series of funtions, literals may be injecte dand will replace the reduction at the point of injection
pipe = (...values) => async (arg) => {
return await reduce(values,async (arg,value) => {
return typeof(value)==="function"
? await value(arg)
: value
},arg)
},
// returns condition (a function, a RegExp, a literal to compare to arg) evaluation
// if true before returning sets a timeout to start processing all callbacks
trigger = (condition,...results) => async (arg) => {
let done;
if(condition!==undefined && await toTest(condition)(arg)) {
setTimeout(() => results.forEach((result) => result(arg)));
return true;
}
return false;
},
// condition = (a function, a RegExp, a literal to compare to arg) is true;
// evaluates the results using the arg until one satifies condition or all are evaluated, return satisfying result
// else returns the arg passed in (which may have been modified)
until = (condition,...functions) => async (arg) => {
const test = toTest(await condition);
let result;
for(let value of functions) {
value = await toTest(await value);
if(typeof(value)==="function") {
value = await value(arg);
}
if(await test(value)) {
return value;
}
}
return arg;
},
// if condition (a function, a RegExp, a literal to compare to arg) is true;
// evaluates the results using the arg until one is undefined or all are evaluated, return final evaluation
// else returns the arg passed in (which may have been modified)
when = (condition,...results) => async (arg) => {
let done;
if(condition!==undefined && await toTest(condition)(arg)) {
let result;
for(let value of results) {
value = await value;
if(typeof(value)==="function") {
result = await value(arg);
}
if(result && typeof(result)==="object") {
const keys = Object.keys(result);
if(keys.length<=2 && result.done && keys.every(key => key==="done" || key==="value")) {
done = result;
}
}
if(result===undefined || done) break;
}
return done && done.value!==undefined ? done.value : result;
}
return arg;
},
// if condition (a function, a RegExp, a literal to compare to arg) is true;
// evaluates the results using the arg until one is undefined, {done: true}, or all are evaluated
// returns value from {value:value,done:true} when it occurs or undefined no step returns {value:value,done:true}
route = (condition,...results) => async (arg) => {
if(condition!==undefined && await toTest(condition)(arg)) {
for(let value of results) {
value = await value;
if(typeof(value)==="function") {
value = await value(arg);
}
if(value && typeof(value)==="object") {
const keys = Object.keys(value);
if(value.done && keys.every(key => key==="done" || key==="value")) {
return value.value;
}
}
if(value===undefined) return;
}
}
return;
},
router = (...routes) => {
return async (arg) => {
const result = await until((arg) => arg!==undefined,...routes)(arg);
return result && typeof(result)==="object" && Object.keys(arg).every(key => key==="done" || key==="value") ? result.value : result!==undefined ? result : arg;
}
},
parallel = (...values) => async (...args) => {
const promises = [],
results = [];
let i = 0;
for(const value of values) {
const promise = (async () => typeof(value)==="function" ? value(...args).then(result => results.push(result)) : value)();
promises.push(promise);
i++;
}
await Promise.all(promises);
return results;
};
const assentials = {every,exposes,flow,forEach,map,parallel,pipe,reduce,route,router,some,when,trigger,leftEqual};
if(typeof(module)!=="undefined") {
module.exports = assentials;
}
if(typeof(window)!=="undefined") {
window.assentials = assentials;
}
}).call(this);