rubico 2.8.5 → 2.10.0
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.
- package/README.md +2 -1
- package/_internal/FlatMappingAsyncIterator.js +41 -30
- package/_internal/LinkedList.js +54 -0
- package/_internal/LinkedList.test.js +34 -0
- package/_internal/MappingAsyncIterator.js +65 -15
- package/_internal/SerialMappingAsyncIterator.js +79 -0
- package/_internal/curryArity.js +3 -3
- package/_internal/iteratorFind.js +0 -2
- package/_internal/iteratorForEachSeries.js +1 -1
- package/_internal/objectFlatten.js +4 -1
- package/all.js +33 -59
- package/and.js +1 -1
- package/assign.js +14 -16
- package/compose.js +12 -13
- package/curry.js +1 -1
- package/dist/Transducer.es.js +1 -1
- package/dist/Transducer.es.min.js +1 -1
- package/dist/Transducer.js +1 -1
- package/dist/Transducer.min.js +1 -1
- package/dist/Transducer.mjs +1 -1
- package/dist/__.es.js +1 -1
- package/dist/__.es.min.js +1 -1
- package/dist/__.js +1 -1
- package/dist/__.min.js +1 -1
- package/dist/__.mjs +1 -1
- package/dist/all.es.js +1 -1
- package/dist/all.es.min.js +1 -1
- package/dist/all.js +1 -1
- package/dist/all.min.js +1 -1
- package/dist/all.mjs +1 -1
- package/dist/always.es.js +1 -1
- package/dist/always.es.min.js +1 -1
- package/dist/always.js +1 -1
- package/dist/always.min.js +1 -1
- package/dist/always.mjs +1 -1
- package/dist/and.es.js +1 -1
- package/dist/and.es.min.js +1 -1
- package/dist/and.js +1 -1
- package/dist/and.min.js +1 -1
- package/dist/and.mjs +1 -1
- package/dist/assign.es.js +1 -1
- package/dist/assign.es.min.js +1 -1
- package/dist/assign.js +1 -1
- package/dist/assign.min.js +1 -1
- package/dist/assign.mjs +1 -1
- package/dist/compose.es.js +1 -1
- package/dist/compose.es.min.js +1 -1
- package/dist/compose.js +1 -1
- package/dist/compose.min.js +1 -1
- package/dist/compose.mjs +1 -1
- package/dist/curry.es.js +4 -4
- package/dist/curry.es.min.js +2 -2
- package/dist/curry.js +4 -4
- package/dist/curry.min.js +2 -2
- package/dist/curry.mjs +4 -4
- package/dist/eq.es.js +1 -1
- package/dist/eq.es.min.js +1 -1
- package/dist/eq.js +1 -1
- package/dist/eq.min.js +1 -1
- package/dist/eq.mjs +1 -1
- package/dist/every.es.js +1 -1
- package/dist/every.es.min.js +1 -1
- package/dist/every.js +1 -1
- package/dist/every.min.js +1 -1
- package/dist/every.mjs +1 -1
- package/dist/filter.es.js +4 -4
- package/dist/filter.es.min.js +2 -2
- package/dist/filter.js +4 -4
- package/dist/filter.min.js +2 -2
- package/dist/filter.mjs +4 -4
- package/dist/flatMap.es.js +44 -28
- package/dist/flatMap.es.min.js +2 -2
- package/dist/flatMap.js +44 -28
- package/dist/flatMap.min.js +2 -2
- package/dist/flatMap.mjs +44 -28
- package/dist/forEach.es.js +6 -6
- package/dist/forEach.es.min.js +2 -2
- package/dist/forEach.js +6 -6
- package/dist/forEach.min.js +2 -2
- package/dist/forEach.mjs +6 -6
- package/dist/get.es.js +27 -3
- package/dist/get.es.min.js +2 -2
- package/dist/get.js +27 -3
- package/dist/get.min.js +2 -2
- package/dist/get.mjs +27 -3
- package/dist/gt.es.js +1 -1
- package/dist/gt.es.min.js +1 -1
- package/dist/gt.js +1 -1
- package/dist/gt.min.js +1 -1
- package/dist/gt.mjs +1 -1
- package/dist/gte.es.js +1 -1
- package/dist/gte.es.min.js +1 -1
- package/dist/gte.js +1 -1
- package/dist/gte.min.js +1 -1
- package/dist/gte.mjs +1 -1
- package/dist/lt.es.js +1 -1
- package/dist/lt.es.min.js +1 -1
- package/dist/lt.js +1 -1
- package/dist/lt.min.js +1 -1
- package/dist/lt.mjs +1 -1
- package/dist/lte.es.js +1 -1
- package/dist/lte.es.min.js +1 -1
- package/dist/lte.js +1 -1
- package/dist/lte.min.js +1 -1
- package/dist/lte.mjs +1 -1
- package/dist/map.es.js +190 -42
- package/dist/map.es.min.js +2 -2
- package/dist/map.js +190 -42
- package/dist/map.min.js +2 -2
- package/dist/map.mjs +190 -42
- package/dist/not.es.js +1 -1
- package/dist/not.es.min.js +1 -1
- package/dist/not.js +1 -1
- package/dist/not.min.js +1 -1
- package/dist/not.mjs +1 -1
- package/dist/omit.es.js +1 -1
- package/dist/omit.es.min.js +1 -1
- package/dist/omit.js +1 -1
- package/dist/omit.min.js +1 -1
- package/dist/omit.mjs +1 -1
- package/dist/or.es.js +1 -1
- package/dist/or.es.min.js +1 -1
- package/dist/or.js +1 -1
- package/dist/or.min.js +1 -1
- package/dist/or.mjs +1 -1
- package/dist/pick.es.js +1 -1
- package/dist/pick.es.min.js +1 -1
- package/dist/pick.js +1 -1
- package/dist/pick.min.js +1 -1
- package/dist/pick.mjs +1 -1
- package/dist/pipe.es.js +1 -1
- package/dist/pipe.es.min.js +1 -1
- package/dist/pipe.js +1 -1
- package/dist/pipe.min.js +1 -1
- package/dist/pipe.mjs +1 -1
- package/dist/reduce.es.js +1 -1
- package/dist/reduce.es.min.js +1 -1
- package/dist/reduce.js +1 -1
- package/dist/reduce.min.js +1 -1
- package/dist/reduce.mjs +1 -1
- package/dist/rubico.es.js +238 -83
- package/dist/rubico.es.min.js +2 -2
- package/dist/rubico.global.js +238 -83
- package/dist/rubico.global.min.js +2 -2
- package/dist/rubico.js +238 -83
- package/dist/rubico.min.js +2 -2
- package/dist/rubico.mjs +238 -83
- package/dist/set.es.js +1 -1
- package/dist/set.es.min.js +1 -1
- package/dist/set.js +1 -1
- package/dist/set.min.js +1 -1
- package/dist/set.mjs +1 -1
- package/dist/some.es.js +1 -1
- package/dist/some.es.min.js +1 -1
- package/dist/some.js +1 -1
- package/dist/some.min.js +1 -1
- package/dist/some.mjs +1 -1
- package/dist/switchCase.es.js +1 -1
- package/dist/switchCase.es.min.js +1 -1
- package/dist/switchCase.js +1 -1
- package/dist/switchCase.min.js +1 -1
- package/dist/switchCase.mjs +1 -1
- package/dist/tap.es.js +1 -1
- package/dist/tap.es.min.js +1 -1
- package/dist/tap.js +1 -1
- package/dist/tap.min.js +1 -1
- package/dist/tap.mjs +1 -1
- package/dist/thunkify.es.js +1 -1
- package/dist/thunkify.es.min.js +1 -1
- package/dist/thunkify.js +1 -1
- package/dist/thunkify.min.js +1 -1
- package/dist/thunkify.mjs +1 -1
- package/dist/transform.es.js +1 -1
- package/dist/transform.es.min.js +1 -1
- package/dist/transform.js +1 -1
- package/dist/transform.min.js +1 -1
- package/dist/transform.mjs +1 -1
- package/dist/tryCatch.es.js +1 -1
- package/dist/tryCatch.es.min.js +1 -1
- package/dist/tryCatch.js +1 -1
- package/dist/tryCatch.min.js +1 -1
- package/dist/tryCatch.mjs +1 -1
- package/dist/x/append.es.js +1 -1
- package/dist/x/append.es.min.js +1 -1
- package/dist/x/append.js +1 -1
- package/dist/x/append.min.js +1 -1
- package/dist/x/append.mjs +1 -1
- package/dist/x/callProp.es.js +1 -1
- package/dist/x/callProp.es.min.js +1 -1
- package/dist/x/callProp.js +1 -1
- package/dist/x/callProp.min.js +1 -1
- package/dist/x/callProp.mjs +1 -1
- package/dist/x/defaultsDeep.es.js +1 -1
- package/dist/x/defaultsDeep.es.min.js +1 -1
- package/dist/x/defaultsDeep.js +1 -1
- package/dist/x/defaultsDeep.min.js +1 -1
- package/dist/x/defaultsDeep.mjs +1 -1
- package/dist/x/differenceWith.es.js +1 -1
- package/dist/x/differenceWith.es.min.js +1 -1
- package/dist/x/differenceWith.js +1 -1
- package/dist/x/differenceWith.min.js +1 -1
- package/dist/x/differenceWith.mjs +1 -1
- package/dist/x/filterOut.es.js +4 -4
- package/dist/x/filterOut.es.min.js +2 -2
- package/dist/x/filterOut.js +4 -4
- package/dist/x/filterOut.min.js +2 -2
- package/dist/x/filterOut.mjs +4 -4
- package/dist/x/find.es.js +1 -3
- package/dist/x/find.es.min.js +1 -1
- package/dist/x/find.js +1 -3
- package/dist/x/find.min.js +1 -1
- package/dist/x/find.mjs +1 -3
- package/dist/x/findIndex.es.js +1 -1
- package/dist/x/findIndex.es.min.js +1 -1
- package/dist/x/findIndex.js +1 -1
- package/dist/x/findIndex.min.js +1 -1
- package/dist/x/findIndex.mjs +1 -1
- package/dist/x/first.es.js +1 -1
- package/dist/x/first.es.min.js +1 -1
- package/dist/x/first.js +1 -1
- package/dist/x/first.min.js +1 -1
- package/dist/x/first.mjs +1 -1
- package/dist/x/flatten.es.js +44 -28
- package/dist/x/flatten.es.min.js +2 -2
- package/dist/x/flatten.js +44 -28
- package/dist/x/flatten.min.js +2 -2
- package/dist/x/flatten.mjs +44 -28
- package/dist/x/groupBy.es.js +1 -1
- package/dist/x/groupBy.es.min.js +1 -1
- package/dist/x/groupBy.js +1 -1
- package/dist/x/groupBy.min.js +1 -1
- package/dist/x/groupBy.mjs +1 -1
- package/dist/x/has.es.js +1 -1
- package/dist/x/has.es.min.js +1 -1
- package/dist/x/has.js +1 -1
- package/dist/x/has.min.js +1 -1
- package/dist/x/has.mjs +1 -1
- package/dist/x/identity.es.js +1 -1
- package/dist/x/identity.es.min.js +1 -1
- package/dist/x/identity.js +1 -1
- package/dist/x/identity.min.js +1 -1
- package/dist/x/identity.mjs +1 -1
- package/dist/x/includes.es.js +1 -1
- package/dist/x/includes.es.min.js +1 -1
- package/dist/x/includes.js +1 -1
- package/dist/x/includes.min.js +1 -1
- package/dist/x/includes.mjs +1 -1
- package/dist/x/isDeepEqual.es.js +1 -1
- package/dist/x/isDeepEqual.es.min.js +1 -1
- package/dist/x/isDeepEqual.js +1 -1
- package/dist/x/isDeepEqual.min.js +1 -1
- package/dist/x/isDeepEqual.mjs +1 -1
- package/dist/x/isEmpty.es.js +1 -1
- package/dist/x/isEmpty.es.min.js +1 -1
- package/dist/x/isEmpty.js +1 -1
- package/dist/x/isEmpty.min.js +1 -1
- package/dist/x/isEmpty.mjs +1 -1
- package/dist/x/isEqual.es.js +1 -1
- package/dist/x/isEqual.es.min.js +1 -1
- package/dist/x/isEqual.js +1 -1
- package/dist/x/isEqual.min.js +1 -1
- package/dist/x/isEqual.mjs +1 -1
- package/dist/x/isFunction.es.js +1 -1
- package/dist/x/isFunction.es.min.js +1 -1
- package/dist/x/isFunction.js +1 -1
- package/dist/x/isFunction.min.js +1 -1
- package/dist/x/isFunction.mjs +1 -1
- package/dist/x/isIn.es.js +1 -1
- package/dist/x/isIn.es.min.js +1 -1
- package/dist/x/isIn.js +1 -1
- package/dist/x/isIn.min.js +1 -1
- package/dist/x/isIn.mjs +1 -1
- package/dist/x/isObject.es.js +1 -1
- package/dist/x/isObject.es.min.js +1 -1
- package/dist/x/isObject.js +1 -1
- package/dist/x/isObject.min.js +1 -1
- package/dist/x/isObject.mjs +1 -1
- package/dist/x/isString.es.js +1 -1
- package/dist/x/isString.es.min.js +1 -1
- package/dist/x/isString.js +1 -1
- package/dist/x/isString.min.js +1 -1
- package/dist/x/isString.mjs +1 -1
- package/dist/x/keys.es.js +1 -1
- package/dist/x/keys.es.min.js +1 -1
- package/dist/x/keys.js +1 -1
- package/dist/x/keys.min.js +1 -1
- package/dist/x/keys.mjs +1 -1
- package/dist/x/last.es.js +1 -1
- package/dist/x/last.es.min.js +1 -1
- package/dist/x/last.js +1 -1
- package/dist/x/last.min.js +1 -1
- package/dist/x/last.mjs +1 -1
- package/dist/x/maxBy.es.js +27 -23
- package/dist/x/maxBy.es.min.js +2 -2
- package/dist/x/maxBy.js +27 -23
- package/dist/x/maxBy.min.js +2 -2
- package/dist/x/maxBy.mjs +27 -23
- package/dist/x/noop.es.js +1 -1
- package/dist/x/noop.es.min.js +1 -1
- package/dist/x/noop.js +1 -1
- package/dist/x/noop.min.js +1 -1
- package/dist/x/noop.mjs +1 -1
- package/dist/x/pluck.es.js +194 -44
- package/dist/x/pluck.es.min.js +2 -2
- package/dist/x/pluck.js +194 -44
- package/dist/x/pluck.min.js +2 -2
- package/dist/x/pluck.mjs +194 -44
- package/dist/x/prepend.es.js +1 -1
- package/dist/x/prepend.es.min.js +1 -1
- package/dist/x/prepend.js +1 -1
- package/dist/x/prepend.min.js +1 -1
- package/dist/x/prepend.mjs +1 -1
- package/dist/x/size.es.js +1 -1
- package/dist/x/size.es.min.js +1 -1
- package/dist/x/size.js +1 -1
- package/dist/x/size.min.js +1 -1
- package/dist/x/size.mjs +1 -1
- package/dist/x/trace.es.js +1 -1
- package/dist/x/trace.es.min.js +1 -1
- package/dist/x/trace.js +1 -1
- package/dist/x/trace.min.js +1 -1
- package/dist/x/trace.mjs +1 -1
- package/dist/x/unionWith.es.js +1 -1
- package/dist/x/unionWith.es.min.js +1 -1
- package/dist/x/unionWith.js +1 -1
- package/dist/x/unionWith.min.js +1 -1
- package/dist/x/unionWith.mjs +1 -1
- package/dist/x/uniq.es.js +1 -1
- package/dist/x/uniq.es.min.js +1 -1
- package/dist/x/uniq.js +1 -1
- package/dist/x/uniq.min.js +1 -1
- package/dist/x/uniq.mjs +1 -1
- package/dist/x/unless.es.js +1 -1
- package/dist/x/unless.es.min.js +1 -1
- package/dist/x/unless.js +1 -1
- package/dist/x/unless.min.js +1 -1
- package/dist/x/unless.mjs +1 -1
- package/dist/x/values.es.js +1 -1
- package/dist/x/values.es.min.js +1 -1
- package/dist/x/values.js +1 -1
- package/dist/x/values.min.js +1 -1
- package/dist/x/values.mjs +1 -1
- package/dist/x/when.es.js +1 -1
- package/dist/x/when.es.min.js +1 -1
- package/dist/x/when.js +1 -1
- package/dist/x/when.min.js +1 -1
- package/dist/x/when.mjs +1 -1
- package/eq.js +2 -2
- package/es.js +238 -83
- package/every.js +2 -2
- package/filter.js +4 -4
- package/flatMap.js +61 -54
- package/forEach.js +33 -56
- package/get.js +49 -25
- package/gt.js +2 -2
- package/gte.js +2 -2
- package/index.js +238 -83
- package/lt.js +2 -2
- package/lte.js +2 -2
- package/map.js +115 -151
- package/monad/Mux.js +6 -4
- package/monad/PossiblePromise.js +6 -4
- package/monad/README.md +61 -40
- package/monad/Struct.js +6 -4
- package/not.js +1 -1
- package/omit.js +22 -19
- package/or.js +1 -1
- package/package.json +1 -1
- package/pick.js +14 -11
- package/pipe.js +18 -24
- package/reduce.js +1 -1
- package/set.js +17 -18
- package/some.js +2 -2
- package/switchCase.js +51 -43
- package/tap.js +17 -23
- package/transform.js +1 -1
- package/tryCatch.js +16 -24
- package/x/defaultsDeep.js +1 -1
- package/x/defaultsDeep.test.js +1 -1
- package/_internal/NextIteration.js +0 -12
- package/_internal/generatorFunctionFilter.js +0 -28
- package/_internal/iterationMap.js +0 -27
package/es.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Rubico v2.
|
|
2
|
+
* Rubico v2.10.0
|
|
3
3
|
* https://rubico.land/
|
|
4
4
|
*
|
|
5
5
|
* © Richard Yufei Tong, King of Software
|
|
@@ -601,26 +601,167 @@ const MappingIterator = (iterator, mapper) => ({
|
|
|
601
601
|
},
|
|
602
602
|
})
|
|
603
603
|
|
|
604
|
-
const
|
|
604
|
+
const promiseRace = Promise.race.bind(Promise)
|
|
605
605
|
|
|
606
|
-
|
|
606
|
+
class LinkedList {
|
|
607
|
+
constructor() {
|
|
608
|
+
this.first = null
|
|
609
|
+
this.last = null
|
|
610
|
+
this.length = 0
|
|
611
|
+
}
|
|
607
612
|
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
613
|
+
// popFirst() -> firstValue any
|
|
614
|
+
popFirst() {
|
|
615
|
+
const first = this.first
|
|
616
|
+
|
|
617
|
+
if (first == null) {
|
|
618
|
+
return undefined
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (first.next) {
|
|
622
|
+
this.first = first.next
|
|
623
|
+
} else {
|
|
624
|
+
this.first = null
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
this.length -= 1
|
|
628
|
+
|
|
629
|
+
return first.value
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// append(value any) -> undefined
|
|
633
|
+
append(value) {
|
|
634
|
+
const node = { value }
|
|
635
|
+
if (this.first == null) {
|
|
636
|
+
this.first = node
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (this.last == null) {
|
|
640
|
+
this.last = node
|
|
641
|
+
} else {
|
|
642
|
+
this.last.next = node
|
|
643
|
+
this.last = node
|
|
616
644
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
? mapped.then(NextIteration)
|
|
620
|
-
: { value: mapped, done: false }
|
|
645
|
+
|
|
646
|
+
this.length += 1
|
|
621
647
|
}
|
|
648
|
+
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const symbolAsyncIterator = Symbol.asyncIterator
|
|
652
|
+
|
|
653
|
+
const arrayPush = function (array, value) {
|
|
654
|
+
array.push(value)
|
|
655
|
+
return array
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const sleep = time => new Promise(resolve => {
|
|
659
|
+
setTimeout(resolve, time)
|
|
622
660
|
})
|
|
623
661
|
|
|
662
|
+
const MappingAsyncIterator = (asyncIterator, mapper) => {
|
|
663
|
+
const buffer = new LinkedList()
|
|
664
|
+
|
|
665
|
+
let index = -1
|
|
666
|
+
let consumingAsyncIterator = false
|
|
667
|
+
let isAsyncIteratorDone = false
|
|
668
|
+
|
|
669
|
+
return {
|
|
670
|
+
[symbolAsyncIterator]() {
|
|
671
|
+
return this
|
|
672
|
+
},
|
|
673
|
+
|
|
674
|
+
// _consumeAsyncIterator() -> Promise<>
|
|
675
|
+
async _consumeAsyncIterator() {
|
|
676
|
+
for await (const item of asyncIterator) {
|
|
677
|
+
index += 1
|
|
678
|
+
const mappedItem = mapper(item)
|
|
679
|
+
buffer.append(mappedItem)
|
|
680
|
+
}
|
|
681
|
+
isAsyncIteratorDone = true
|
|
682
|
+
},
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
async next() {
|
|
686
|
+
if (!consumingAsyncIterator) {
|
|
687
|
+
this._consumeAsyncIterator()
|
|
688
|
+
consumingAsyncIterator = true
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
while (!isAsyncIteratorDone) {
|
|
692
|
+
if (buffer.length > 0) {
|
|
693
|
+
let value = buffer.popFirst()
|
|
694
|
+
if (isPromise(value)) {
|
|
695
|
+
value = await value
|
|
696
|
+
}
|
|
697
|
+
return { value, done: false }
|
|
698
|
+
}
|
|
699
|
+
await sleep(10)
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
if (buffer.length > 0) {
|
|
703
|
+
let value = buffer.popFirst()
|
|
704
|
+
if (isPromise(value)) {
|
|
705
|
+
value = await value
|
|
706
|
+
}
|
|
707
|
+
return { value, done: false }
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
return { value: undefined, done: true }
|
|
711
|
+
},
|
|
712
|
+
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const SerialMappingAsyncIterator = (asyncIterator, mapper) => {
|
|
717
|
+
const buffer = new LinkedList()
|
|
718
|
+
|
|
719
|
+
let index = -1
|
|
720
|
+
let consumingAsyncIterator = false
|
|
721
|
+
let isAsyncIteratorDone = false
|
|
722
|
+
|
|
723
|
+
return {
|
|
724
|
+
[symbolAsyncIterator]() {
|
|
725
|
+
return this
|
|
726
|
+
},
|
|
727
|
+
|
|
728
|
+
// _consumeAsyncIterator() -> Promise<>
|
|
729
|
+
async _consumeAsyncIterator() {
|
|
730
|
+
for await (const item of asyncIterator) {
|
|
731
|
+
index += 1
|
|
732
|
+
let mappedItem = mapper(item)
|
|
733
|
+
if (isPromise(mappedItem)) {
|
|
734
|
+
mappedItem = await mappedItem
|
|
735
|
+
}
|
|
736
|
+
buffer.append(mappedItem)
|
|
737
|
+
}
|
|
738
|
+
isAsyncIteratorDone = true
|
|
739
|
+
},
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
async next() {
|
|
743
|
+
if (!consumingAsyncIterator) {
|
|
744
|
+
this._consumeAsyncIterator()
|
|
745
|
+
consumingAsyncIterator = true
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
while (!isAsyncIteratorDone) {
|
|
749
|
+
if (buffer.length > 0) {
|
|
750
|
+
return { value: buffer.popFirst(), done: false }
|
|
751
|
+
}
|
|
752
|
+
await sleep(10)
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
if (buffer.length > 0) {
|
|
756
|
+
return { value: buffer.popFirst(), done: false }
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
return { value: undefined, done: true }
|
|
760
|
+
},
|
|
761
|
+
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
624
765
|
const isObject = value => {
|
|
625
766
|
if (value == null) {
|
|
626
767
|
return false
|
|
@@ -866,8 +1007,6 @@ const tapSync = func => function tapping(...args) {
|
|
|
866
1007
|
return args[0]
|
|
867
1008
|
}
|
|
868
1009
|
|
|
869
|
-
const promiseRace = Promise.race.bind(Promise)
|
|
870
|
-
|
|
871
1010
|
const arrayMapPoolAsync = async function (
|
|
872
1011
|
array, f, concurrencyLimit, result, index, promises,
|
|
873
1012
|
) {
|
|
@@ -1073,7 +1212,7 @@ const _curryArity = (arity, func, context, args) => function curried(...curriedA
|
|
|
1073
1212
|
curriedArgsIndex = -1,
|
|
1074
1213
|
numCurriedPlaceholders = 0
|
|
1075
1214
|
|
|
1076
|
-
while (
|
|
1215
|
+
while ((argsIndex += 1) < argsLength) {
|
|
1077
1216
|
const arg = args[argsIndex]
|
|
1078
1217
|
if (arg == __ && (curriedArgsIndex += 1) < curriedArgsLength) {
|
|
1079
1218
|
const curriedArg = curriedArgs[curriedArgsIndex]
|
|
@@ -1097,7 +1236,7 @@ const _curryArity = (arity, func, context, args) => function curried(...curriedA
|
|
|
1097
1236
|
}
|
|
1098
1237
|
}
|
|
1099
1238
|
|
|
1100
|
-
while (
|
|
1239
|
+
while ((curriedArgsIndex += 1) < curriedArgsLength) {
|
|
1101
1240
|
const curriedArg = curriedArgs[curriedArgsIndex]
|
|
1102
1241
|
if (curriedArg == __) {
|
|
1103
1242
|
numCurriedPlaceholders += 1
|
|
@@ -1127,7 +1266,7 @@ const curryArity = function (arity, func, context, args) {
|
|
|
1127
1266
|
return _curryArity(arity, func, context, args)
|
|
1128
1267
|
}
|
|
1129
1268
|
let argsIndex = -1
|
|
1130
|
-
while (
|
|
1269
|
+
while ((argsIndex += 1) < argsLength) {
|
|
1131
1270
|
const arg = args[argsIndex]
|
|
1132
1271
|
if (arg == __) {
|
|
1133
1272
|
return _curryArity(arity, func, context, args)
|
|
@@ -1190,9 +1329,6 @@ const _map = function (value, f) {
|
|
|
1190
1329
|
if (typeof value.then == 'function') {
|
|
1191
1330
|
return value.then(f)
|
|
1192
1331
|
}
|
|
1193
|
-
if (typeof value.map == 'function') {
|
|
1194
|
-
return value.map(f)
|
|
1195
|
-
}
|
|
1196
1332
|
if (typeof value == 'string' || value.constructor == String) {
|
|
1197
1333
|
return stringMap(value, f)
|
|
1198
1334
|
}
|
|
@@ -1208,6 +1344,9 @@ const _map = function (value, f) {
|
|
|
1208
1344
|
if (typeof value[symbolAsyncIterator] == 'function') {
|
|
1209
1345
|
return MappingAsyncIterator(value[symbolAsyncIterator](), f)
|
|
1210
1346
|
}
|
|
1347
|
+
if (typeof value.map == 'function') {
|
|
1348
|
+
return value.map(f)
|
|
1349
|
+
}
|
|
1211
1350
|
if (value.constructor == Object) {
|
|
1212
1351
|
return objectMap(value, f)
|
|
1213
1352
|
}
|
|
@@ -1246,27 +1385,36 @@ map.entries = function mapEntries(arg0, arg1) {
|
|
|
1246
1385
|
: _mapEntries(arg0, arg1)
|
|
1247
1386
|
}
|
|
1248
1387
|
|
|
1249
|
-
const _mapSeries = function (
|
|
1250
|
-
if (isArray(
|
|
1251
|
-
return arrayMapSeries(
|
|
1388
|
+
const _mapSeries = function (functor, f) {
|
|
1389
|
+
if (isArray(functor)) {
|
|
1390
|
+
return arrayMapSeries(functor, f)
|
|
1252
1391
|
}
|
|
1253
|
-
if (
|
|
1254
|
-
throw new TypeError(`invalid
|
|
1392
|
+
if (functor == null) {
|
|
1393
|
+
throw new TypeError(`invalid functor ${functor}`)
|
|
1255
1394
|
}
|
|
1256
1395
|
|
|
1257
|
-
if (typeof
|
|
1258
|
-
return stringMapSeries(
|
|
1396
|
+
if (typeof functor == 'string' || functor.constructor == String) {
|
|
1397
|
+
return stringMapSeries(functor, f)
|
|
1259
1398
|
}
|
|
1260
|
-
if (
|
|
1261
|
-
return setMapSeries(
|
|
1399
|
+
if (functor.constructor == Set) {
|
|
1400
|
+
return setMapSeries(functor, f)
|
|
1262
1401
|
}
|
|
1263
|
-
if (
|
|
1264
|
-
return mapMapSeries(
|
|
1402
|
+
if (functor.constructor == Map) {
|
|
1403
|
+
return mapMapSeries(functor, f)
|
|
1265
1404
|
}
|
|
1266
|
-
if (
|
|
1267
|
-
return
|
|
1405
|
+
if (typeof functor[symbolIterator] == 'function') {
|
|
1406
|
+
return MappingIterator(functor[symbolIterator](), f)
|
|
1268
1407
|
}
|
|
1269
|
-
|
|
1408
|
+
if (typeof functor[symbolAsyncIterator] == 'function') {
|
|
1409
|
+
return SerialMappingAsyncIterator(functor[symbolAsyncIterator](), f)
|
|
1410
|
+
}
|
|
1411
|
+
if (typeof functor.map == 'function') {
|
|
1412
|
+
return functor.map(f)
|
|
1413
|
+
}
|
|
1414
|
+
if (functor.constructor == Object) {
|
|
1415
|
+
return objectMapSeries(functor, f)
|
|
1416
|
+
}
|
|
1417
|
+
throw new TypeError(`invalid functor ${functor}`)
|
|
1270
1418
|
}
|
|
1271
1419
|
|
|
1272
1420
|
map.series = function mapSeries(arg0, arg1) {
|
|
@@ -1483,15 +1631,15 @@ const _filter = function (value, predicate) {
|
|
|
1483
1631
|
if (value.constructor == Map) {
|
|
1484
1632
|
return mapFilter(value, predicate)
|
|
1485
1633
|
}
|
|
1486
|
-
if (typeof value.filter == 'function') {
|
|
1487
|
-
return value.filter(predicate)
|
|
1488
|
-
}
|
|
1489
1634
|
if (typeof value[symbolIterator] == 'function') {
|
|
1490
1635
|
return FilteringIterator(value[symbolIterator](), predicate)
|
|
1491
1636
|
}
|
|
1492
1637
|
if (typeof value[symbolAsyncIterator] == 'function') {
|
|
1493
1638
|
return FilteringAsyncIterator(value[symbolAsyncIterator](), predicate)
|
|
1494
1639
|
}
|
|
1640
|
+
if (typeof value.filter == 'function') {
|
|
1641
|
+
return value.filter(predicate)
|
|
1642
|
+
}
|
|
1495
1643
|
if (value.constructor == Object) {
|
|
1496
1644
|
return objectFilter(value, predicate)
|
|
1497
1645
|
}
|
|
@@ -1938,11 +2086,6 @@ const transform = function (...args) {
|
|
|
1938
2086
|
return _transform(args[0], args[1], args[2])
|
|
1939
2087
|
}
|
|
1940
2088
|
|
|
1941
|
-
const arrayPush = function (array, value) {
|
|
1942
|
-
array.push(value)
|
|
1943
|
-
return array
|
|
1944
|
-
}
|
|
1945
|
-
|
|
1946
2089
|
const FlatMappingIterator = function (iterator, flatMapper) {
|
|
1947
2090
|
let buffer = [],
|
|
1948
2091
|
bufferIndex = 0
|
|
@@ -1977,13 +2120,12 @@ const FlatMappingIterator = function (iterator, flatMapper) {
|
|
|
1977
2120
|
}
|
|
1978
2121
|
}
|
|
1979
2122
|
|
|
1980
|
-
const sleep = time => new Promise(resolve => {
|
|
1981
|
-
setTimeout(resolve, time)
|
|
1982
|
-
})
|
|
1983
|
-
|
|
1984
2123
|
const FlatMappingAsyncIterator = function (asyncIterator, flatMapper) {
|
|
1985
|
-
const buffer = []
|
|
1986
|
-
|
|
2124
|
+
const buffer = []
|
|
2125
|
+
const promises = new Set()
|
|
2126
|
+
|
|
2127
|
+
let consumingAsyncIterator = false
|
|
2128
|
+
let isAsyncIteratorDone = false
|
|
1987
2129
|
|
|
1988
2130
|
return {
|
|
1989
2131
|
isAsyncIteratorDone: false,
|
|
@@ -1994,38 +2136,48 @@ const FlatMappingAsyncIterator = function (asyncIterator, flatMapper) {
|
|
|
1994
2136
|
return '[object FlatMappingAsyncIterator]'
|
|
1995
2137
|
},
|
|
1996
2138
|
|
|
1997
|
-
|
|
1998
|
-
async
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
const promise = bufferLoading.then(() => promises.delete(promise))
|
|
2012
|
-
promises.add(promise)
|
|
2013
|
-
} else {
|
|
2014
|
-
const bufferLoading = genericReduce(monad, arrayPush, buffer)
|
|
2015
|
-
if (isPromise(bufferLoading)) {
|
|
2016
|
-
const promise = bufferLoading.then(() => promises.delete(promise))
|
|
2017
|
-
promises.add(promise)
|
|
2018
|
-
}
|
|
2019
|
-
}
|
|
2139
|
+
// _consumeAsyncIterator() -> Promise<>
|
|
2140
|
+
async _consumeAsyncIterator() {
|
|
2141
|
+
for await (const item of asyncIterator) {
|
|
2142
|
+
const monad = flatMapper(item)
|
|
2143
|
+
if (isPromise(monad)) {
|
|
2144
|
+
const bufferLoading =
|
|
2145
|
+
monad.then(curry3(genericReduce, __, arrayPush, buffer))
|
|
2146
|
+
const promise = bufferLoading.then(() => promises.delete(promise))
|
|
2147
|
+
promises.add(promise)
|
|
2148
|
+
} else {
|
|
2149
|
+
const bufferLoading = genericReduce(monad, arrayPush, buffer)
|
|
2150
|
+
if (isPromise(bufferLoading)) {
|
|
2151
|
+
const promise = bufferLoading.then(() => promises.delete(promise))
|
|
2152
|
+
promises.add(promise)
|
|
2020
2153
|
}
|
|
2021
2154
|
}
|
|
2155
|
+
}
|
|
2156
|
+
isAsyncIteratorDone = true
|
|
2157
|
+
},
|
|
2158
|
+
|
|
2159
|
+
|
|
2160
|
+
async next() {
|
|
2161
|
+
if (!consumingAsyncIterator) {
|
|
2162
|
+
this._consumeAsyncIterator()
|
|
2163
|
+
consumingAsyncIterator = true
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
while (!isAsyncIteratorDone || promises.size > 0) {
|
|
2022
2167
|
if (buffer.length > 0) {
|
|
2023
2168
|
return { value: buffer.shift(), done: false }
|
|
2024
2169
|
}
|
|
2025
2170
|
if (promises.size > 0) {
|
|
2026
|
-
await promiseRace(
|
|
2171
|
+
await promiseRace(promises)
|
|
2172
|
+
} else {
|
|
2173
|
+
await sleep(10)
|
|
2027
2174
|
}
|
|
2028
2175
|
}
|
|
2176
|
+
|
|
2177
|
+
if (buffer.length > 0) {
|
|
2178
|
+
return { value: buffer.shift(), done: false }
|
|
2179
|
+
}
|
|
2180
|
+
|
|
2029
2181
|
return { value: undefined, done: true }
|
|
2030
2182
|
},
|
|
2031
2183
|
}
|
|
@@ -2121,9 +2273,12 @@ const objectFlatten = function (object) {
|
|
|
2121
2273
|
|
|
2122
2274
|
for (const key in object) {
|
|
2123
2275
|
const element = object[key]
|
|
2276
|
+
|
|
2124
2277
|
if (element == null) {
|
|
2125
2278
|
continue
|
|
2126
|
-
}
|
|
2279
|
+
}
|
|
2280
|
+
|
|
2281
|
+
if (typeof element[symbolIterator] == 'function') {
|
|
2127
2282
|
for (const monadElement of element) {
|
|
2128
2283
|
objectAssign(result, monadElement)
|
|
2129
2284
|
}
|
|
@@ -2384,7 +2539,7 @@ const _iteratorForEachSeriesAsync = async function (iterator, callback) {
|
|
|
2384
2539
|
|
|
2385
2540
|
const iteratorForEachSeries = function (iterator, callback) {
|
|
2386
2541
|
let iteration = iterator.next()
|
|
2387
|
-
while (!
|
|
2542
|
+
while (!iteration.done) {
|
|
2388
2543
|
const operation = callback(iteration.value)
|
|
2389
2544
|
if (isPromise(operation)) {
|
|
2390
2545
|
return operation
|
|
@@ -2414,16 +2569,16 @@ const _forEach = function (collection, callback) {
|
|
|
2414
2569
|
if (collection == null) {
|
|
2415
2570
|
return collection
|
|
2416
2571
|
}
|
|
2417
|
-
if (typeof collection.forEach == 'function') {
|
|
2418
|
-
collection.forEach(callback)
|
|
2419
|
-
return collection
|
|
2420
|
-
}
|
|
2421
2572
|
if (typeof collection[symbolIterator] == 'function') {
|
|
2422
2573
|
return iteratorForEach(collection[symbolIterator](), callback)
|
|
2423
2574
|
}
|
|
2424
2575
|
if (typeof collection[symbolAsyncIterator] == 'function') {
|
|
2425
2576
|
return asyncIteratorForEach(collection[symbolAsyncIterator](), callback)
|
|
2426
2577
|
}
|
|
2578
|
+
if (typeof collection.forEach == 'function') {
|
|
2579
|
+
collection.forEach(callback)
|
|
2580
|
+
return collection
|
|
2581
|
+
}
|
|
2427
2582
|
if (collection.constructor == Object) {
|
|
2428
2583
|
return objectForEach(collection, callback)
|
|
2429
2584
|
}
|
|
@@ -3120,8 +3275,8 @@ const get = function (arg0, arg1, arg2) {
|
|
|
3120
3275
|
if (typeof arg0 == 'string' || typeof arg0 == 'number' || isArray(arg0)) {
|
|
3121
3276
|
return curry3(_get, __, arg0, arg1)
|
|
3122
3277
|
}
|
|
3123
|
-
if (isPromise(arg0)) {
|
|
3124
|
-
return arg0.then(
|
|
3278
|
+
if (isPromise(arg0) || isPromise(arg2)) {
|
|
3279
|
+
return promiseAll([arg0, arg1, arg2]).then(curry2(funcApply, _get, __))
|
|
3125
3280
|
}
|
|
3126
3281
|
return _get(arg0, arg1, arg2)
|
|
3127
3282
|
}
|
package/every.js
CHANGED
|
@@ -84,7 +84,7 @@ const _every = function (collection, predicate) {
|
|
|
84
84
|
* every(asyncNumbers(), async number => number < 6).then(console.log) // true
|
|
85
85
|
* ```
|
|
86
86
|
*
|
|
87
|
-
* `every` supports a lazy
|
|
87
|
+
* `every` supports a lazy interface for composability.
|
|
88
88
|
*
|
|
89
89
|
* ```javascript [playground]
|
|
90
90
|
* pipe([1, 2, 3], [
|
|
@@ -93,7 +93,7 @@ const _every = function (collection, predicate) {
|
|
|
93
93
|
* ])
|
|
94
94
|
* ```
|
|
95
95
|
*
|
|
96
|
-
* Any promises passed in argument position are resolved for their values before further execution.
|
|
96
|
+
* Any promises passed in data argument position are resolved for their values before further execution.
|
|
97
97
|
*
|
|
98
98
|
* ```javascript [playground]
|
|
99
99
|
* every(Promise.resolve([1, 2, 3, 4, 5]), n => n < 6).then(console.log) // true
|
package/filter.js
CHANGED
|
@@ -70,15 +70,15 @@ const _filter = function (value, predicate) {
|
|
|
70
70
|
if (value.constructor == Map) {
|
|
71
71
|
return mapFilter(value, predicate)
|
|
72
72
|
}
|
|
73
|
-
if (typeof value.filter == 'function') {
|
|
74
|
-
return value.filter(predicate)
|
|
75
|
-
}
|
|
76
73
|
if (typeof value[symbolIterator] == 'function') {
|
|
77
74
|
return FilteringIterator(value[symbolIterator](), predicate)
|
|
78
75
|
}
|
|
79
76
|
if (typeof value[symbolAsyncIterator] == 'function') {
|
|
80
77
|
return FilteringAsyncIterator(value[symbolAsyncIterator](), predicate)
|
|
81
78
|
}
|
|
79
|
+
if (typeof value.filter == 'function') {
|
|
80
|
+
return value.filter(predicate)
|
|
81
|
+
}
|
|
82
82
|
if (value.constructor == Object) {
|
|
83
83
|
return objectFilter(value, predicate)
|
|
84
84
|
}
|
|
@@ -256,7 +256,7 @@ const _filter = function (value, predicate) {
|
|
|
256
256
|
* }
|
|
257
257
|
* ```
|
|
258
258
|
*
|
|
259
|
-
* Any promises passed in argument position are resolved for their values before further execution.
|
|
259
|
+
* Any promises passed in data argument position are resolved for their values before further execution.
|
|
260
260
|
*
|
|
261
261
|
* ```javascript [playground]
|
|
262
262
|
* const isOdd = number => number % 2 == 1
|
package/flatMap.js
CHANGED
|
@@ -20,7 +20,7 @@ const __ = require('./_internal/placeholder')
|
|
|
20
20
|
*
|
|
21
21
|
* _flatMap(
|
|
22
22
|
* m Monad,
|
|
23
|
-
* flatMapper (
|
|
23
|
+
* flatMapper (item any)=>Promise|Iterable,
|
|
24
24
|
* ) -> result Promise|Monad
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
@@ -64,66 +64,84 @@ const _flatMap = function (value, flatMapper) {
|
|
|
64
64
|
*
|
|
65
65
|
* @synopsis
|
|
66
66
|
* ```coffeescript [specscript]
|
|
67
|
-
* type Monad = Array|
|
|
67
|
+
* type Monad = Array|string|Set|Generator|AsyncGenerator|{ flatMap: string }|{ chain: string }|Object
|
|
68
68
|
*
|
|
69
69
|
* type SyncOrAsyncFlatMapper = (
|
|
70
|
-
*
|
|
70
|
+
* item any,
|
|
71
71
|
* indexOrKey number|string|any,
|
|
72
72
|
* monad Monad
|
|
73
|
-
* )=>Promise|Monad|any
|
|
73
|
+
* )=>(flatMappedItem Promise|Monad|any)
|
|
74
74
|
*
|
|
75
|
-
* flatMapper SyncOrAsyncFlatMapper
|
|
76
|
-
*
|
|
77
|
-
* flatMap(monad Promise|Monad, flatMapper) -> result Promise|Monad
|
|
78
|
-
* flatMap(flatMapper)(monad Monad) -> result Promise|Monad
|
|
75
|
+
* flatMap(monad Promise|Monad, flatMapper SyncOrAsyncFlatMapper) -> flatMappedMonad Promise|Monad
|
|
76
|
+
* flatMap(flatMapper SyncOrAsyncFlatMapper)(monad Monad) -> flatMappedMonad Promise|Monad
|
|
79
77
|
* ```
|
|
80
78
|
*
|
|
81
79
|
* @description
|
|
82
|
-
* Applies a
|
|
80
|
+
* Applies a flat-mapper function to each item of a monad, returning a flat-mapped monad of the same type.
|
|
83
81
|
*
|
|
84
|
-
* A
|
|
82
|
+
* A flat-mapping operation iterates through each item of a monad and applies the flat-mapper function to each item, flattening the result of the execution into the returned monad.
|
|
85
83
|
*
|
|
86
|
-
* If the
|
|
84
|
+
* If the flat-mapper is asynchronous, it is executed concurrently. The execution result may be asynchronously iterable, in which case it is muxed into the returned monad.
|
|
87
85
|
*
|
|
88
|
-
* The following data types are considered to be monads
|
|
86
|
+
* The following data types are considered to be monads:
|
|
89
87
|
* * `array`
|
|
90
88
|
* * `string`
|
|
91
89
|
* * `set`
|
|
92
|
-
* * `
|
|
90
|
+
* * `generator`
|
|
93
91
|
* * `async generator`
|
|
94
92
|
* * `object with .flatMap method`
|
|
95
93
|
* * `object with .chain method`
|
|
96
94
|
* * `object`
|
|
97
95
|
*
|
|
98
|
-
*
|
|
96
|
+
* The flat-mapper function signature changes depending on the provided monad.
|
|
97
|
+
*
|
|
98
|
+
* If the monad is an array:
|
|
99
|
+
* ```coffeescript [specscript]
|
|
100
|
+
* flatMapper(item any, index number, monad Array) -> flatMappedItem Promise|Monad|any
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* If the monad is a string:
|
|
104
|
+
* ```coffeescript [specscript]
|
|
105
|
+
* flatMapper(character string, index number, monad string) -> flatMappedItem Promise|Monad|any
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* If the monad is a set:
|
|
109
|
+
* ```coffeescript [specscript]
|
|
110
|
+
* flatMapper(item any, item any, monad set) -> flatMappedItem Promise|Monad|any
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* If the monad is a generator:
|
|
114
|
+
* ```coffeescript [specscript]
|
|
115
|
+
* flatMapper(item any) -> flatMappedItem Monad|any
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* If the monad is an async generator:
|
|
119
|
+
* ```coffeescript [specscript]
|
|
120
|
+
* flatMapper(item any) -> flatMappedItem Promise|Monad|any
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* If the monad is a plain object:
|
|
124
|
+
* ```coffeescript [specscript]
|
|
125
|
+
* flatMapper(item any, key string, monad Object) -> flatMappedItem Promise|Monad|any
|
|
126
|
+
* ```
|
|
99
127
|
*
|
|
100
128
|
* ```javascript [playground]
|
|
101
|
-
* const
|
|
102
|
-
*
|
|
103
|
-
* flatMap(
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* (function* () { yield 3; yield 3 })(),
|
|
107
|
-
* (async function* () { yield 7; yield 7 })(),
|
|
108
|
-
* { a: 5, b: 5 }, // object
|
|
109
|
-
* new Uint8Array([8]), // typedArray
|
|
110
|
-
* ]).then(console.log)
|
|
111
|
-
* // [1, 1, 2, 3, 3, 5, 5, 8, 7, 7]
|
|
129
|
+
* const duplicate = value => [value, value]
|
|
130
|
+
*
|
|
131
|
+
* const duplicated = flatMap([1, 2, 3, 4, 5], duplicate)
|
|
132
|
+
*
|
|
133
|
+
* console.log(duplicated)
|
|
112
134
|
* ```
|
|
113
135
|
*
|
|
136
|
+
* If the iterable is an object with a `.flatMap` or `.chain` method, the flat-mapper function signature is defined externally.
|
|
137
|
+
*
|
|
114
138
|
* Values from async generators are muxed. Muxing, or asynchronously "mixing", is the process of combining multiple asynchronous sources into one source, with order determined by the asynchronous resolution of the individual promise elements.
|
|
115
139
|
*
|
|
140
|
+
* For other types of monads, order is preserved from the original monad and applied to the flat-mapped items, which are then concatenated into the flat-mapped monad. The order of the items of a given flat-mapped item is determined by the structure of the flat-mapped item.
|
|
141
|
+
*
|
|
116
142
|
* ```javascript [playground]
|
|
117
143
|
* const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
|
|
118
144
|
*
|
|
119
|
-
* const repeat3 = function* (message) {
|
|
120
|
-
* yield message; yield message; yield message
|
|
121
|
-
* }
|
|
122
|
-
*
|
|
123
|
-
* console.log( // sync is concatenated
|
|
124
|
-
* flatMap(['foo', 'bar', 'baz'], repeat3),
|
|
125
|
-
* ) // ['foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz']
|
|
126
|
-
*
|
|
127
145
|
* const asyncRepeat3 = async function* (message) {
|
|
128
146
|
* yield message
|
|
129
147
|
* await sleep(100)
|
|
@@ -133,35 +151,24 @@ const _flatMap = function (value, flatMapper) {
|
|
|
133
151
|
* }
|
|
134
152
|
*
|
|
135
153
|
* // values from async generators are muxed
|
|
136
|
-
* flatMap(['foo', 'bar', 'baz'], asyncRepeat3)
|
|
137
|
-
* // ['foo', 'bar', 'baz', 'foo', 'bar', 'baz', 'foo', 'bar', 'baz']
|
|
138
|
-
* ```
|
|
154
|
+
* const muxed = await flatMap(['foo', 'bar', 'baz'], asyncRepeat3)
|
|
139
155
|
*
|
|
140
|
-
*
|
|
156
|
+
* console.log(muxed)
|
|
141
157
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
* console.log(
|
|
146
|
-
* flatMap([1, 2, 3, 4, 5], duplicate)
|
|
147
|
-
* ) // [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
|
|
148
|
-
* ```
|
|
149
|
-
*
|
|
150
|
-
* `flatMap` acts on each character of a string.
|
|
158
|
+
* const repeat3 = function* (message) {
|
|
159
|
+
* yield message; yield message; yield message
|
|
160
|
+
* }
|
|
151
161
|
*
|
|
152
|
-
*
|
|
153
|
-
* const
|
|
162
|
+
* // values from generators and other monads are concatenated
|
|
163
|
+
* const repeated = flatMap(['foo', 'bar', 'baz'], repeat3)
|
|
154
164
|
*
|
|
155
|
-
* console.log(
|
|
156
|
-
* flatMap('12345', duplicate)
|
|
157
|
-
* ) // 1122334455
|
|
165
|
+
* console.log(repeated)
|
|
158
166
|
* ```
|
|
159
167
|
*
|
|
160
|
-
*
|
|
168
|
+
* If the monad is a promise, it is resolved for its value before further execution for the eager interface only.
|
|
161
169
|
*
|
|
162
170
|
* ```javascript [playground]
|
|
163
171
|
* flatMap(Promise.resolve([1, 2, 3, 4, 5]), n => [n, n]).then(console.log)
|
|
164
|
-
* // [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
|
|
165
172
|
* ```
|
|
166
173
|
*
|
|
167
174
|
* See also:
|
|
@@ -179,7 +186,7 @@ const _flatMap = function (value, flatMapper) {
|
|
|
179
186
|
* @archive
|
|
180
187
|
* * For typed arrays (type [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects)) and Node.js buffers (type [`Buffer`](https://nodejs.org/api/buffer.html)), `flatMap` applies a flatMapper function to each value of the typed array/buffer, joining the result of each execution with `.set` into the resulting typed array
|
|
181
188
|
*
|
|
182
|
-
* * For Node.js duplex streams (type [Stream](https://nodejs.org/api/stream.html#class-streamduplex)), `flatMap` applies a flatMapper function to each
|
|
189
|
+
* * For Node.js duplex streams (type [Stream](https://nodejs.org/api/stream.html#class-streamduplex)), `flatMap` applies a flatMapper function to each item of the stream, writing (`.write`) each item of each execution into the duplex stream
|
|
183
190
|
*/
|
|
184
191
|
const flatMap = (arg0, arg1) => {
|
|
185
192
|
if (typeof arg0 == 'function') {
|