-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathes.js
3322 lines (2985 loc) · 87.7 KB
/
es.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* rubico v2.6.2
* https://github.com/a-synchronous/rubico
* (c) 2019-2024 Richard Tong
* rubico may be freely distributed under the MIT license.
*/
const isArray = Array.isArray
const isPromise = value => value != null && typeof value.then == 'function'
const areAnyValuesPromises = function (values) {
if (isArray(values)) {
const length = values.length
let index = -1
while (++index < length) {
const value = values[index]
if (isPromise(value)) {
return true
}
}
return false
}
for (const key in values) {
const value = values[key]
if (isPromise(value)) {
return true
}
}
return false
}
const promiseAll = Promise.all.bind(Promise)
const funcConcat = (
funcA, funcB,
) => function pipedFunction(...args) {
const intermediate = funcA(...args)
return isPromise(intermediate)
? intermediate.then(funcB)
: funcB(intermediate)
}
const funcApply = (func, args) => func(...args)
const __ = Symbol.for('placeholder')
// argument resolver for curry2
const curry2ResolveArg0 = (
baseFunc, arg1,
) => function arg0Resolver(arg0) {
return baseFunc(arg0, arg1)
}
// argument resolver for curry2
const curry2ResolveArg1 = (
baseFunc, arg0,
) => function arg1Resolver(arg1) {
return baseFunc(arg0, arg1)
}
const curry2 = function (baseFunc, arg0, arg1) {
return arg0 == __
? curry2ResolveArg0(baseFunc, arg1)
: curry2ResolveArg1(baseFunc, arg0)
}
const pipe = function (...args) {
const funcs = args.pop()
const pipeline = funcs.reduce(funcConcat)
if (args.length == 0) {
return pipeline
}
if (areAnyValuesPromises(args)) {
return promiseAll(args).then(curry2(funcApply, pipeline, __))
}
return pipeline(...args)
}
const compose = function (...args) {
const funcs = args.pop()
const composition = funcs.reduceRight(funcConcat)
if (args.length == 0) {
return composition
}
if (areAnyValuesPromises(args)) {
return promiseAll(args).then(curry2(funcApply, composition, __))
}
return composition(...args)
}
const always = value => function getter() { return value }
const thunkifyArgs = (func, args) => function thunk() {
return func(...args)
}
const thunkConditional = (
conditionalExpression, thunkOnTruthy, thunkOnFalsy,
) => conditionalExpression ? thunkOnTruthy() : thunkOnFalsy()
// argument resolver for curry3
const curry3ResolveArg0 = (
baseFunc, arg1, arg2,
) => function arg0Resolver(arg0) {
return baseFunc(arg0, arg1, arg2)
}
// argument resolver for curry3
const curry3ResolveArg1 = (
baseFunc, arg0, arg2,
) => function arg1Resolver(arg1) {
return baseFunc(arg0, arg1, arg2)
}
// argument resolver for curry3
const curry3ResolveArg2 = (
baseFunc, arg0, arg1,
) => function arg2Resolver(arg2) {
return baseFunc(arg0, arg1, arg2)
}
const curry3 = function (baseFunc, arg0, arg1, arg2) {
if (arg0 == __) {
return curry3ResolveArg0(baseFunc, arg1, arg2)
}
if (arg1 == __) {
return curry3ResolveArg1(baseFunc, arg0, arg2)
}
return curry3ResolveArg2(baseFunc, arg0, arg1)
}
// argument resolver for curryArgs2
const curryArgs2ResolveArgs0 = (
baseFunc, arg1, arg2,
) => function args0Resolver(...args) {
return baseFunc(args, arg1)
}
// argument resolver for curryArgs2
const curryArgs2ResolveArgs1 = (
baseFunc, arg0, arg2,
) => function arg1Resolver(...args) {
return baseFunc(arg0, args)
}
const curryArgs2 = function (baseFunc, arg0, arg1) {
if (arg0 == __) {
return curryArgs2ResolveArgs0(baseFunc, arg1)
}
return curryArgs2ResolveArgs1(baseFunc, arg0)
}
// argument resolver for curryArgs3
const curryArgs3ResolveArgs0 = (
baseFunc, arg1, arg2,
) => function args0Resolver(...args) {
return baseFunc(args, arg1, arg2)
}
// argument resolver for curryArgs3
const curryArgs3ResolveArgs1 = (
baseFunc, arg0, arg2,
) => function arg1Resolver(...args) {
return baseFunc(arg0, args, arg2)
}
// argument resolver for curryArgs3
const curryArgs3ResolveArgs2 = (
baseFunc, arg0, arg1,
) => function arg2Resolver(...args) {
return baseFunc(arg0, arg1, args)
}
const curryArgs3 = function (baseFunc, arg0, arg1, arg2) {
if (arg0 == __) {
return curryArgs3ResolveArgs0(baseFunc, arg1, arg2)
}
if (arg1 == __) {
return curryArgs3ResolveArgs1(baseFunc, arg0, arg2)
}
return curryArgs3ResolveArgs2(baseFunc, arg0, arg1)
}
// _tap(args Array, f function) -> Promise|any
const _tap = function (args, f) {
const result = args[0],
call = f(...args)
return isPromise(call) ? call.then(always(result)) : result
}
const tap = function (...args) {
const f = args.pop()
if (args.length == 0) {
return curryArgs2(_tap, __, f)
}
if (areAnyValuesPromises(args)) {
return promiseAll(args).then(curry2(_tap, __, f))
}
return _tap(args, f)
}
const _tapIf = function (predicate, f, args) {
const b = predicate(...args)
if (isPromise(b)) {
return b.then(curry3(
thunkConditional,
__,
thunkifyArgs(tap(f), args),
always(args[0]),
))
}
if (b) {
const execution = f(...args)
if (isPromise(execution)) {
return execution.then(always(args[0]))
}
}
return args[0]
}
tap.if = function (...args) {
if (args.length == 2) {
return curryArgs3(_tapIf, args[0], args[1], __)
}
const argsLength = args.length
const f = args[argsLength - 1]
const predicate = args[argsLength - 2]
const argValues = args.slice(0, -2)
if (areAnyValuesPromises(argValues)) {
return promiseAll(argValues).then(curry3(_tapIf, predicate, f, __))
}
return _tapIf(predicate, f, args)
}
const areAllValuesNonfunctions = function (values) {
if (isArray(values)) {
const length = values.length
let index = -1
while (++index < length) {
if (typeof values[index] == 'function') {
return false
}
}
return true
}
for (const key in values) {
if (typeof values[key] == 'function') {
return false
}
}
return true
}
const promiseObjectAllExecutor = object => function executor(resolve) {
const result = {}
let numPromises = 0
for (const key in object) {
const value = object[key]
if (isPromise(value)) {
numPromises += 1
value.then((key => function (res) {
result[key] = res
numPromises -= 1
if (numPromises == 0) {
resolve(result)
}
})(key))
} else {
result[key] = value
}
}
if (numPromises == 0) {
resolve(result)
}
}
const promiseObjectAll = object => new Promise(promiseObjectAllExecutor(object))
const functionArrayAll = function (funcs, args) {
const funcsLength = funcs.length,
result = Array(funcsLength)
let funcsIndex = -1, isAsync = false
while (++funcsIndex < funcsLength) {
const f = funcs[funcsIndex]
const resultItem = typeof f == 'function' ? f(...args) : f
if (isPromise(resultItem)) {
isAsync = true
}
result[funcsIndex] = resultItem
}
return isAsync ? promiseAll(result) : result
}
// argument resolver for curry4
const curry4ResolveArg0 = (
baseFunc, arg1, arg2, arg3,
) => function arg0Resolver(arg0) {
return baseFunc(arg0, arg1, arg2, arg3)
}
// argument resolver for curry4
const curry4ResolveArg1 = (
baseFunc, arg0, arg2, arg3,
) => function arg1Resolver(arg1) {
return baseFunc(arg0, arg1, arg2, arg3)
}
// argument resolver for curry4
const curry4ResolveArg2 = (
baseFunc, arg0, arg1, arg3,
) => function arg2Resolver(arg2) {
return baseFunc(arg0, arg1, arg2, arg3)
}
// argument resolver for curry4
const curry4ResolveArg3 = (
baseFunc, arg0, arg1, arg2,
) => function arg3Resolver(arg3) {
return baseFunc(arg0, arg1, arg2, arg3)
}
const curry4 = function (baseFunc, arg0, arg1, arg2, arg3) {
if (arg0 == __) {
return curry4ResolveArg0(baseFunc, arg1, arg2, arg3)
}
if (arg1 == __) {
return curry4ResolveArg1(baseFunc, arg0, arg2, arg3)
}
if (arg2 == __) {
return curry4ResolveArg2(baseFunc, arg0, arg1, arg3)
}
return curry4ResolveArg3(baseFunc, arg0, arg1, arg2)
}
const objectSet = function (object, property, value) {
object[property] = value
return object
}
const asyncFunctionArrayAllSeries = async function (funcs, args, result, funcsIndex) {
const funcsLength = funcs.length
while (++funcsIndex < funcsLength) {
const resultItem = funcs[funcsIndex](...args)
result[funcsIndex] = isPromise(resultItem) ? await resultItem : resultItem
}
return result
}
const functionArrayAllSeries = function (funcs, args) {
const funcsLength = funcs.length, result = []
let funcsIndex = -1
while (++funcsIndex < funcsLength) {
const resultItem = funcs[funcsIndex](...args)
if (isPromise(resultItem)) {
return resultItem.then(funcConcat(
curry3(objectSet, result, funcsIndex, __),
curry4(asyncFunctionArrayAllSeries, funcs, args, __, funcsIndex)))
}
result[funcsIndex] = resultItem
}
return result
}
const functionObjectAll = function (funcs, args) {
const result = {}, promises = []
for (const key in funcs) {
const f = funcs[key]
const resultItem = typeof f == 'function' ? f(...args) : f
if (isPromise(resultItem)) {
promises.push(resultItem.then(curry3(objectSet, result, key, __)))
} else {
result[key] = resultItem
}
}
return promises.length == 0 ? result : promiseAll(promises).then(always(result))
}
const _allValues = function (values) {
if (isArray(values)) {
return areAnyValuesPromises(values)
? promiseAll(values)
: values
}
return areAnyValuesPromises(values)
? promiseObjectAll(values)
: values
}
const all = function (...args) {
if (args.length == 1) {
const resolversOrValues = args[0]
if (isPromise(resolversOrValues)) {
return resolversOrValues.then(_allValues)
}
if (areAllValuesNonfunctions(resolversOrValues)) {
return _allValues(resolversOrValues)
}
return isArray(resolversOrValues)
? curryArgs2(functionArrayAll, resolversOrValues, __)
: curryArgs2(functionObjectAll, resolversOrValues, __)
}
const resolversOrValues = args[args.length - 1]
const argValues = args.slice(0, -1)
if (areAnyValuesPromises(argValues)) {
return isArray(resolversOrValues)
? promiseAll(argValues)
.then(curry2(functionArrayAll, resolversOrValues, __))
: promiseAll(argValues)
.then(curry2(functionObjectAll, resolversOrValues, __))
}
return isArray(resolversOrValues)
? functionArrayAll(resolversOrValues, argValues)
: functionObjectAll(resolversOrValues, argValues)
/*
////////////////////////////////////////////////////////////////
const funcs = args.pop()
if (args.length == 0) {
return isArray(funcs)
? curryArgs2(functionArrayAll, funcs, __)
: curryArgs2(functionObjectAll, funcs, __)
}
if (areAnyValuesPromises(args)) {
return isArray(funcs)
? promiseAll(args).then(curry2(functionArrayAll, funcs, __))
: promiseAll(args).then(curry2(functionObjectAll, funcs, __))
}
return isArray(funcs)
? functionArrayAll(funcs, args)
: functionObjectAll(funcs, args)
*/
}
all.series = function allSeries(...args) {
const funcs = args.pop()
if (args.length == 0) {
return curryArgs2(functionArrayAllSeries, funcs, __)
}
if (areAnyValuesPromises(args)) {
return promiseAll(args).then(curry2(functionArrayAllSeries, funcs, __))
}
return functionArrayAllSeries(funcs, args)
}
const objectAssign = Object.assign
// _assign(object Object, funcs Object<function>) -> Promise|Object
const _assign = function (object, funcs) {
const result = functionObjectAll(funcs, [object])
return isPromise(result)
? result.then(curry3(objectAssign, {}, object, __))
: ({ ...object, ...result })
}
const assign = function (arg0, arg1) {
if (arg1 == null) {
return curry2(_assign, __, arg0)
}
return isPromise(arg0)
? arg0.then(curry2(_assign, __, arg1))
: _assign(arg0, arg1)
}
const catcherApply = function (catcher, err, args) {
return catcher(err, ...args)
}
// _tryCatch(tryer function, catcher function, args Array) -> Promise
const _tryCatch = function (tryer, catcher, args) {
try {
const result = tryer(...args)
return isPromise(result)
? result.catch(curry3(catcherApply, catcher, __, args))
: result
} catch (error) {
return catcher(error, ...args)
}
}
const tryCatch = function (...args) {
if (args.length > 2) {
const catcher = args.pop(),
tryer = args.pop()
if (areAnyValuesPromises(args)) {
return promiseAll(args)
.then(curry3(_tryCatch, tryer, catcher, __))
}
return _tryCatch(tryer, catcher, args)
}
const tryer = args[0],
catcher = args[1]
return function tryCatcher(...args) {
return _tryCatch(tryer, catcher, args)
}
}
const thunkify3 = (func, arg0, arg1, arg2) => function thunk() {
return func(arg0, arg1, arg2)
}
const arrayConditional = function (array, args, funcsIndex) {
const lastIndex = array.length - 1
while ((funcsIndex += 2) < lastIndex) {
const predicate = array[funcsIndex],
resolverOrValue = array[funcsIndex + 1]
const predication = typeof predicate == 'function'
? predicate(...args)
: predicate
if (isPromise(predication)) {
return predication.then(curry3(
thunkConditional,
__,
typeof resolverOrValue == 'function'
? thunkifyArgs(resolverOrValue, args)
: always(resolverOrValue),
thunkify3(arrayConditional, array, args, funcsIndex),
))
}
if (predication) {
return typeof resolverOrValue == 'function'
? resolverOrValue(...args)
: resolverOrValue
}
}
// even number of array
if (funcsIndex == array.length) {
return undefined
}
const defaultResolverOrValue = array[lastIndex]
return typeof defaultResolverOrValue == 'function'
? defaultResolverOrValue(...args)
: defaultResolverOrValue
}
const thunkify2 = (func, arg0, arg1) => function thunk() {
return func(arg0, arg1)
}
const nonfunctionsConditional = function (array, index) {
const length = array.length,
lastIndex = length - 1
while ((index += 2) < lastIndex) {
const predication = array[index],
value = array[index + 1]
if (isPromise(predication)) {
return predication.then(curry3(
thunkConditional,
__,
always(value),
thunkify2(nonfunctionsConditional, array, index),
))
}
if (predication) {
return value
}
}
// even number of array values
if (index == length) {
return undefined
}
return array[index]
}
const switchCase = (...args) => {
const values = args.pop()
if (areAllValuesNonfunctions(values)) {
return nonfunctionsConditional(values, -2)
}
if (args.length == 0) {
return curryArgs3(arrayConditional, values, __, -2)
}
if (areAnyValuesPromises(args)) {
return promiseAll(args).then(curry3(arrayConditional, values, __, -2))
}
return arrayConditional(values, args, -2)
}
const symbolIterator = Symbol.iterator
const MappingIterator = (iterator, mapper) => ({
toString() {
return '[object MappingIterator]'
},
[symbolIterator]() {
return this
},
next() {
const iteration = iterator.next()
return iteration.done ? iteration
: { value: mapper(iteration.value), done: false }
},
})
const NextIteration = value => ({ value, done: false })
const symbolAsyncIterator = Symbol.asyncIterator
const MappingAsyncIterator = (asyncIterator, mapper) => ({
[symbolAsyncIterator]() {
return this
},
async next() {
const iteration = await asyncIterator.next()
if (iteration.done) {
return iteration
}
const mapped = mapper(iteration.value)
return isPromise(mapped)
? mapped.then(NextIteration)
: { value: mapped, done: false }
}
})
const isObject = value => {
if (value == null) {
return false
}
const typeofValue = typeof value
return (typeofValue == 'object') || (typeofValue == 'function')
}
const arrayMap = function (array, mapper) {
const arrayLength = array.length,
result = Array(arrayLength)
let index = -1,
isAsync = false
while (++index < arrayLength) {
const resultItem = mapper(array[index], index, array)
if (isPromise(resultItem)) {
isAsync = true
}
result[index] = resultItem
}
return isAsync ? promiseAll(result) : result
}
const callPropUnary = (value, property, arg0) => value[property](arg0)
const stringMap = function (string, mapper) {
const result = arrayMap(string, mapper)
return isPromise(result)
? result.then(curry3(callPropUnary, __, 'join', ''))
: result.join('')
}
const setMap = function (set, mapper) {
const result = new Set(),
promises = []
for (const item of set) {
const resultItem = mapper(item, item, set)
if (isPromise(resultItem)) {
promises.push(resultItem.then(curry3(callPropUnary, result, 'add', __)))
} else {
result.add(resultItem)
}
}
return promises.length == 0
? result
: promiseAll(promises).then(always(result))
}
const callPropBinary = (value, property, arg0, arg1) => value[property](arg0, arg1)
const mapMap = function (value, mapper) {
const result = new Map(),
promises = []
for (const [key, item] of value) {
const resultItem = mapper(item, key, value)
if (isPromise(resultItem)) {
promises.push(resultItem.then(
curry4(callPropBinary, result, 'set', key, __)))
} else {
result.set(key, resultItem)
}
}
return promises.length == 0
? result
: promiseAll(promises).then(always(result))
}
const objectMap = function (object, mapper) {
const result = {}
let isAsync = false
for (const key in object) {
const resultItem = mapper(object[key], key, object)
if (isPromise(resultItem)) {
isAsync = true
}
result[key] = resultItem
}
return isAsync ? promiseObjectAll(result) : result
}
const arrayMapSeriesAsync = async function (
array, mapper, result, index,
) {
const arrayLength = array.length
while (++index < arrayLength) {
const resultItem = mapper(array[index], index)
result[index] = isPromise(resultItem) ? await resultItem : resultItem
}
return result
}
const arrayMapSeries = function (array, mapper) {
const arrayLength = array.length,
result = Array(arrayLength)
let index = -1
while (++index < arrayLength) {
const resultItem = mapper(array[index], index)
if (isPromise(resultItem)) {
return resultItem.then(funcConcat(
curry3(objectSet, result, index, __),
curry4(arrayMapSeriesAsync, array, mapper, __, index)))
}
result[index] = resultItem
}
return result
}
const stringMapSeries = function (string, mapper) {
const result = arrayMapSeries(string, mapper)
return isPromise(result)
? result.then(curry3(callPropUnary, __, 'join', ''))
: result.join('')
}
const thunkify4 = (func, arg0, arg1, arg2, arg3) => function thunk() {
return func(arg0, arg1, arg2, arg3)
}
// _objectMapSeriesAsync(
// object Object,
// f function,
// result Object,
// doneKeys Object
// ) -> Promise<object>
const _objectMapSeriesAsync = async function (object, f, result, doneKeys) {
for (const key in object) {
if (key in doneKeys) {
continue
}
let resultItem = f(object[key])
if (isPromise(resultItem)) {
resultItem = await resultItem
}
result[key] = resultItem
}
return result
}
const objectMapSeries = function (object, f) {
const result = {}
const doneKeys = {}
for (const key in object) {
doneKeys[key] = true
const resultItem = f(object[key], key, object)
if (isPromise(resultItem)) {
return resultItem.then(funcConcat(
curry3(objectSet, result, key, __),
thunkify4(_objectMapSeriesAsync, object, f, result, doneKeys),
))
}
result[key] = resultItem
}
return result
}
const setAdd = function (set, value) {
set.add(value)
return set
}
// _setMapSeriesAsync(
// iterator Iterator,
// f function,
// result Set,
// ) -> Promise<Set>
const _setMapSeriesAsync = async function (iterator, f, result) {
let iteration = iterator.next()
while (!iteration.done) {
let resultItem = f(iteration.value)
if (isPromise(resultItem)) {
resultItem = await resultItem
}
result.add(resultItem)
iteration = iterator.next()
}
return result
}
const setMapSeries = function (set, f) {
const result = new Set()
const iterator = set[symbolIterator]()
let iteration = iterator.next()
while (!iteration.done) {
const resultItem = f(iteration.value)
if (isPromise(resultItem)) {
return resultItem.then(funcConcat(
curry2(setAdd, result, __),
thunkify3(_setMapSeriesAsync, iterator, f, result),
))
}
result.add(resultItem)
iteration = iterator.next()
}
return result
}
const mapSet = function setting(source, key, value) {
return source.set(key, value)
}
// _mapMapSeriesAsync(
// iterator Iterator,
// f function,
// result Map,
// ) -> Promise<Map>
const _mapMapSeriesAsync = async function (iterator, f, result) {
let iteration = iterator.next()
while (!iteration.done) {
let resultItem = f(iteration.value[1])
if (isPromise(resultItem)) {
resultItem = await resultItem
}
result.set(iteration.value[0], resultItem)
iteration = iterator.next()
}
return result
}
const mapMapSeries = function (map, f) {
const result = new Map()
const iterator = map[symbolIterator]()
let iteration = iterator.next()
while (!iteration.done) {
const key = iteration.value[0]
const resultItem = f(iteration.value[1])
if (isPromise(resultItem)) {
return resultItem.then(funcConcat(
curry3(mapSet, result, key, __),
thunkify3(_mapMapSeriesAsync, iterator, f, result),
))
}
result.set(key, resultItem)
iteration = iterator.next()
}
return result
}
const tapSync = func => function tapping(...args) {
func(...args)
return args[0]
}
const promiseRace = Promise.race.bind(Promise)
const arrayMapPoolAsync = async function (
array, f, concurrencyLimit, result, index, promises,
) {
const arrayLength = array.length
while (++index < arrayLength) {
if (promises.size >= concurrencyLimit) {
await promiseRace(promises)
}
const resultItem = f(array[index])
if (isPromise(resultItem)) {
const selfDeletingPromise = resultItem.then(
tapSync(() => promises.delete(selfDeletingPromise)))
promises.add(selfDeletingPromise)
result[index] = selfDeletingPromise
} else {
result[index] = resultItem
}
}
return promiseAll(result)
}
const arrayMapPool = function (array, concurrency, f) {
const arrayLength = array.length,
result = Array(arrayLength)
let index = -1
while (++index < arrayLength) {
const resultItem = f(array[index])
if (isPromise(resultItem)) {
const promises = new Set(),
selfDeletingPromise = resultItem.then(
tapSync(() => promises.delete(selfDeletingPromise)))
promises.add(selfDeletingPromise)
result[index] = selfDeletingPromise
return arrayMapPoolAsync(
array, f, concurrency, result, index, promises)
}
result[index] = resultItem
}
return result
}
const stringMapPool = function (s, concurrency, f) {
const result = arrayMapPool(s, concurrency, f)
return isPromise(result)
? result.then(curry3(callPropUnary, __, 'join', ''))
: result.join('')
}
const _setMapPoolAsync = async function (
s, iterator, concurrency, f, result, promises,
) {
let iteration = iterator.next()
while (!iteration.done) {
if (promises.size >= concurrency) {
await promiseRace(promises)
}
const resultItem = f(iteration.value, iteration.value, s)
if (isPromise(resultItem)) {
const selfDeletingPromise = resultItem.then(resolvedValue => {
promises.delete(selfDeletingPromise)
result.add(resolvedValue)
})
promises.add(selfDeletingPromise)
} else {
result.add(resultItem)
}
iteration = iterator.next()
}
if (promises.size > 0) {
await promiseAll(promises)
}
return result
}
const setMapPool = function (s, concurrency, f) {
const result = new Set()
const iterator = s[symbolIterator]()
let iteration = iterator.next()
while (!iteration.done) {
const resultItem = f(iteration.value, iteration.value, s)
if (isPromise(resultItem)) {
const promises = new Set()
const selfDeletingPromise = resultItem.then(resolvedValue => {
promises.delete(selfDeletingPromise)
result.add(resolvedValue)
})
promises.add(selfDeletingPromise)
return _setMapPoolAsync(s, iterator, concurrency, f, result, promises)
}
result.add(resultItem)
iteration = iterator.next()
}
return result
}
const _mapMapPoolAsync = async function (
m, iterator, concurrency, f, result, promises,
) {
let iteration = iterator.next()
while (!iteration.done) {
if (promises.size >= concurrency) {
await promiseRace(promises)
}
const key = iteration.value[0]
const resultItem = f(iteration.value[1], key, m)
if (isPromise(resultItem)) {
result.set(key, resultItem)
const selfDeletingPromise = resultItem.then(resolvedValue => {
promises.delete(selfDeletingPromise)
result.set(key, resolvedValue)
})
promises.add(selfDeletingPromise)
} else {
result.set(key, resultItem)
}
iteration = iterator.next()
}
if (promises.size > 0) {
await promiseAll(promises)