rubico 2.1.0 → 2.2.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 -0
- package/_internal/genericTransform.js +30 -28
- 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 +1 -1
- package/dist/curry.es.min.js +1 -1
- package/dist/curry.js +1 -1
- package/dist/curry.min.js +1 -1
- package/dist/curry.mjs +1 -1
- 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 +1 -1
- package/dist/filter.es.min.js +1 -1
- package/dist/filter.js +1 -1
- package/dist/filter.min.js +1 -1
- package/dist/filter.mjs +1 -1
- package/dist/flatMap.es.js +1 -1
- package/dist/flatMap.es.min.js +1 -1
- package/dist/flatMap.js +1 -1
- package/dist/flatMap.min.js +1 -1
- package/dist/flatMap.mjs +1 -1
- package/dist/forEach.es.js +1 -1
- package/dist/forEach.es.min.js +1 -1
- package/dist/forEach.js +1 -1
- package/dist/forEach.min.js +1 -1
- package/dist/forEach.mjs +1 -1
- package/dist/get.es.js +1 -1
- package/dist/get.es.min.js +1 -1
- package/dist/get.js +1 -1
- package/dist/get.min.js +1 -1
- package/dist/get.mjs +1 -1
- 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 +1 -1
- package/dist/map.es.min.js +1 -1
- package/dist/map.js +1 -1
- package/dist/map.min.js +1 -1
- package/dist/map.mjs +1 -1
- 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 +57 -46
- package/dist/rubico.es.min.js +2 -2
- package/dist/rubico.global.js +57 -46
- package/dist/rubico.global.min.js +2 -2
- package/dist/rubico.js +57 -46
- package/dist/rubico.min.js +2 -2
- package/dist/rubico.mjs +57 -46
- 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 +32 -2
- package/dist/tap.es.min.js +2 -2
- package/dist/tap.js +32 -2
- package/dist/tap.min.js +2 -2
- package/dist/tap.mjs +32 -2
- 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 +26 -24
- package/dist/transform.es.min.js +2 -2
- package/dist/transform.js +26 -24
- package/dist/transform.min.js +2 -2
- package/dist/transform.mjs +26 -24
- 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 +1 -1
- package/dist/x/filterOut.es.min.js +1 -1
- package/dist/x/filterOut.js +1 -1
- package/dist/x/filterOut.min.js +1 -1
- package/dist/x/filterOut.mjs +1 -1
- package/dist/x/find.es.js +1 -1
- package/dist/x/find.es.min.js +1 -1
- package/dist/x/find.js +1 -1
- package/dist/x/find.min.js +1 -1
- package/dist/x/find.mjs +1 -1
- 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 +1 -1
- package/dist/x/flatten.es.min.js +1 -1
- package/dist/x/flatten.js +1 -1
- package/dist/x/flatten.min.js +1 -1
- package/dist/x/flatten.mjs +1 -1
- 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 +1 -1
- package/dist/x/maxBy.es.min.js +1 -1
- package/dist/x/maxBy.js +1 -1
- package/dist/x/maxBy.min.js +1 -1
- package/dist/x/maxBy.mjs +1 -1
- 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 +1 -1
- package/dist/x/pluck.es.min.js +1 -1
- package/dist/x/pluck.js +1 -1
- package/dist/x/pluck.min.js +1 -1
- package/dist/x/pluck.mjs +1 -1
- 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 +32 -2
- package/dist/x/trace.es.min.js +2 -2
- package/dist/x/trace.js +32 -2
- package/dist/x/trace.min.js +2 -2
- package/dist/x/trace.mjs +32 -2
- 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/es.js +57 -46
- package/index.js +57 -46
- package/package.json +43 -2
- package/tap.js +15 -4
- package/.eslintignore +0 -5
- package/.eslintrc.js +0 -330
- package/.github/workflows/nodejs.yml +0 -27
- package/.vscode/launch.json +0 -25
- package/AggregateReducer.test.js +0 -82
- package/CHANGELOG.md +0 -310
- package/CNAME +0 -1
- package/CODE_OF_CONDUCT.md +0 -128
- package/CONTRIBUTING.md +0 -4
- package/Transducer.test.js +0 -154
- package/_config.yml +0 -1
- package/_internal/improvedGenericTransform.js +0 -93
- package/archive/FlatMappingIterator-2020-09-28.js +0 -68
- package/archive/_Promise-2023-05-29.js +0 -93
- package/archive/arrayMap2-2023-05-29.js +0 -73
- package/archive/benchmarks-v1.9.7/all.js +0 -34
- package/archive/benchmarks-v1.9.7/and.js +0 -24
- package/archive/benchmarks-v1.9.7/any.js +0 -32
- package/archive/benchmarks-v1.9.7/assign.js +0 -174
- package/archive/benchmarks-v1.9.7/curry.js +0 -55
- package/archive/benchmarks-v1.9.7/eq.js +0 -25
- package/archive/benchmarks-v1.9.7/filter.js +0 -1322
- package/archive/benchmarks-v1.9.7/flatMap.js +0 -48
- package/archive/benchmarks-v1.9.7/forEach.js +0 -33
- package/archive/benchmarks-v1.9.7/fork.js +0 -123
- package/archive/benchmarks-v1.9.7/get.js +0 -44
- package/archive/benchmarks-v1.9.7/gt.js +0 -25
- package/archive/benchmarks-v1.9.7/gte.js +0 -25
- package/archive/benchmarks-v1.9.7/integration.js +0 -88
- package/archive/benchmarks-v1.9.7/lt.js +0 -25
- package/archive/benchmarks-v1.9.7/lte.js +0 -25
- package/archive/benchmarks-v1.9.7/map.js +0 -892
- package/archive/benchmarks-v1.9.7/misc.js +0 -470
- package/archive/benchmarks-v1.9.7/omit.js +0 -28
- package/archive/benchmarks-v1.9.7/or.js +0 -51
- package/archive/benchmarks-v1.9.7/pick.js +0 -24
- package/archive/benchmarks-v1.9.7/pipe.js +0 -152
- package/archive/benchmarks-v1.9.7/reduce.js +0 -739
- package/archive/benchmarks-v1.9.7/switchCase.js +0 -256
- package/archive/benchmarks-v1.9.7/tap.js +0 -90
- package/archive/benchmarks-v1.9.7/transform.js +0 -218
- package/archive/benchmarks-v1.9.7/tryCatch.js +0 -108
- package/archive/curry.js +0 -13
- package/archive/examples-v1.6.11/browserModule.html +0 -31
- package/archive/examples-v1.6.11/browserScript.html +0 -32
- package/archive/examples-v1.6.11/coolTransformToConsole.js +0 -1
- package/archive/examples-v1.6.11/denoLargeGasMap.js +0 -85
- package/archive/examples-v1.6.11/denoServer.js +0 -9
- package/archive/examples-v1.6.11/denoServerWithMiddleware.js +0 -55
- package/archive/examples-v1.6.11/logTodosRange.js +0 -21
- package/archive/examples-v1.6.11/makeARequest.html +0 -46
- package/archive/examples-v1.6.11/nodeComplexTransformation.js +0 -106
- package/archive/examples-v1.6.11/nodeConditionallyExtractIDs.js +0 -50
- package/archive/examples-v1.6.11/nodeFloatingPoint.js +0 -55
- package/archive/examples-v1.6.11/nodeGetAllJiraBoards.js +0 -45
- package/archive/examples-v1.6.11/nodeGetSocketIOClientsTotalCount.js +0 -34
- package/archive/examples-v1.6.11/nodeKafkaConsumer.js +0 -63
- package/archive/examples-v1.6.11/nodeMakeMultipleRequests.js +0 -55
- package/archive/examples-v1.6.11/nodeOnRecordAddTask.js +0 -34
- package/archive/examples-v1.6.11/nodePostRequestWithAuth.js +0 -36
- package/archive/examples-v1.6.11/nodeSpeedbackMatchmakingAlgorithm.js +0 -96
- package/archive/examples-v1.6.11/nodeTireData.js +0 -36
- package/archive/examples-v1.6.11/nodeTripleMap.js +0 -85
- package/archive/examples-v1.6.11/squaredOdds.ts +0 -16
- package/archive/examples-v1.6.11/transducerMaxBuyer.js +0 -67
- package/archive/examples-v1.6.11/transformLogTodosRange.js +0 -31
- package/archive/examples-v1.6.11/transformStreamRandomInts.js +0 -19
- package/archive/genericReduceConcurrent.js +0 -277
- package/archive/index.v0.2.9.js +0 -1685
- package/archive/objectValuesGenerator.js +0 -13
- package/archive/test.v0.2.9.js +0 -2334
- package/bench +0 -65
- package/benchmark-output/v1.9.7 +0 -268
- package/benchmarks/all.async.js +0 -43
- package/benchmarks/all.js +0 -43
- package/benchmarks/always.js +0 -15
- package/benchmarks/and.async.js +0 -25
- package/benchmarks/and.js +0 -25
- package/benchmarks/assign.async.js +0 -27
- package/benchmarks/assign.js +0 -27
- package/benchmarks/curry.js +0 -35
- package/benchmarks/eq.async.js +0 -23
- package/benchmarks/eq.js +0 -23
- package/benchmarks/every.async.js +0 -19
- package/benchmarks/every.js +0 -19
- package/benchmarks/filter.async.js +0 -32
- package/benchmarks/filter.js +0 -38
- package/benchmarks/flatMap.async.js +0 -26
- package/benchmarks/flatMap.js +0 -38
- package/benchmarks/get.async.js +0 -19
- package/benchmarks/get.js +0 -39
- package/benchmarks/gt.async.js +0 -23
- package/benchmarks/gt.js +0 -23
- package/benchmarks/gte.async.js +0 -23
- package/benchmarks/gte.js +0 -23
- package/benchmarks/lt.async.js +0 -23
- package/benchmarks/lt.js +0 -23
- package/benchmarks/lte.async.js +0 -23
- package/benchmarks/lte.js +0 -23
- package/benchmarks/map.async.js +0 -43
- package/benchmarks/map.js +0 -40
- package/benchmarks/misc/Promise.js +0 -26
- package/benchmarks/misc/isPromise.js +0 -30
- package/benchmarks/misc/promiseAll.js +0 -36
- package/benchmarks/not.js +0 -23
- package/benchmarks/omit.js +0 -38
- package/benchmarks/or.async.js +0 -25
- package/benchmarks/or.js +0 -25
- package/benchmarks/pick.js +0 -38
- package/benchmarks/pipe.async.js +0 -47
- package/benchmarks/pipe.js +0 -47
- package/benchmarks/reduce.async.js +0 -32
- package/benchmarks/reduce.js +0 -38
- package/benchmarks/set.async.js +0 -19
- package/benchmarks/set.js +0 -41
- package/benchmarks/some.async.js +0 -19
- package/benchmarks/some.js +0 -19
- package/benchmarks/switchCase.async.js +0 -27
- package/benchmarks/switchCase.js +0 -55
- package/benchmarks/tap.js +0 -15
- package/benchmarks/thunkify.js +0 -15
- package/benchmarks/transform.async.js +0 -27
- package/benchmarks/transform.js +0 -40
- package/benchmarks/tryCatch.async.js +0 -25
- package/benchmarks/tryCatch.js +0 -32
- package/build +0 -472
- package/dist-test.js +0 -37
- package/examples/type-to-search-wiki/README.md +0 -19
- package/examples/type-to-search-wiki/package.json +0 -13
- package/examples/type-to-search-wiki/src/app.js +0 -46
- package/examples/type-to-search-wiki/src/index.html +0 -18
- package/import-test.html +0 -73
- package/memory-usage/reduce.js +0 -40
- package/monad/AsyncPool.js +0 -0
- package/monad/BrokenPromise.js +0 -63
- package/monad/BrokenPromise.test.js +0 -22
- package/monad/Cancellable.js +0 -84
- package/monad/Cancellable.memoryUsage.js +0 -60
- package/monad/Cancellable.test.js +0 -20
- package/monad/Function.js +0 -0
- package/monad/Instance.benchmark.js +0 -378
- package/monad/Instance.js +0 -351
- package/monad/Instance.test.js +0 -445
- package/monad/Mux.benchmark.js +0 -29
- package/monad/Mux.js +0 -589
- package/monad/Mux.test.js +0 -512
- package/monad/PossiblePromise.benchmark.js +0 -87
- package/monad/PossiblePromise.js +0 -144
- package/monad/PossiblePromise.test.js +0 -89
- package/monad/README.md +0 -75
- package/monad/Reducer.js +0 -0
- package/monad/Sequence.benchmark.js +0 -42
- package/monad/Sequence.js +0 -36
- package/monad/SpecScript.js +0 -0
- package/monad/Stack.js +0 -0
- package/monad/Struct.benchmark.js +0 -154
- package/monad/Struct.js +0 -249
- package/monad/Struct.test.js +0 -237
- package/rubico.js +0 -66
- package/test.js +0 -4242
- package/tmp.js +0 -1
package/test.js
DELETED
|
@@ -1,4242 +0,0 @@
|
|
|
1
|
-
const Test = require('thunk-test')
|
|
2
|
-
const assert = require('assert')
|
|
3
|
-
const stream = require('stream')
|
|
4
|
-
const path = require('path')
|
|
5
|
-
const fs = require('fs')
|
|
6
|
-
const { Readable, Writable } = require('stream')
|
|
7
|
-
const util = require('util')
|
|
8
|
-
const Transducer = require('./Transducer')
|
|
9
|
-
const rubico = require('./rubico')
|
|
10
|
-
const isDeepEqual = require('./x/isDeepEqual')
|
|
11
|
-
const mapFrom = require('./_internal/mapFrom')
|
|
12
|
-
const sha256 = require('./_internal/sha256')
|
|
13
|
-
|
|
14
|
-
const {
|
|
15
|
-
pipe, compose, tap,
|
|
16
|
-
tryCatch, switchCase,
|
|
17
|
-
all, assign, get, set, pick, omit,
|
|
18
|
-
map, filter, flatMap, forEach, reduce, transform,
|
|
19
|
-
some, every, and, or, not,
|
|
20
|
-
eq, gt, lt, gte, lte,
|
|
21
|
-
thunkify, always,
|
|
22
|
-
curry, __,
|
|
23
|
-
} = rubico
|
|
24
|
-
|
|
25
|
-
const objectProto = Object.prototype
|
|
26
|
-
|
|
27
|
-
const symbolAsyncIterator = Symbol.asyncIterator
|
|
28
|
-
|
|
29
|
-
const nativeObjectToString = objectProto.toString
|
|
30
|
-
|
|
31
|
-
const objectToString = x => nativeObjectToString.call(x)
|
|
32
|
-
|
|
33
|
-
const ase = assert.strictEqual
|
|
34
|
-
|
|
35
|
-
const ade = assert.deepEqual
|
|
36
|
-
|
|
37
|
-
const aok = assert.ok
|
|
38
|
-
|
|
39
|
-
const sleep = ms => new Promise(resolve => { setTimeout(resolve, ms) })
|
|
40
|
-
|
|
41
|
-
const hi = x => `${x}hi`
|
|
42
|
-
|
|
43
|
-
const ho = x => `${x}ho`
|
|
44
|
-
|
|
45
|
-
const isOdd = x => (x % 2 === 1)
|
|
46
|
-
|
|
47
|
-
const square = x => x ** 2
|
|
48
|
-
|
|
49
|
-
const arrayOf = (getter, length) => Array.from(
|
|
50
|
-
{ length },
|
|
51
|
-
typeof getter === 'function' ? getter : () => getter,
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
const async = func => async function asyncFunc(...args) {
|
|
55
|
-
return func(...args)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const delay = (func, ms) => async function delayed(...args) {
|
|
59
|
-
await new Promise(resolve => setTimeout(resolve, ms))
|
|
60
|
-
return func(...args)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const asyncIsEven = x => new Promise(resolve => {
|
|
64
|
-
setImmediate(() => resolve(x % 2 === 0))
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
const asyncHey = x => new Promise(resolve => {
|
|
68
|
-
setImmediate(() => resolve(`${x}hey`))
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
const asyncMult = (y, xi) => new Promise(resolve => {
|
|
72
|
-
setImmediate(() => resolve(y * xi))
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
const constructReadStream = iterable => {
|
|
76
|
-
const y = new stream.Readable({ objectMode: true })
|
|
77
|
-
y._read = () => {}
|
|
78
|
-
for (const x of iterable) y.push(x)
|
|
79
|
-
y.push(null)
|
|
80
|
-
return y
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const readStreamToArray = readStream => new Promise((resolve, reject) => {
|
|
84
|
-
let result = ''
|
|
85
|
-
readStream.on('data', chunk => (result += chunk))
|
|
86
|
-
readStream.on('end', () => resolve(result))
|
|
87
|
-
readStream.on('error', reject)
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
const consumeReadStreamPush = s => new Promise((resolve, reject) => {
|
|
91
|
-
let y = ''
|
|
92
|
-
s.on('data', chunk => { y += chunk })
|
|
93
|
-
s.on('end', () => resolve(y))
|
|
94
|
-
s.on('error', err => reject(err))
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
const consumeReadStreamPull = s => new Promise((resolve, reject) => {
|
|
98
|
-
let y = ''
|
|
99
|
-
s.on('readable', () => {
|
|
100
|
-
let chunk = s.read()
|
|
101
|
-
while (chunk != null) {
|
|
102
|
-
y += `${chunk}`
|
|
103
|
-
chunk = s.read()
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
s.on('end', () => resolve(y))
|
|
107
|
-
s.on('error', err => reject(err))
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
const makeNumbers = function*() {
|
|
111
|
-
for (let i = 0; i < 5; i++) yield i + 1
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const makeAsyncNumbers = async function*() {
|
|
115
|
-
for (let i = 0; i < 5; i++) yield i + 1
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const iteratorToArray = x => {
|
|
119
|
-
let isAsync = false
|
|
120
|
-
const y = []
|
|
121
|
-
for (const xi of x) {
|
|
122
|
-
if (xi instanceof Promise) isAsync = true
|
|
123
|
-
y.push(xi)
|
|
124
|
-
}
|
|
125
|
-
return isAsync ? Promise.all(y) : y
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const asyncIteratorToArray = async x => {
|
|
129
|
-
const y = []
|
|
130
|
-
for await (const xi of x) y.push(xi)
|
|
131
|
-
return y
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const typedArrayConstructors = [
|
|
135
|
-
Uint8ClampedArray,
|
|
136
|
-
Uint8Array, Int8Array,
|
|
137
|
-
Uint16Array, Int16Array,
|
|
138
|
-
Uint32Array, Int32Array,
|
|
139
|
-
Float32Array, Float64Array,
|
|
140
|
-
BigUint64Array, BigInt64Array,
|
|
141
|
-
]
|
|
142
|
-
|
|
143
|
-
const numberTypedArrayConstructors = [
|
|
144
|
-
Uint8ClampedArray,
|
|
145
|
-
Uint8Array, Int8Array,
|
|
146
|
-
Uint16Array, Int16Array,
|
|
147
|
-
Uint32Array, Int32Array,
|
|
148
|
-
Float32Array, Float64Array,
|
|
149
|
-
]
|
|
150
|
-
|
|
151
|
-
const bigIntTypedArrayConstructors = [
|
|
152
|
-
BigUint64Array, BigInt64Array,
|
|
153
|
-
]
|
|
154
|
-
|
|
155
|
-
const AssertionError = function (message) {
|
|
156
|
-
this.message = message
|
|
157
|
-
this.stack = new Error().stack
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/* Assertion.test(reduce(add, 0), {
|
|
161
|
-
args: { 0: [1, 2, 3, 4, 5] },
|
|
162
|
-
result: 15,
|
|
163
|
-
}) */
|
|
164
|
-
|
|
165
|
-
const prettify = value => JSON.stringify(value, null, 2)
|
|
166
|
-
|
|
167
|
-
const Assertion = (func, result, ...args) => async function test() {
|
|
168
|
-
const evaluatedResult = await func(...args)
|
|
169
|
-
if (isDeepEqual(result, evaluatedResult)) {
|
|
170
|
-
return undefined
|
|
171
|
-
}
|
|
172
|
-
throw new AssertionError(`
|
|
173
|
-
${func.toString()}
|
|
174
|
-
|
|
175
|
-
args - ${prettify(args)}
|
|
176
|
-
|
|
177
|
-
expected - ${prettify(result)}
|
|
178
|
-
|
|
179
|
-
actual - ${prettify(evaluatedResult)}
|
|
180
|
-
`)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const binaryToReadableStream = function (binary) {
|
|
184
|
-
return new Readable({
|
|
185
|
-
read() {
|
|
186
|
-
this.push(binary)
|
|
187
|
-
this.push(null)
|
|
188
|
-
},
|
|
189
|
-
})
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const arrayToObjectReadableStream = function (array) {
|
|
193
|
-
return new Readable({
|
|
194
|
-
objectMode: true,
|
|
195
|
-
read() {
|
|
196
|
-
for (const item of array) {
|
|
197
|
-
this.push(item)
|
|
198
|
-
}
|
|
199
|
-
this.push(null)
|
|
200
|
-
},
|
|
201
|
-
})
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const MockWritable = function (array = []) {
|
|
205
|
-
Writable.call(this)
|
|
206
|
-
this.array = array
|
|
207
|
-
this.handlers = new Map()
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
MockWritable.prototype = {
|
|
211
|
-
write(chunk, encoding, callback) {
|
|
212
|
-
this.array.push(chunk)
|
|
213
|
-
},
|
|
214
|
-
on(eventName, handler) {
|
|
215
|
-
if (this.handlers.has(eventName)) {
|
|
216
|
-
this.handlers.get(eventName).push(handler)
|
|
217
|
-
} else {
|
|
218
|
-
this.handlers.set(eventName, [handler])
|
|
219
|
-
}
|
|
220
|
-
},
|
|
221
|
-
once(eventName, handler) {
|
|
222
|
-
this.handlers.set(eventName, [handler])
|
|
223
|
-
},
|
|
224
|
-
emit(eventName, ...args) {
|
|
225
|
-
if (!this.handlers.has(eventName)) {
|
|
226
|
-
return undefined
|
|
227
|
-
}
|
|
228
|
-
return this.handlers.get(eventName).map(handler => handler(...args))
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const MockDuplexStream = function (values) {
|
|
233
|
-
MockWritable.call(this)
|
|
234
|
-
this.values = [...values]
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
MockDuplexStream.prototype = {
|
|
238
|
-
...MockWritable.prototype,
|
|
239
|
-
async* [symbolAsyncIterator]() {
|
|
240
|
-
for (const item of this.values) {
|
|
241
|
-
yield item
|
|
242
|
-
}
|
|
243
|
-
},
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const AsyncMockFoldable = function (values) {
|
|
247
|
-
this.values = values
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
AsyncMockFoldable.prototype = {
|
|
251
|
-
async reduce(reducer, init) {
|
|
252
|
-
return this.values.reduce(reducer, init)
|
|
253
|
-
},
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const MockFoldable = function (values) {
|
|
257
|
-
this.values = values
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
MockFoldable.prototype = {
|
|
261
|
-
reduce(reducer, init) {
|
|
262
|
-
return this.values.reduce(reducer, init)
|
|
263
|
-
},
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
describe('rubico', () => {
|
|
267
|
-
describe('pipe', () => {
|
|
268
|
-
it('behaves eagerly when passed any amount of arguments before the array of functions', async () => {
|
|
269
|
-
const array = pipe(1, 2, 3, [
|
|
270
|
-
Array.of,
|
|
271
|
-
numbers => numbers.map(number => number * 3),
|
|
272
|
-
])
|
|
273
|
-
assert.deepEqual(array, [3, 6, 9])
|
|
274
|
-
})
|
|
275
|
-
it('behaves eagerly and resolves any amount of Promise arguments before the array of functions', async () => {
|
|
276
|
-
const array = await pipe(Promise.resolve(1), 2, Promise.resolve(3), [
|
|
277
|
-
Array.of,
|
|
278
|
-
numbers => numbers.map(number => number * 3),
|
|
279
|
-
])
|
|
280
|
-
assert.deepEqual(array, [3, 6, 9])
|
|
281
|
-
})
|
|
282
|
-
it('chains async and regular functions together', async () => {
|
|
283
|
-
ase(await pipe([hi, ho, asyncHey])('yo'), 'yohihohey')
|
|
284
|
-
})
|
|
285
|
-
it('does something without arguments', async () => {
|
|
286
|
-
ase(await pipe([hi, ho, asyncHey])(), 'undefinedhihohey')
|
|
287
|
-
})
|
|
288
|
-
it('chaining one fn is the same as just calling that fn', async () => {
|
|
289
|
-
ase(await pipe([asyncHey])('yo'), await asyncHey('yo'))
|
|
290
|
-
})
|
|
291
|
-
it('returns the raw value (no promise required) if all functions are sync', async () => {
|
|
292
|
-
ase(pipe([hi, hi, hi])('yo'), 'yohihihi')
|
|
293
|
-
})
|
|
294
|
-
it('returns a promise if any fns async', async () => {
|
|
295
|
-
aok(pipe([hi, hi, hi, asyncHey])('yo') instanceof Promise)
|
|
296
|
-
})
|
|
297
|
-
it('pipe(Array<GeneratorFunction>)(Generator)', async () => {
|
|
298
|
-
const numbersGen = function* () {
|
|
299
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
300
|
-
}
|
|
301
|
-
const isOddGen = function* (iter) {
|
|
302
|
-
for (const item of iter) {
|
|
303
|
-
if (item % 2 == 1) yield item
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
const squareGen = function* (iter) {
|
|
307
|
-
for (const item of iter) {
|
|
308
|
-
yield item ** 2
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
const iter = pipe([isOddGen, squareGen])(numbersGen())
|
|
312
|
-
const arr = [...iter]
|
|
313
|
-
assert.deepEqual(arr, [1, 9, 25])
|
|
314
|
-
})
|
|
315
|
-
it('pipe(Array<AsyncGeneratorFunction>)(AsyncGenerator)', async () => {
|
|
316
|
-
const asyncNumbersGen = async function* () {
|
|
317
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
318
|
-
}
|
|
319
|
-
const isOddGen = async function* (iter) {
|
|
320
|
-
for await (const item of iter) {
|
|
321
|
-
if (item % 2 == 1) yield item
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
const squareGen = async function* (iter) {
|
|
325
|
-
for await (const item of iter) {
|
|
326
|
-
yield item ** 2
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
const iter = pipe([isOddGen, squareGen])(asyncNumbersGen())
|
|
330
|
-
const arr = []
|
|
331
|
-
for await (const item of iter) arr.push(item)
|
|
332
|
-
assert.deepEqual(arr, [1, 9, 25])
|
|
333
|
-
})
|
|
334
|
-
it('throws a TypeError if first argument not an array', async () => {
|
|
335
|
-
assert.throws(
|
|
336
|
-
() => {
|
|
337
|
-
pipe(() => 1, undefined, () => 2)()
|
|
338
|
-
},
|
|
339
|
-
new TypeError('funcs.reduce is not a function')
|
|
340
|
-
)
|
|
341
|
-
})
|
|
342
|
-
it('throws a TypeError if passed less than one function', async () => {
|
|
343
|
-
assert.throws(
|
|
344
|
-
() => pipe([])(),
|
|
345
|
-
new TypeError('Reduce of empty array with no initial value'),
|
|
346
|
-
)
|
|
347
|
-
})
|
|
348
|
-
it('throws a TypeError if any arguments are not a function', async () => {
|
|
349
|
-
assert.throws(
|
|
350
|
-
() => {
|
|
351
|
-
pipe([() => 1, undefined, () => 2])()
|
|
352
|
-
},
|
|
353
|
-
new TypeError('funcB is not a function'),
|
|
354
|
-
)
|
|
355
|
-
})
|
|
356
|
-
it('handles sync errors', async () => {
|
|
357
|
-
assert.throws(
|
|
358
|
-
() => pipe([hi, hi, x => { throw new Error(`throwing ${x}`) }])('yo'),
|
|
359
|
-
new Error('throwing yohihi'),
|
|
360
|
-
)
|
|
361
|
-
})
|
|
362
|
-
it('handles async errors', async () => {
|
|
363
|
-
assert.rejects(
|
|
364
|
-
() => pipe([hi, asyncHey, x => { throw new Error(`throwing ${x}`) }])('yo'),
|
|
365
|
-
new Error('throwing yohihey'),
|
|
366
|
-
)
|
|
367
|
-
})
|
|
368
|
-
})
|
|
369
|
-
|
|
370
|
-
describe('compose', () => {
|
|
371
|
-
it('chains functions in reverse', async () => {
|
|
372
|
-
ade(
|
|
373
|
-
compose([1, 2, 3, 4, 5], [
|
|
374
|
-
map(number => number ** 2),
|
|
375
|
-
map(number => number + 2),
|
|
376
|
-
filter(number => number % 2 == 1)
|
|
377
|
-
]),
|
|
378
|
-
[9, 25, 49],
|
|
379
|
-
)
|
|
380
|
-
|
|
381
|
-
ade(
|
|
382
|
-
compose([
|
|
383
|
-
map(number => number ** 2),
|
|
384
|
-
map(number => number + 2),
|
|
385
|
-
filter(number => number % 2 == 1)
|
|
386
|
-
])([1, 2, 3, 4, 5]),
|
|
387
|
-
[9, 25, 49],
|
|
388
|
-
)
|
|
389
|
-
|
|
390
|
-
ade(
|
|
391
|
-
await compose([
|
|
392
|
-
map(async number => number ** 2),
|
|
393
|
-
map(number => number + 2),
|
|
394
|
-
filter(async number => number % 2 == 1)
|
|
395
|
-
])([1, 2, 3, 4, 5]),
|
|
396
|
-
[9, 25, 49],
|
|
397
|
-
)
|
|
398
|
-
|
|
399
|
-
ade(
|
|
400
|
-
await compose(Promise.resolve([1, 2, 3, 4, 5]), [
|
|
401
|
-
map(number => number ** 2),
|
|
402
|
-
map(number => number + 2),
|
|
403
|
-
filter(number => number % 2 == 1)
|
|
404
|
-
]),
|
|
405
|
-
[9, 25, 49],
|
|
406
|
-
)
|
|
407
|
-
})
|
|
408
|
-
})
|
|
409
|
-
|
|
410
|
-
describe('all', () => {
|
|
411
|
-
it('API coverage', async () => {
|
|
412
|
-
ade(
|
|
413
|
-
all(1, 2, 3, [
|
|
414
|
-
Array.of,
|
|
415
|
-
Array.of,
|
|
416
|
-
Array.of,
|
|
417
|
-
]),
|
|
418
|
-
[
|
|
419
|
-
[1, 2, 3],
|
|
420
|
-
[1, 2, 3],
|
|
421
|
-
[1, 2, 3],
|
|
422
|
-
],
|
|
423
|
-
)
|
|
424
|
-
|
|
425
|
-
ade(
|
|
426
|
-
all([
|
|
427
|
-
Array.of,
|
|
428
|
-
Array.of,
|
|
429
|
-
Array.of,
|
|
430
|
-
])(1, 2, 3),
|
|
431
|
-
[
|
|
432
|
-
[1, 2, 3],
|
|
433
|
-
[1, 2, 3],
|
|
434
|
-
[1, 2, 3],
|
|
435
|
-
],
|
|
436
|
-
)
|
|
437
|
-
|
|
438
|
-
ade(
|
|
439
|
-
await all(Promise.resolve(1), 2, Promise.resolve(3), [
|
|
440
|
-
Array.of,
|
|
441
|
-
Array.of,
|
|
442
|
-
Array.of,
|
|
443
|
-
]),
|
|
444
|
-
[
|
|
445
|
-
[1, 2, 3],
|
|
446
|
-
[1, 2, 3],
|
|
447
|
-
[1, 2, 3],
|
|
448
|
-
],
|
|
449
|
-
)
|
|
450
|
-
|
|
451
|
-
ade(
|
|
452
|
-
all(1, 2, 3, {
|
|
453
|
-
a: Array.of,
|
|
454
|
-
b: Array.of,
|
|
455
|
-
c: Array.of,
|
|
456
|
-
}),
|
|
457
|
-
{
|
|
458
|
-
a: [1, 2, 3],
|
|
459
|
-
b: [1, 2, 3],
|
|
460
|
-
c: [1, 2, 3],
|
|
461
|
-
},
|
|
462
|
-
)
|
|
463
|
-
|
|
464
|
-
ade(
|
|
465
|
-
all({
|
|
466
|
-
a: Array.of,
|
|
467
|
-
b: Array.of,
|
|
468
|
-
c: Array.of,
|
|
469
|
-
})(1, 2, 3),
|
|
470
|
-
{
|
|
471
|
-
a: [1, 2, 3],
|
|
472
|
-
b: [1, 2, 3],
|
|
473
|
-
c: [1, 2, 3],
|
|
474
|
-
},
|
|
475
|
-
)
|
|
476
|
-
|
|
477
|
-
ade(
|
|
478
|
-
await all(Promise.resolve(1), 2, Promise.resolve(3), {
|
|
479
|
-
a: Array.of,
|
|
480
|
-
b: Array.of,
|
|
481
|
-
c: Array.of,
|
|
482
|
-
}),
|
|
483
|
-
{
|
|
484
|
-
a: [1, 2, 3],
|
|
485
|
-
b: [1, 2, 3],
|
|
486
|
-
c: [1, 2, 3],
|
|
487
|
-
},
|
|
488
|
-
)
|
|
489
|
-
})
|
|
490
|
-
it('maps input to array of sync functions', async () => {
|
|
491
|
-
ade(all([hi, hi, hi])('yo'), ['yohi', 'yohi', 'yohi'])
|
|
492
|
-
})
|
|
493
|
-
it('maps input to object of sync functions', async () => {
|
|
494
|
-
ade(
|
|
495
|
-
all({ a: hi, b: hi, c: hi })('yo'),
|
|
496
|
-
{ a: 'yohi', b: 'yohi', c: 'yohi' },
|
|
497
|
-
)
|
|
498
|
-
})
|
|
499
|
-
it('maps input to array of async functions', async () => {
|
|
500
|
-
aok(all([asyncHey, asyncHey, asyncHey])('yo') instanceof Promise)
|
|
501
|
-
ade(
|
|
502
|
-
await all([asyncHey, asyncHey, asyncHey])('yo'),
|
|
503
|
-
['yohey', 'yohey', 'yohey'],
|
|
504
|
-
)
|
|
505
|
-
})
|
|
506
|
-
it('maps input to object of async functions', async () => {
|
|
507
|
-
aok(all({ a: asyncHey, b: asyncHey, c: asyncHey })('yo') instanceof Promise)
|
|
508
|
-
ade(
|
|
509
|
-
await all({ a: asyncHey, b: asyncHey, c: asyncHey })('yo'),
|
|
510
|
-
{ a: 'yohey', b: 'yohey', c: 'yohey' },
|
|
511
|
-
)
|
|
512
|
-
})
|
|
513
|
-
it('any functions async => Promise', async () => {
|
|
514
|
-
aok(all([asyncHey, asyncHey, hi])('yo') instanceof Promise)
|
|
515
|
-
ade(
|
|
516
|
-
await all([asyncHey, asyncHey, hi])('yo'),
|
|
517
|
-
['yohey', 'yohey', 'yohi'],
|
|
518
|
-
)
|
|
519
|
-
})
|
|
520
|
-
it('all([])() -> []', async () => {
|
|
521
|
-
ade(all([])(), [])
|
|
522
|
-
ade(all([])('hey'), [])
|
|
523
|
-
})
|
|
524
|
-
it('all({})() -> {}', async () => {
|
|
525
|
-
ade(all({})(), {})
|
|
526
|
-
ade(all({})('hey'), {})
|
|
527
|
-
})
|
|
528
|
-
it('TypeError for all([\'hey\'])()', async () => {
|
|
529
|
-
assert.throws(
|
|
530
|
-
() => all(['hey'])(),
|
|
531
|
-
new TypeError('funcs[funcsIndex] is not a function'),
|
|
532
|
-
)
|
|
533
|
-
})
|
|
534
|
-
it('throws TypeError for all({ a: nonFunction })', async () => {
|
|
535
|
-
assert.throws(
|
|
536
|
-
() => all({ a: 'hey' })(),
|
|
537
|
-
new TypeError('funcs[key] is not a function'),
|
|
538
|
-
)
|
|
539
|
-
})
|
|
540
|
-
it('throws TypeError for String', async () => {
|
|
541
|
-
assert.throws(
|
|
542
|
-
() => all('ayelmao')(),
|
|
543
|
-
new TypeError('funcs[key] is not a function'),
|
|
544
|
-
)
|
|
545
|
-
})
|
|
546
|
-
it('{} for Set<[func]>; no functions exposed via in', async () => {
|
|
547
|
-
assert.deepEqual(all(new Set([() => 'yo']))('hey'), {})
|
|
548
|
-
})
|
|
549
|
-
it('{} for Map<[[1, func]]>', async () => {
|
|
550
|
-
assert.deepEqual(all(new Map([[1, () => 'yo']]))('hey'), {})
|
|
551
|
-
})
|
|
552
|
-
})
|
|
553
|
-
|
|
554
|
-
describe('all.series', () => {
|
|
555
|
-
it('API coverage', async () => {
|
|
556
|
-
ade(
|
|
557
|
-
all.series(1, 2, 3, [
|
|
558
|
-
Array.of,
|
|
559
|
-
Array.of,
|
|
560
|
-
Array.of,
|
|
561
|
-
]),
|
|
562
|
-
[
|
|
563
|
-
[1, 2, 3],
|
|
564
|
-
[1, 2, 3],
|
|
565
|
-
[1, 2, 3],
|
|
566
|
-
],
|
|
567
|
-
)
|
|
568
|
-
|
|
569
|
-
ade(
|
|
570
|
-
all.series([
|
|
571
|
-
Array.of,
|
|
572
|
-
Array.of,
|
|
573
|
-
Array.of,
|
|
574
|
-
])(1, 2, 3),
|
|
575
|
-
[
|
|
576
|
-
[1, 2, 3],
|
|
577
|
-
[1, 2, 3],
|
|
578
|
-
[1, 2, 3],
|
|
579
|
-
],
|
|
580
|
-
)
|
|
581
|
-
|
|
582
|
-
ade(
|
|
583
|
-
await all.series(Promise.resolve(1), 2, Promise.resolve(3), [
|
|
584
|
-
Array.of,
|
|
585
|
-
Array.of,
|
|
586
|
-
Array.of,
|
|
587
|
-
]),
|
|
588
|
-
[
|
|
589
|
-
[1, 2, 3],
|
|
590
|
-
[1, 2, 3],
|
|
591
|
-
[1, 2, 3],
|
|
592
|
-
],
|
|
593
|
-
)
|
|
594
|
-
})
|
|
595
|
-
|
|
596
|
-
it('evaluates input against array of functions in series (sync)', async () => {
|
|
597
|
-
const arr = []
|
|
598
|
-
ade(
|
|
599
|
-
all.series([
|
|
600
|
-
() => { arr.push(1); return 'a' },
|
|
601
|
-
() => { arr.push(2); return 'b' },
|
|
602
|
-
() => { arr.push(3); return 'c' },
|
|
603
|
-
])(),
|
|
604
|
-
['a', 'b', 'c']
|
|
605
|
-
)
|
|
606
|
-
ade(arr, [1, 2, 3])
|
|
607
|
-
})
|
|
608
|
-
it('evaluates input against array of functions in series (async)', async () => {
|
|
609
|
-
const arr = []
|
|
610
|
-
const staggeredPush = all.series([
|
|
611
|
-
() => sleep(10).then(() => { arr.push(1); return 'a' }),
|
|
612
|
-
() => sleep(5).then(() => { arr.push(2); return 'b' }),
|
|
613
|
-
() => { arr.push(3); return 'c' },
|
|
614
|
-
])()
|
|
615
|
-
aok(staggeredPush instanceof Promise)
|
|
616
|
-
ade(await staggeredPush, ['a', 'b', 'c'])
|
|
617
|
-
ade(arr, [1, 2, 3])
|
|
618
|
-
const arr2 = []
|
|
619
|
-
const parallelPush = all([
|
|
620
|
-
() => sleep(10).then(() => { arr2.push(1); return 'a' }),
|
|
621
|
-
() => sleep(5).then(() => { arr2.push(2); return 'b' }),
|
|
622
|
-
() => { arr2.push(3); return 'c' },
|
|
623
|
-
])()
|
|
624
|
-
aok(parallelPush instanceof Promise)
|
|
625
|
-
ade(await parallelPush, ['a', 'b', 'c'])
|
|
626
|
-
ade(arr2, [3, 2, 1])
|
|
627
|
-
})
|
|
628
|
-
it('throws TypeError for all([])', async () => {
|
|
629
|
-
assert.deepEqual(all.series([])(), [])
|
|
630
|
-
})
|
|
631
|
-
it('throws TypeError for all([nonFunction])', async () => {
|
|
632
|
-
assert.throws(
|
|
633
|
-
() => all.series(['hey'])(),
|
|
634
|
-
new TypeError('funcs[funcsIndex] is not a function')
|
|
635
|
-
)
|
|
636
|
-
})
|
|
637
|
-
})
|
|
638
|
-
|
|
639
|
-
describe('assign', () => {
|
|
640
|
-
it('API coverage', async () => {
|
|
641
|
-
ade(assign({}, {
|
|
642
|
-
a: () => 1,
|
|
643
|
-
b: () => 2,
|
|
644
|
-
c: () => 3,
|
|
645
|
-
}), { a: 1, b: 2, c: 3 })
|
|
646
|
-
|
|
647
|
-
ade(assign({
|
|
648
|
-
a: () => 1,
|
|
649
|
-
b: () => 2,
|
|
650
|
-
c: () => 3,
|
|
651
|
-
})({}), { a: 1, b: 2, c: 3 })
|
|
652
|
-
|
|
653
|
-
ade(await assign(Promise.resolve({}), {
|
|
654
|
-
a: () => 1,
|
|
655
|
-
b: () => 2,
|
|
656
|
-
c: () => 3,
|
|
657
|
-
}), { a: 1, b: 2, c: 3 })
|
|
658
|
-
})
|
|
659
|
-
it('maps input to object of sync functions then merges', async () => {
|
|
660
|
-
ade(
|
|
661
|
-
assign({
|
|
662
|
-
b: x => `${x.a}yo`,
|
|
663
|
-
c: x => `${x.a}yaya`,
|
|
664
|
-
})({ a: 'a' }),
|
|
665
|
-
{ a: 'a', b: 'ayo', c: 'ayaya' },
|
|
666
|
-
)
|
|
667
|
-
})
|
|
668
|
-
it('maps input to object of async functions then merges', async () => {
|
|
669
|
-
aok(assign({
|
|
670
|
-
b: async x => `${x.a}yo`,
|
|
671
|
-
c: async x => `${x.a}yaya`,
|
|
672
|
-
})({ a: 'a' }) instanceof Promise)
|
|
673
|
-
ade(
|
|
674
|
-
await assign({
|
|
675
|
-
b: async x => `${x.a}yo`,
|
|
676
|
-
c: async x => `${x.a}yaya`,
|
|
677
|
-
})({ a: 'a' }),
|
|
678
|
-
{ a: 'a', b: 'ayo', c: 'ayaya' },
|
|
679
|
-
)
|
|
680
|
-
})
|
|
681
|
-
it('assign(Set)(nonObject) -> {}', async () => {
|
|
682
|
-
assert.deepEqual(assign(new Set(['hey']))(), {})
|
|
683
|
-
})
|
|
684
|
-
it('assign(...)(string) -> { 0: s, 1: t, ... }', async () => {
|
|
685
|
-
assert.deepEqual(
|
|
686
|
-
assign({ a: value => value })('hi'),
|
|
687
|
-
{ '0': 'h', '1': 'i', a: 'hi' },
|
|
688
|
-
)
|
|
689
|
-
})
|
|
690
|
-
})
|
|
691
|
-
|
|
692
|
-
describe('tap', () => {
|
|
693
|
-
it('[sync] calls a provided function with input, returning input', async () => {
|
|
694
|
-
ase(tap(x => x + 1)(1), 1)
|
|
695
|
-
})
|
|
696
|
-
it('[async] calls a provided function with input, returning input', async () => {
|
|
697
|
-
aok(tap(async x => x + 1)(1) instanceof Promise)
|
|
698
|
-
ase(await tap(async x => x + 1)(1), 1)
|
|
699
|
-
})
|
|
700
|
-
it('multiple arguments', async () => {
|
|
701
|
-
const array = []
|
|
702
|
-
const point = tap((...args) => (array.push(...args)))(1, 2, 3, 4, 5)
|
|
703
|
-
assert.strictEqual(point, 1)
|
|
704
|
-
assert.deepEqual(array, [1, 2, 3, 4, 5])
|
|
705
|
-
})
|
|
706
|
-
it('throws a TypeError if passed a non function', async () => {
|
|
707
|
-
assert.throws(
|
|
708
|
-
() => tap('hey')(),
|
|
709
|
-
new TypeError('func is not a function'),
|
|
710
|
-
)
|
|
711
|
-
})
|
|
712
|
-
})
|
|
713
|
-
|
|
714
|
-
describe('tap.if', () => {
|
|
715
|
-
describe('tap.if(condition, f); conditional tap; only calls fn on truthy condition', () => {
|
|
716
|
-
it('sync', async () => {
|
|
717
|
-
const isOdd = x => x % 2 === 1
|
|
718
|
-
const oddNumbers = []
|
|
719
|
-
const pushOddNumbers = number => oddNumbers.push(number)
|
|
720
|
-
const numbers = [1, 2, 3, 4, 5]
|
|
721
|
-
numbers.forEach(number => {
|
|
722
|
-
assert.strictEqual(tap.if(isOdd, pushOddNumbers)(number), number)
|
|
723
|
-
})
|
|
724
|
-
assert.deepEqual(oddNumbers, [1, 3, 5])
|
|
725
|
-
})
|
|
726
|
-
it('async', async () => {
|
|
727
|
-
const isOdd = x => x % 2 === 1
|
|
728
|
-
const oddNumbers = []
|
|
729
|
-
const pushOddNumbers = number => oddNumbers.push(number)
|
|
730
|
-
const numbers = [1, 2, 3, 4, 5]
|
|
731
|
-
for (const number of numbers) {
|
|
732
|
-
assert.strictEqual(await tap.if(async(isOdd), pushOddNumbers)(number), number)
|
|
733
|
-
}
|
|
734
|
-
assert.deepEqual(oddNumbers, [1, 3, 5])
|
|
735
|
-
for (const number of numbers) {
|
|
736
|
-
assert.strictEqual(await tap.if(isOdd, delay(pushOddNumbers, 10))(number), number)
|
|
737
|
-
}
|
|
738
|
-
assert.deepEqual(oddNumbers, [1, 3, 5, 1, 3, 5])
|
|
739
|
-
})
|
|
740
|
-
})
|
|
741
|
-
})
|
|
742
|
-
|
|
743
|
-
describe('tryCatch', () => {
|
|
744
|
-
it('behaves eagerly when supplied any amount of arguments before the tryer and catcher', async () => {
|
|
745
|
-
const add = (a, b) => a + b
|
|
746
|
-
|
|
747
|
-
let sum = null
|
|
748
|
-
|
|
749
|
-
tryCatch(1, 2, 3, function throwSum(...numbers) {
|
|
750
|
-
const sum = numbers.reduce(add)
|
|
751
|
-
throw new Error(`${sum}`)
|
|
752
|
-
}, function logErrorMessage(error) {
|
|
753
|
-
sum = Number(error.message)
|
|
754
|
-
})
|
|
755
|
-
|
|
756
|
-
assert.equal(sum, 6)
|
|
757
|
-
})
|
|
758
|
-
|
|
759
|
-
it('behaves eagerly when supplied any amount of Promise arguments before the tryer and catcher', async () => {
|
|
760
|
-
const add = (a, b) => a + b
|
|
761
|
-
|
|
762
|
-
let sum = null
|
|
763
|
-
|
|
764
|
-
await tryCatch(Promise.resolve(1), 2, Promise.resolve(3), function throwSum(...numbers) {
|
|
765
|
-
const sum = numbers.reduce(add)
|
|
766
|
-
throw new Error(`${sum}`)
|
|
767
|
-
}, function logErrorMessage(error) {
|
|
768
|
-
sum = Number(error.message)
|
|
769
|
-
})
|
|
770
|
-
|
|
771
|
-
assert.equal(sum, 6)
|
|
772
|
-
})
|
|
773
|
-
|
|
774
|
-
it('async eager tryCatch', async () => {
|
|
775
|
-
const add = (a, b) => a + b
|
|
776
|
-
|
|
777
|
-
let sum = null
|
|
778
|
-
|
|
779
|
-
await tryCatch(1, 2, 3, async function throwSum(...numbers) {
|
|
780
|
-
const sum = numbers.reduce(add)
|
|
781
|
-
throw new Error(`${sum}`)
|
|
782
|
-
}, async function logErrorMessage(error) {
|
|
783
|
-
sum = Number(error.message)
|
|
784
|
-
})
|
|
785
|
-
|
|
786
|
-
assert.equal(sum, 6)
|
|
787
|
-
})
|
|
788
|
-
|
|
789
|
-
it('tries a sync function and catches with a sync function', async () => {
|
|
790
|
-
const errProp = (err, x) => { err.x = x; return err }
|
|
791
|
-
const throwError = x => { throw new Error(x) }
|
|
792
|
-
ase(tryCatch(x => x + 1, errProp)(1), 2)
|
|
793
|
-
const e1 = tryCatch(throwError, errProp)(1)
|
|
794
|
-
aok(e1 instanceof Error)
|
|
795
|
-
ase(e1.name, 'Error')
|
|
796
|
-
ase(e1.message, '1')
|
|
797
|
-
ase(e1.x, 1)
|
|
798
|
-
})
|
|
799
|
-
|
|
800
|
-
it('tries an async function and catches with a sync function', async () => {
|
|
801
|
-
const errProp = (err, x) => { err.x = x; return err }
|
|
802
|
-
const asyncThrowError = async x => { throw new Error(x) }
|
|
803
|
-
const reject = x => Promise.reject(new Error(x))
|
|
804
|
-
aok(tryCatch(async x => x + 1, errProp)(1) instanceof Promise)
|
|
805
|
-
ase(await tryCatch(async x => x + 1, errProp)(1), 2)
|
|
806
|
-
aok(tryCatch(asyncThrowError, errProp)(1) instanceof Promise)
|
|
807
|
-
const e1 = await tryCatch(asyncThrowError, errProp)(1)
|
|
808
|
-
aok(e1 instanceof Error)
|
|
809
|
-
ase(e1.name, 'Error')
|
|
810
|
-
ase(e1.message, '1')
|
|
811
|
-
ase(e1.x, 1)
|
|
812
|
-
aok(tryCatch(reject, errProp)(1) instanceof Promise)
|
|
813
|
-
const e2 = await tryCatch(reject, errProp)(1)
|
|
814
|
-
aok(e2 instanceof Error)
|
|
815
|
-
ase(e2.name, 'Error')
|
|
816
|
-
ase(e2.message, '1')
|
|
817
|
-
ase(e2.x, 1)
|
|
818
|
-
})
|
|
819
|
-
it('tries a sync function and catches with an async function', async () => {
|
|
820
|
-
const asyncErrProp = async (err, x) => { err.x = x; return err }
|
|
821
|
-
const throwError = x => { throw new Error(x) }
|
|
822
|
-
ase(tryCatch(x => x + 1, asyncErrProp)(1), 2)
|
|
823
|
-
aok(tryCatch(throwError, asyncErrProp)(1) instanceof Promise)
|
|
824
|
-
const e1 = await tryCatch(throwError, asyncErrProp)(1)
|
|
825
|
-
aok(e1 instanceof Error)
|
|
826
|
-
ase(e1.name, 'Error')
|
|
827
|
-
ase(e1.message, '1')
|
|
828
|
-
ase(e1.x, 1)
|
|
829
|
-
})
|
|
830
|
-
it('tries an async function and catches with an async function', async () => {
|
|
831
|
-
const asyncErrProp = async (err, x) => { err.x = x; return err }
|
|
832
|
-
const asyncThrowError = async x => { throw new Error(x) }
|
|
833
|
-
const reject = x => Promise.reject(new Error(x))
|
|
834
|
-
aok(tryCatch(async x => x + 1, asyncErrProp)(1) instanceof Promise)
|
|
835
|
-
ase(await tryCatch(async x => x + 1, asyncErrProp)(1), 2)
|
|
836
|
-
aok(tryCatch(asyncThrowError, asyncErrProp)(1) instanceof Promise)
|
|
837
|
-
const e1 = await tryCatch(asyncThrowError, asyncErrProp)(1)
|
|
838
|
-
aok(e1 instanceof Error)
|
|
839
|
-
ase(e1.name, 'Error')
|
|
840
|
-
ase(e1.message, '1')
|
|
841
|
-
ase(e1.x, 1)
|
|
842
|
-
aok(tryCatch(reject, asyncErrProp)(1) instanceof Promise)
|
|
843
|
-
const e2 = await tryCatch(reject, asyncErrProp)(1)
|
|
844
|
-
aok(e2 instanceof Error)
|
|
845
|
-
ase(e2.name, 'Error')
|
|
846
|
-
ase(e2.message, '1')
|
|
847
|
-
ase(e2.x, 1)
|
|
848
|
-
})
|
|
849
|
-
it('throws a TypeError if passed a non function tryer', async () => {
|
|
850
|
-
assert.deepEqual(
|
|
851
|
-
tryCatch('hey', (...args) => args)('sup'),
|
|
852
|
-
[new TypeError('tryer is not a function'), 'sup'],
|
|
853
|
-
)
|
|
854
|
-
})
|
|
855
|
-
it('won\'t enforce the catcher function typecheck', async () => {
|
|
856
|
-
assert.deepEqual(
|
|
857
|
-
tryCatch(() => 'ayo', Buffer.from('abc'))('hey'),
|
|
858
|
-
'ayo',
|
|
859
|
-
)
|
|
860
|
-
})
|
|
861
|
-
})
|
|
862
|
-
|
|
863
|
-
describe('switchCase', () => {
|
|
864
|
-
it('API coverage', async () => {
|
|
865
|
-
ase(
|
|
866
|
-
switchCase(1, [
|
|
867
|
-
x => x === 1, () => 'hi',
|
|
868
|
-
x => x === 2, () => 'ho',
|
|
869
|
-
() => 'hey',
|
|
870
|
-
]),
|
|
871
|
-
'hi',
|
|
872
|
-
)
|
|
873
|
-
|
|
874
|
-
ase(
|
|
875
|
-
await switchCase(Promise.resolve(1), [
|
|
876
|
-
x => x === 1, () => 'hi',
|
|
877
|
-
x => x === 2, () => 'ho',
|
|
878
|
-
() => 'hey',
|
|
879
|
-
]),
|
|
880
|
-
'hi',
|
|
881
|
-
)
|
|
882
|
-
})
|
|
883
|
-
it('switches on values (including Promises), evaluating eagerly', async () => {
|
|
884
|
-
ase(switchCase([true, 'hey', 'ho']), 'hey')
|
|
885
|
-
ase(await switchCase([
|
|
886
|
-
Promise.resolve(true),
|
|
887
|
-
Promise.resolve('hey'),
|
|
888
|
-
'ho',
|
|
889
|
-
]), 'hey')
|
|
890
|
-
ase(await switchCase([
|
|
891
|
-
true,
|
|
892
|
-
Promise.resolve('hey'),
|
|
893
|
-
'ho',
|
|
894
|
-
]), 'hey')
|
|
895
|
-
ase(await switchCase([
|
|
896
|
-
false,
|
|
897
|
-
Promise.resolve('hey'),
|
|
898
|
-
'ho',
|
|
899
|
-
]), 'ho')
|
|
900
|
-
ase(await switchCase([
|
|
901
|
-
false,
|
|
902
|
-
Promise.resolve('hey'),
|
|
903
|
-
Promise.resolve('ho'),
|
|
904
|
-
]), 'ho')
|
|
905
|
-
ase(switchCase([false, 'hey', false, 'ho', 'default']), 'default')
|
|
906
|
-
ase(switchCase([false, 'hey', false, 'hey', true, 'ho']), 'ho')
|
|
907
|
-
ase(switchCase([false, 'hey', false, 'hey', false, 'ho']), undefined)
|
|
908
|
-
})
|
|
909
|
-
it('defaults with undefined for an even number of cases', async () => {
|
|
910
|
-
ase(switchCase([false, 'hey', false, 'hey', false, 'hey']), undefined)
|
|
911
|
-
})
|
|
912
|
-
it('switches on provided sync functions', async () => {
|
|
913
|
-
ase(
|
|
914
|
-
switchCase([
|
|
915
|
-
x => x === 1, () => 'hi',
|
|
916
|
-
x => x === 2, () => 'ho',
|
|
917
|
-
() => 'hey',
|
|
918
|
-
])(1),
|
|
919
|
-
'hi',
|
|
920
|
-
)
|
|
921
|
-
ase(
|
|
922
|
-
switchCase([
|
|
923
|
-
x => x === 1, () => 'hi',
|
|
924
|
-
x => x === 2, () => 'ho',
|
|
925
|
-
() => 'hey',
|
|
926
|
-
])(2),
|
|
927
|
-
'ho',
|
|
928
|
-
)
|
|
929
|
-
ase(
|
|
930
|
-
switchCase([
|
|
931
|
-
x => x === 1, () => 'hi',
|
|
932
|
-
x => x === 2, () => 'ho',
|
|
933
|
-
() => 'hey',
|
|
934
|
-
])(3),
|
|
935
|
-
'hey',
|
|
936
|
-
)
|
|
937
|
-
ase(
|
|
938
|
-
switchCase([
|
|
939
|
-
x => x === 1, async () => 'hi',
|
|
940
|
-
x => x === 2, async () => 'ho',
|
|
941
|
-
() => 'hey',
|
|
942
|
-
])(3),
|
|
943
|
-
'hey',
|
|
944
|
-
)
|
|
945
|
-
})
|
|
946
|
-
it('switches on provided async functions or values', async () => {
|
|
947
|
-
aok(
|
|
948
|
-
switchCase([
|
|
949
|
-
async x => x === 1, async () => 'hi',
|
|
950
|
-
async x => x === 2, async () => 'ho',
|
|
951
|
-
async () => 'hey',
|
|
952
|
-
])(1) instanceof Promise,
|
|
953
|
-
)
|
|
954
|
-
aok(
|
|
955
|
-
switchCase([
|
|
956
|
-
async x => x === 1, () => 'hi',
|
|
957
|
-
x => x === 2, () => 'ho',
|
|
958
|
-
() => 'hey',
|
|
959
|
-
])(1) instanceof Promise,
|
|
960
|
-
)
|
|
961
|
-
ase(
|
|
962
|
-
await switchCase([
|
|
963
|
-
async x => x === 1, async () => 'hi',
|
|
964
|
-
async x => x === 2, async () => 'ho',
|
|
965
|
-
async () => 'hey',
|
|
966
|
-
])(1),
|
|
967
|
-
'hi',
|
|
968
|
-
)
|
|
969
|
-
ase(
|
|
970
|
-
await switchCase([
|
|
971
|
-
async x => x === 1, 'hi',
|
|
972
|
-
async x => x === 2, async () => 'ho',
|
|
973
|
-
async () => 'hey',
|
|
974
|
-
])(1),
|
|
975
|
-
'hi',
|
|
976
|
-
)
|
|
977
|
-
ase(
|
|
978
|
-
await switchCase([
|
|
979
|
-
async x => x === 1, async () => 'hi',
|
|
980
|
-
async x => x === 2, async () => 'ho',
|
|
981
|
-
async () => 'hey',
|
|
982
|
-
])(2),
|
|
983
|
-
'ho',
|
|
984
|
-
)
|
|
985
|
-
ase(
|
|
986
|
-
await switchCase([
|
|
987
|
-
async x => x === 1, async () => 'hi',
|
|
988
|
-
async x => x === 2, async () => 'ho',
|
|
989
|
-
async () => 'hey',
|
|
990
|
-
])(3),
|
|
991
|
-
'hey',
|
|
992
|
-
)
|
|
993
|
-
})
|
|
994
|
-
it('undefined for empty array', async () => {
|
|
995
|
-
ase(switchCase([]), undefined)
|
|
996
|
-
})
|
|
997
|
-
it('acts as identity for a single case', async () => {
|
|
998
|
-
assert.strictEqual(switchCase([() => false])(), false)
|
|
999
|
-
})
|
|
1000
|
-
it('even number of array items default returns undefined', async () => {
|
|
1001
|
-
assert.strictEqual(switchCase([() => true, () => 'hey'])(), 'hey')
|
|
1002
|
-
assert.strictEqual(switchCase([() => false, () => 'hey'])(), undefined)
|
|
1003
|
-
assert.strictEqual(switchCase([
|
|
1004
|
-
() => false, 'hey',
|
|
1005
|
-
false, 'hey',
|
|
1006
|
-
false, 'hey',
|
|
1007
|
-
])(), undefined)
|
|
1008
|
-
})
|
|
1009
|
-
})
|
|
1010
|
-
|
|
1011
|
-
describe('map', () => {
|
|
1012
|
-
it('API coverage', async () => {
|
|
1013
|
-
const myArray = [1, 2, 3]
|
|
1014
|
-
assert.deepEqual(
|
|
1015
|
-
map(myArray, number => number ** 2),
|
|
1016
|
-
[1, 4, 9]
|
|
1017
|
-
)
|
|
1018
|
-
assert.deepEqual(
|
|
1019
|
-
await map(Promise.resolve(myArray), number => number ** 2),
|
|
1020
|
-
[1, 4, 9]
|
|
1021
|
-
)
|
|
1022
|
-
})
|
|
1023
|
-
|
|
1024
|
-
it('Test', Test('map', map(number => number ** 2))
|
|
1025
|
-
.case(Promise.resolve(1), 1)
|
|
1026
|
-
.case(Promise.resolve(2), 4)
|
|
1027
|
-
.case(Promise.resolve(3), 9)
|
|
1028
|
-
)
|
|
1029
|
-
|
|
1030
|
-
describe('map(func A=>Promise|B)(Array<A>) -> Promise|Array<B>', () => {
|
|
1031
|
-
it('func A=>B; B', async () => {
|
|
1032
|
-
assert.deepEqual(
|
|
1033
|
-
map(number => number ** 2)([1, 2, 3, 4, 5]),
|
|
1034
|
-
[1, 4, 9, 16, 25],
|
|
1035
|
-
)
|
|
1036
|
-
})
|
|
1037
|
-
it('func A=>Promise<B>; Promise<B>', async () => {
|
|
1038
|
-
assert.deepEqual(
|
|
1039
|
-
await map(async number => number ** 2)([1, 2, 3, 4, 5]),
|
|
1040
|
-
[1, 4, 9, 16, 25],
|
|
1041
|
-
)
|
|
1042
|
-
})
|
|
1043
|
-
})
|
|
1044
|
-
|
|
1045
|
-
describe('map(func A=>Promise|B)(Set<A>) -> Promise|Set<B>', () => {
|
|
1046
|
-
it('func A=>B; B', async () => {
|
|
1047
|
-
assert.deepEqual(
|
|
1048
|
-
map(number => number ** 2)(new Set([1, 2, 3, 4, 5])),
|
|
1049
|
-
new Set([1, 4, 9, 16, 25]),
|
|
1050
|
-
)
|
|
1051
|
-
})
|
|
1052
|
-
it('func A=>Promise<B>; Promise<B>', async () => {
|
|
1053
|
-
assert.deepEqual(
|
|
1054
|
-
await map(async number => number ** 2)(new Set([1, 2, 3, 4, 5])),
|
|
1055
|
-
new Set([1, 4, 9, 16, 25]),
|
|
1056
|
-
)
|
|
1057
|
-
})
|
|
1058
|
-
})
|
|
1059
|
-
|
|
1060
|
-
describe('map(func A=>Promise|B)(Map<any, A>) -> Promise|Map<any, B>', () => {
|
|
1061
|
-
it('func A=>B; B', async () => {
|
|
1062
|
-
assert.deepEqual(
|
|
1063
|
-
map(number => number ** 2)(new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])),
|
|
1064
|
-
new Map([['a', 1], ['b', 4], ['c', 9], ['d', 16], ['e', 25]]),
|
|
1065
|
-
)
|
|
1066
|
-
})
|
|
1067
|
-
it('func A=>Promise<B>; Promise<B>', async () => {
|
|
1068
|
-
assert.deepEqual(
|
|
1069
|
-
await map(async number => number ** 2)(new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])),
|
|
1070
|
-
new Map([['a', 1], ['b', 4], ['c', 9], ['d', 16], ['e', 25]]),
|
|
1071
|
-
)
|
|
1072
|
-
})
|
|
1073
|
-
})
|
|
1074
|
-
|
|
1075
|
-
describe('map(func A=>Promise|B)(String<A>) -> Promise|String<B>', () => {
|
|
1076
|
-
it('func A=>B; B', async () => {
|
|
1077
|
-
assert.deepEqual(
|
|
1078
|
-
map(number => number ** 2)('12345'),
|
|
1079
|
-
'1491625',
|
|
1080
|
-
)
|
|
1081
|
-
})
|
|
1082
|
-
it('func A=>Promise<B>; Promise<B>', async () => {
|
|
1083
|
-
assert.deepEqual(
|
|
1084
|
-
await map(async number => number ** 2)('12345'),
|
|
1085
|
-
'1491625',
|
|
1086
|
-
)
|
|
1087
|
-
})
|
|
1088
|
-
})
|
|
1089
|
-
|
|
1090
|
-
describe('map(func A=>Promise|B)(Object<A>) -> Promise|Object<B>', () => {
|
|
1091
|
-
it('func A=>B; B', async () => {
|
|
1092
|
-
assert.deepEqual(
|
|
1093
|
-
map(number => number ** 2)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
|
|
1094
|
-
{ a: 1, b: 4, c: 9, d: 16, e: 25 },
|
|
1095
|
-
)
|
|
1096
|
-
})
|
|
1097
|
-
it('func A=>Promise<B>; Promise<B>', async () => {
|
|
1098
|
-
assert.deepEqual(
|
|
1099
|
-
await map(async number => number ** 2)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
|
|
1100
|
-
{ a: 1, b: 4, c: 9, d: 16, e: 25 },
|
|
1101
|
-
)
|
|
1102
|
-
assert.deepEqual(
|
|
1103
|
-
await map(
|
|
1104
|
-
number => number % 2 == 1 ? Promise.resolve(number ** 2) : number ** 2,
|
|
1105
|
-
)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
|
|
1106
|
-
{ a: 1, b: 4, c: 9, d: 16, e: 25 },
|
|
1107
|
-
)
|
|
1108
|
-
})
|
|
1109
|
-
})
|
|
1110
|
-
|
|
1111
|
-
describe('map(func A=>B)(Iterator<A>) -> Iterator<B>', () => {
|
|
1112
|
-
it('func A=>B; B', async () => {
|
|
1113
|
-
const numbers = function* () { let i = 0; while (++i < 6) yield i }
|
|
1114
|
-
const squaresIterator = map(number => number ** 2)(numbers())
|
|
1115
|
-
assert.strictEqual(`${squaresIterator}`, '[object MappingIterator]')
|
|
1116
|
-
assert.equal(objectToString(squaresIterator), '[object Object]')
|
|
1117
|
-
assert.deepEqual([...squaresIterator], [1, 4, 9, 16, 25])
|
|
1118
|
-
})
|
|
1119
|
-
})
|
|
1120
|
-
|
|
1121
|
-
describe('map(func A=>Promise|B)(AsyncIterator<A>) -> AsyncIterator<B>', () => {
|
|
1122
|
-
it('func A=>B; AsyncIterator<B>', async () => {
|
|
1123
|
-
const asyncNumbers = async function* () { let i = 0; while (++i < 6) yield i }
|
|
1124
|
-
const asyncSquaresGenerator = map(number => number ** 2)(asyncNumbers())
|
|
1125
|
-
assert.equal(objectToString(asyncSquaresGenerator), '[object Object]')
|
|
1126
|
-
const squaresArray = []
|
|
1127
|
-
for await (const number of asyncSquaresGenerator) squaresArray.push(number)
|
|
1128
|
-
assert.deepEqual(squaresArray, [1, 4, 9, 16, 25])
|
|
1129
|
-
})
|
|
1130
|
-
it('func A=>Promise<B>; AsyncIterator<B>', async () => {
|
|
1131
|
-
const asyncNumbers = async function* () { let i = 0; while (++i < 6) yield i }
|
|
1132
|
-
const asyncSquaresGenerator = map(async number => number ** 2)(asyncNumbers())
|
|
1133
|
-
assert.equal(objectToString(asyncSquaresGenerator), '[object Object]')
|
|
1134
|
-
const squaresArray = []
|
|
1135
|
-
for await (const number of asyncSquaresGenerator) squaresArray.push(number)
|
|
1136
|
-
assert.deepEqual(squaresArray, [1, 4, 9, 16, 25])
|
|
1137
|
-
})
|
|
1138
|
-
})
|
|
1139
|
-
|
|
1140
|
-
describe('map(func A=>Promise|B)(Reducer<A>) -> Reducer<B>', () => {
|
|
1141
|
-
it('func A=>B; Reducer<B>', async () => {
|
|
1142
|
-
const add = (a, b) => a + b
|
|
1143
|
-
assert.strictEqual(
|
|
1144
|
-
[1, 2, 3, 4, 5].reduce(Transducer.map(number => number ** 2)(add), 0),
|
|
1145
|
-
55,
|
|
1146
|
-
)
|
|
1147
|
-
})
|
|
1148
|
-
it('func A=>B; async Reducer<A>; Reducer<B>', async () => {
|
|
1149
|
-
const asyncAdd = async (a, b) => a + b
|
|
1150
|
-
const asyncReducer = Transducer.map(number => number ** 2)(asyncAdd)
|
|
1151
|
-
let total = 0
|
|
1152
|
-
for (const number of [1, 2, 3, 4, 5]) {
|
|
1153
|
-
total = await asyncReducer(total, number)
|
|
1154
|
-
}
|
|
1155
|
-
assert.strictEqual(total, 55)
|
|
1156
|
-
})
|
|
1157
|
-
it('func A=>Promise<B>; Reducer<B>', async () => {
|
|
1158
|
-
const add = (a, b) => a + b
|
|
1159
|
-
const asyncReducer = Transducer.map(async number => number ** 2)(add)
|
|
1160
|
-
let total = 0
|
|
1161
|
-
for (const number of [1, 2, 3, 4, 5]) {
|
|
1162
|
-
total = await asyncReducer(total, number)
|
|
1163
|
-
}
|
|
1164
|
-
assert.strictEqual(total, 55)
|
|
1165
|
-
})
|
|
1166
|
-
})
|
|
1167
|
-
|
|
1168
|
-
describe('map(func A=>B)(Mappable<A>) -> Mappable<B>', () => {
|
|
1169
|
-
it('func A=>B; B', async () => {
|
|
1170
|
-
const Mappable = function (value) {
|
|
1171
|
-
this.value = value
|
|
1172
|
-
}
|
|
1173
|
-
Mappable.prototype.map = function (func) {
|
|
1174
|
-
return new Mappable(func(this.value))
|
|
1175
|
-
}
|
|
1176
|
-
assert.deepEqual(
|
|
1177
|
-
map(value => value ** 2)(new Mappable(3)),
|
|
1178
|
-
new Mappable(9),
|
|
1179
|
-
)
|
|
1180
|
-
})
|
|
1181
|
-
})
|
|
1182
|
-
|
|
1183
|
-
describe('map(func A=>Promise|B)(A) -> Promise|B', () => {
|
|
1184
|
-
it('func A=>B; B', async () => {
|
|
1185
|
-
assert.strictEqual(map(value => value)(1), 1)
|
|
1186
|
-
})
|
|
1187
|
-
it('func A=>Promise<B>; Promise<B>', async () => {
|
|
1188
|
-
assert.strictEqual(await map(async value => value)(1), 1)
|
|
1189
|
-
})
|
|
1190
|
-
})
|
|
1191
|
-
|
|
1192
|
-
it('undefined; undefined', async () => {
|
|
1193
|
-
assert.strictEqual(map(value => value)(undefined), undefined)
|
|
1194
|
-
})
|
|
1195
|
-
|
|
1196
|
-
it('null; null', async () => {
|
|
1197
|
-
assert.strictEqual(map(value => value)(null), null)
|
|
1198
|
-
})
|
|
1199
|
-
})
|
|
1200
|
-
|
|
1201
|
-
describe('map.entries', () => {
|
|
1202
|
-
it('is like map but over entries rather than values',
|
|
1203
|
-
Test(map.entries)
|
|
1204
|
-
.case(([key, value]) => [key, value], mappingEntries => {
|
|
1205
|
-
assert.deepEqual(mappingEntries({ a: 1, b: 2, c: 3 }), { a: 1, b: 2, c: 3 })
|
|
1206
|
-
assert.deepEqual(mappingEntries(mapFrom({ a: 1, b: 2, c: 3 })), mapFrom({ a: 1, b: 2, c: 3 }))
|
|
1207
|
-
assert.throws(() => mappingEntries([]), new TypeError('value is not an Object or Map'))
|
|
1208
|
-
assert.throws(() => mappingEntries(null), new TypeError('value is not an Object or Map'))
|
|
1209
|
-
assert.throws(() => mappingEntries(), new TypeError('value is not an Object or Map'))
|
|
1210
|
-
})
|
|
1211
|
-
.case(async ([key, value]) => [key, value], async mappingEntries => {
|
|
1212
|
-
assert.deepEqual(await mappingEntries({ a: 1, b: 2, c: 3 }), { a: 1, b: 2, c: 3 })
|
|
1213
|
-
assert.deepEqual(await mappingEntries(mapFrom({ a: 1, b: 2, c: 3 })), mapFrom({ a: 1, b: 2, c: 3 }))
|
|
1214
|
-
assert.throws(() => mappingEntries([]), new TypeError('value is not an Object or Map'))
|
|
1215
|
-
assert.throws(() => mappingEntries(null), new TypeError('value is not an Object or Map'))
|
|
1216
|
-
assert.throws(() => mappingEntries(), new TypeError('value is not an Object or Map'))
|
|
1217
|
-
})
|
|
1218
|
-
.case(([key, value]) => value % 2 == 1 ? Promise.resolve([key, value]) : [key, value], async mappingEntries => {
|
|
1219
|
-
assert.deepEqual(await mappingEntries({ a: 1, b: 2, c: 3 }), { a: 1, b: 2, c: 3 })
|
|
1220
|
-
assert.deepEqual(await mappingEntries(mapFrom({ a: 1, b: 2, c: 3 })), mapFrom({ a: 1, b: 2, c: 3 }))
|
|
1221
|
-
assert.throws(() => mappingEntries([]), new TypeError('value is not an Object or Map'))
|
|
1222
|
-
assert.throws(() => mappingEntries(null), new TypeError('value is not an Object or Map'))
|
|
1223
|
-
assert.throws(() => mappingEntries(), new TypeError('value is not an Object or Map'))
|
|
1224
|
-
})
|
|
1225
|
-
)
|
|
1226
|
-
})
|
|
1227
|
-
|
|
1228
|
-
describe('map.series', () => {
|
|
1229
|
-
describe('map.series(func A=>Promise|B)(Array<A>) -> Promise|Array<B>', () => {
|
|
1230
|
-
it('func A=>Promise<B>', async () => {
|
|
1231
|
-
const createNumbersPromises = () => [
|
|
1232
|
-
sleep(15).then(() => 1),
|
|
1233
|
-
sleep(10).then(() => 2),
|
|
1234
|
-
sleep(5).then(() => 3),
|
|
1235
|
-
]
|
|
1236
|
-
{
|
|
1237
|
-
const arr = []
|
|
1238
|
-
await map(
|
|
1239
|
-
p => p.then(number => arr.push(number))
|
|
1240
|
-
)(createNumbersPromises())
|
|
1241
|
-
assert.deepEqual(arr, [3, 2, 1])
|
|
1242
|
-
}
|
|
1243
|
-
{
|
|
1244
|
-
const arr = []
|
|
1245
|
-
await map.series(
|
|
1246
|
-
p => p.then(number => arr.push(number))
|
|
1247
|
-
)(createNumbersPromises())
|
|
1248
|
-
assert.deepEqual(arr, [1, 2, 3])
|
|
1249
|
-
}
|
|
1250
|
-
})
|
|
1251
|
-
it('func A=>B', async () => {
|
|
1252
|
-
const square = number => number ** 2
|
|
1253
|
-
assert.deepEqual(
|
|
1254
|
-
map.series(square)([1, 2, 3, 4, 5]),
|
|
1255
|
-
[1, 4, 9, 16, 25],
|
|
1256
|
-
)
|
|
1257
|
-
})
|
|
1258
|
-
it('func A=>B', async () => {
|
|
1259
|
-
const variadicAsyncSquare = number => number % 2 == 1 ? number ** 2 : Promise.resolve(number ** 2)
|
|
1260
|
-
const variadicAsyncSquare2 = number => number % 2 == 0 ? number ** 2 : Promise.resolve(number ** 2)
|
|
1261
|
-
assert.deepEqual(
|
|
1262
|
-
await map.series(variadicAsyncSquare)([1, 2, 3, 4, 5]),
|
|
1263
|
-
[1, 4, 9, 16, 25],
|
|
1264
|
-
)
|
|
1265
|
-
assert.deepEqual(
|
|
1266
|
-
await map.series(variadicAsyncSquare2)([1, 2, 3, 4, 5]),
|
|
1267
|
-
[1, 4, 9, 16, 25],
|
|
1268
|
-
)
|
|
1269
|
-
})
|
|
1270
|
-
})
|
|
1271
|
-
it('syncly maps into array of functions', async () => {
|
|
1272
|
-
const arr = []
|
|
1273
|
-
ade(
|
|
1274
|
-
map.series(x => { arr.push(x); return x })([1, 2, 3]),
|
|
1275
|
-
[1, 2, 3],
|
|
1276
|
-
)
|
|
1277
|
-
ade(arr, [1, 2, 3])
|
|
1278
|
-
})
|
|
1279
|
-
it('throws TypeError for non functions', async () => {
|
|
1280
|
-
assert.throws(
|
|
1281
|
-
() => map.series('hey')([1]),
|
|
1282
|
-
new TypeError('mapper is not a function'),
|
|
1283
|
-
)
|
|
1284
|
-
})
|
|
1285
|
-
it('throws TypeError for non functions', async () => {
|
|
1286
|
-
assert.throws(
|
|
1287
|
-
() => map.series('hey')(),
|
|
1288
|
-
new TypeError('undefined is not an Array'),
|
|
1289
|
-
)
|
|
1290
|
-
})
|
|
1291
|
-
it('throws TypeError for non array input', async () => {
|
|
1292
|
-
assert.throws(
|
|
1293
|
-
() => map.series(() => 1)('hey'),
|
|
1294
|
-
new TypeError('hey is not an Array'),
|
|
1295
|
-
)
|
|
1296
|
-
})
|
|
1297
|
-
})
|
|
1298
|
-
|
|
1299
|
-
describe('map.pool', () => {
|
|
1300
|
-
const asyncSquare = async x => x ** 2
|
|
1301
|
-
const possiblyAsyncSquare = x => x % 2 == 1 ? Promise.resolve(x ** 2) : x ** 2
|
|
1302
|
-
it('maps with asynchronous limit for Arrays', async () => {
|
|
1303
|
-
aok(map.pool(1, asyncSquare)([1, 2, 3, 4, 5]) instanceof Promise)
|
|
1304
|
-
ade(await map.pool(1, asyncSquare)([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25])
|
|
1305
|
-
ade(await map.pool(9, asyncSquare)([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25])
|
|
1306
|
-
ade(await map.pool(100, asyncSquare)([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25])
|
|
1307
|
-
ade(await map.pool(1, asyncSquare)([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25])
|
|
1308
|
-
ade(await map.pool(9, asyncSquare)([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25])
|
|
1309
|
-
})
|
|
1310
|
-
it('alternating Promises', async () => {
|
|
1311
|
-
ade(await map.pool(1, possiblyAsyncSquare)([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25])
|
|
1312
|
-
ade(await map.pool(9, possiblyAsyncSquare)([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25])
|
|
1313
|
-
})
|
|
1314
|
-
it('=> [] for empty array', async () => {
|
|
1315
|
-
assert.deepEqual(map.pool(1, asyncSquare)([]), [])
|
|
1316
|
-
})
|
|
1317
|
-
it('works for arrays of undefined values', async () => {
|
|
1318
|
-
ade(await map.pool(1, x => x)(Array(5)), Array(5).fill(undefined))
|
|
1319
|
-
ade(await map.pool(1, x => x)(Array(5).fill(null)), Array(5).fill(null))
|
|
1320
|
-
})
|
|
1321
|
-
xit('maps with asynchronous limit for Sets', async () => {
|
|
1322
|
-
const numbersSet = new Set([1, 2, 3, 4, 5])
|
|
1323
|
-
const squaresSet = new Set([1, 4, 9, 16, 25])
|
|
1324
|
-
aok(map.pool(1, asyncSquare)(numbersSet) instanceof Promise)
|
|
1325
|
-
ade(await map.pool(1, asyncSquare)(numbersSet), squaresSet)
|
|
1326
|
-
ade(await map.pool(9, asyncSquare)(numbersSet), squaresSet)
|
|
1327
|
-
ade(await map.pool(100, asyncSquare)(numbersSet), squaresSet)
|
|
1328
|
-
ade(await map.pool(1, asyncSquare)(numbersSet), squaresSet)
|
|
1329
|
-
ade(await map.pool(9, asyncSquare)(numbersSet), squaresSet)
|
|
1330
|
-
})
|
|
1331
|
-
xit('maps with asynchronous limit for Maps', async () => {
|
|
1332
|
-
const squareEntry = entry => entry.map(asyncSquare)
|
|
1333
|
-
const asyncSquareEntry = async entry => entry.map(asyncSquare)
|
|
1334
|
-
const numbersMap = new Map([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]])
|
|
1335
|
-
const squaresMap = new Map([[1, 1], [4, 4], [9, 9], [16, 16], [25, 25]])
|
|
1336
|
-
aok(map.pool(1, squareEntry)(numbersMap) instanceof Promise)
|
|
1337
|
-
ade(await map.pool(1, squareEntry)(numbersMap), squaresMap)
|
|
1338
|
-
ade(await map.pool(9, squareEntry)(numbersMap), squaresMap)
|
|
1339
|
-
ade(await map.pool(100, squareEntry)(numbersMap), squaresMap)
|
|
1340
|
-
ade(await map.pool(1, asyncSquareEntry)(numbersMap), squaresMap)
|
|
1341
|
-
ade(await map.pool(9, asyncSquareEntry)(numbersMap), squaresMap)
|
|
1342
|
-
})
|
|
1343
|
-
it('abides by asynchronous limit for arrays and sets', async () => {
|
|
1344
|
-
const numbers = [1, 2, 3, 4, 5, 6]
|
|
1345
|
-
const period = 10
|
|
1346
|
-
let i = 0
|
|
1347
|
-
let maxi = 0
|
|
1348
|
-
const plusSleepMinus = n => (async () => {
|
|
1349
|
-
i += 1
|
|
1350
|
-
maxi = Math.max(maxi, i)
|
|
1351
|
-
})().then(() => sleep(period)).
|
|
1352
|
-
then(() => {
|
|
1353
|
-
i -= 1
|
|
1354
|
-
return n
|
|
1355
|
-
})
|
|
1356
|
-
ade(await map.pool(2, plusSleepMinus)(numbers), numbers)
|
|
1357
|
-
assert.strictEqual(maxi, 2)
|
|
1358
|
-
assert.strictEqual(i, 0)
|
|
1359
|
-
maxi = 0
|
|
1360
|
-
ade(await map.pool(3, plusSleepMinus)([1, 2, 3, 4, 5, 6]), numbers)
|
|
1361
|
-
assert.strictEqual(maxi, 3)
|
|
1362
|
-
assert.strictEqual(i, 0)
|
|
1363
|
-
maxi = 0
|
|
1364
|
-
}).timeout(20000)
|
|
1365
|
-
it('throws TypeError on map.pool(NaN)', async () => {
|
|
1366
|
-
assert.throws(
|
|
1367
|
-
() => map.pool(NaN)(NaN),
|
|
1368
|
-
new TypeError('NaN is not an Array'),
|
|
1369
|
-
)
|
|
1370
|
-
})
|
|
1371
|
-
it('throws TypeError on map.pool(lessThan1)', async () => {
|
|
1372
|
-
assert.throws(
|
|
1373
|
-
() => map.pool(1, () => {})('yo'),
|
|
1374
|
-
new TypeError('yo is not an Array'),
|
|
1375
|
-
)
|
|
1376
|
-
})
|
|
1377
|
-
it('handles sync errors good', async () => {
|
|
1378
|
-
assert.throws(
|
|
1379
|
-
() => map.pool(1, x => { throw new Error(`throwing ${x}`) })(['yo']),
|
|
1380
|
-
new Error('throwing yo')
|
|
1381
|
-
)
|
|
1382
|
-
})
|
|
1383
|
-
it('handles async errors good', async () => {
|
|
1384
|
-
assert.rejects(
|
|
1385
|
-
() => map.pool(1, async x => { throw new Error(`throwing ${x}`) })(['yo']),
|
|
1386
|
-
new Error('throwing yo'),
|
|
1387
|
-
)
|
|
1388
|
-
})
|
|
1389
|
-
})
|
|
1390
|
-
|
|
1391
|
-
describe('filter', () => {
|
|
1392
|
-
it('eager', async () => {
|
|
1393
|
-
const numbers = [1, 2, 3]
|
|
1394
|
-
const odds = filter(numbers, number => number % 2 == 1)
|
|
1395
|
-
assert.deepEqual(odds, [1, 3])
|
|
1396
|
-
})
|
|
1397
|
-
|
|
1398
|
-
describe('filter(predicate T=>Promise|boolean)(Array<T>) -> Promise|Array<T>', () => {
|
|
1399
|
-
it('predicate T=>boolean', async () => {
|
|
1400
|
-
const isOdd = number => number % 2 == 1
|
|
1401
|
-
assert.deepEqual(
|
|
1402
|
-
filter(isOdd)([1, 2, 3, 4, 5]),
|
|
1403
|
-
[1, 3, 5],
|
|
1404
|
-
)
|
|
1405
|
-
})
|
|
1406
|
-
it('predicate T=>Promise<boolean>', async () => {
|
|
1407
|
-
const asyncIsOdd = async number => number % 2 == 1
|
|
1408
|
-
assert.deepEqual(
|
|
1409
|
-
await filter(asyncIsOdd)([1, 2, 3, 4, 5]),
|
|
1410
|
-
[1, 3, 5],
|
|
1411
|
-
)
|
|
1412
|
-
})
|
|
1413
|
-
it('predicate T=>Promise|boolean', async () => {
|
|
1414
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
1415
|
-
assert.deepEqual(
|
|
1416
|
-
await filter(variadicAsyncIsOdd)([1, 2, 3, 4, 5]),
|
|
1417
|
-
[1, 3, 5],
|
|
1418
|
-
)
|
|
1419
|
-
assert.deepEqual(
|
|
1420
|
-
await filter(variadicAsyncIsOdd)([2, 3, 4, 5, 6]),
|
|
1421
|
-
[3, 5],
|
|
1422
|
-
)
|
|
1423
|
-
})
|
|
1424
|
-
})
|
|
1425
|
-
|
|
1426
|
-
describe('filter(predicate T=>Promise|boolean)(Set<T>) -> Promise|Set<T>', () => {
|
|
1427
|
-
it('predicate T=>Promise|boolean', async () => {
|
|
1428
|
-
const isOdd = number => number % 2 == 1
|
|
1429
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
1430
|
-
assert.deepEqual(
|
|
1431
|
-
filter(isOdd)(new Set([1, 2, 3, 4, 5])),
|
|
1432
|
-
new Set([1, 3, 5]),
|
|
1433
|
-
)
|
|
1434
|
-
assert.deepEqual(
|
|
1435
|
-
await filter(async(isOdd))(new Set([1, 2, 3, 4, 5])),
|
|
1436
|
-
new Set([1, 3, 5]),
|
|
1437
|
-
)
|
|
1438
|
-
assert.deepEqual(
|
|
1439
|
-
await filter(variadicAsyncIsOdd)(new Set([1, 2, 3, 4, 5])),
|
|
1440
|
-
new Set([1, 3, 5]),
|
|
1441
|
-
)
|
|
1442
|
-
})
|
|
1443
|
-
})
|
|
1444
|
-
|
|
1445
|
-
describe('filter(predicate T=>Promise|boolean)(Map<any=>T>) -> Promise|Map<any=>T>', () => {
|
|
1446
|
-
it('predicate T=>Promise|boolean', async () => {
|
|
1447
|
-
const isOdd = number => number % 2 == 1
|
|
1448
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
1449
|
-
const numbersMap = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])
|
|
1450
|
-
const oddNumbersMap = new Map([['a', 1], ['c', 3], ['e', 5]])
|
|
1451
|
-
assert.deepEqual(
|
|
1452
|
-
filter(isOdd)(numbersMap),
|
|
1453
|
-
oddNumbersMap,
|
|
1454
|
-
)
|
|
1455
|
-
assert.deepEqual(
|
|
1456
|
-
await filter(async(isOdd))(numbersMap),
|
|
1457
|
-
oddNumbersMap,
|
|
1458
|
-
)
|
|
1459
|
-
assert.deepEqual(
|
|
1460
|
-
await filter(variadicAsyncIsOdd)(numbersMap),
|
|
1461
|
-
oddNumbersMap,
|
|
1462
|
-
)
|
|
1463
|
-
})
|
|
1464
|
-
})
|
|
1465
|
-
|
|
1466
|
-
describe('filter(predicate T=>Promise|boolean)({ filter: function }) -> Promise|any', () => {
|
|
1467
|
-
it('predicate T=>Promise|boolean', async () => {
|
|
1468
|
-
const myFilterable = { filter: () => 'hey' }
|
|
1469
|
-
assert.strictEqual(filter(isOdd)(myFilterable), 'hey')
|
|
1470
|
-
})
|
|
1471
|
-
})
|
|
1472
|
-
|
|
1473
|
-
describe('filter(predicate T=>Promise|boolean)(number) -> number', () => {
|
|
1474
|
-
it('id', async () => {
|
|
1475
|
-
assert.strictEqual(filter(isOdd)(1), 1)
|
|
1476
|
-
})
|
|
1477
|
-
})
|
|
1478
|
-
|
|
1479
|
-
describe('filter(predicate T=>Promise|boolean)(String<T>) -> Promise|String<T>', () => {
|
|
1480
|
-
it('predicate T=>Promise|boolean', async () => {
|
|
1481
|
-
const isOdd = number => Number(number) % 2 == 1
|
|
1482
|
-
const variadicAsyncIsOdd = number => Number(number) % 2 == 1 ? Promise.resolve(true) : false
|
|
1483
|
-
assert.deepEqual(
|
|
1484
|
-
filter(isOdd)('12345'),
|
|
1485
|
-
'135',
|
|
1486
|
-
)
|
|
1487
|
-
assert.deepEqual(
|
|
1488
|
-
await filter(async(isOdd))('12345'),
|
|
1489
|
-
'135',
|
|
1490
|
-
)
|
|
1491
|
-
assert.deepEqual(
|
|
1492
|
-
await filter(variadicAsyncIsOdd)('12345'),
|
|
1493
|
-
'135',
|
|
1494
|
-
)
|
|
1495
|
-
})
|
|
1496
|
-
|
|
1497
|
-
it('predicate T=>Promise|boolean', async () => {
|
|
1498
|
-
const isOdd = number => number % 2 == 1
|
|
1499
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
1500
|
-
const numbersMap = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])
|
|
1501
|
-
const oddNumbersMap = new Map([['a', 1], ['c', 3], ['e', 5]])
|
|
1502
|
-
assert.deepEqual(
|
|
1503
|
-
filter(isOdd)(numbersMap),
|
|
1504
|
-
oddNumbersMap,
|
|
1505
|
-
)
|
|
1506
|
-
assert.deepEqual(
|
|
1507
|
-
await filter(async(isOdd))(numbersMap),
|
|
1508
|
-
oddNumbersMap,
|
|
1509
|
-
)
|
|
1510
|
-
assert.deepEqual(
|
|
1511
|
-
await filter(variadicAsyncIsOdd)(numbersMap),
|
|
1512
|
-
oddNumbersMap,
|
|
1513
|
-
)
|
|
1514
|
-
})
|
|
1515
|
-
})
|
|
1516
|
-
|
|
1517
|
-
describe('filter(predicate T=>Promise|boolean)(Object<T>) -> Promise|Object<T>', () => {
|
|
1518
|
-
it('predicate T=>boolean', async () => {
|
|
1519
|
-
const isOdd = number => number % 2 == 1
|
|
1520
|
-
assert.deepEqual(
|
|
1521
|
-
filter(isOdd)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
|
|
1522
|
-
{ a: 1, c: 3, e: 5 },
|
|
1523
|
-
)
|
|
1524
|
-
})
|
|
1525
|
-
it('predicate T=>Promise<boolean>', async () => {
|
|
1526
|
-
const asyncIsOdd = async number => number % 2 == 1
|
|
1527
|
-
assert.deepEqual(
|
|
1528
|
-
await filter(asyncIsOdd)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
|
|
1529
|
-
{ a: 1, c: 3, e: 5 },
|
|
1530
|
-
)
|
|
1531
|
-
})
|
|
1532
|
-
it('predicate T=>Promise|boolean', async () => {
|
|
1533
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
1534
|
-
assert.deepEqual(
|
|
1535
|
-
await filter(variadicAsyncIsOdd)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
|
|
1536
|
-
{ a: 1, c: 3, e: 5 },
|
|
1537
|
-
)
|
|
1538
|
-
assert.deepEqual(
|
|
1539
|
-
await filter(variadicAsyncIsOdd)({ b: 2, c: 3, d: 4, e: 5, f: 6 }),
|
|
1540
|
-
{ c: 3, e: 5 },
|
|
1541
|
-
)
|
|
1542
|
-
})
|
|
1543
|
-
})
|
|
1544
|
-
describe('filter(predicate T=>boolean)(Iterator<T>) -> Iterator<T>', () => {
|
|
1545
|
-
it('predicate T=>boolean', async () => {
|
|
1546
|
-
const numbers = function* () { let i = 0; while (++i < 6) yield i }
|
|
1547
|
-
const isOdd = number => number % 2 == 1
|
|
1548
|
-
const oddNumbersIterator = filter(isOdd)(numbers())
|
|
1549
|
-
assert.equal(objectToString(oddNumbersIterator), '[object Object]')
|
|
1550
|
-
assert.deepEqual([...oddNumbersIterator], [1, 3, 5])
|
|
1551
|
-
})
|
|
1552
|
-
})
|
|
1553
|
-
describe('filter(predicate T=>Promise|boolean)(AsyncIterator<T>) -> AsyncIterator<T>', () => {
|
|
1554
|
-
const asyncNumbers = async function* () { let i = 0; while (++i < 6) yield i }
|
|
1555
|
-
it('predicate T=>boolean', async () => {
|
|
1556
|
-
const isOdd = number => number % 2 == 1
|
|
1557
|
-
const asyncOddNumbersIterator = filter(isOdd)(asyncNumbers())
|
|
1558
|
-
assert.equal(objectToString(asyncOddNumbersIterator), '[object Object]')
|
|
1559
|
-
const oddNumbersArray = []
|
|
1560
|
-
for await (const number of asyncOddNumbersIterator) oddNumbersArray.push(number)
|
|
1561
|
-
assert.deepEqual(oddNumbersArray, [1, 3, 5])
|
|
1562
|
-
})
|
|
1563
|
-
})
|
|
1564
|
-
describe('filter(predicate T=>Promise|boolean)(Reducer<T>) -> Reducer<T>', () => {
|
|
1565
|
-
const concat = (array, values) => array.concat(values)
|
|
1566
|
-
it('predicate T=>boolean', async () => {
|
|
1567
|
-
const isOdd = number => number % 2 == 1
|
|
1568
|
-
const concatOdds = Transducer.filter(isOdd)(concat)
|
|
1569
|
-
assert.deepEqual([1, 2, 3, 4, 5].reduce(concatOdds, []), [1, 3, 5])
|
|
1570
|
-
})
|
|
1571
|
-
it('predicate T=>Promise<boolean>', async () => {
|
|
1572
|
-
const asyncIsOdd = async number => number % 2 == 1
|
|
1573
|
-
const concatOdds = Transducer.filter(asyncIsOdd)(concat)
|
|
1574
|
-
let oddNumbers = []
|
|
1575
|
-
for (const number of [1, 2, 3, 4, 5]) {
|
|
1576
|
-
oddNumbers = await concatOdds(oddNumbers, number)
|
|
1577
|
-
}
|
|
1578
|
-
assert.deepEqual(oddNumbers, [1, 3, 5])
|
|
1579
|
-
})
|
|
1580
|
-
it('predicate T=>Promise|boolean', async () => {
|
|
1581
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
1582
|
-
const concatOdds = Transducer.filter(variadicAsyncIsOdd)(concat)
|
|
1583
|
-
let oddNumbers = []
|
|
1584
|
-
for (const number of [1, 2, 3, 4, 5]) {
|
|
1585
|
-
oddNumbers = await concatOdds(oddNumbers, number)
|
|
1586
|
-
}
|
|
1587
|
-
assert.deepEqual(oddNumbers, [1, 3, 5])
|
|
1588
|
-
})
|
|
1589
|
-
})
|
|
1590
|
-
describe('filter(predicate T=>boolean)(Filterable<T>) -> Filterable<T>', () => {
|
|
1591
|
-
it('predicate T=>boolean', async () => {
|
|
1592
|
-
const isOdd = number => number % 2 == 1
|
|
1593
|
-
const Filterable = function (value) {
|
|
1594
|
-
this.value = value
|
|
1595
|
-
}
|
|
1596
|
-
Filterable.prototype.filter = function (func) {
|
|
1597
|
-
return new Filterable(this.value.filter(func))
|
|
1598
|
-
}
|
|
1599
|
-
assert.deepEqual(
|
|
1600
|
-
filter(isOdd)(new Filterable([1, 2, 3, 4, 5])),
|
|
1601
|
-
new Filterable([1, 3, 5]),
|
|
1602
|
-
)
|
|
1603
|
-
})
|
|
1604
|
-
})
|
|
1605
|
-
describe('filter(predicate T=>boolean)(null) -> null', () => {
|
|
1606
|
-
it('predicate T=>boolean', async () => {
|
|
1607
|
-
assert.strictEqual(filter(() => true)(null), null)
|
|
1608
|
-
})
|
|
1609
|
-
})
|
|
1610
|
-
describe('filter(predicate T=>boolean)(undefined) -> undefined', () => {
|
|
1611
|
-
it('predicate T=>boolean', async () => {
|
|
1612
|
-
assert.strictEqual(filter(() => true)(undefined), undefined)
|
|
1613
|
-
})
|
|
1614
|
-
})
|
|
1615
|
-
})
|
|
1616
|
-
|
|
1617
|
-
describe('reduce', () => {
|
|
1618
|
-
it('eager', async () => {
|
|
1619
|
-
const numbers = [1, 2, 3, 4, 5]
|
|
1620
|
-
const sum1 = reduce(numbers, (a, b) => a + b)
|
|
1621
|
-
const sum2 = reduce(numbers, (a, b) => a + b, 0)
|
|
1622
|
-
const sum3 = reduce(numbers, (a, b) => a + b, () => 0)
|
|
1623
|
-
const sum4 = await reduce(numbers, (a, b) => a + b, async () => 0)
|
|
1624
|
-
const sum5 = await reduce(numbers, (a, b) => a + b, Promise.resolve(0))
|
|
1625
|
-
assert.equal(sum1, 15)
|
|
1626
|
-
assert.equal(sum2, 15)
|
|
1627
|
-
assert.equal(sum3, 15)
|
|
1628
|
-
assert.equal(sum4, 15)
|
|
1629
|
-
assert.equal(sum5, 15)
|
|
1630
|
-
})
|
|
1631
|
-
|
|
1632
|
-
it('eager Promise arguments', async () => {
|
|
1633
|
-
const numbers = Promise.resolve([1, 2, 3, 4, 5])
|
|
1634
|
-
const sum1 = await reduce(numbers, (a, b) => a + b)
|
|
1635
|
-
const sum2 = await reduce(numbers, (a, b) => a + b, 0)
|
|
1636
|
-
const sum3 = await reduce(numbers, (a, b) => a + b, () => 0)
|
|
1637
|
-
const sum4 = await reduce(numbers, (a, b) => a + b, async () => 0)
|
|
1638
|
-
const sum5 = await reduce(numbers, (a, b) => a + b, Promise.resolve(0))
|
|
1639
|
-
assert.equal(sum1, 15)
|
|
1640
|
-
assert.equal(sum2, 15)
|
|
1641
|
-
assert.equal(sum3, 15)
|
|
1642
|
-
assert.equal(sum4, 15)
|
|
1643
|
-
assert.equal(sum5, 15)
|
|
1644
|
-
})
|
|
1645
|
-
|
|
1646
|
-
describe(`
|
|
1647
|
-
Reducer<T> (any, T)=>Promise|any
|
|
1648
|
-
|
|
1649
|
-
Reducible<T> {
|
|
1650
|
-
reduce: (reducer Reducer<T>, init any)=>(result any),
|
|
1651
|
-
}
|
|
1652
|
-
|
|
1653
|
-
reduce(
|
|
1654
|
-
reducer Reducer,
|
|
1655
|
-
init undefined|function|any,
|
|
1656
|
-
) -> ...any=>function|Promise|any
|
|
1657
|
-
|
|
1658
|
-
reduce(
|
|
1659
|
-
reducer Reducer<T>,
|
|
1660
|
-
init undefined
|
|
1661
|
-
|((collection, ...restArgs)=>Promise|any)
|
|
1662
|
-
|any,
|
|
1663
|
-
)(
|
|
1664
|
-
collection Iterable<T>|Iterator<T>
|
|
1665
|
-
|AsyncIterable<T>|AsyncIterator<T>
|
|
1666
|
-
|Reducible<T>|Object<T>,
|
|
1667
|
-
restArgs ...any,
|
|
1668
|
-
) -> Promise|any
|
|
1669
|
-
|
|
1670
|
-
reduce(
|
|
1671
|
-
reducer Reducer<T>,
|
|
1672
|
-
init undefined
|
|
1673
|
-
|((collectionFactory, ...restArgs)=>Promise|any)
|
|
1674
|
-
|any,
|
|
1675
|
-
)(
|
|
1676
|
-
collectionFactory (...any=>Iterator<T>)
|
|
1677
|
-
|(...any=>AsyncIterator<T>)
|
|
1678
|
-
|Reducer<T>,
|
|
1679
|
-
restArgs ...any,
|
|
1680
|
-
) -> reducingFunction ...any=>Promise|any
|
|
1681
|
-
`, () => {
|
|
1682
|
-
describe('reducer (any, T)=>Promise|any', () => {
|
|
1683
|
-
const add = (a, b) => a + b
|
|
1684
|
-
const asyncAdd = async (a, b) => a + b
|
|
1685
|
-
const variadicAsyncAdd = (a, b) => b % 2 == 1 ? Promise.resolve(a + b) : a + b
|
|
1686
|
-
xit('collection Promise<Array<number>>', async () => {
|
|
1687
|
-
assert.strictEqual(
|
|
1688
|
-
reduce(add, 0)(Promise.resolve([1, 2, 3, 4, 5])), 15)
|
|
1689
|
-
assert.strictEqual(
|
|
1690
|
-
await reduce(asyncAdd, 0)(Promise.resolve([1, 2, 3, 4, 5])), 15)
|
|
1691
|
-
assert.strictEqual(
|
|
1692
|
-
await reduce(variadicAsyncAdd, 0)(Promise.resolve([1, 2, 3, 4, 5])), 15)
|
|
1693
|
-
})
|
|
1694
|
-
it('collection Array<number>', async () => {
|
|
1695
|
-
assert.strictEqual(
|
|
1696
|
-
reduce(add, 0)([1, 2, 3, 4, 5]), 15)
|
|
1697
|
-
assert.strictEqual(
|
|
1698
|
-
await reduce(asyncAdd, 0)([1, 2, 3, 4, 5]), 15)
|
|
1699
|
-
assert.strictEqual(
|
|
1700
|
-
await reduce(variadicAsyncAdd, 0)([1, 2, 3, 4, 5]), 15)
|
|
1701
|
-
})
|
|
1702
|
-
it('collection Array<>', async () => {
|
|
1703
|
-
assert.strictEqual(
|
|
1704
|
-
reduce(add, 0)([]), 0)
|
|
1705
|
-
assert.strictEqual(
|
|
1706
|
-
reduce(asyncAdd, 0)([]), 0)
|
|
1707
|
-
assert.strictEqual(
|
|
1708
|
-
reduce(variadicAsyncAdd, 0)([]), 0)
|
|
1709
|
-
})
|
|
1710
|
-
it('collection { chain: function }', async () => {
|
|
1711
|
-
const Max = function (number) {
|
|
1712
|
-
this.number = number
|
|
1713
|
-
}
|
|
1714
|
-
Max.prototype.chain = function (flatMapper) {
|
|
1715
|
-
const monad = flatMapper(this.number)
|
|
1716
|
-
if (monad.constructor == Max) {
|
|
1717
|
-
return new Max(monad.number > this.number ? monad.number : this.number)
|
|
1718
|
-
}
|
|
1719
|
-
return monad
|
|
1720
|
-
}
|
|
1721
|
-
assert.strictEqual(reduce(add, 0)(new Max(5)), 5)
|
|
1722
|
-
assert.strictEqual(await reduce(asyncAdd, 0)(new Max(5)), 5)
|
|
1723
|
-
assert.strictEqual(await reduce(variadicAsyncAdd, 0)(new Max(5)), 5)
|
|
1724
|
-
})
|
|
1725
|
-
it('collection { chain: function }', async () => {
|
|
1726
|
-
const AsyncMax = function (number) {
|
|
1727
|
-
this.number = number
|
|
1728
|
-
}
|
|
1729
|
-
AsyncMax.prototype.chain = async function (flatMapper) {
|
|
1730
|
-
const monad = await flatMapper(this.number)
|
|
1731
|
-
if (monad.constructor == AsyncMax) {
|
|
1732
|
-
return new AsyncMax(monad.number > this.number ? monad.number : this.number)
|
|
1733
|
-
}
|
|
1734
|
-
return monad
|
|
1735
|
-
}
|
|
1736
|
-
assert.strictEqual(await reduce(add, 0)(new AsyncMax(5)), 5)
|
|
1737
|
-
assert.strictEqual(await reduce(asyncAdd, 0)(new AsyncMax(5)), 5)
|
|
1738
|
-
assert.strictEqual(await reduce(variadicAsyncAdd, 0)(new AsyncMax(5)), 5)
|
|
1739
|
-
})
|
|
1740
|
-
it('collection { flatMap: function }', async () => {
|
|
1741
|
-
const Max = function (number) {
|
|
1742
|
-
this.number = number
|
|
1743
|
-
}
|
|
1744
|
-
Max.prototype.flatMap = function (flatMapper) {
|
|
1745
|
-
const monad = flatMapper(this.number)
|
|
1746
|
-
if (monad.constructor == Max) {
|
|
1747
|
-
return new Max(monad.number > this.number ? monad.number : this.number)
|
|
1748
|
-
}
|
|
1749
|
-
return monad
|
|
1750
|
-
}
|
|
1751
|
-
assert.strictEqual(reduce(add, 0)(new Max(5)), 5)
|
|
1752
|
-
assert.strictEqual(await reduce(asyncAdd, 0)(new Max(5)), 5)
|
|
1753
|
-
assert.strictEqual(await reduce(variadicAsyncAdd, 0)(new Max(5)), 5)
|
|
1754
|
-
})
|
|
1755
|
-
it('collection Generator<number>', async () => {
|
|
1756
|
-
const numbers = function* () { yield 1; yield 2; yield 3; yield 4; yield 5 }
|
|
1757
|
-
assert.strictEqual(
|
|
1758
|
-
reduce(add, 0)(numbers()), 15)
|
|
1759
|
-
assert.strictEqual(
|
|
1760
|
-
await reduce(asyncAdd, 0)(numbers()), 15)
|
|
1761
|
-
assert.strictEqual(
|
|
1762
|
-
await reduce(variadicAsyncAdd, 0)(numbers()), 15)
|
|
1763
|
-
})
|
|
1764
|
-
it('collection Generator<>', async () => {
|
|
1765
|
-
const numbers = function* () {}
|
|
1766
|
-
assert.strictEqual(
|
|
1767
|
-
reduce(add, 0)(numbers()), 0)
|
|
1768
|
-
assert.strictEqual(
|
|
1769
|
-
await reduce(asyncAdd, 0)(numbers()), 0)
|
|
1770
|
-
assert.strictEqual(
|
|
1771
|
-
await reduce(variadicAsyncAdd, 0)(numbers()), 0)
|
|
1772
|
-
})
|
|
1773
|
-
it('collection AsyncGenerator<number>', async () => {
|
|
1774
|
-
const numbers = async function* () { yield 1; yield 2; yield 3; yield 4; yield 5 }
|
|
1775
|
-
assert.strictEqual(
|
|
1776
|
-
await reduce(add, 0)(numbers()), 15)
|
|
1777
|
-
assert.strictEqual(
|
|
1778
|
-
await reduce(asyncAdd, 0)(numbers()), 15)
|
|
1779
|
-
assert.strictEqual(
|
|
1780
|
-
await reduce(variadicAsyncAdd, 0)(numbers()), 15)
|
|
1781
|
-
})
|
|
1782
|
-
it('collection AsyncGenerator<>', async () => {
|
|
1783
|
-
const numbers = async function* () {}
|
|
1784
|
-
assert.equal(typeof reduce(add, 0)(numbers()).then, 'function')
|
|
1785
|
-
assert.strictEqual(
|
|
1786
|
-
await reduce(add, 0)(numbers()), 0)
|
|
1787
|
-
assert.strictEqual(
|
|
1788
|
-
await reduce(asyncAdd, 0)(numbers()), 0)
|
|
1789
|
-
assert.strictEqual(
|
|
1790
|
-
await reduce(variadicAsyncAdd, 0)(numbers()), 0)
|
|
1791
|
-
})
|
|
1792
|
-
it('collection Reducible<number>', async () => {
|
|
1793
|
-
const reducible = {
|
|
1794
|
-
reduce(reducer, init) {
|
|
1795
|
-
return [1, 2, 3, 4, 5].reduce(reducer, init)
|
|
1796
|
-
},
|
|
1797
|
-
}
|
|
1798
|
-
assert.strictEqual(
|
|
1799
|
-
reduce(add, 0)(reducible), 15)
|
|
1800
|
-
})
|
|
1801
|
-
it('collection Object<number>', async () => {
|
|
1802
|
-
const numbers = { a: 1, b: 2, c: 3, d: 4, e: 5 }
|
|
1803
|
-
assert.strictEqual(
|
|
1804
|
-
reduce(add, 0)(numbers), 15)
|
|
1805
|
-
assert.strictEqual(
|
|
1806
|
-
reduce(add, () => 0)(numbers), 15)
|
|
1807
|
-
assert.strictEqual(
|
|
1808
|
-
reduce(add)(numbers), 15)
|
|
1809
|
-
assert.strictEqual(
|
|
1810
|
-
await reduce(asyncAdd, 0)(numbers), 15)
|
|
1811
|
-
assert.strictEqual(
|
|
1812
|
-
await reduce(variadicAsyncAdd, 0)(numbers), 15)
|
|
1813
|
-
})
|
|
1814
|
-
it('collection Object<>', async () => {
|
|
1815
|
-
const numbers = {}
|
|
1816
|
-
assert.strictEqual(
|
|
1817
|
-
reduce(add, 0)(numbers), 0)
|
|
1818
|
-
assert.strictEqual(
|
|
1819
|
-
reduce(add, () => 0)(numbers), 0)
|
|
1820
|
-
assert.strictEqual(
|
|
1821
|
-
await reduce(asyncAdd, 0)(numbers), 0)
|
|
1822
|
-
assert.strictEqual(
|
|
1823
|
-
await reduce(variadicAsyncAdd, 0)(numbers), 0)
|
|
1824
|
-
})
|
|
1825
|
-
it('collection Map<[string, number]>', async () => {
|
|
1826
|
-
const StringNumberMap = function (size) {
|
|
1827
|
-
const result = new Map()
|
|
1828
|
-
let index = -1
|
|
1829
|
-
while (++index < size) {
|
|
1830
|
-
result.set(sha256(String(index)), index)
|
|
1831
|
-
}
|
|
1832
|
-
return result
|
|
1833
|
-
}
|
|
1834
|
-
const stringNumberMap5 = StringNumberMap(5)
|
|
1835
|
-
assert.equal(reduce(add, 0)(stringNumberMap5), 10)
|
|
1836
|
-
assert.equal(reduce(add, () => 0)(stringNumberMap5), 10)
|
|
1837
|
-
assert.equal(await reduce(add, async () => 0)(stringNumberMap5), 10)
|
|
1838
|
-
assert.equal(await reduce(asyncAdd, async () => 0)(stringNumberMap5), 10)
|
|
1839
|
-
assert.equal(reduce(add)(stringNumberMap5), 10)
|
|
1840
|
-
})
|
|
1841
|
-
it('collection atomic', async () => {
|
|
1842
|
-
assert.strictEqual(
|
|
1843
|
-
reduce((a, b) => a + b, 0)(10),
|
|
1844
|
-
10,
|
|
1845
|
-
)
|
|
1846
|
-
assert.strictEqual(
|
|
1847
|
-
reduce((a, b) => a + b)(10)(5),
|
|
1848
|
-
15,
|
|
1849
|
-
)
|
|
1850
|
-
assert.strictEqual(
|
|
1851
|
-
reduce((a, b) => a + b, 0)(10),
|
|
1852
|
-
10,
|
|
1853
|
-
)
|
|
1854
|
-
})
|
|
1855
|
-
it('collection null', async () => {
|
|
1856
|
-
assert.strictEqual(
|
|
1857
|
-
reduce(value => value, 'hey')(null),
|
|
1858
|
-
'hey',
|
|
1859
|
-
)
|
|
1860
|
-
})
|
|
1861
|
-
it('collection undefined', async () => {
|
|
1862
|
-
assert.strictEqual(
|
|
1863
|
-
reduce(value => value, () => 'hey')(undefined),
|
|
1864
|
-
'hey',
|
|
1865
|
-
)
|
|
1866
|
-
assert.strictEqual(
|
|
1867
|
-
await reduce(value => value, async () => 'hey')(undefined),
|
|
1868
|
-
'hey',
|
|
1869
|
-
)
|
|
1870
|
-
assert.strictEqual(
|
|
1871
|
-
reduce(value => value, 'hey')(),
|
|
1872
|
-
'hey',
|
|
1873
|
-
)
|
|
1874
|
-
assert.equal(
|
|
1875
|
-
typeof reduce(value => value, undefined)(undefined),
|
|
1876
|
-
'function'
|
|
1877
|
-
)
|
|
1878
|
-
})
|
|
1879
|
-
})
|
|
1880
|
-
})
|
|
1881
|
-
})
|
|
1882
|
-
|
|
1883
|
-
describe('reduce - v1.5.10 regression', () => {
|
|
1884
|
-
const iterables = [
|
|
1885
|
-
[1, 2, 3, 4, 5],
|
|
1886
|
-
{ a: 1, b: 2, c: 3, d: 4, e: 5 },
|
|
1887
|
-
new Set([1, 2, 3, 4, 5]),
|
|
1888
|
-
{
|
|
1889
|
-
[Symbol.iterator]: function* () {
|
|
1890
|
-
for (let i = 1; i < 6; i++) yield i
|
|
1891
|
-
},
|
|
1892
|
-
},
|
|
1893
|
-
]
|
|
1894
|
-
const makeAsyncIterables = () => [
|
|
1895
|
-
stream.Readable.from([1, 2, 3, 4, 5]),
|
|
1896
|
-
stream.Readable.from((async function* () {
|
|
1897
|
-
for (let i = 1; i < 6; i++) yield i
|
|
1898
|
-
})()),
|
|
1899
|
-
constructReadStream([1, 2, 3, 4, 5]),
|
|
1900
|
-
{
|
|
1901
|
-
[Symbol.asyncIterator]: async function* () {
|
|
1902
|
-
await sleep(1)
|
|
1903
|
-
for (let i = 1; i < 6; i++) yield i
|
|
1904
|
-
},
|
|
1905
|
-
},
|
|
1906
|
-
]
|
|
1907
|
-
it('reduces any iterable with a sync reducer', async () => {
|
|
1908
|
-
for (const x of iterables) {
|
|
1909
|
-
ase(reduce((y, xi) => y + xi)(x), 15)
|
|
1910
|
-
ase(reduce((y, xi) => y + xi, 10)(x), 25)
|
|
1911
|
-
}
|
|
1912
|
-
})
|
|
1913
|
-
it('reduces any iterable with an async reducer', async () => {
|
|
1914
|
-
aok(asyncMult(1, 2) instanceof Promise)
|
|
1915
|
-
for (const x of iterables) {
|
|
1916
|
-
aok(reduce(asyncMult)(x) instanceof Promise)
|
|
1917
|
-
ase(await reduce(asyncMult)(x), 120)
|
|
1918
|
-
ase(await reduce(asyncMult, 10)(x), 1200)
|
|
1919
|
-
}
|
|
1920
|
-
})
|
|
1921
|
-
it('reduces any async iterable with a sync reducer', async () => {
|
|
1922
|
-
for (const x of makeAsyncIterables()) {
|
|
1923
|
-
ase(await reduce((y, xi) => Number(y) + Number(xi))(x), 15)
|
|
1924
|
-
}
|
|
1925
|
-
for (const x of makeAsyncIterables()) {
|
|
1926
|
-
ase(await reduce((y, xi) => Number(y) + Number(xi), 10)(x), 25)
|
|
1927
|
-
}
|
|
1928
|
-
})
|
|
1929
|
-
it('reduces any async iterable with an async reducer', async () => {
|
|
1930
|
-
aok(asyncMult(1, 2) instanceof Promise)
|
|
1931
|
-
for (const x of makeAsyncIterables()) {
|
|
1932
|
-
aok(reduce(asyncMult)(x) instanceof Promise)
|
|
1933
|
-
}
|
|
1934
|
-
for (const x of makeAsyncIterables()) {
|
|
1935
|
-
ase(await reduce(asyncMult)(x), 120)
|
|
1936
|
-
}
|
|
1937
|
-
for (const x of makeAsyncIterables()) {
|
|
1938
|
-
ase(await reduce(asyncMult, 10)(x), 1200)
|
|
1939
|
-
}
|
|
1940
|
-
})
|
|
1941
|
-
it('reduces an iterable with variadic sync/async reducer', async () => {
|
|
1942
|
-
ade(
|
|
1943
|
-
await reduce(
|
|
1944
|
-
(a, b) => b === 1 ? a + b : Promise.resolve(a + b),
|
|
1945
|
-
0,
|
|
1946
|
-
)([1, 2, 3, 4, 5]),
|
|
1947
|
-
15,
|
|
1948
|
-
)
|
|
1949
|
-
})
|
|
1950
|
-
it('initial value can be a function', async () => {
|
|
1951
|
-
ade(
|
|
1952
|
-
reduce((a, b) => a + b, () => 0)([1, 2, 3, 4, 5]),
|
|
1953
|
-
15,
|
|
1954
|
-
)
|
|
1955
|
-
const asyncNumbers = async function*() {
|
|
1956
|
-
for (let i = 1; i <= 5; i++) yield i
|
|
1957
|
-
}
|
|
1958
|
-
ade(
|
|
1959
|
-
await reduce((a, b) => a + b, () => 0)(asyncNumbers()),
|
|
1960
|
-
15,
|
|
1961
|
-
)
|
|
1962
|
-
ade(
|
|
1963
|
-
await reduce((a, b) => a + b, () => 0)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
|
|
1964
|
-
15,
|
|
1965
|
-
)
|
|
1966
|
-
})
|
|
1967
|
-
it('initial value can be an async function', async () => {
|
|
1968
|
-
ade(
|
|
1969
|
-
await reduce((a, b) => a + b, async () => 0)([1, 2, 3, 4, 5]),
|
|
1970
|
-
15,
|
|
1971
|
-
)
|
|
1972
|
-
const asyncNumbers = async function*() {
|
|
1973
|
-
for (let i = 1; i <= 5; i++) yield i
|
|
1974
|
-
}
|
|
1975
|
-
ade(
|
|
1976
|
-
await reduce((a, b) => a + b, async () => 0)(asyncNumbers()),
|
|
1977
|
-
15,
|
|
1978
|
-
)
|
|
1979
|
-
ade(
|
|
1980
|
-
await reduce((a, b) => a + b, async () => 0)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
|
|
1981
|
-
15,
|
|
1982
|
-
)
|
|
1983
|
-
})
|
|
1984
|
-
it('referential initial values are unsafe', async () => {
|
|
1985
|
-
const square = x => x ** 2
|
|
1986
|
-
const unsafeSquareAll = reduce((a, b) => { a.push(square(b)); return a }, [])
|
|
1987
|
-
ade(
|
|
1988
|
-
unsafeSquareAll([1, 2, 3]),
|
|
1989
|
-
[1, 4, 9],
|
|
1990
|
-
)
|
|
1991
|
-
ade(
|
|
1992
|
-
unsafeSquareAll([1, 2, 3]),
|
|
1993
|
-
[1, 4, 9, 1, 4, 9],
|
|
1994
|
-
)
|
|
1995
|
-
const safeSquareAll = reduce((a, b) => { a.push(square(b)); return a }, () => [])
|
|
1996
|
-
ade(
|
|
1997
|
-
safeSquareAll([1, 2, 3]),
|
|
1998
|
-
[1, 4, 9],
|
|
1999
|
-
)
|
|
2000
|
-
ade(
|
|
2001
|
-
safeSquareAll([1, 2, 3]),
|
|
2002
|
-
[1, 4, 9],
|
|
2003
|
-
)
|
|
2004
|
-
})
|
|
2005
|
-
it('=> [] for initial () => [] and input []', async () => {
|
|
2006
|
-
const square = x => x ** 2
|
|
2007
|
-
ade(
|
|
2008
|
-
reduce(map(square)((a, b) => (a.push(b), a)), () => [])([]),
|
|
2009
|
-
[],
|
|
2010
|
-
)
|
|
2011
|
-
const emptyAsyncIterator = (async function*(){})()
|
|
2012
|
-
ade(
|
|
2013
|
-
await reduce(map(square)((a, b) => (a.push(b), a)), () => [])(emptyAsyncIterator),
|
|
2014
|
-
[],
|
|
2015
|
-
)
|
|
2016
|
-
})
|
|
2017
|
-
})
|
|
2018
|
-
|
|
2019
|
-
describe('transform', () => {
|
|
2020
|
-
describe(`
|
|
2021
|
-
transform(
|
|
2022
|
-
transducer function,
|
|
2023
|
-
init function|any,
|
|
2024
|
-
)(...any) -> Promise|any
|
|
2025
|
-
|
|
2026
|
-
Reducer<T> = (any, T)=>Promise|any
|
|
2027
|
-
|
|
2028
|
-
Transducer = Reducer=>Reducer
|
|
2029
|
-
|
|
2030
|
-
Semigroup = Array|string|Set|TypedArray
|
|
2031
|
-
|{ concat: function }|{ write: function }|Object
|
|
2032
|
-
|
|
2033
|
-
transform(
|
|
2034
|
-
transducer Transducer,
|
|
2035
|
-
init ((collection, ...restArgs)=>Promise|Semigroup|any)
|
|
2036
|
-
|Semigroup
|
|
2037
|
-
|any,
|
|
2038
|
-
)(
|
|
2039
|
-
collection Iterable|Iterator
|
|
2040
|
-
|AsyncIterable|AsyncIterator
|
|
2041
|
-
|{ reduce: function }|Object|any,
|
|
2042
|
-
restArgs ...any
|
|
2043
|
-
) -> Semigroup Promise|any
|
|
2044
|
-
|
|
2045
|
-
transform(
|
|
2046
|
-
transducer Transducer,
|
|
2047
|
-
init (...args=>Promise|Semigroup|any)
|
|
2048
|
-
|Semigroup
|
|
2049
|
-
|any,
|
|
2050
|
-
)(
|
|
2051
|
-
generatorFunction (...args=>Generator)|(...args=>AsyncGenerator),
|
|
2052
|
-
) -> reducingFunction (args ...any)=>Promise|Semigroup|any
|
|
2053
|
-
|
|
2054
|
-
transform(
|
|
2055
|
-
transducer Transducer,
|
|
2056
|
-
init (...args=>Promise|Semigroup|any)
|
|
2057
|
-
|Semigroup
|
|
2058
|
-
|any,
|
|
2059
|
-
)(
|
|
2060
|
-
anotherReducer Reducer, moreReducers ...Reducer
|
|
2061
|
-
) -> chainedReducingFunction (args ...any)=>Promise|any
|
|
2062
|
-
`, () => {
|
|
2063
|
-
describe('collection x init', () => {
|
|
2064
|
-
const square = number => number ** 2
|
|
2065
|
-
it('API coverage', async () => {
|
|
2066
|
-
assert.deepEqual(
|
|
2067
|
-
transform([1, 2, 3, 4, 5], Transducer.map(square), []),
|
|
2068
|
-
[1, 4, 9, 16, 25],
|
|
2069
|
-
)
|
|
2070
|
-
assert.deepEqual(
|
|
2071
|
-
transform([1, 2, 3, 4, 5], Transducer.map(square), () => []),
|
|
2072
|
-
[1, 4, 9, 16, 25],
|
|
2073
|
-
)
|
|
2074
|
-
assert.deepEqual(
|
|
2075
|
-
await transform([1, 2, 3, 4, 5], Transducer.map(square), async () => []),
|
|
2076
|
-
[1, 4, 9, 16, 25],
|
|
2077
|
-
)
|
|
2078
|
-
assert.deepEqual(
|
|
2079
|
-
await transform([1, 2, 3, 4, 5], Transducer.map(square), Promise.resolve([])),
|
|
2080
|
-
[1, 4, 9, 16, 25],
|
|
2081
|
-
)
|
|
2082
|
-
assert.deepEqual(
|
|
2083
|
-
await transform(Promise.resolve([1, 2, 3, 4, 5]), Transducer.map(square), []),
|
|
2084
|
-
[1, 4, 9, 16, 25],
|
|
2085
|
-
)
|
|
2086
|
-
})
|
|
2087
|
-
it('init []|() => []', async () => {
|
|
2088
|
-
assert.deepEqual(
|
|
2089
|
-
transform(Transducer.map(square), [])([1, 2, 3, 4, 5]),
|
|
2090
|
-
[1, 4, 9, 16, 25],
|
|
2091
|
-
)
|
|
2092
|
-
assert.deepEqual(
|
|
2093
|
-
await transform(Transducer.map(square), Promise.resolve([]))([1, 2, 3, 4, 5]),
|
|
2094
|
-
[1, 4, 9, 16, 25],
|
|
2095
|
-
)
|
|
2096
|
-
assert.deepEqual(
|
|
2097
|
-
transform(Transducer.map(square), () => [])([1, 2, 3, 4, 5]),
|
|
2098
|
-
[1, 4, 9, 16, 25],
|
|
2099
|
-
)
|
|
2100
|
-
assert.deepEqual(
|
|
2101
|
-
await transform(Transducer.map(square), async () => [])([1, 2, 3, 4, 5]),
|
|
2102
|
-
[1, 4, 9, 16, 25],
|
|
2103
|
-
)
|
|
2104
|
-
assert.deepEqual(
|
|
2105
|
-
transform(Transducer.map(square), [])([1, 2, 3, 4, 5][Symbol.iterator]()),
|
|
2106
|
-
[1, 4, 9, 16, 25],
|
|
2107
|
-
)
|
|
2108
|
-
const duplicate = item => [item, item]
|
|
2109
|
-
assert.deepEqual(
|
|
2110
|
-
transform(Transducer.map(duplicate), [])([1, 2, 3, 4, 5]),
|
|
2111
|
-
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5],
|
|
2112
|
-
)
|
|
2113
|
-
assert.deepEqual(
|
|
2114
|
-
transform(Transducer.map(duplicate), () => [])([1, 2, 3, 4, 5]),
|
|
2115
|
-
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5],
|
|
2116
|
-
)
|
|
2117
|
-
assert.deepEqual(
|
|
2118
|
-
await transform(Transducer.map(async number => duplicate(number)), async () => [])([1, 2, 3, 4, 5]),
|
|
2119
|
-
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5],
|
|
2120
|
-
)
|
|
2121
|
-
})
|
|
2122
|
-
it('init \'\'|(()=>\'\')', async () => {
|
|
2123
|
-
assert.strictEqual(
|
|
2124
|
-
transform(Transducer.map(square), '')([1, 2, 3, 4, 5]),
|
|
2125
|
-
'1491625',
|
|
2126
|
-
)
|
|
2127
|
-
assert.strictEqual(
|
|
2128
|
-
transform(Transducer.map(square), () => '')([1, 2, 3, 4, 5]),
|
|
2129
|
-
'1491625',
|
|
2130
|
-
)
|
|
2131
|
-
assert.strictEqual(
|
|
2132
|
-
await transform(Transducer.map(async number => number ** 2), async () => '')([1, 2, 3, 4, 5]),
|
|
2133
|
-
'1491625',
|
|
2134
|
-
)
|
|
2135
|
-
})
|
|
2136
|
-
it('init Set|()=>Set', async () => {
|
|
2137
|
-
assert.deepEqual(
|
|
2138
|
-
transform(Transducer.map(square), new Set())([1, 2, 3, 4, 5]),
|
|
2139
|
-
new Set([1, 4, 9, 16, 25]),
|
|
2140
|
-
)
|
|
2141
|
-
assert.deepEqual(
|
|
2142
|
-
transform(Transducer.map(square), () => new Set())([1, 2, 3, 4, 5]),
|
|
2143
|
-
new Set([1, 4, 9, 16, 25]),
|
|
2144
|
-
)
|
|
2145
|
-
assert.deepEqual(
|
|
2146
|
-
await transform(Transducer.map(square), async () => new Set())([1, 2, 3, 4, 5]),
|
|
2147
|
-
new Set([1, 4, 9, 16, 25]),
|
|
2148
|
-
)
|
|
2149
|
-
const SetWithSquare = number => new Set([number, number ** 2])
|
|
2150
|
-
assert.deepEqual(
|
|
2151
|
-
transform(Transducer.map(SetWithSquare), new Set())([1, 2, 3, 4, 5]),
|
|
2152
|
-
new Set([1, 2, 4, 3, 9, 4, 16, 5, 25]),
|
|
2153
|
-
)
|
|
2154
|
-
assert.deepEqual(
|
|
2155
|
-
transform(Transducer.map(SetWithSquare), () => new Set())([1, 2, 3, 4, 5]),
|
|
2156
|
-
new Set([1, 2, 4, 3, 9, 4, 16, 5, 25]),
|
|
2157
|
-
)
|
|
2158
|
-
assert.deepEqual(
|
|
2159
|
-
await transform(Transducer.map(async number => SetWithSquare(number)), async () => new Set())([1, 2, 3, 4, 5]),
|
|
2160
|
-
new Set([1, 2, 4, 3, 9, 4, 16, 5, 25]),
|
|
2161
|
-
)
|
|
2162
|
-
})
|
|
2163
|
-
it('init TypedArray|()=>TypedArray', async () => {
|
|
2164
|
-
for (const constructor of numberTypedArrayConstructors) {
|
|
2165
|
-
assert.deepEqual(
|
|
2166
|
-
transform(Transducer.map(square), new constructor())([1, 2, 3, 4, 5]),
|
|
2167
|
-
new constructor([1, 4, 9, 16, 25])
|
|
2168
|
-
)
|
|
2169
|
-
assert.deepEqual(
|
|
2170
|
-
transform(Transducer.map(square), () => new constructor())([1, 2, 3, 4, 5]),
|
|
2171
|
-
new constructor([1, 4, 9, 16, 25])
|
|
2172
|
-
)
|
|
2173
|
-
assert.deepEqual(
|
|
2174
|
-
await transform(Transducer.map(async number => number ** 2), async () => new constructor())([1, 2, 3, 4, 5]),
|
|
2175
|
-
new constructor([1, 4, 9, 16, 25])
|
|
2176
|
-
)
|
|
2177
|
-
assert.deepEqual(
|
|
2178
|
-
transform(Transducer.map(number => new constructor([number ** 2])), () => new constructor())([1, 2, 3, 4, 5]),
|
|
2179
|
-
new constructor([1, 4, 9, 16, 25])
|
|
2180
|
-
)
|
|
2181
|
-
assert.deepEqual(
|
|
2182
|
-
await transform(Transducer.map(async number => new constructor([number ** 2])), async () => new constructor())([1, 2, 3, 4, 5]),
|
|
2183
|
-
new constructor([1, 4, 9, 16, 25])
|
|
2184
|
-
)
|
|
2185
|
-
}
|
|
2186
|
-
for (const constructor of bigIntTypedArrayConstructors) {
|
|
2187
|
-
assert.deepEqual(
|
|
2188
|
-
transform(Transducer.map(number => number ** 2n), new constructor())([1n, 2n, 3n, 4n, 5n]),
|
|
2189
|
-
new constructor([1n, 4n, 9n, 16n, 25n])
|
|
2190
|
-
)
|
|
2191
|
-
assert.deepEqual(
|
|
2192
|
-
transform(Transducer.map(number => number ** 2n), () => new constructor())([1n, 2n, 3n, 4n, 5n]),
|
|
2193
|
-
new constructor([1n, 4n, 9n, 16n, 25n])
|
|
2194
|
-
)
|
|
2195
|
-
assert.deepEqual(
|
|
2196
|
-
await transform(Transducer.map(async number => number ** 2n), async () => new constructor())([1n, 2n, 3n, 4n, 5n]),
|
|
2197
|
-
new constructor([1n, 4n, 9n, 16n, 25n])
|
|
2198
|
-
)
|
|
2199
|
-
assert.deepEqual(
|
|
2200
|
-
transform(Transducer.map(number => new constructor([number ** 2n])), () => new constructor())([1n, 2n, 3n, 4n, 5n]),
|
|
2201
|
-
new constructor([1n, 4n, 9n, 16n, 25n])
|
|
2202
|
-
)
|
|
2203
|
-
assert.deepEqual(
|
|
2204
|
-
await transform(Transducer.map(async number => new constructor([number ** 2n])), async () => new constructor())([1n, 2n, 3n, 4n, 5n]),
|
|
2205
|
-
new constructor([1n, 4n, 9n, 16n, 25n])
|
|
2206
|
-
)
|
|
2207
|
-
}
|
|
2208
|
-
})
|
|
2209
|
-
it('init { concat: function }|()=>({ concat: function })', async () => {
|
|
2210
|
-
const Max = function (number) {
|
|
2211
|
-
this.number = number
|
|
2212
|
-
}
|
|
2213
|
-
Max.prototype.concat = function (otherMax) {
|
|
2214
|
-
return new Max(otherMax.constructor == Max
|
|
2215
|
-
? Math.max(this.number, otherMax.number)
|
|
2216
|
-
: Math.max(this.number, otherMax))
|
|
2217
|
-
}
|
|
2218
|
-
assert.deepEqual(
|
|
2219
|
-
transform(Transducer.map(number => number ** 2), new Max(-Infinity))([1, 2, 3, 4, 5]),
|
|
2220
|
-
new Max(25),
|
|
2221
|
-
)
|
|
2222
|
-
assert.deepEqual(
|
|
2223
|
-
transform(Transducer.map(number => number ** 2), () => new Max(-Infinity))([1, 2, 3, 4, 5]),
|
|
2224
|
-
new Max(25),
|
|
2225
|
-
)
|
|
2226
|
-
assert.deepEqual(
|
|
2227
|
-
await transform(Transducer.map(async number => number ** 2), async () => new Max(-Infinity))([1, 2, 3, 4, 5]),
|
|
2228
|
-
new Max(25),
|
|
2229
|
-
)
|
|
2230
|
-
})
|
|
2231
|
-
it('init { write: function }|()=>({ write: function })', async () => {
|
|
2232
|
-
assert.deepEqual(
|
|
2233
|
-
transform(Transducer.map(number => number ** 2), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2234
|
-
new MockWritable([1, 4, 9, 16, 25]),
|
|
2235
|
-
)
|
|
2236
|
-
assert.deepEqual(
|
|
2237
|
-
transform(Transducer.map(number => number ** 2), () => new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2238
|
-
new MockWritable([1, 4, 9, 16, 25]),
|
|
2239
|
-
)
|
|
2240
|
-
assert.deepEqual(
|
|
2241
|
-
await transform(Transducer.map(async number => number ** 2), async () => new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2242
|
-
new MockWritable([1, 4, 9, 16, 25]),
|
|
2243
|
-
)
|
|
2244
|
-
const numberToSquaredReadable = number => binaryToReadableStream(Buffer.from([number ** 2]))
|
|
2245
|
-
assert.deepEqual(
|
|
2246
|
-
await transform(Transducer.map(numberToSquaredReadable), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2247
|
-
new MockWritable([1, 4, 9, 16, 25].map(number => new Uint8Array([number]))),
|
|
2248
|
-
)
|
|
2249
|
-
const numberToNumberObjectReadable = number => arrayToObjectReadableStream([{ number }])
|
|
2250
|
-
assert.deepEqual(
|
|
2251
|
-
await transform(Transducer.map(numberToNumberObjectReadable), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2252
|
-
new MockWritable([
|
|
2253
|
-
{ number: 1 },
|
|
2254
|
-
{ number: 2 },
|
|
2255
|
-
{ number: 3 },
|
|
2256
|
-
{ number: 4 },
|
|
2257
|
-
{ number: 5 },
|
|
2258
|
-
]),
|
|
2259
|
-
)
|
|
2260
|
-
const numberToDuplicateString = number => `${number}${number}`
|
|
2261
|
-
assert.deepEqual(
|
|
2262
|
-
await transform(Transducer.map(numberToDuplicateString), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2263
|
-
new MockWritable(['11', '22', '33', '44', '55']),
|
|
2264
|
-
)
|
|
2265
|
-
const numberToDuplicateUint8Array = number => new Uint8Array([number, number])
|
|
2266
|
-
assert.deepEqual(
|
|
2267
|
-
transform(Transducer.map(numberToDuplicateUint8Array), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2268
|
-
new MockWritable([
|
|
2269
|
-
new Uint8Array([1, 1]),
|
|
2270
|
-
new Uint8Array([2, 2]),
|
|
2271
|
-
new Uint8Array([3, 3]),
|
|
2272
|
-
new Uint8Array([4, 4]),
|
|
2273
|
-
new Uint8Array([5, 5]),
|
|
2274
|
-
]),
|
|
2275
|
-
)
|
|
2276
|
-
const numberToObjectNumber = number => ({ number })
|
|
2277
|
-
assert.deepEqual(
|
|
2278
|
-
transform(Transducer.map(numberToObjectNumber), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2279
|
-
new MockWritable([
|
|
2280
|
-
{ number: 1 },
|
|
2281
|
-
{ number: 2 },
|
|
2282
|
-
{ number: 3 },
|
|
2283
|
-
{ number: 4 },
|
|
2284
|
-
{ number: 5 },
|
|
2285
|
-
]),
|
|
2286
|
-
)
|
|
2287
|
-
const toNull = () => null
|
|
2288
|
-
assert.deepEqual(
|
|
2289
|
-
transform(Transducer.map(toNull), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2290
|
-
new MockWritable([null, null, null, null, null]),
|
|
2291
|
-
)
|
|
2292
|
-
const getEmptyReadable = () => binaryToReadableStream(Buffer.from([]))
|
|
2293
|
-
assert.deepEqual(
|
|
2294
|
-
await transform(Transducer.map(getEmptyReadable), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2295
|
-
new MockWritable([]),
|
|
2296
|
-
)
|
|
2297
|
-
assert.throws(
|
|
2298
|
-
() => transform(Transducer.map(function () {
|
|
2299
|
-
throw new Error('hey')
|
|
2300
|
-
}), new MockWritable([]))([1, 2, 3, 4, 5]),
|
|
2301
|
-
new Error('hey'),
|
|
2302
|
-
)
|
|
2303
|
-
})
|
|
2304
|
-
it('init 3', async () => {
|
|
2305
|
-
assert.strictEqual(
|
|
2306
|
-
transform(Transducer.map(square), 3)([1, 2, 3, 4, 5]),
|
|
2307
|
-
3,
|
|
2308
|
-
)
|
|
2309
|
-
})
|
|
2310
|
-
it('init null|undefined', async () => {
|
|
2311
|
-
assert.strictEqual(
|
|
2312
|
-
transform(Transducer.map(square), null)([1, 2, 3, 4, 5]),
|
|
2313
|
-
null,
|
|
2314
|
-
)
|
|
2315
|
-
assert.strictEqual(
|
|
2316
|
-
transform(Transducer.map(square), undefined)([1, 2, 3, 4, 5]),
|
|
2317
|
-
undefined,
|
|
2318
|
-
)
|
|
2319
|
-
assert.strictEqual(
|
|
2320
|
-
transform(Transducer.map(square))([1, 2, 3, 4, 5]),
|
|
2321
|
-
undefined,
|
|
2322
|
-
)
|
|
2323
|
-
})
|
|
2324
|
-
})
|
|
2325
|
-
})
|
|
2326
|
-
})
|
|
2327
|
-
|
|
2328
|
-
describe('transform - misc', () => {
|
|
2329
|
-
const squareOdds = compose([Transducer.filter(isOdd), Transducer.map(square)])
|
|
2330
|
-
const asyncEvens = Transducer.filter(asyncIsEven)
|
|
2331
|
-
const bigNumbers = [1n, 2n, 3n, 4n, 5n]
|
|
2332
|
-
const squareOddsToString = compose([
|
|
2333
|
-
Transducer.filter(isOdd),
|
|
2334
|
-
Transducer.map(pipe([square, x => `${x}`])),
|
|
2335
|
-
])
|
|
2336
|
-
it('sync transforms iterable to null', async () => {
|
|
2337
|
-
let y = ''
|
|
2338
|
-
ase(transform(Transducer.map(tap(x => { y += x })), null)([1, 2, 3, 4, 5]), null)
|
|
2339
|
-
ase(y, '12345')
|
|
2340
|
-
})
|
|
2341
|
-
it('async transforms iterable to null', async () => {
|
|
2342
|
-
let y = ''
|
|
2343
|
-
aok(transform(Transducer.map(tap(async () => {})), null)([1, 2, 3, 4, 5]) instanceof Promise)
|
|
2344
|
-
ase(await transform(Transducer.map(tap(async x => { y += x })), null)([1, 2, 3, 4, 5]), null)
|
|
2345
|
-
ase(y, '12345')
|
|
2346
|
-
})
|
|
2347
|
-
it('sync transforms iterable to array', async () => {
|
|
2348
|
-
ade(transform(squareOdds, [])([1, 2, 3, 4, 5]), [1, 9, 25])
|
|
2349
|
-
})
|
|
2350
|
-
it('async transforms iterable to array', async () => {
|
|
2351
|
-
aok(transform(asyncEvens, [99])([1, 2, 3, 4, 5]) instanceof Promise)
|
|
2352
|
-
ade(await transform(asyncEvens, [99])([1, 2, 3, 4, 5]), [99, 2, 4])
|
|
2353
|
-
})
|
|
2354
|
-
it('sync transforms iterable to string', async () => {
|
|
2355
|
-
ase(transform(squareOdds, '')([1, 2, 3, 4, 5]), '1925')
|
|
2356
|
-
})
|
|
2357
|
-
it('async transforms iterable to string', async () => {
|
|
2358
|
-
aok(transform(asyncEvens, '99')([1, 2, 3, 4, 5]) instanceof Promise)
|
|
2359
|
-
ase(await transform(asyncEvens, '99')([1, 2, 3, 4, 5]), '9924')
|
|
2360
|
-
})
|
|
2361
|
-
it('sync transforms iterable to set', async () => {
|
|
2362
|
-
ade(transform(squareOdds, new Set())([1, 2, 3, 4, 5]), new Set([1, 9, 25]))
|
|
2363
|
-
})
|
|
2364
|
-
it('async transforms iterable to set', async () => {
|
|
2365
|
-
aok(transform(asyncEvens, new Set([99]))([1, 2, 3, 4, 5]) instanceof Promise)
|
|
2366
|
-
ade(
|
|
2367
|
-
await transform(asyncEvens, new Set([99, 2]))([1, 2, 3, 4, 5]),
|
|
2368
|
-
new Set([99, 2, 4]),
|
|
2369
|
-
)
|
|
2370
|
-
})
|
|
2371
|
-
it('strings are converted to numbers', async () => {
|
|
2372
|
-
for (const constructor of numberTypedArrayConstructors) {
|
|
2373
|
-
ade(
|
|
2374
|
-
transform(squareOddsToString, new constructor(0))([1, 2, 3, 4, 5]),
|
|
2375
|
-
new constructor([1, 9, 25]),
|
|
2376
|
-
)
|
|
2377
|
-
}
|
|
2378
|
-
ade(
|
|
2379
|
-
transform(squareOddsToString, Buffer.alloc(0))([1, 2, 3, 4, 5]),
|
|
2380
|
-
Buffer.from([1, 9, 25]),
|
|
2381
|
-
)
|
|
2382
|
-
})
|
|
2383
|
-
it('coerces booleans to 0 and 1', async () => {
|
|
2384
|
-
for (const constructor of numberTypedArrayConstructors) {
|
|
2385
|
-
assert.deepEqual(
|
|
2386
|
-
transform(Transducer.map(x => x), new constructor(0))([true, false, false]),
|
|
2387
|
-
new constructor([1, 0, 0])
|
|
2388
|
-
)
|
|
2389
|
-
}
|
|
2390
|
-
for (const constructor of bigIntTypedArrayConstructors) {
|
|
2391
|
-
assert.deepEqual(
|
|
2392
|
-
transform(Transducer.map(x => x), new constructor(0))([true, false, false]),
|
|
2393
|
-
new constructor([1n, 0n, 0n])
|
|
2394
|
-
)
|
|
2395
|
-
}
|
|
2396
|
-
})
|
|
2397
|
-
it('async transforms iterable to number TypedArray', async () => {
|
|
2398
|
-
for (const constructor of numberTypedArrayConstructors) {
|
|
2399
|
-
const buffer99 = new constructor([9, 9])
|
|
2400
|
-
const buffer9924 = transform(asyncEvens, buffer99)([1, 2, 3, 4, 5])
|
|
2401
|
-
aok(buffer9924 instanceof Promise)
|
|
2402
|
-
ade(await buffer9924, new constructor([9, 9, 2, 4]))
|
|
2403
|
-
}
|
|
2404
|
-
})
|
|
2405
|
-
it('sync transforms iterable to a bigint TypedArray', async () => {
|
|
2406
|
-
const isBigOdd = x => (x % 2n === 1n)
|
|
2407
|
-
const bigSquare = x => x ** 2n
|
|
2408
|
-
const squareBigOdds = compose([Transducer.filter(isBigOdd), Transducer.map(bigSquare)])
|
|
2409
|
-
for (const constructor of bigIntTypedArrayConstructors) {
|
|
2410
|
-
ade(
|
|
2411
|
-
transform(squareBigOdds, new constructor(0))(bigNumbers),
|
|
2412
|
-
new constructor([1n, 9n, 25n]),
|
|
2413
|
-
)
|
|
2414
|
-
}
|
|
2415
|
-
})
|
|
2416
|
-
it('async transforms iterable to a bigint TypedArray', async () => {
|
|
2417
|
-
const asyncIsBigEven = async x => (x % 2n === 0n)
|
|
2418
|
-
const asyncBigEvens = Transducer.filter(asyncIsBigEven)
|
|
2419
|
-
for (const constructor of bigIntTypedArrayConstructors) {
|
|
2420
|
-
const buffer99 = new constructor([9n, 9n])
|
|
2421
|
-
const buffer9924 = transform(asyncBigEvens, buffer99)(bigNumbers)
|
|
2422
|
-
aok(buffer9924 instanceof Promise)
|
|
2423
|
-
ade(await buffer9924, new constructor([9n, 9n, 2n, 4n]))
|
|
2424
|
-
}
|
|
2425
|
-
})
|
|
2426
|
-
it('sync transforms iterable to writeable stream', async () => {
|
|
2427
|
-
const tmpWriter = fs.createWriteStream(path.join(__dirname, './tmp'))
|
|
2428
|
-
await transform(squareOddsToString, tmpWriter)([1, 2, 3, 4, 5])
|
|
2429
|
-
ase(await consumeReadStreamPush(
|
|
2430
|
-
fs.createReadStream(path.join(__dirname, './tmp')),
|
|
2431
|
-
), '1925')
|
|
2432
|
-
ase(await consumeReadStreamPull(
|
|
2433
|
-
fs.createReadStream(path.join(__dirname, './tmp')),
|
|
2434
|
-
), '1925')
|
|
2435
|
-
await fs.promises.unlink('./tmp')
|
|
2436
|
-
})
|
|
2437
|
-
it('async transforms iterable to writeable stream', async () => {
|
|
2438
|
-
const asyncEvensToString = compose([
|
|
2439
|
-
Transducer.filter(asyncIsEven),
|
|
2440
|
-
Transducer.map(x => `${x}`)
|
|
2441
|
-
])
|
|
2442
|
-
const tmpWriter = fs.createWriteStream(path.join(__dirname, './tmp'))
|
|
2443
|
-
tmpWriter.write('99')
|
|
2444
|
-
const writeEvens = transform(asyncEvensToString, tmpWriter)([1, 2, 3, 4, 5])
|
|
2445
|
-
aok(writeEvens instanceof Promise)
|
|
2446
|
-
await writeEvens
|
|
2447
|
-
ase(await consumeReadStreamPush(
|
|
2448
|
-
fs.createReadStream(path.join(__dirname, './tmp')),
|
|
2449
|
-
), '9924')
|
|
2450
|
-
ase(await consumeReadStreamPull(
|
|
2451
|
-
fs.createReadStream(path.join(__dirname, './tmp')),
|
|
2452
|
-
), '9924')
|
|
2453
|
-
await fs.promises.unlink('./tmp')
|
|
2454
|
-
})
|
|
2455
|
-
it('sync transforms an iterable to an object', async () => {
|
|
2456
|
-
ade(
|
|
2457
|
-
transform(Transducer.map(n => ({ [n]: n })), {})([1, 2, 3, 4, 5]),
|
|
2458
|
-
{ '1': 1, '2': 2, '3': 3, '4': 4, '5': 5 },
|
|
2459
|
-
)
|
|
2460
|
-
})
|
|
2461
|
-
it('async transforms an iterable to an object', async () => {
|
|
2462
|
-
aok(
|
|
2463
|
-
transform(Transducer.map(async n => [n, n]), {})([1, 2, 3, 4, 5]) instanceof Promise,
|
|
2464
|
-
)
|
|
2465
|
-
aok(
|
|
2466
|
-
transform(Transducer.map(async n => ({ [n]: n })), {})([1, 2, 3, 4, 5]) instanceof Promise,
|
|
2467
|
-
)
|
|
2468
|
-
ade(
|
|
2469
|
-
await transform(Transducer.map(async n => ({ [n]: n })), {})([1, 2, 3, 4, 5]),
|
|
2470
|
-
{ '1': 1, '2': 2, '3': 3, '4': 4, '5': 5 },
|
|
2471
|
-
)
|
|
2472
|
-
})
|
|
2473
|
-
it('initial value can be a function', async () => {
|
|
2474
|
-
const square = x => x ** 2
|
|
2475
|
-
ade(
|
|
2476
|
-
transform(Transducer.map(square), () => [])([1, 2, 3]),
|
|
2477
|
-
[1, 4, 9],
|
|
2478
|
-
)
|
|
2479
|
-
})
|
|
2480
|
-
it('initial value can be an async function', async () => {
|
|
2481
|
-
const square = x => x ** 2
|
|
2482
|
-
aok(
|
|
2483
|
-
transform(Transducer.map(square), async () => [])([1, 2, 3]) instanceof Promise,
|
|
2484
|
-
)
|
|
2485
|
-
ade(
|
|
2486
|
-
await transform(Transducer.map(square), async () => [])([1, 2, 3]),
|
|
2487
|
-
[1, 4, 9],
|
|
2488
|
-
)
|
|
2489
|
-
})
|
|
2490
|
-
it('nonfunction initial values can be unsafe', async () => {
|
|
2491
|
-
const square = x => x ** 2
|
|
2492
|
-
const unsafeSquareAllTransform = transform(Transducer.map(square), [])
|
|
2493
|
-
ade(
|
|
2494
|
-
unsafeSquareAllTransform([1, 2, 3]),
|
|
2495
|
-
[1, 4, 9],
|
|
2496
|
-
)
|
|
2497
|
-
ade(
|
|
2498
|
-
unsafeSquareAllTransform([1, 2, 3]),
|
|
2499
|
-
[1, 4, 9, 1, 4, 9],
|
|
2500
|
-
)
|
|
2501
|
-
const safeSquareAllTransform = transform(Transducer.map(square), () => [])
|
|
2502
|
-
ade(
|
|
2503
|
-
safeSquareAllTransform([1, 2, 3]),
|
|
2504
|
-
[1, 4, 9],
|
|
2505
|
-
)
|
|
2506
|
-
ade(
|
|
2507
|
-
safeSquareAllTransform([1, 2, 3]),
|
|
2508
|
-
[1, 4, 9],
|
|
2509
|
-
)
|
|
2510
|
-
})
|
|
2511
|
-
it('=> [] for initial () => [] and input []', async () => {
|
|
2512
|
-
const square = x => x ** 2
|
|
2513
|
-
ade(
|
|
2514
|
-
transform(Transducer.map(square), () => [])([]),
|
|
2515
|
-
[],
|
|
2516
|
-
)
|
|
2517
|
-
const emptyAsyncIterator = (async function*(){})()
|
|
2518
|
-
ade(
|
|
2519
|
-
await transform(Transducer.map(square), () => [])(emptyAsyncIterator),
|
|
2520
|
-
[],
|
|
2521
|
-
)
|
|
2522
|
-
})
|
|
2523
|
-
})
|
|
2524
|
-
|
|
2525
|
-
describe('flatMap', () => {
|
|
2526
|
-
describe(`
|
|
2527
|
-
type FlatMappable = Array|String|Set|Iterator|AsyncIterator
|
|
2528
|
-
type Iterable = Iterable|AsyncIterable|Object<value any>
|
|
2529
|
-
|
|
2530
|
-
flatMap(
|
|
2531
|
-
value FlatMappable,
|
|
2532
|
-
flatMapper (item any)=>Promise|Iterable,
|
|
2533
|
-
) -> result Promise|FlatMappable
|
|
2534
|
-
|
|
2535
|
-
flatMap(
|
|
2536
|
-
flatMapper (item any)=>Promise|Iterable,
|
|
2537
|
-
)(value FlatMappable) -> result Promise|FlatMappable
|
|
2538
|
-
`, () => {
|
|
2539
|
-
|
|
2540
|
-
it('API coverage', async () => {
|
|
2541
|
-
assert.deepEqual(
|
|
2542
|
-
flatMap([1, 2, 3, 4, 5], number => isOdd(number) ? [number] : []),
|
|
2543
|
-
[1, 3, 5],
|
|
2544
|
-
)
|
|
2545
|
-
assert.deepEqual(
|
|
2546
|
-
flatMap(number => isOdd(number) ? [number] : [])([1, 2, 3, 4, 5]),
|
|
2547
|
-
[1, 3, 5],
|
|
2548
|
-
)
|
|
2549
|
-
assert.deepEqual(
|
|
2550
|
-
await flatMap(Promise.resolve([1, 2, 3, 4, 5]), number => isOdd(number) ? [number] : []),
|
|
2551
|
-
[1, 3, 5],
|
|
2552
|
-
)
|
|
2553
|
-
})
|
|
2554
|
-
|
|
2555
|
-
const async = func => async function asyncFunc(...args) {
|
|
2556
|
-
return func(...args)
|
|
2557
|
-
}
|
|
2558
|
-
|
|
2559
|
-
const numbersArray = [1, 2, 3, 4, 5]
|
|
2560
|
-
const numbersDuplicates = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
|
|
2561
|
-
const numbersObject = { a: 1, b: 2, c: 3, d: 4, e: 5 }
|
|
2562
|
-
const numbersUint8Array = new Uint8Array([1, 2, 3, 4, 5])
|
|
2563
|
-
const numbersUint8ArrayDuplicates = new Uint8Array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
|
|
2564
|
-
const numbersInt8Array = new Int8Array([1, 2, 3, 4, 5])
|
|
2565
|
-
const numbersInt8ArrayDuplicates = new Int8Array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
|
|
2566
|
-
const numbersUint16Array = new Uint16Array([1, 2, 3, 4, 5])
|
|
2567
|
-
const numbersUint16ArrayDuplicates = new Uint16Array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
|
|
2568
|
-
const numbersInt16Array = new Int16Array([1, 2, 3, 4, 5])
|
|
2569
|
-
const numbersInt16ArrayDuplicates = new Int16Array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
|
|
2570
|
-
const numbersUint32Array = new Uint32Array([1, 2, 3, 4, 5])
|
|
2571
|
-
const numbersUint32ArrayDuplicates = new Uint32Array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
|
|
2572
|
-
const numbersInt32Array = new Int32Array([1, 2, 3, 4, 5])
|
|
2573
|
-
const numbersInt32ArrayDuplicates = new Int32Array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
|
|
2574
|
-
const numbersFloat32Array = new Float32Array([1, 2, 3, 4, 5])
|
|
2575
|
-
const numbersFloat32ArrayDuplicates = new Float32Array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
|
|
2576
|
-
const numbersFloat64Array = new Float64Array([1, 2, 3, 4, 5])
|
|
2577
|
-
const numbersFloat64ArrayDuplicates = new Float64Array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
|
|
2578
|
-
const numbersBigUint64Array = new BigUint64Array([1n, 2n, 3n, 4n, 5n])
|
|
2579
|
-
const numbersBigUint64ArrayDuplicates = new BigUint64Array([1n, 1n, 2n, 2n, 3n, 3n, 4n, 4n, 5n, 5n])
|
|
2580
|
-
const numbersBigInt64Array = new BigInt64Array([1n, 2n, 3n, 4n, 5n])
|
|
2581
|
-
const numbersBigInt64ArrayDuplicates = new BigInt64Array([1n, 1n, 2n, 2n, 3n, 3n, 4n, 4n, 5n, 5n])
|
|
2582
|
-
const bigIntsArray = [1n, 2n, 3n, 4n, 5n]
|
|
2583
|
-
const bigIntsDuplicates = [1n, 1n, 2n, 2n, 3n, 3n, 4n, 4n, 5n, 5n]
|
|
2584
|
-
const numbersSet = new Set([1, 2, 3, 4, 5])
|
|
2585
|
-
const bigIntsSet = new Set([1n, 2n, 3n, 4n, 5n])
|
|
2586
|
-
const numbersSetWithAyo = new Set([1, 2, 3, 4, 5, 'ayo'])
|
|
2587
|
-
const alphabetString = 'abcde'
|
|
2588
|
-
const numbersDuplexStream = new MockDuplexStream([1, 2, 3, 4, 5])
|
|
2589
|
-
const numbersGeneratorFunction = function* () { for (let i = 1; i < 6; i++) yield i }
|
|
2590
|
-
const asyncNumbersGeneratorFunction = async function* () { for (let i = 1; i < 6; i++) yield i }
|
|
2591
|
-
|
|
2592
|
-
const identity = value => value
|
|
2593
|
-
const duplicateArray = value => [value, value]
|
|
2594
|
-
const duplicateObject = value => ({ [value * 10]: value, [value * 100]: value })
|
|
2595
|
-
const duplicateObjectString = value => ({ [value]: value, [`${value}${value}`]: value })
|
|
2596
|
-
const duplicateObjectBigInt = value => ({ [value * 10n]: value, [value * 100n]: value })
|
|
2597
|
-
const nestedObject = value => ({ a: { a: value }, b: { b: value } })
|
|
2598
|
-
const duplicateString = value => `${value}${value}`
|
|
2599
|
-
const setWithAyo = value => new Set([value, 'ayo'])
|
|
2600
|
-
const duplicateUint8ClampedArray = value => new Uint8ClampedArray([value, value])
|
|
2601
|
-
const duplicateUint8Array = value => new Uint8Array([value, value])
|
|
2602
|
-
const duplicateArrayOfUint8Array = value => [new Uint8Array([value]), new Uint8Array([value])]
|
|
2603
|
-
const duplicateInt8Array = value => new Int8Array([value, value])
|
|
2604
|
-
const duplicateUint16Array = value => new Uint16Array([value, value])
|
|
2605
|
-
const duplicateInt16Array = value => new Int16Array([value, value])
|
|
2606
|
-
const duplicateUint32Array = value => new Uint32Array([value, value])
|
|
2607
|
-
const duplicateInt32Array = value => new Int32Array([value, value])
|
|
2608
|
-
const duplicateFloat32Array = value => new Float32Array([value, value])
|
|
2609
|
-
const duplicateFloat64Array = value => new Float64Array([value, value])
|
|
2610
|
-
const duplicateBigUint64Array = value => new BigUint64Array([value, value])
|
|
2611
|
-
const duplicateBigInt64Array = value => new BigInt64Array([value, value])
|
|
2612
|
-
const duplicateReadableStream = value => binaryToReadableStream(Buffer.from([value, value]))
|
|
2613
|
-
const duplicateBuffer = value => Buffer.from([value, value])
|
|
2614
|
-
const duplicateMockFoldable = value => new MockFoldable([value, value])
|
|
2615
|
-
const duplicateAsyncMockFoldable = value => new AsyncMockFoldable([value, value])
|
|
2616
|
-
const duplicateMockFoldableObjects = value => new MockFoldable([{ [value * 10]: value }, { [value * 100]: value }])
|
|
2617
|
-
|
|
2618
|
-
const DuplicateArray = function (value) {
|
|
2619
|
-
this.value = value
|
|
2620
|
-
}
|
|
2621
|
-
DuplicateArray.prototype = {
|
|
2622
|
-
chain(flatMapper) {
|
|
2623
|
-
return flatMapper([this.value, this.value])
|
|
2624
|
-
},
|
|
2625
|
-
}
|
|
2626
|
-
DuplicateArray.of = value => new DuplicateArray(value)
|
|
2627
|
-
|
|
2628
|
-
const AsyncDuplicateArray = value => ({
|
|
2629
|
-
async chain(flatMapper) {
|
|
2630
|
-
return flatMapper([value, value])
|
|
2631
|
-
},
|
|
2632
|
-
})
|
|
2633
|
-
const AsyncDuplicateArrayFlatMap = value => ({
|
|
2634
|
-
async flatMap(flatMapper) {
|
|
2635
|
-
return flatMapper([value, value])
|
|
2636
|
-
},
|
|
2637
|
-
})
|
|
2638
|
-
|
|
2639
|
-
const DuplicateObject = function (value) {
|
|
2640
|
-
this.value = value
|
|
2641
|
-
}
|
|
2642
|
-
DuplicateObject.prototype = {
|
|
2643
|
-
chain(flatMapper) {
|
|
2644
|
-
const value = this.value
|
|
2645
|
-
return flatMapper({ [value * 10]: value, [value * 100]: value })
|
|
2646
|
-
},
|
|
2647
|
-
}
|
|
2648
|
-
DuplicateObject.of = value => new DuplicateObject(value)
|
|
2649
|
-
|
|
2650
|
-
const AsyncDuplicateObject = value => ({
|
|
2651
|
-
async chain(flatMapper) {
|
|
2652
|
-
return flatMapper({ [value * 10]: value, [value * 100]: value })
|
|
2653
|
-
}
|
|
2654
|
-
})
|
|
2655
|
-
const AsyncDuplicateObjectFlatMap = value => ({
|
|
2656
|
-
async flatMap(flatMapper) {
|
|
2657
|
-
return flatMapper({ [value * 10]: value, [value * 100]: value })
|
|
2658
|
-
}
|
|
2659
|
-
})
|
|
2660
|
-
|
|
2661
|
-
const Identity = function (value) {
|
|
2662
|
-
this.value = value
|
|
2663
|
-
}
|
|
2664
|
-
Identity.prototype = {
|
|
2665
|
-
flatMap(flatMapper) {
|
|
2666
|
-
return flatMapper(this.value)
|
|
2667
|
-
},
|
|
2668
|
-
}
|
|
2669
|
-
Identity.of = value => new Identity(value)
|
|
2670
|
-
|
|
2671
|
-
const assertions = [
|
|
2672
|
-
[Promise.resolve(1), identity, 1],
|
|
2673
|
-
[numbersArray, () => null, [null, null, null, null, null]],
|
|
2674
|
-
[numbersArray, () => [null, null], [null, null, null, null, null, null, null, null, null, null]],
|
|
2675
|
-
[numbersArray, () => undefined, [undefined, undefined, undefined, undefined, undefined]],
|
|
2676
|
-
[numbersArray, identity, numbersArray],
|
|
2677
|
-
[numbersArray, Promise.resolve.bind(Promise), numbersArray],
|
|
2678
|
-
[numbersArray, duplicateArray, numbersDuplicates],
|
|
2679
|
-
[numbersArray, async(duplicateArray), numbersDuplicates],
|
|
2680
|
-
[numbersArray, DuplicateArray.of, numbersArray.map(duplicateArray)], // calling .chain is 1 flat
|
|
2681
|
-
[numbersArray, async(DuplicateArray.of), numbersArray.map(duplicateArray)],
|
|
2682
|
-
[numbersArray, AsyncDuplicateArray, numbersArray.map(duplicateArray)], // calling .chain is 1 flat
|
|
2683
|
-
[numbersArray, async(AsyncDuplicateArray), numbersArray.map(duplicateArray)],
|
|
2684
|
-
[numbersArray, AsyncDuplicateArrayFlatMap, numbersArray.map(duplicateArray)], // calling .chain is 1 flat
|
|
2685
|
-
[numbersArray, async(AsyncDuplicateArrayFlatMap), numbersArray.map(duplicateArray)],
|
|
2686
|
-
[numbersArray, Identity.of, numbersArray],
|
|
2687
|
-
[numbersArray, async(Identity.of), numbersArray],
|
|
2688
|
-
[numbersArray, duplicateMockFoldable, numbersDuplicates],
|
|
2689
|
-
[numbersArray, async(duplicateMockFoldable), numbersDuplicates],
|
|
2690
|
-
[numbersArray, duplicateAsyncMockFoldable, numbersDuplicates],
|
|
2691
|
-
[numbersArray, async(duplicateAsyncMockFoldable), numbersDuplicates],
|
|
2692
|
-
[numbersArray, duplicateObject, numbersDuplicates],
|
|
2693
|
-
[numbersArray, async(duplicateObject), numbersDuplicates],
|
|
2694
|
-
[numbersArray, duplicateString, ['1', '1', '2', '2', '3', '3', '4', '4', '5', '5']],
|
|
2695
|
-
[numbersArray, async(duplicateString), ['1', '1', '2', '2', '3', '3', '4', '4', '5', '5']],
|
|
2696
|
-
[numbersArray, setWithAyo, [1, 'ayo', 2, 'ayo', 3, 'ayo', 4, 'ayo', 5, 'ayo']],
|
|
2697
|
-
[numbersArray, async(setWithAyo), [1, 'ayo', 2, 'ayo', 3, 'ayo', 4, 'ayo', 5, 'ayo']],
|
|
2698
|
-
[numbersArray, duplicateUint8ClampedArray, numbersDuplicates],
|
|
2699
|
-
[numbersArray, async(duplicateUint8ClampedArray), numbersDuplicates],
|
|
2700
|
-
[numbersArray, duplicateUint8Array, numbersDuplicates],
|
|
2701
|
-
[numbersArray, async(duplicateUint8Array), numbersDuplicates],
|
|
2702
|
-
[numbersArray, duplicateInt8Array, numbersDuplicates],
|
|
2703
|
-
[numbersArray, async(duplicateInt8Array), numbersDuplicates],
|
|
2704
|
-
[numbersArray, duplicateUint16Array, numbersDuplicates],
|
|
2705
|
-
[numbersArray, async(duplicateUint16Array), numbersDuplicates],
|
|
2706
|
-
[numbersArray, duplicateInt16Array, numbersDuplicates],
|
|
2707
|
-
[numbersArray, async(duplicateInt16Array), numbersDuplicates],
|
|
2708
|
-
[numbersArray, duplicateUint32Array, numbersDuplicates],
|
|
2709
|
-
[numbersArray, async(duplicateUint32Array), numbersDuplicates],
|
|
2710
|
-
[numbersArray, duplicateInt32Array, numbersDuplicates],
|
|
2711
|
-
[numbersArray, async(duplicateInt32Array), numbersDuplicates],
|
|
2712
|
-
[numbersArray, duplicateFloat32Array, numbersDuplicates],
|
|
2713
|
-
[numbersArray, async(duplicateFloat32Array), numbersDuplicates],
|
|
2714
|
-
[numbersArray, duplicateFloat64Array, numbersDuplicates],
|
|
2715
|
-
[numbersArray, async(duplicateFloat64Array), numbersDuplicates],
|
|
2716
|
-
[numbersArray, duplicateReadableStream, numbersArray.map(duplicateBuffer)], // 1 flat removes the stream
|
|
2717
|
-
[numbersArray, async(duplicateReadableStream), numbersArray.map(duplicateBuffer)],
|
|
2718
|
-
[bigIntsArray, identity, bigIntsArray],
|
|
2719
|
-
[bigIntsArray, duplicateBigUint64Array, bigIntsDuplicates],
|
|
2720
|
-
[bigIntsArray, async(duplicateBigUint64Array), bigIntsDuplicates],
|
|
2721
|
-
[bigIntsArray, duplicateBigInt64Array, bigIntsDuplicates],
|
|
2722
|
-
[bigIntsArray, async(duplicateBigInt64Array), bigIntsDuplicates],
|
|
2723
|
-
|
|
2724
|
-
[alphabetString, identity, alphabetString],
|
|
2725
|
-
[alphabetString, duplicateArray, 'aabbccddee'],
|
|
2726
|
-
[alphabetString, async(duplicateArray), 'aabbccddee'],
|
|
2727
|
-
[alphabetString, DuplicateArray.of, 'a,ab,bc,cd,de,e'], // calling .chain is 1 flat
|
|
2728
|
-
[alphabetString, async(DuplicateArray.of), 'a,ab,bc,cd,de,e'],
|
|
2729
|
-
[alphabetString, Identity.of, alphabetString],
|
|
2730
|
-
[alphabetString, async(Identity.of), alphabetString],
|
|
2731
|
-
[alphabetString, duplicateMockFoldable, 'aabbccddee'],
|
|
2732
|
-
[alphabetString, async(duplicateMockFoldable), 'aabbccddee'],
|
|
2733
|
-
[alphabetString, duplicateAsyncMockFoldable, 'aabbccddee'],
|
|
2734
|
-
[alphabetString, async(duplicateAsyncMockFoldable), 'aabbccddee'],
|
|
2735
|
-
[alphabetString, duplicateObjectString, 'aabbccddee'],
|
|
2736
|
-
[alphabetString, async(duplicateObjectString), 'aabbccddee'],
|
|
2737
|
-
[alphabetString, duplicateString, 'aabbccddee'],
|
|
2738
|
-
[alphabetString, async(duplicateString), 'aabbccddee'],
|
|
2739
|
-
[alphabetString, setWithAyo, 'aayobayocayodayoeayo'],
|
|
2740
|
-
[alphabetString, async(setWithAyo), 'aayobayocayodayoeayo'],
|
|
2741
|
-
|
|
2742
|
-
[numbersSet, identity, numbersSet],
|
|
2743
|
-
[numbersSet, () => null, new Set([null])],
|
|
2744
|
-
[numbersSet, duplicateArray, numbersSet],
|
|
2745
|
-
[numbersSet, async(duplicateArray), numbersSet],
|
|
2746
|
-
[numbersSet, DuplicateArray.of, new Set([...numbersSet].map(duplicateArray))],
|
|
2747
|
-
[numbersSet, async(DuplicateArray.of), new Set([...numbersSet].map(duplicateArray))],
|
|
2748
|
-
[numbersSet, Identity.of, numbersSet],
|
|
2749
|
-
[numbersSet, async(Identity.of), numbersSet],
|
|
2750
|
-
[numbersSet, duplicateMockFoldable, numbersSet],
|
|
2751
|
-
[numbersSet, async(duplicateMockFoldable), numbersSet],
|
|
2752
|
-
[numbersSet, duplicateAsyncMockFoldable, numbersSet],
|
|
2753
|
-
[numbersSet, async(duplicateAsyncMockFoldable), numbersSet],
|
|
2754
|
-
[numbersSet, duplicateObject, numbersSet],
|
|
2755
|
-
[numbersSet, async(duplicateObject), numbersSet],
|
|
2756
|
-
[numbersSet, setWithAyo, new Set([1, 2, 3, 4, 5, 'ayo'])],
|
|
2757
|
-
[numbersSet, async(setWithAyo), new Set([1, 2, 3, 4, 5, 'ayo'])],
|
|
2758
|
-
[numbersSet, duplicateString, new Set(['1', '2', '3', '4', '5'])],
|
|
2759
|
-
[numbersSet, async(duplicateString), new Set(['1', '2', '3', '4', '5'])],
|
|
2760
|
-
[numbersSet, duplicateUint8Array, numbersSet],
|
|
2761
|
-
[numbersSet, async(duplicateUint8Array), numbersSet],
|
|
2762
|
-
[numbersSet, duplicateInt8Array, numbersSet],
|
|
2763
|
-
[numbersSet, async(duplicateInt8Array), numbersSet],
|
|
2764
|
-
[numbersSet, duplicateUint16Array, numbersSet],
|
|
2765
|
-
[numbersSet, async(duplicateUint16Array), numbersSet],
|
|
2766
|
-
[numbersSet, duplicateInt16Array, numbersSet],
|
|
2767
|
-
[numbersSet, async(duplicateInt16Array), numbersSet],
|
|
2768
|
-
[numbersSet, duplicateUint32Array, numbersSet],
|
|
2769
|
-
[numbersSet, async(duplicateUint32Array), numbersSet],
|
|
2770
|
-
[numbersSet, duplicateInt32Array, numbersSet],
|
|
2771
|
-
[numbersSet, async(duplicateInt32Array), numbersSet],
|
|
2772
|
-
[numbersSet, duplicateFloat32Array, numbersSet],
|
|
2773
|
-
[numbersSet, async(duplicateFloat32Array), numbersSet],
|
|
2774
|
-
[numbersSet, duplicateFloat64Array, numbersSet],
|
|
2775
|
-
[numbersSet, async(duplicateFloat64Array), numbersSet],
|
|
2776
|
-
[numbersSet, duplicateReadableStream, new Set(numbersArray.map(duplicateBuffer))],
|
|
2777
|
-
[numbersSet, async(duplicateReadableStream), new Set(numbersArray.map(duplicateBuffer))],
|
|
2778
|
-
|
|
2779
|
-
[numbersGeneratorFunction(), identity, numbersArray, function (expected, actual) {
|
|
2780
|
-
assert.deepEqual([...actual], expected)
|
|
2781
|
-
}],
|
|
2782
|
-
[numbersGeneratorFunction(), duplicateArray, numbersDuplicates, function (expected, actual) {
|
|
2783
|
-
assert.deepEqual([...actual], expected)
|
|
2784
|
-
}],
|
|
2785
|
-
[numbersGeneratorFunction(), async(duplicateArray), numbersArray.map(duplicateArray), async function (expected, actual) {
|
|
2786
|
-
assert.deepEqual(await Promise.all(actual), expected) // flatten will only see promises, so async does not get flattened
|
|
2787
|
-
}],
|
|
2788
|
-
[numbersGeneratorFunction(), DuplicateArray.of, numbersArray.map(duplicateArray), function (expected, actual) {
|
|
2789
|
-
assert.deepEqual([...actual], expected)
|
|
2790
|
-
}],
|
|
2791
|
-
[numbersGeneratorFunction(), async(DuplicateArray.of), numbersArray.map(DuplicateArray.of), async function (expected, actual) {
|
|
2792
|
-
assert.deepEqual(await Promise.all(actual), expected) // flatten will only see promises, so async does not get flattened
|
|
2793
|
-
}],
|
|
2794
|
-
[numbersGeneratorFunction(), Identity.of, numbersArray, function (expected, actual) {
|
|
2795
|
-
const actualArray = [...actual]
|
|
2796
|
-
assert.deepEqual(actualArray, expected)
|
|
2797
|
-
}],
|
|
2798
|
-
[numbersGeneratorFunction(), async(Identity.of), numbersArray.map(Identity.of), async function (expected, actual) {
|
|
2799
|
-
assert.deepEqual(await Promise.all(actual), expected) // flatten will only see promises, so async does not get flattened
|
|
2800
|
-
}],
|
|
2801
|
-
[numbersGeneratorFunction(), duplicateMockFoldable, numbersDuplicates, function (expected, actual) {
|
|
2802
|
-
assert.deepEqual([...actual], expected)
|
|
2803
|
-
}],
|
|
2804
|
-
[numbersGeneratorFunction(), async(duplicateMockFoldable), numbersArray.map(duplicateMockFoldable), async function (expected, actual) {
|
|
2805
|
-
assert.deepEqual(await Promise.all(actual), expected) // flatten will only see promises, so async does not get flattened
|
|
2806
|
-
}],
|
|
2807
|
-
[numbersGeneratorFunction(), duplicateObject, numbersDuplicates, function (expected, actual) {
|
|
2808
|
-
assert.deepEqual([...actual], expected)
|
|
2809
|
-
}],
|
|
2810
|
-
[numbersGeneratorFunction(), async(duplicateObject), numbersArray.map(duplicateObject), async function (expected, actual) {
|
|
2811
|
-
assert.deepEqual(await Promise.all(actual), expected) // flatten will only see promises, so async does not get flattened
|
|
2812
|
-
}],
|
|
2813
|
-
|
|
2814
|
-
[asyncNumbersGeneratorFunction(), identity, numbersArray, async function (expected, actual) {
|
|
2815
|
-
const actualArray = []
|
|
2816
|
-
for await (const item of actual) actualArray.push(item)
|
|
2817
|
-
assert.deepEqual(actualArray, expected)
|
|
2818
|
-
}],
|
|
2819
|
-
[asyncNumbersGeneratorFunction(), duplicateArray, numbersDuplicates, async function (expected, actual) {
|
|
2820
|
-
const actualArray = []
|
|
2821
|
-
for await (const item of actual) actualArray.push(item)
|
|
2822
|
-
assert.deepEqual(actualArray, expected)
|
|
2823
|
-
}],
|
|
2824
|
-
[asyncNumbersGeneratorFunction(), async(duplicateArray), numbersDuplicates, async function (expected, actual) {
|
|
2825
|
-
const actualArray = []
|
|
2826
|
-
for await (const item of actual) actualArray.push(item)
|
|
2827
|
-
assert.deepEqual(actualArray, expected)
|
|
2828
|
-
}],
|
|
2829
|
-
|
|
2830
|
-
[asyncNumbersGeneratorFunction(), DuplicateArray.of, numbersArray.map(duplicateArray), async function (expected, actual) {
|
|
2831
|
-
const actualArray = []
|
|
2832
|
-
for await (const item of actual) actualArray.push(item)
|
|
2833
|
-
assert.deepEqual(actualArray, expected)
|
|
2834
|
-
}],
|
|
2835
|
-
|
|
2836
|
-
[asyncNumbersGeneratorFunction(), async(DuplicateArray.of), numbersArray.map(duplicateArray), async function (expected, actual) {
|
|
2837
|
-
const actualArray = []
|
|
2838
|
-
for await (const item of actual) actualArray.push(item)
|
|
2839
|
-
assert.deepEqual(actualArray, expected)
|
|
2840
|
-
}],
|
|
2841
|
-
[asyncNumbersGeneratorFunction(), duplicateMockFoldable, numbersDuplicates, async function (expected, actual) {
|
|
2842
|
-
const actualArray = []
|
|
2843
|
-
for await (const item of actual) actualArray.push(item)
|
|
2844
|
-
assert.deepEqual(actualArray, expected)
|
|
2845
|
-
}],
|
|
2846
|
-
[asyncNumbersGeneratorFunction(), async(duplicateMockFoldable), numbersDuplicates, async function (expected, actual) {
|
|
2847
|
-
const actualArray = []
|
|
2848
|
-
for await (const item of actual) actualArray.push(item)
|
|
2849
|
-
assert.deepEqual(actualArray, expected)
|
|
2850
|
-
}],
|
|
2851
|
-
[asyncNumbersGeneratorFunction(), duplicateObject, numbersDuplicates, async function (expected, actual) {
|
|
2852
|
-
const actualArray = []
|
|
2853
|
-
for await (const item of actual) actualArray.push(item)
|
|
2854
|
-
assert.deepEqual(actualArray, expected)
|
|
2855
|
-
}],
|
|
2856
|
-
[asyncNumbersGeneratorFunction(), async(duplicateObject), numbersDuplicates, async function (expected, actual) {
|
|
2857
|
-
const actualArray = []
|
|
2858
|
-
for await (const item of actual) actualArray.push(item)
|
|
2859
|
-
assert.deepEqual(actualArray, expected)
|
|
2860
|
-
}],
|
|
2861
|
-
[asyncNumbersGeneratorFunction(), duplicateString, numbersDuplicates, async function (expected, actual) {
|
|
2862
|
-
const actualArray = []
|
|
2863
|
-
for await (const item of actual) actualArray.push(item)
|
|
2864
|
-
assert.deepEqual(actualArray, expected)
|
|
2865
|
-
}],
|
|
2866
|
-
[asyncNumbersGeneratorFunction(), async(duplicateString), numbersDuplicates, async function (expected, actual) {
|
|
2867
|
-
const actualArray = []
|
|
2868
|
-
for await (const item of actual) actualArray.push(item)
|
|
2869
|
-
assert.deepEqual(actualArray, expected)
|
|
2870
|
-
}],
|
|
2871
|
-
[asyncNumbersGeneratorFunction(), duplicateBuffer, numbersDuplicates, async function (expected, actual) {
|
|
2872
|
-
const actualArray = []
|
|
2873
|
-
for await (const item of actual) actualArray.push(item)
|
|
2874
|
-
assert.deepEqual(actualArray, expected)
|
|
2875
|
-
}],
|
|
2876
|
-
[asyncNumbersGeneratorFunction(), async(duplicateBuffer), numbersDuplicates, async function (expected, actual) {
|
|
2877
|
-
const actualArray = []
|
|
2878
|
-
for await (const item of actual) actualArray.push(item)
|
|
2879
|
-
assert.deepEqual(actualArray, expected)
|
|
2880
|
-
}],
|
|
2881
|
-
[asyncNumbersGeneratorFunction(), duplicateUint8Array, numbersDuplicates, async function (expected, actual) {
|
|
2882
|
-
const actualArray = []
|
|
2883
|
-
for await (const item of actual) actualArray.push(item)
|
|
2884
|
-
assert.deepEqual(actualArray, expected)
|
|
2885
|
-
}],
|
|
2886
|
-
[asyncNumbersGeneratorFunction(), async(duplicateUint8Array), numbersDuplicates, async function (expected, actual) {
|
|
2887
|
-
const actualArray = []
|
|
2888
|
-
for await (const item of actual) actualArray.push(item)
|
|
2889
|
-
assert.deepEqual(actualArray, expected)
|
|
2890
|
-
}],
|
|
2891
|
-
[asyncNumbersGeneratorFunction(), duplicateReadableStream, numbersArray.map(duplicateBuffer), async function (expected, actual) {
|
|
2892
|
-
const actualArray = []
|
|
2893
|
-
for await (const item of actual) actualArray.push(item)
|
|
2894
|
-
assert.deepEqual(actualArray, expected)
|
|
2895
|
-
}],
|
|
2896
|
-
[asyncNumbersGeneratorFunction(), async(duplicateReadableStream), numbersArray.map(duplicateBuffer), async function (expected, actual) {
|
|
2897
|
-
const actualArray = []
|
|
2898
|
-
for await (const item of actual) actualArray.push(item)
|
|
2899
|
-
assert.deepEqual(actualArray, expected)
|
|
2900
|
-
}],
|
|
2901
|
-
|
|
2902
|
-
[numbersObject, identity, {}],
|
|
2903
|
-
[numbersObject, async(identity), {}],
|
|
2904
|
-
[numbersObject, duplicateArray, {}],
|
|
2905
|
-
[numbersObject, async(duplicateArray), {}],
|
|
2906
|
-
[numbersObject, number => [{ [number]: number }, { [number]: number }], { 1: 1, 2: 2, 3: 3, 4: 4, 5: 5 }],
|
|
2907
|
-
[numbersObject, number => null, {}],
|
|
2908
|
-
[numbersObject, number => [{ [number]: null }, { [number]: null }], { 1: null, 2: null, 3: null, 4: null, 5: null }],
|
|
2909
|
-
[numbersObject, duplicateObject, {
|
|
2910
|
-
'10': 1, '100': 1, '20': 2, '200': 2, '30': 3, '300': 3, '40': 4, '400': 4, '50': 5, '500': 5,
|
|
2911
|
-
}],
|
|
2912
|
-
[numbersObject, async(duplicateObject), {
|
|
2913
|
-
'10': 1, '100': 1, '20': 2, '200': 2, '30': 3, '300': 3, '40': 4, '400': 4, '50': 5, '500': 5,
|
|
2914
|
-
}],
|
|
2915
|
-
[numbersObject, duplicateReadableStream, { 0: 5, 1: 5 }],
|
|
2916
|
-
[numbersObject, async(duplicateReadableStream), { 0: 5, 1: 5 }],
|
|
2917
|
-
[numbersObject, duplicateObjectString, { 1: 1, 11: 1, 2: 2, 22: 2, 3: 3, 33: 3, 4: 4, 44: 4, 5: 5, 55: 5 }],
|
|
2918
|
-
[numbersObject, async(duplicateObjectString), { 1: 1, 11: 1, 2: 2, 22: 2, 3: 3, 33: 3, 4: 4, 44: 4, 5: 5, 55: 5 }],
|
|
2919
|
-
[numbersObject, duplicateMockFoldable, {}],
|
|
2920
|
-
[numbersObject, duplicateAsyncMockFoldable, {}],
|
|
2921
|
-
[numbersObject, async(duplicateMockFoldable), {}],
|
|
2922
|
-
[numbersObject, duplicateMockFoldableObjects, { 10: 1, 100: 1, 20: 2, 200: 2, 30: 3, 300: 3, 40: 4, 400: 4, 50: 5, 500: 5 }],
|
|
2923
|
-
[numbersObject, async(duplicateMockFoldableObjects), { 10: 1, 100: 1, 20: 2, 200: 2, 30: 3, 300: 3, 40: 4, 400: 4, 50: 5, 500: 5 }],
|
|
2924
|
-
[numbersObject, DuplicateObject.of, { 10: 1, 100: 1, 20: 2, 200: 2, 30: 3, 300: 3, 40: 4, 400: 4, 50: 5, 500: 5 }],
|
|
2925
|
-
[numbersObject, async(DuplicateObject.of), { 10: 1, 100: 1, 20: 2, 200: 2, 30: 3, 300: 3, 40: 4, 400: 4, 50: 5, 500: 5 }],
|
|
2926
|
-
[numbersObject, AsyncDuplicateObject, { 10: 1, 100: 1, 20: 2, 200: 2, 30: 3, 300: 3, 40: 4, 400: 4, 50: 5, 500: 5 }],
|
|
2927
|
-
[numbersObject, async(AsyncDuplicateObject), { 10: 1, 100: 1, 20: 2, 200: 2, 30: 3, 300: 3, 40: 4, 400: 4, 50: 5, 500: 5 }],
|
|
2928
|
-
[numbersObject, AsyncDuplicateObjectFlatMap, { 10: 1, 100: 1, 20: 2, 200: 2, 30: 3, 300: 3, 40: 4, 400: 4, 50: 5, 500: 5 }],
|
|
2929
|
-
[numbersObject, async(AsyncDuplicateObjectFlatMap), { 10: 1, 100: 1, 20: 2, 200: 2, 30: 3, 300: 3, 40: 4, 400: 4, 50: 5, 500: 5 }],
|
|
2930
|
-
[numbersObject, Identity.of, {}],
|
|
2931
|
-
[numbersObject, async(Identity.of), {}],
|
|
2932
|
-
[{ a: { a: 1 }, b: { b: 2 }, c: { c: 3 }, d: { d: 4 }, e: { e: 5 } }, Identity.of, numbersObject],
|
|
2933
|
-
[{ a: { a: 1 }, b: { b: 2 }, c: { c: 3 }, d: { d: 4 }, e: { e: 5 } }, async(Identity.of), numbersObject],
|
|
2934
|
-
|
|
2935
|
-
[{ chain: flatMapper => flatMapper('ayo') }, duplicateArray, ['ayo', 'ayo']],
|
|
2936
|
-
[{ chain: flatMapper => flatMapper('ayo') }, async(duplicateArray), ['ayo', 'ayo']],
|
|
2937
|
-
[DuplicateArray.of('ayo'), DuplicateArray.of, DuplicateArray.of(['ayo', 'ayo'])],
|
|
2938
|
-
[DuplicateArray.of('ayo'), async(DuplicateArray.of), DuplicateArray.of(['ayo', 'ayo'])],
|
|
2939
|
-
[Identity.of('ayo'), Identity.of, Identity.of('ayo')],
|
|
2940
|
-
[Identity.of('ayo'), async(Identity.of), Identity.of('ayo')],
|
|
2941
|
-
|
|
2942
|
-
]
|
|
2943
|
-
|
|
2944
|
-
const arrayOfLast = (array, n = 1) => array.slice(array.length - n, array.length)
|
|
2945
|
-
|
|
2946
|
-
assertions.forEach(function ([value, flatMapper, result, asserter = assert.deepEqual]) {
|
|
2947
|
-
it(`flatMap(${flatMapper.name})(${value.constructor.name}<${value}>) - ${result}`, async () => {
|
|
2948
|
-
await asserter(
|
|
2949
|
-
result,
|
|
2950
|
-
await flatMap(flatMapper)(value),
|
|
2951
|
-
)
|
|
2952
|
-
})
|
|
2953
|
-
})
|
|
2954
|
-
|
|
2955
|
-
it('value undefined', async () => {
|
|
2956
|
-
assert.strictEqual(flatMap(() => 'hey')(undefined), 'hey')
|
|
2957
|
-
assert.strictEqual(flatMap(() => 'hey')(), 'hey')
|
|
2958
|
-
})
|
|
2959
|
-
it('value null', async () => {
|
|
2960
|
-
assert.strictEqual(flatMap(() => 'hey')(null), 'hey')
|
|
2961
|
-
})
|
|
2962
|
-
|
|
2963
|
-
it('value number', async () => {
|
|
2964
|
-
assert.deepEqual(flatMap(duplicateArray)(1), [1, 1])
|
|
2965
|
-
assert.deepEqual(flatMap(1, duplicateArray), [1, 1])
|
|
2966
|
-
})
|
|
2967
|
-
})
|
|
2968
|
-
|
|
2969
|
-
describe('FlatMappingAsyncIterator', () => {
|
|
2970
|
-
const asyncNumbers = async function* () {
|
|
2971
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
2972
|
-
}
|
|
2973
|
-
const flatMappingAsyncIterator = flatMap(value => value)(asyncNumbers())
|
|
2974
|
-
it('[object FlatMappingAsyncIterator]', async () => {
|
|
2975
|
-
assert.strictEqual(flatMappingAsyncIterator.toString(), '[object FlatMappingAsyncIterator]')
|
|
2976
|
-
})
|
|
2977
|
-
})
|
|
2978
|
-
})
|
|
2979
|
-
|
|
2980
|
-
describe('flatMap - misc', () => {
|
|
2981
|
-
it('maps then flattens an array, async + parallel', async () => {
|
|
2982
|
-
const asyncPowers = async x => [x ** 2, x ** 3]
|
|
2983
|
-
aok(flatMap(asyncPowers)([1, 2, 3, 4, 5]) instanceof Promise)
|
|
2984
|
-
ade(
|
|
2985
|
-
await flatMap(asyncPowers)([1, 2, 3, 4, 5]),
|
|
2986
|
-
[1, 1, 4, 8, 9, 27, 16, 64, 25, 125],
|
|
2987
|
-
)
|
|
2988
|
-
})
|
|
2989
|
-
it('maps then flattens an array, sync', async () => {
|
|
2990
|
-
const powers = x => [x ** 2, x ** 3]
|
|
2991
|
-
ade(
|
|
2992
|
-
flatMap(powers)([1, 2, 3, 4, 5]),
|
|
2993
|
-
[1, 1, 4, 8, 9, 27, 16, 64, 25, 125],
|
|
2994
|
-
)
|
|
2995
|
-
ade(
|
|
2996
|
-
flatMap(powers)([1, 2, 3, 4, 5]),
|
|
2997
|
-
[1, 1, 4, 8, 9, 27, 16, 64, 25, 125],
|
|
2998
|
-
)
|
|
2999
|
-
ade(
|
|
3000
|
-
flatMap(x => new Set([x ** 2]))([1, 2, 3, 4, 5]),
|
|
3001
|
-
[1, 4, 9, 16, 25],
|
|
3002
|
-
)
|
|
3003
|
-
ade(
|
|
3004
|
-
flatMap(
|
|
3005
|
-
x => x,
|
|
3006
|
-
)(
|
|
3007
|
-
flatMap(x => new Map([[x, x ** 2]]))([1, 2, 3, 4, 5]),
|
|
3008
|
-
),
|
|
3009
|
-
[1, 1, 2, 4, 3, 9, 4, 16, 5, 25],
|
|
3010
|
-
)
|
|
3011
|
-
ade(
|
|
3012
|
-
flatMap(x => x)([1, 2, [3, 4], 5]),
|
|
3013
|
-
[1, 2, 3, 4, 5],
|
|
3014
|
-
)
|
|
3015
|
-
})
|
|
3016
|
-
it('maps then flattens objects', async () => {
|
|
3017
|
-
const createObject = () => ({ a: 1, b: 2, c: 3 })
|
|
3018
|
-
ade(
|
|
3019
|
-
flatMap(x => x)(arrayOf(createObject, 3)),
|
|
3020
|
-
[1, 2, 3, 1, 2, 3, 1, 2, 3],
|
|
3021
|
-
)
|
|
3022
|
-
})
|
|
3023
|
-
it('maps then flattens a Set', async () => {
|
|
3024
|
-
const powers = x => [x ** 2, x ** 3]
|
|
3025
|
-
ade(
|
|
3026
|
-
flatMap(
|
|
3027
|
-
powers,
|
|
3028
|
-
)(new Set([1, 2, 3])),
|
|
3029
|
-
new Set([1, 4, 8, 9, 27]),
|
|
3030
|
-
)
|
|
3031
|
-
ade(
|
|
3032
|
-
flatMap(
|
|
3033
|
-
x => new Set(powers(x)),
|
|
3034
|
-
)(new Set([1, 2, 3])),
|
|
3035
|
-
new Set([1, 4, 8, 9, 27]),
|
|
3036
|
-
)
|
|
3037
|
-
})
|
|
3038
|
-
it('maps then flattens objects', async () => {
|
|
3039
|
-
ade(
|
|
3040
|
-
flatMap(x => ({ square: x ** 2, cube: x ** 3 }))(new Set([1, 2, 3])),
|
|
3041
|
-
new Set([1, 4, 8, 9, 27])
|
|
3042
|
-
)
|
|
3043
|
-
})
|
|
3044
|
-
it('[async] maps then flattens a Set', async () => {
|
|
3045
|
-
const powers = async x => [x ** 2, x ** 3]
|
|
3046
|
-
aok(
|
|
3047
|
-
flatMap(
|
|
3048
|
-
powers,
|
|
3049
|
-
)(new Set([1, 2, 3])) instanceof Promise
|
|
3050
|
-
)
|
|
3051
|
-
ade(
|
|
3052
|
-
await flatMap(
|
|
3053
|
-
powers,
|
|
3054
|
-
)(new Set([1, 2, 3])),
|
|
3055
|
-
new Set([1, 4, 8, 9, 27]),
|
|
3056
|
-
)
|
|
3057
|
-
aok(
|
|
3058
|
-
flatMap(
|
|
3059
|
-
x => powers(x).then(x => new Set(x))
|
|
3060
|
-
)(new Set([1, 2, 3])) instanceof Promise
|
|
3061
|
-
)
|
|
3062
|
-
ade(
|
|
3063
|
-
await flatMap(
|
|
3064
|
-
x => powers(x).then(x => new Set(x))
|
|
3065
|
-
)(new Set([1, 2, 3])),
|
|
3066
|
-
new Set([1, 4, 8, 9, 27]),
|
|
3067
|
-
)
|
|
3068
|
-
})
|
|
3069
|
-
it('maps then flattens a reducer function', async () => {
|
|
3070
|
-
const powers = x => [x ** 2, x ** 3]
|
|
3071
|
-
ade(
|
|
3072
|
-
transform(
|
|
3073
|
-
Transducer.flatMap(powers),
|
|
3074
|
-
[],
|
|
3075
|
-
)([1, 2, 3]),
|
|
3076
|
-
[1, 1, 4, 8, 9, 27],
|
|
3077
|
-
)
|
|
3078
|
-
})
|
|
3079
|
-
it('[async] maps then flattens a reducer function', async () => {
|
|
3080
|
-
const powers = async x => [x ** 2, x ** 3]
|
|
3081
|
-
aok(
|
|
3082
|
-
transform(
|
|
3083
|
-
Transducer.flatMap(powers),
|
|
3084
|
-
[],
|
|
3085
|
-
)([1, 2, 3]) instanceof Promise
|
|
3086
|
-
)
|
|
3087
|
-
ade(
|
|
3088
|
-
await transform(
|
|
3089
|
-
Transducer.flatMap(powers),
|
|
3090
|
-
[],
|
|
3091
|
-
)([1, 2, 3]),
|
|
3092
|
-
[1, 1, 4, 8, 9, 27],
|
|
3093
|
-
)
|
|
3094
|
-
})
|
|
3095
|
-
})
|
|
3096
|
-
|
|
3097
|
-
describe('forEach', () => {
|
|
3098
|
-
describe(`
|
|
3099
|
-
Reducer<T> = (any, T)=>Promise|any
|
|
3100
|
-
|
|
3101
|
-
var T any,
|
|
3102
|
-
callback T=>(),
|
|
3103
|
-
collection Iterable<T>|AsyncIterable<T>{ forEach: callback=>() }|Object<T>
|
|
3104
|
-
|
|
3105
|
-
forEach(callback)(collection) -> ()
|
|
3106
|
-
`,
|
|
3107
|
-
() => {
|
|
3108
|
-
it('API coverage', async () => {
|
|
3109
|
-
let sum1 = 0
|
|
3110
|
-
forEach([1, 2, 3, 4, 5], number => {
|
|
3111
|
-
sum1 += number
|
|
3112
|
-
})
|
|
3113
|
-
assert.equal(sum1, 15)
|
|
3114
|
-
|
|
3115
|
-
let sum2 = 0
|
|
3116
|
-
await forEach(Promise.resolve([1, 2, 3, 4, 5]), number => {
|
|
3117
|
-
sum2 += number
|
|
3118
|
-
})
|
|
3119
|
-
assert.equal(sum2, 15)
|
|
3120
|
-
})
|
|
3121
|
-
it('collection Array testing promises', async () => {
|
|
3122
|
-
let total = 0
|
|
3123
|
-
const addTotal = number => (total += number),
|
|
3124
|
-
variadicAsyncAddTotal = number => number % 2 == 1 ? (total += number) : Promise.resolve(total += number)
|
|
3125
|
-
forEach(addTotal)([1, 2, 3, 4, 5])
|
|
3126
|
-
assert.strictEqual(total, 15)
|
|
3127
|
-
total = 0
|
|
3128
|
-
const operation = forEach(async(addTotal))([1, 2, 3, 4, 5])
|
|
3129
|
-
assert(typeof operation.then == 'function')
|
|
3130
|
-
await operation
|
|
3131
|
-
assert.strictEqual(total, 15)
|
|
3132
|
-
})
|
|
3133
|
-
|
|
3134
|
-
let total = 0
|
|
3135
|
-
const addTotal = number => (total += number),
|
|
3136
|
-
variadicAsyncAddTotal = number => number % 2 == 1 ? (total += number) : Promise.resolve(total += number)
|
|
3137
|
-
|
|
3138
|
-
const ThunkAssertion = (
|
|
3139
|
-
func, value, asserter,
|
|
3140
|
-
) => function thunkAssertion() {
|
|
3141
|
-
it(`${func.name}(${util.inspect(value)}) - ${asserter}`, async () => {
|
|
3142
|
-
await asserter(await func(value))
|
|
3143
|
-
})
|
|
3144
|
-
}
|
|
3145
|
-
|
|
3146
|
-
const numbersGenerator = function* () {
|
|
3147
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
3148
|
-
}
|
|
3149
|
-
const numbersAsyncGenerator = async function* () {
|
|
3150
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
3151
|
-
}
|
|
3152
|
-
|
|
3153
|
-
const thunkAssertions = [
|
|
3154
|
-
ThunkAssertion(forEach(addTotal), [1, 2, 3, 4, 5], result => {
|
|
3155
|
-
assert.deepEqual(result, [1, 2, 3, 4, 5])
|
|
3156
|
-
assert.strictEqual(total, 15)
|
|
3157
|
-
total = 0
|
|
3158
|
-
}),
|
|
3159
|
-
ThunkAssertion(forEach(async(addTotal)), [1, 2, 3, 4, 5], result => {
|
|
3160
|
-
assert.deepEqual(result, [1, 2, 3, 4, 5])
|
|
3161
|
-
assert.strictEqual(total, 15)
|
|
3162
|
-
total = 0
|
|
3163
|
-
}),
|
|
3164
|
-
ThunkAssertion(forEach(variadicAsyncAddTotal), [1, 2, 3, 4, 5], result => {
|
|
3165
|
-
assert.deepEqual(result, [1, 2, 3, 4, 5])
|
|
3166
|
-
assert.strictEqual(total, 15)
|
|
3167
|
-
total = 0
|
|
3168
|
-
}),
|
|
3169
|
-
ThunkAssertion(forEach(addTotal), numbersGenerator(), result => {
|
|
3170
|
-
assert.deepEqual([...result], [])
|
|
3171
|
-
assert.strictEqual(total, 15)
|
|
3172
|
-
total = 0
|
|
3173
|
-
}),
|
|
3174
|
-
ThunkAssertion(forEach(async(addTotal)), numbersGenerator(), result => {
|
|
3175
|
-
assert.deepEqual([...result], [])
|
|
3176
|
-
assert.strictEqual(total, 15)
|
|
3177
|
-
total = 0
|
|
3178
|
-
}),
|
|
3179
|
-
ThunkAssertion(forEach(variadicAsyncAddTotal), numbersGenerator(), result => {
|
|
3180
|
-
assert.deepEqual([...result], [])
|
|
3181
|
-
assert.strictEqual(total, 15)
|
|
3182
|
-
total = 0
|
|
3183
|
-
}),
|
|
3184
|
-
ThunkAssertion(forEach(addTotal), numbersAsyncGenerator(), async result => {
|
|
3185
|
-
assert.deepEqual(await asyncIteratorToArray(result), [])
|
|
3186
|
-
assert.strictEqual(total, 15)
|
|
3187
|
-
total = 0
|
|
3188
|
-
}),
|
|
3189
|
-
ThunkAssertion(forEach(async(addTotal)), numbersAsyncGenerator(), async result => {
|
|
3190
|
-
assert.deepEqual(await asyncIteratorToArray(result), [])
|
|
3191
|
-
assert.strictEqual(total, 15)
|
|
3192
|
-
total = 0
|
|
3193
|
-
}),
|
|
3194
|
-
ThunkAssertion(forEach(variadicAsyncAddTotal), numbersAsyncGenerator(), async result => {
|
|
3195
|
-
assert.deepEqual(await asyncIteratorToArray(result), [])
|
|
3196
|
-
assert.strictEqual(total, 15)
|
|
3197
|
-
total = 0
|
|
3198
|
-
}),
|
|
3199
|
-
ThunkAssertion(forEach(addTotal), { a: 1, b: 2, c: 3, d: 4, e: 5 }, result => {
|
|
3200
|
-
assert.deepEqual(result, { a: 1, b: 2, c: 3, d: 4, e: 5 })
|
|
3201
|
-
assert.strictEqual(total, 15)
|
|
3202
|
-
total = 0
|
|
3203
|
-
}),
|
|
3204
|
-
ThunkAssertion(forEach(async(addTotal)), { a: 1, b: 2, c: 3, d: 4, e: 5 }, result => {
|
|
3205
|
-
assert.deepEqual(result, { a: 1, b: 2, c: 3, d: 4, e: 5 })
|
|
3206
|
-
assert.strictEqual(total, 15)
|
|
3207
|
-
total = 0
|
|
3208
|
-
}),
|
|
3209
|
-
ThunkAssertion(forEach(variadicAsyncAddTotal), { a: 1, b: 2, c: 3, d: 4, e: 5 }, result => {
|
|
3210
|
-
assert.deepEqual(result, { a: 1, b: 2, c: 3, d: 4, e: 5 })
|
|
3211
|
-
assert.strictEqual(total, 15)
|
|
3212
|
-
total = 0
|
|
3213
|
-
}),
|
|
3214
|
-
]
|
|
3215
|
-
|
|
3216
|
-
thunkAssertions.forEach(thunk => thunk())
|
|
3217
|
-
|
|
3218
|
-
it('forEach(noop)({ forEach: function })', async () => {
|
|
3219
|
-
const forEachable = {
|
|
3220
|
-
forEach() {
|
|
3221
|
-
return 1
|
|
3222
|
-
},
|
|
3223
|
-
}
|
|
3224
|
-
assert.strictEqual(forEach(noop)(forEachable), 1)
|
|
3225
|
-
})
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
const noop = function () {}
|
|
3229
|
-
it('forEach(noop)(1)', async () => {
|
|
3230
|
-
assert.strictEqual(forEach(noop)(1), 1)
|
|
3231
|
-
})
|
|
3232
|
-
it('forEach(noop)(null)', async () => {
|
|
3233
|
-
assert.strictEqual(forEach(noop)(null), null)
|
|
3234
|
-
})
|
|
3235
|
-
it('forEach(noop)(undefined)', async () => {
|
|
3236
|
-
assert.strictEqual(forEach(noop)(undefined), undefined)
|
|
3237
|
-
})
|
|
3238
|
-
it('forEach(noop)()', async () => {
|
|
3239
|
-
assert.strictEqual(forEach(noop)(), undefined)
|
|
3240
|
-
})
|
|
3241
|
-
|
|
3242
|
-
it('execute a function for each item of a collection, returning the collection', async () => {
|
|
3243
|
-
let total = 0
|
|
3244
|
-
assert.deepEqual(
|
|
3245
|
-
forEach(number => total += number)([1, 2, 3]),
|
|
3246
|
-
[1, 2, 3],
|
|
3247
|
-
)
|
|
3248
|
-
assert.strictEqual(total, 6)
|
|
3249
|
-
})
|
|
3250
|
-
})
|
|
3251
|
-
})
|
|
3252
|
-
|
|
3253
|
-
describe('get', () => {
|
|
3254
|
-
const aaaaa = { a: { a: { a: { a: { a: 1 } } } } }
|
|
3255
|
-
const nested = [[[[[1]]]]]
|
|
3256
|
-
it('API coverage', async () => {
|
|
3257
|
-
ase(get({ a: 1 }, 'a'), 1)
|
|
3258
|
-
ase(await get(Promise.resolve({ a: 1 }), 'a'), 1)
|
|
3259
|
-
ase(get('a')({ a: 1 }), 1)
|
|
3260
|
-
})
|
|
3261
|
-
it('accesses a property of an object by name', async () => {
|
|
3262
|
-
ase(get('a')({ a: 1 }), 1)
|
|
3263
|
-
ase(get('b')({ a: 1 }), undefined)
|
|
3264
|
-
ase(get('b', 0)({ a: 1 }), 0)
|
|
3265
|
-
ase(get(0)([1]), 1)
|
|
3266
|
-
ase(get(1)([1]), undefined)
|
|
3267
|
-
ase(get(1, 0)([1]), 0)
|
|
3268
|
-
})
|
|
3269
|
-
it('accesses a property by array[index]', async () => {
|
|
3270
|
-
ase(get('[0][0][0][0][0]')(nested), 1)
|
|
3271
|
-
ase(get('[0][0][0][0][0')(nested), 1)
|
|
3272
|
-
ase(get('0][0][0][0][0]')(nested), 1)
|
|
3273
|
-
ase(get('0][0][0][0][0')(nested), 1)
|
|
3274
|
-
})
|
|
3275
|
-
it('accesses a property of an object by dot notation', async () => {
|
|
3276
|
-
ase(get('a.a.a.a.a')(aaaaa), 1)
|
|
3277
|
-
ase(get('a.a.a.a.b')(aaaaa), undefined)
|
|
3278
|
-
ase(get('a.a.a.a.b', 0)(aaaaa), 0)
|
|
3279
|
-
ase(get('0.0.0.0.0')(nested), 1)
|
|
3280
|
-
ase(get('0.0.0.0.1')(nested), undefined)
|
|
3281
|
-
ase(get('0.0.0.0.1', 0)(nested), 0)
|
|
3282
|
-
})
|
|
3283
|
-
it('accesses a property of an object by array', async () => {
|
|
3284
|
-
ase(get(['a', 'a', 'a', 'a', 'a'])(aaaaa), 1)
|
|
3285
|
-
ase(get(['a', 'a', 'a', 'a', 'b'])(aaaaa), undefined)
|
|
3286
|
-
ase(get(['a', 'a', 'a', 'a', 'b'], 0)(aaaaa), 0)
|
|
3287
|
-
ase(get([0, 0, 0, 0, 0])(nested), 1)
|
|
3288
|
-
ase(get([0, 0, 0, 0, 1])(nested), undefined)
|
|
3289
|
-
ase(get([0, 0, 0, 0, 1], 0)(nested), 0)
|
|
3290
|
-
ase(get(['a', 0])({ a: [1] }), 1)
|
|
3291
|
-
})
|
|
3292
|
-
it('handles null and undefined initial value with default or undefined', async () => {
|
|
3293
|
-
ase(get('a')(null), undefined)
|
|
3294
|
-
ase(get('a', () => 'yo')(null), 'yo')
|
|
3295
|
-
ase(get('a', 'yo')(null), 'yo')
|
|
3296
|
-
ase(get('a', 'yo')(undefined), 'yo')
|
|
3297
|
-
})
|
|
3298
|
-
it('defaultValue can be a function', async () => {
|
|
3299
|
-
ase(get('a', obj => obj.b)({ b: 1 }), 1)
|
|
3300
|
-
ase(get('a', obj => obj.b)({}), undefined)
|
|
3301
|
-
})
|
|
3302
|
-
it('clears cache at size 500', async () => {
|
|
3303
|
-
})
|
|
3304
|
-
})
|
|
3305
|
-
|
|
3306
|
-
describe('set', () => {
|
|
3307
|
-
it('set a property of an object', async () => {
|
|
3308
|
-
ade(set('a', 1)(null), null)
|
|
3309
|
-
ade(set('a', 1)(undefined), undefined)
|
|
3310
|
-
ade(set('a', 1)('yo'), 'yo')
|
|
3311
|
-
ade(set('a', 1)({ b: 2 }), { a: 1, b: 2 })
|
|
3312
|
-
ade(await set('a', Promise.resolve(1))({ b: 2 }), { a: 1, b: 2 })
|
|
3313
|
-
ade(set('a.b', 1)({ a: { c: 2 } }), { a: { b: 1, c: 2 } })
|
|
3314
|
-
ade(set('a.b', 1)({ a: 1 }), { a: { b: 1 } })
|
|
3315
|
-
ade(set(['a', 'b'], 1)({ a: { c: 2 } }), { a: { b: 1, c: 2 } })
|
|
3316
|
-
ade(set('a[0].b.c', 4)({ 'a': [{ 'b': { 'c': 3 } }] }), { 'a': [{ 'b': { 'c': 4 } }] })
|
|
3317
|
-
ade(set('a.b.c.d', 1)({}), { a: { b: { c: { d: 1 } } } })
|
|
3318
|
-
})
|
|
3319
|
-
|
|
3320
|
-
it('eagerly set a property of an object', async () => {
|
|
3321
|
-
ade(set(null, 'a', 1), null)
|
|
3322
|
-
ade(set(undefined, 'a', 1), undefined)
|
|
3323
|
-
ade(set('yo', 'a', 1), 'yo')
|
|
3324
|
-
ade(set({ b: 2 }, 'a', 1), { a: 1, b: 2 })
|
|
3325
|
-
ade(set({ a: { c: 2 } }, 'a.b', 1), { a: { b: 1, c: 2 } })
|
|
3326
|
-
ade(set({ a: 1 }, 'a.b', 1), { a: { b: 1 } })
|
|
3327
|
-
ade(set({ a: { c: 2 } }, ['a', 'b'], 1), { a: { b: 1, c: 2 } })
|
|
3328
|
-
ade(await set(Promise.resolve({ 'a': [{ 'b': { 'c': 3 } }] }), 'a[0].b.c', 4), { 'a': [{ 'b': { 'c': 4 } }] })
|
|
3329
|
-
ade(await set({ 'a': [{ 'b': { 'c': 3 } }] }, 'a[0].b.c', Promise.resolve(4)), { 'a': [{ 'b': { 'c': 4 } }] })
|
|
3330
|
-
ade(await set(Promise.resolve({}), 'a.b.c.d', 1), { a: { b: { c: { d: 1 } } } })
|
|
3331
|
-
ade(await set({}, 'a.b.c.d', Promise.resolve(1)), { a: { b: { c: { d: 1 } } } })
|
|
3332
|
-
})
|
|
3333
|
-
|
|
3334
|
-
it('the property value may be a resolver', async () => {
|
|
3335
|
-
{ // sync
|
|
3336
|
-
const myObj = { a: 1 }
|
|
3337
|
-
const myNewObj = set('b', obj => obj.a + 2)(myObj)
|
|
3338
|
-
assert.deepEqual(myNewObj, { a: 1, b: 3 })
|
|
3339
|
-
}
|
|
3340
|
-
|
|
3341
|
-
{ // async
|
|
3342
|
-
const myObj = { a: 1 }
|
|
3343
|
-
const myNewObj = await set('b', async obj => obj.a + 2)(myObj)
|
|
3344
|
-
assert.deepEqual(myNewObj, { a: 1, b: 3 })
|
|
3345
|
-
}
|
|
3346
|
-
})
|
|
3347
|
-
})
|
|
3348
|
-
|
|
3349
|
-
describe('pick', () => {
|
|
3350
|
-
const abc = { a: 1, b: 2, c: 3 }
|
|
3351
|
-
const nested = { a: { b: { c: { d: 1, e: [2, 3] } } } }
|
|
3352
|
-
|
|
3353
|
-
it('API coverage', async () => {
|
|
3354
|
-
ade(pick(abc, ['a']), { a: 1 })
|
|
3355
|
-
ade(pick(abc, ['a', 'd']), { a: 1 })
|
|
3356
|
-
ade(pick(abc, ['d']), {})
|
|
3357
|
-
ade(await pick(Promise.resolve(abc), ['a']), { a: 1 })
|
|
3358
|
-
ade(await pick(Promise.resolve(abc), ['a', 'd']), { a: 1 })
|
|
3359
|
-
ade(await pick(Promise.resolve(abc), ['d']), {})
|
|
3360
|
-
})
|
|
3361
|
-
it('picks properties off an object defined by array', async () => {
|
|
3362
|
-
ade(pick(['a'])(abc), { a: 1 })
|
|
3363
|
-
ade(pick(['a', 'd'])(abc), { a: 1 })
|
|
3364
|
-
ade(pick(['d'])(abc), {})
|
|
3365
|
-
ade(pick(['d'])(null), null)
|
|
3366
|
-
ade(pick(['d'])(undefined), undefined)
|
|
3367
|
-
ade(pick(['d'])(), undefined)
|
|
3368
|
-
})
|
|
3369
|
-
it('picks nested properties', async () => {
|
|
3370
|
-
assert.deepEqual(pick(['a.b.c.d'])(nested), { a: { b: { c: { d: 1 } } } })
|
|
3371
|
-
// assert.deepEqual(pick(['a.b.f.g'])(nested), nested)
|
|
3372
|
-
// assert.deepEqual(pick(['a.b.c.d', 'a.b.c.e[0]'])(nested), { a: { b: { c: { e: [, 3] } } } })
|
|
3373
|
-
// assert.deepEqual(pick(['a[0][0].d'])({ a: [[{ b: 1, c: 2, d: 3 }]] }), { a: [[{ b: 1, c: 2 }]] })
|
|
3374
|
-
// assert.deepEqual(pick(['a[0][0].d'])({ a: [[{ b: 1, c: 2, d: null }]] }), { a: [[{ b: 1, c: 2 }]] })
|
|
3375
|
-
})
|
|
3376
|
-
})
|
|
3377
|
-
|
|
3378
|
-
describe('omit', () => {
|
|
3379
|
-
const abc = { a: 1, b: 2, c: 3 }
|
|
3380
|
-
const nested = { a: { b: { c: { d: 1, e: [2, 3] } } } }
|
|
3381
|
-
|
|
3382
|
-
it('API coverage', async () => {
|
|
3383
|
-
ade(omit(nested, []), nested)
|
|
3384
|
-
ade(await omit(Promise.resolve(nested), []), nested)
|
|
3385
|
-
ade(omit([1, 2, 3], []), [1, 2, 3])
|
|
3386
|
-
ade(await omit(Promise.resolve([1, 2, 3]), []), [1, 2, 3])
|
|
3387
|
-
ade(omit(abc, ['a']), { b: 2, c: 3 })
|
|
3388
|
-
ade(omit(abc, ['a', 'd']), { b: 2, c: 3 })
|
|
3389
|
-
ade(omit(abc, ['d']), { a: 1, b: 2, c: 3 })
|
|
3390
|
-
})
|
|
3391
|
-
it('omits properties from an object defined by array', async () => {
|
|
3392
|
-
assert(omit([])(nested) !== nested)
|
|
3393
|
-
ade(omit([])(nested), nested)
|
|
3394
|
-
ade(omit([])([1, 2, 3]), [1, 2, 3])
|
|
3395
|
-
ade(omit(['a'])(abc), { b: 2, c: 3 })
|
|
3396
|
-
ade(omit(['a', 'd'])(abc), { b: 2, c: 3 })
|
|
3397
|
-
ade(omit(['d'])(abc), { a: 1, b: 2, c: 3 })
|
|
3398
|
-
ade(omit(['d'])({ d: null }), {})
|
|
3399
|
-
ase(omit(['d'])(null), null)
|
|
3400
|
-
ase(omit(['d'])(undefined), undefined)
|
|
3401
|
-
ase(omit(['d'])(), undefined)
|
|
3402
|
-
ase(omit([])(null), null)
|
|
3403
|
-
ase(omit([])(1), 1)
|
|
3404
|
-
})
|
|
3405
|
-
it('omits nested properties', async () => {
|
|
3406
|
-
assert.deepEqual(omit(['a.b.c.d'])(nested), { a: { b: { c: { e: [2, 3] } } } })
|
|
3407
|
-
assert.deepEqual(omit(['a.b.f.g'])(nested), nested)
|
|
3408
|
-
assert.deepEqual(omit(['a.b.c.d', 'a.b.c.e[0]'])(nested), { a: { b: { c: { e: [, 3] } } } })
|
|
3409
|
-
assert.deepEqual(omit(['a[0][0].d'])({ a: [[{ b: 1, c: 2, d: 3 }]] }), { a: [[{ b: 1, c: 2 }]] })
|
|
3410
|
-
assert.deepEqual(omit(['a[0][0].d'])({ a: [[{ b: 1, c: 2, d: null }]] }), { a: [[{ b: 1, c: 2 }]] })
|
|
3411
|
-
})
|
|
3412
|
-
it('more nested',
|
|
3413
|
-
Test(
|
|
3414
|
-
'omit nested',
|
|
3415
|
-
omit(['a.b.c.e[0]']))
|
|
3416
|
-
.case({
|
|
3417
|
-
a: {
|
|
3418
|
-
b: {
|
|
3419
|
-
c: {
|
|
3420
|
-
d: 1,
|
|
3421
|
-
e: [2, 3],
|
|
3422
|
-
},
|
|
3423
|
-
}
|
|
3424
|
-
},
|
|
3425
|
-
}, {
|
|
3426
|
-
a: {
|
|
3427
|
-
b: {
|
|
3428
|
-
c: {
|
|
3429
|
-
d: 1,
|
|
3430
|
-
e: [, 3],
|
|
3431
|
-
},
|
|
3432
|
-
}
|
|
3433
|
-
},
|
|
3434
|
-
})
|
|
3435
|
-
.case(null, null)
|
|
3436
|
-
.case({}, {})
|
|
3437
|
-
.case([], []))
|
|
3438
|
-
})
|
|
3439
|
-
|
|
3440
|
-
describe('some', () => {
|
|
3441
|
-
it('API coverage', async () => {
|
|
3442
|
-
ase(
|
|
3443
|
-
some([1, 2, 3, 4, 5], number => number > 3),
|
|
3444
|
-
true,
|
|
3445
|
-
)
|
|
3446
|
-
|
|
3447
|
-
ase(
|
|
3448
|
-
await some(Promise.resolve([1, 2, 3, 4, 5]), number => number > 3),
|
|
3449
|
-
true,
|
|
3450
|
-
)
|
|
3451
|
-
})
|
|
3452
|
-
|
|
3453
|
-
const numbersArray = [1, 2, 3, 4, 5]
|
|
3454
|
-
const evenNumbersArray = [2, 4, 6, 8, 10]
|
|
3455
|
-
const numbersGenerator = function* () {
|
|
3456
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
3457
|
-
}
|
|
3458
|
-
const evenNumbersGenerator = function* () {
|
|
3459
|
-
yield 2; yield 4; yield 6; yield 8; yield 10
|
|
3460
|
-
}
|
|
3461
|
-
const asyncNumbersGenerator = async function* () {
|
|
3462
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
3463
|
-
}
|
|
3464
|
-
const asyncEvenNumbersGenerator = async function* () {
|
|
3465
|
-
yield 2; yield 4; yield 6; yield 8; yield 10
|
|
3466
|
-
}
|
|
3467
|
-
const isOdd = number => number % 2 == 1
|
|
3468
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
3469
|
-
const variadicAsyncIsEven = number => number % 2 == 0 ? Promise.resolve(true) : false
|
|
3470
|
-
|
|
3471
|
-
const cases = [
|
|
3472
|
-
[isOdd, [2, 3, 4], true],
|
|
3473
|
-
[async(isOdd), [2, 3, 4], true],
|
|
3474
|
-
[isOdd, new MockFoldable([2, 3, 4]), true],
|
|
3475
|
-
[isOdd, numbersArray, true],
|
|
3476
|
-
[isOdd, evenNumbersArray, false],
|
|
3477
|
-
[isOdd, new MockFoldable(numbersArray), true],
|
|
3478
|
-
[isOdd, new MockFoldable(evenNumbersArray), false],
|
|
3479
|
-
[isOdd, new AsyncMockFoldable(numbersArray), true],
|
|
3480
|
-
[isOdd, new AsyncMockFoldable(evenNumbersArray), false],
|
|
3481
|
-
[async(isOdd), new MockFoldable(numbersArray), true],
|
|
3482
|
-
[async(isOdd), new MockFoldable(evenNumbersArray), false],
|
|
3483
|
-
[async(isOdd), new AsyncMockFoldable(numbersArray), true],
|
|
3484
|
-
[async(isOdd), new AsyncMockFoldable(evenNumbersArray), false],
|
|
3485
|
-
[variadicAsyncIsOdd, numbersArray, true],
|
|
3486
|
-
[variadicAsyncIsOdd, evenNumbersArray, false],
|
|
3487
|
-
[async(isOdd), numbersArray, true],
|
|
3488
|
-
[async(isOdd), evenNumbersArray, false],
|
|
3489
|
-
[isOdd, numbersGenerator(), true],
|
|
3490
|
-
[isOdd, evenNumbersGenerator(), false],
|
|
3491
|
-
[async(isOdd), numbersGenerator(), true],
|
|
3492
|
-
[async(isOdd), evenNumbersGenerator(), false],
|
|
3493
|
-
[isOdd, asyncNumbersGenerator(), true],
|
|
3494
|
-
[isOdd, asyncEvenNumbersGenerator(), false],
|
|
3495
|
-
[async(isOdd), asyncNumbersGenerator(), true],
|
|
3496
|
-
[async(isOdd), asyncEvenNumbersGenerator(), false],
|
|
3497
|
-
[variadicAsyncIsOdd, asyncNumbersGenerator(), true],
|
|
3498
|
-
[variadicAsyncIsOdd, asyncEvenNumbersGenerator(), false],
|
|
3499
|
-
[isOdd, asyncNumbersGenerator(), true],
|
|
3500
|
-
[isOdd, asyncEvenNumbersGenerator(), false],
|
|
3501
|
-
[async(isOdd), asyncNumbersGenerator(), true],
|
|
3502
|
-
[async(isOdd), asyncEvenNumbersGenerator(), false],
|
|
3503
|
-
[variadicAsyncIsOdd, asyncNumbersGenerator(), true],
|
|
3504
|
-
[variadicAsyncIsOdd, asyncEvenNumbersGenerator(), false],
|
|
3505
|
-
]
|
|
3506
|
-
|
|
3507
|
-
cases.forEach(([func, value, result, asserter = assert.strictEqual]) => {
|
|
3508
|
-
it(`${func.name}(${JSON.stringify(value)}) -> ${result}`, async () => {
|
|
3509
|
-
asserter(await some(func)(value), result)
|
|
3510
|
-
})
|
|
3511
|
-
})
|
|
3512
|
-
|
|
3513
|
-
it('max concurrency 20', async () => {
|
|
3514
|
-
const asyncRange = async function* (to) {
|
|
3515
|
-
for (let index = 1; index < to; index++) {
|
|
3516
|
-
yield index
|
|
3517
|
-
}
|
|
3518
|
-
}
|
|
3519
|
-
let concurrencyCount = 0,
|
|
3520
|
-
maxConcurrencyCount = 0
|
|
3521
|
-
assert.strictEqual(await some(async number => {
|
|
3522
|
-
concurrencyCount += 1
|
|
3523
|
-
maxConcurrencyCount = Math.max(maxConcurrencyCount, concurrencyCount)
|
|
3524
|
-
await sleep(10)
|
|
3525
|
-
concurrencyCount -= 1
|
|
3526
|
-
return false
|
|
3527
|
-
})(asyncRange(40)), false)
|
|
3528
|
-
assert.strictEqual(concurrencyCount, 0)
|
|
3529
|
-
assert.strictEqual(maxConcurrencyCount, 20)
|
|
3530
|
-
assert.strictEqual(await some(async number => {
|
|
3531
|
-
maxConcurrencyCount = Math.max(maxConcurrencyCount, concurrencyCount)
|
|
3532
|
-
await sleep(10)
|
|
3533
|
-
if (number == 19) {
|
|
3534
|
-
return true
|
|
3535
|
-
}
|
|
3536
|
-
return false
|
|
3537
|
-
})(asyncRange(40)), true)
|
|
3538
|
-
})
|
|
3539
|
-
|
|
3540
|
-
it('1', async () => {
|
|
3541
|
-
assert.strictEqual(some(() => true)(1), true)
|
|
3542
|
-
})
|
|
3543
|
-
it('null', async () => {
|
|
3544
|
-
assert.strictEqual(some(() => true)(null), true)
|
|
3545
|
-
})
|
|
3546
|
-
it('undefined', async () => {
|
|
3547
|
-
assert.strictEqual(some(() => true)(undefined), true)
|
|
3548
|
-
assert.strictEqual(some(() => true)(), true)
|
|
3549
|
-
})
|
|
3550
|
-
})
|
|
3551
|
-
|
|
3552
|
-
describe('some - v1.5.15 regression', () => {
|
|
3553
|
-
const numbers = [1, 2, 3, 4, 5]
|
|
3554
|
-
const numbersObject = { a: 1, b: 2, c: 3, d: 4, e: 5 }
|
|
3555
|
-
it('[sync] tests fn against all items of iterable, true if any evaluation is truthy', async () => {
|
|
3556
|
-
ase(some(x => x > 5)(numbers), false)
|
|
3557
|
-
ase(some(x => x > 0)(numbers), true)
|
|
3558
|
-
ase(some(x => x > 5)(new Set(numbers)), false)
|
|
3559
|
-
ase(some(x => x > 0)(new Set(numbers)), true)
|
|
3560
|
-
ase(some(x => x > 5)(numbersObject), false)
|
|
3561
|
-
ase(some(x => x > 0)(numbersObject), true)
|
|
3562
|
-
})
|
|
3563
|
-
it('[async] tests fn against all items of iterable, true if any evaluation is truthy', async () => {
|
|
3564
|
-
aok(some(async x => x > 5)(numbers) instanceof Promise)
|
|
3565
|
-
ase(await some(async x => x > 5)(numbers), false)
|
|
3566
|
-
ase(await some(async x => x > 0)(numbers), true)
|
|
3567
|
-
ase(await some(async x => x > 5)(new Set(numbers)), false)
|
|
3568
|
-
ase(await some(async x => x > 0)(new Set(numbers)), true)
|
|
3569
|
-
ase(await some(async x => x > 5)(numbersObject), false)
|
|
3570
|
-
ase(await some(async x => x > 0)(numbersObject), true)
|
|
3571
|
-
})
|
|
3572
|
-
it('tests a variadic async function', async () => {
|
|
3573
|
-
ase(
|
|
3574
|
-
await some(x => x < 2 ? Promise.resolve(false) : true)([1, 2, 3, 4, 5]),
|
|
3575
|
-
true,
|
|
3576
|
-
)
|
|
3577
|
-
})
|
|
3578
|
-
})
|
|
3579
|
-
|
|
3580
|
-
describe('every', () => {
|
|
3581
|
-
it('API coverage', async () => {
|
|
3582
|
-
ase(
|
|
3583
|
-
every([1, 2, 3, 4, 5], number => number > 0),
|
|
3584
|
-
true,
|
|
3585
|
-
)
|
|
3586
|
-
|
|
3587
|
-
ase(
|
|
3588
|
-
await every(Promise.resolve([1, 2, 3, 4, 5]), number => number > 0),
|
|
3589
|
-
true,
|
|
3590
|
-
)
|
|
3591
|
-
})
|
|
3592
|
-
|
|
3593
|
-
const numbersArray = [1, 2, 3, 4, 5]
|
|
3594
|
-
const evenNumbersArray = [2, 4, 6, 8, 10]
|
|
3595
|
-
const numbersGenerator = function* () {
|
|
3596
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
3597
|
-
}
|
|
3598
|
-
const evenNumbersGenerator = function* () {
|
|
3599
|
-
yield 2; yield 4; yield 6; yield 8; yield 10
|
|
3600
|
-
}
|
|
3601
|
-
const asyncNumbersGenerator = async function* () {
|
|
3602
|
-
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
3603
|
-
}
|
|
3604
|
-
const asyncEvenNumbersGenerator = async function* () {
|
|
3605
|
-
yield 2; yield 4; yield 6; yield 8; yield 10
|
|
3606
|
-
}
|
|
3607
|
-
const isOdd = number => number % 2 == 1
|
|
3608
|
-
const isEven = number => number % 2 == 0
|
|
3609
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
3610
|
-
const variadicAsyncIsEven = number => number % 2 == 0 ? Promise.resolve(true) : false
|
|
3611
|
-
|
|
3612
|
-
const cases = [
|
|
3613
|
-
[isEven, numbersArray, false],
|
|
3614
|
-
[async(isEven), numbersArray, false],
|
|
3615
|
-
[isEven, evenNumbersArray, true],
|
|
3616
|
-
[isEven, new MockFoldable(numbersArray), false],
|
|
3617
|
-
[isEven, new MockFoldable(evenNumbersArray), true],
|
|
3618
|
-
[async(isEven), new MockFoldable(numbersArray), false],
|
|
3619
|
-
[async(isEven), new MockFoldable(evenNumbersArray), true],
|
|
3620
|
-
[async(isEven), evenNumbersArray, true],
|
|
3621
|
-
[variadicAsyncIsEven, numbersArray, false],
|
|
3622
|
-
[variadicAsyncIsEven, evenNumbersArray, true],
|
|
3623
|
-
[isEven, numbersGenerator(), false],
|
|
3624
|
-
[isEven, evenNumbersGenerator(), true],
|
|
3625
|
-
[async(isEven), numbersGenerator(), false],
|
|
3626
|
-
[async(isEven), evenNumbersGenerator(), true],
|
|
3627
|
-
[isEven, asyncNumbersGenerator(), false],
|
|
3628
|
-
[isEven, asyncEvenNumbersGenerator(), true],
|
|
3629
|
-
[async(isEven), asyncNumbersGenerator(), false],
|
|
3630
|
-
[async(isEven), asyncEvenNumbersGenerator(), true],
|
|
3631
|
-
[variadicAsyncIsEven, asyncNumbersGenerator(), false],
|
|
3632
|
-
[variadicAsyncIsEven, asyncEvenNumbersGenerator(), true],
|
|
3633
|
-
[isEven, asyncNumbersGenerator(), false],
|
|
3634
|
-
[isEven, asyncEvenNumbersGenerator(), true],
|
|
3635
|
-
[async(isEven), asyncNumbersGenerator(), false],
|
|
3636
|
-
[async(isEven), asyncEvenNumbersGenerator(), true],
|
|
3637
|
-
[variadicAsyncIsEven, asyncNumbersGenerator(), false],
|
|
3638
|
-
[variadicAsyncIsEven, asyncEvenNumbersGenerator(), true],
|
|
3639
|
-
]
|
|
3640
|
-
|
|
3641
|
-
cases.forEach(([func, value, result, asserter = assert.strictEqual]) => {
|
|
3642
|
-
it(`${func.name}(${JSON.stringify(value)}) -> ${result}`, async () => {
|
|
3643
|
-
asserter(await every(func)(value), result)
|
|
3644
|
-
})
|
|
3645
|
-
})
|
|
3646
|
-
|
|
3647
|
-
it('max concurrency 20', async () => {
|
|
3648
|
-
const asyncRange = async function* (to) {
|
|
3649
|
-
for (let index = 1; index < to; index++) {
|
|
3650
|
-
yield index
|
|
3651
|
-
}
|
|
3652
|
-
}
|
|
3653
|
-
let concurrencyCount = 0,
|
|
3654
|
-
maxConcurrencyCount = 0
|
|
3655
|
-
assert.strictEqual(await every(async number => {
|
|
3656
|
-
concurrencyCount += 1
|
|
3657
|
-
maxConcurrencyCount = Math.max(maxConcurrencyCount, concurrencyCount)
|
|
3658
|
-
await sleep(10)
|
|
3659
|
-
concurrencyCount -= 1
|
|
3660
|
-
return true
|
|
3661
|
-
})(asyncRange(40)), true)
|
|
3662
|
-
assert.strictEqual(concurrencyCount, 0)
|
|
3663
|
-
assert.strictEqual(maxConcurrencyCount, 20)
|
|
3664
|
-
assert.strictEqual(await every(async number => {
|
|
3665
|
-
maxConcurrencyCount = Math.max(maxConcurrencyCount, concurrencyCount)
|
|
3666
|
-
await sleep(10)
|
|
3667
|
-
if (number == 19) {
|
|
3668
|
-
return false
|
|
3669
|
-
}
|
|
3670
|
-
return true
|
|
3671
|
-
})(asyncRange(40)), false)
|
|
3672
|
-
})
|
|
3673
|
-
|
|
3674
|
-
it('1', async () => {
|
|
3675
|
-
assert.strictEqual(every(() => true)(1), true)
|
|
3676
|
-
})
|
|
3677
|
-
it('null', async () => {
|
|
3678
|
-
assert.strictEqual(every(() => true)(null), true)
|
|
3679
|
-
})
|
|
3680
|
-
it('undefined', async () => {
|
|
3681
|
-
assert.strictEqual(every(() => true)(undefined), true)
|
|
3682
|
-
assert.strictEqual(every(() => true)(), true)
|
|
3683
|
-
})
|
|
3684
|
-
})
|
|
3685
|
-
|
|
3686
|
-
describe('every - v1.5.15 regression', () => {
|
|
3687
|
-
const numbers = [1, 2, 3, 4, 5]
|
|
3688
|
-
const numbersObject = { a: 1, b: 2, c: 3, d: 4, e: 5 }
|
|
3689
|
-
it('syncly evaluates fn against all items in iterable, true if all evaluations are truthy', async () => {
|
|
3690
|
-
ase(every(x => x > 5)(numbers), false)
|
|
3691
|
-
ase(every(x => x > 0)(numbers), true)
|
|
3692
|
-
ase(every(x => x > 5)(new Set(numbers)), false)
|
|
3693
|
-
ase(every(x => x > 0)(new Set(numbers)), true)
|
|
3694
|
-
ase(every(x => x > 5)(numbersObject), false)
|
|
3695
|
-
ase(every(x => x > 0)(numbersObject), true)
|
|
3696
|
-
})
|
|
3697
|
-
it('asyncly evaluates fn against all items in iterable, true if all evaluations are truthy', async () => {
|
|
3698
|
-
aok(every(async x => x > 5)(numbers) instanceof Promise)
|
|
3699
|
-
ase(await every(async x => x > 5)(numbers), false)
|
|
3700
|
-
ase(await every(async x => x > 0)(numbers), true)
|
|
3701
|
-
ase(await every(async x => x > 5)(new Set(numbers)), false)
|
|
3702
|
-
ase(await every(async x => x > 0)(new Set(numbers)), true)
|
|
3703
|
-
ase(await every(async x => x > 5)(numbersObject), false)
|
|
3704
|
-
ase(await every(async x => x > 0)(numbersObject), true)
|
|
3705
|
-
})
|
|
3706
|
-
it('tests a variadic async function', async () => {
|
|
3707
|
-
ase(
|
|
3708
|
-
await every(x => x < 2 ? Promise.resolve(true) : false)([1, 2, 3, 4, 5]),
|
|
3709
|
-
false,
|
|
3710
|
-
)
|
|
3711
|
-
})
|
|
3712
|
-
})
|
|
3713
|
-
|
|
3714
|
-
describe('not', () => {
|
|
3715
|
-
it('API coverage', async () => {
|
|
3716
|
-
ase(
|
|
3717
|
-
not(1, 2, 3, (...numbers) => (
|
|
3718
|
-
numbers.every(num => typeof num == 'number')
|
|
3719
|
-
)),
|
|
3720
|
-
false,
|
|
3721
|
-
)
|
|
3722
|
-
|
|
3723
|
-
ase(
|
|
3724
|
-
await not(Promise.resolve(1), 2, Promise.resolve(3), (...numbers) => (
|
|
3725
|
-
numbers.every(num => typeof num == 'number')
|
|
3726
|
-
)),
|
|
3727
|
-
false,
|
|
3728
|
-
)
|
|
3729
|
-
})
|
|
3730
|
-
it('not(someValue) -> !someValue', async () => {
|
|
3731
|
-
assert.strictEqual(not(false), true)
|
|
3732
|
-
assert.strictEqual(not(null), true)
|
|
3733
|
-
assert.strictEqual(not(1), false)
|
|
3734
|
-
assert.strictEqual(not(0), true)
|
|
3735
|
-
})
|
|
3736
|
-
it('[sync] not(isOdd)(x) === !isOdd(x)', async () => {
|
|
3737
|
-
ase(not(isOdd)(2), true)
|
|
3738
|
-
ase(not(isOdd)(1), false)
|
|
3739
|
-
})
|
|
3740
|
-
it('[async] not(isEven)(x) === !(await isEven(x))', async () => {
|
|
3741
|
-
aok(not(asyncIsEven)(2) instanceof Promise)
|
|
3742
|
-
ase(await not(asyncIsEven)(2), false)
|
|
3743
|
-
ase(await not(asyncIsEven)(1), true)
|
|
3744
|
-
})
|
|
3745
|
-
})
|
|
3746
|
-
|
|
3747
|
-
describe('and', () => {
|
|
3748
|
-
it('API coverage', async () => {
|
|
3749
|
-
aok(
|
|
3750
|
-
and(1, 2, 3, [
|
|
3751
|
-
(...numbers) => numbers.every(num => typeof num == 'number'),
|
|
3752
|
-
(...numbers) => numbers.every(num => num < 5),
|
|
3753
|
-
(...numbers) => numbers.every(num => num > 0),
|
|
3754
|
-
])
|
|
3755
|
-
)
|
|
3756
|
-
|
|
3757
|
-
aok(
|
|
3758
|
-
await and(Promise.resolve(1), 2, Promise.resolve(3), [
|
|
3759
|
-
(...numbers) => numbers.every(num => typeof num == 'number'),
|
|
3760
|
-
(...numbers) => numbers.every(num => num < 5),
|
|
3761
|
-
(...numbers) => numbers.every(num => num > 0),
|
|
3762
|
-
])
|
|
3763
|
-
)
|
|
3764
|
-
})
|
|
3765
|
-
|
|
3766
|
-
it('all nonfunctions', async () => {
|
|
3767
|
-
assert.strictEqual(and([true, true, true]), true)
|
|
3768
|
-
assert.strictEqual(and([true, true, false]), false)
|
|
3769
|
-
assert.strictEqual(and([false, true, true]), false)
|
|
3770
|
-
assert.strictEqual(await and([Promise.resolve(true), true, true]), true)
|
|
3771
|
-
assert.strictEqual(await and([true, true, Promise.resolve(false)]), false)
|
|
3772
|
-
assert.strictEqual(await and([Promise.resolve(false), true, true]), false)
|
|
3773
|
-
})
|
|
3774
|
-
|
|
3775
|
-
it('predicates', async () => {
|
|
3776
|
-
const isOdd = number => number % 2 == 1
|
|
3777
|
-
const isEven = number => number % 2 == 0
|
|
3778
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
3779
|
-
assert.strictEqual(and([isOdd, isOdd, isOdd, true])(3), true)
|
|
3780
|
-
assert.strictEqual(and([isOdd, isOdd, isOdd])(2), false)
|
|
3781
|
-
assert.strictEqual(and([isEven, isEven, isOdd])(2), false)
|
|
3782
|
-
assert.strictEqual(await and([async(isOdd), isOdd, isOdd, true])(3), true)
|
|
3783
|
-
assert.strictEqual(await and([isOdd, async(isOdd), isOdd])(3), true)
|
|
3784
|
-
assert.strictEqual(await and([isOdd, isOdd, async(isOdd)])(3), true)
|
|
3785
|
-
assert.strictEqual(await and([async(isOdd), async(isOdd), async(isOdd)])(3), true)
|
|
3786
|
-
assert.strictEqual(await and([variadicAsyncIsOdd, variadicAsyncIsOdd, variadicAsyncIsOdd])(3), true)
|
|
3787
|
-
})
|
|
3788
|
-
|
|
3789
|
-
it('0 predicates true', async () => {
|
|
3790
|
-
assert.strictEqual(and([]), true)
|
|
3791
|
-
})
|
|
3792
|
-
|
|
3793
|
-
it('null', async () => {
|
|
3794
|
-
assert.strictEqual(and([isOdd, isOdd, isOdd])(null), false)
|
|
3795
|
-
})
|
|
3796
|
-
|
|
3797
|
-
it('undefined', async () => {
|
|
3798
|
-
assert.strictEqual(and([isOdd, isOdd, isOdd])(undefined), false)
|
|
3799
|
-
assert.strictEqual(and([isOdd, isOdd, isOdd])(), false)
|
|
3800
|
-
})
|
|
3801
|
-
|
|
3802
|
-
const isGreaterThan1 = x => x > 1
|
|
3803
|
-
it('sync tests input against provided array of functions, true if all evaluations are truthy', async () => {
|
|
3804
|
-
ase(and([isOdd, isGreaterThan1])(3), true)
|
|
3805
|
-
ase(and([isOdd, isGreaterThan1])(1), false)
|
|
3806
|
-
})
|
|
3807
|
-
|
|
3808
|
-
it('async tests input against provided array of functions, true if all evaluations are truthy', async () => {
|
|
3809
|
-
aok(and([asyncIsEven, isGreaterThan1])(2) instanceof Promise)
|
|
3810
|
-
ase(await and([asyncIsEven, isGreaterThan1])(2), true)
|
|
3811
|
-
ase(await and([asyncIsEven, isGreaterThan1])(0), false)
|
|
3812
|
-
})
|
|
3813
|
-
})
|
|
3814
|
-
|
|
3815
|
-
describe('or', () => {
|
|
3816
|
-
it('API coverage', async () => {
|
|
3817
|
-
aok(
|
|
3818
|
-
or(1, 2, 3, [
|
|
3819
|
-
(...numbers) => numbers.every(num => typeof num == 'number'),
|
|
3820
|
-
false,
|
|
3821
|
-
false,
|
|
3822
|
-
])
|
|
3823
|
-
)
|
|
3824
|
-
|
|
3825
|
-
aok(
|
|
3826
|
-
await or(Promise.resolve(1), 2, Promise.resolve(3), [
|
|
3827
|
-
(...numbers) => numbers.every(num => typeof num == 'number'),
|
|
3828
|
-
false,
|
|
3829
|
-
false,
|
|
3830
|
-
])
|
|
3831
|
-
)
|
|
3832
|
-
})
|
|
3833
|
-
|
|
3834
|
-
it('all nonfunctions', async () => {
|
|
3835
|
-
assert.strictEqual(or([true, true, true]), true)
|
|
3836
|
-
assert.strictEqual(or([false, false, false]), false)
|
|
3837
|
-
assert.strictEqual(or([false, false, true]), true)
|
|
3838
|
-
assert.strictEqual(await or([true, true, Promise.resolve(true)]), true)
|
|
3839
|
-
assert.strictEqual(await or([Promise.resolve(false), false, false]), false)
|
|
3840
|
-
assert.strictEqual(await or([false, false, Promise.resolve(true)]), true)
|
|
3841
|
-
})
|
|
3842
|
-
|
|
3843
|
-
const isOdd = number => number % 2 == 1
|
|
3844
|
-
const isEven = number => number % 2 == 0
|
|
3845
|
-
const variadicAsyncIsOdd = number => number % 2 == 1 ? Promise.resolve(true) : false
|
|
3846
|
-
it('predicates', async () => {
|
|
3847
|
-
assert.strictEqual(or([isOdd, isEven, true])(0), true)
|
|
3848
|
-
assert.strictEqual(or([isOdd, isOdd, isOdd])(0), false)
|
|
3849
|
-
assert.strictEqual(or([isOdd, isOdd, isOdd])(1), true)
|
|
3850
|
-
assert.strictEqual(or([isOdd, isOdd, isOdd, false])(3955), true)
|
|
3851
|
-
assert.strictEqual(await or([isOdd, isEven])(0), true)
|
|
3852
|
-
assert.strictEqual(await or([isOdd, async(isOdd), false, async(isEven)])(0), true)
|
|
3853
|
-
assert.strictEqual(await or([false, variadicAsyncIsOdd, variadicAsyncIsOdd, variadicAsyncIsOdd])(3955), true)
|
|
3854
|
-
assert.strictEqual(await or([variadicAsyncIsOdd, () => false, () => false])(3955), true)
|
|
3855
|
-
assert.strictEqual(await or([variadicAsyncIsOdd, () => false, () => false])(3956), false)
|
|
3856
|
-
assert.strictEqual(await or([() => false, variadicAsyncIsOdd, () => false])(3955), true)
|
|
3857
|
-
assert.strictEqual(await or([() => false, variadicAsyncIsOdd, () => false])(3956), false)
|
|
3858
|
-
assert.strictEqual(await or([async(isOdd), isEven])(0), true)
|
|
3859
|
-
assert.strictEqual(await or([async(isOdd), isOdd, isOdd])(0), false)
|
|
3860
|
-
})
|
|
3861
|
-
})
|
|
3862
|
-
|
|
3863
|
-
describe('or - v1.5.15 regression', () => {
|
|
3864
|
-
const isGreaterThan1 = x => x > 1
|
|
3865
|
-
it('sync tests input against provided array of functions, true if any evaluations are truthy', async () => {
|
|
3866
|
-
ase(or([isOdd, isGreaterThan1])(3), true)
|
|
3867
|
-
ase(or([isOdd, isGreaterThan1])(0), false)
|
|
3868
|
-
})
|
|
3869
|
-
it('async tests input against provided array of functions, true if any evaluations are truthy', async () => {
|
|
3870
|
-
aok(or([asyncIsEven, isGreaterThan1])(2) instanceof Promise)
|
|
3871
|
-
ase(await or([asyncIsEven, isGreaterThan1])(2), true)
|
|
3872
|
-
ase(await or([asyncIsEven, isGreaterThan1])(1), false)
|
|
3873
|
-
})
|
|
3874
|
-
})
|
|
3875
|
-
|
|
3876
|
-
describe('eq', () => {
|
|
3877
|
-
it('API coverage', async () => {
|
|
3878
|
-
aok(eq(2, 2))
|
|
3879
|
-
aok(await eq(Promise.resolve(2), 2))
|
|
3880
|
-
aok(await eq(2, Promise.resolve(2)))
|
|
3881
|
-
aok(await eq(Promise.resolve(2), Promise.resolve(2)))
|
|
3882
|
-
aok(
|
|
3883
|
-
eq(2, number => number * 2, number => number + 2)
|
|
3884
|
-
)
|
|
3885
|
-
aok(
|
|
3886
|
-
eq(number => number * 2, number => number + 2)(2)
|
|
3887
|
-
)
|
|
3888
|
-
aok(
|
|
3889
|
-
await eq(Promise.resolve(2), number => number * 2, number => number + 2)
|
|
3890
|
-
)
|
|
3891
|
-
aok(eq(2, number => number * 2, 4))
|
|
3892
|
-
aok(await eq(Promise.resolve(2), number => number * 2, 4))
|
|
3893
|
-
aok(eq(2, 4, number => number ** 2))
|
|
3894
|
-
aok(await eq(Promise.resolve(2), 4, number => number ** 2))
|
|
3895
|
-
})
|
|
3896
|
-
|
|
3897
|
-
it('[sync] eq(f, g)(x) == (f(x) == g(x))', async () => {
|
|
3898
|
-
ase(eq(x => `${x}`, x => x)('hey'), true)
|
|
3899
|
-
ase(eq(x => `${x}`, x => x)(1), true)
|
|
3900
|
-
})
|
|
3901
|
-
it('[async] eq(f, g)(x) == (f(x) == g(x))', async () => {
|
|
3902
|
-
aok(eq(x => `${x}`, async x => x)('hey') instanceof Promise)
|
|
3903
|
-
ase(await eq(async x => `${x}`, async x => x)('hey'), true)
|
|
3904
|
-
ase(await eq(async x => `${x}`, async x => x)(1), true)
|
|
3905
|
-
})
|
|
3906
|
-
it('[sync] eq(f, value)(x) == (valueA == valueB)', async () => {
|
|
3907
|
-
ase(eq(() => 'hey', 'hey')('abc'), true)
|
|
3908
|
-
ase(eq(() => 'hey', 'ho')('abc'), false)
|
|
3909
|
-
})
|
|
3910
|
-
it('[async] eq(f, value)(x) == (valueA == valueB)', async () => {
|
|
3911
|
-
ase(await eq(async () => 'hey', 'hey')('abc'), true)
|
|
3912
|
-
ase(await eq('hey', async () => 'ho')('abc'), false)
|
|
3913
|
-
})
|
|
3914
|
-
it('[sync] eq(value, g)(x) == (valueA == valueB)', async () => {
|
|
3915
|
-
ase(eq('hey', () => 'hey')('abc'), true)
|
|
3916
|
-
ase(eq('hey', () => 'ho')('abc'), false)
|
|
3917
|
-
})
|
|
3918
|
-
it('[async] eq(value, g)(x) == (value == g(x))', async () => {
|
|
3919
|
-
aok(eq('hey', async x => x)('hey') instanceof Promise)
|
|
3920
|
-
ase(await eq('hey', async x => x)('hey'), true)
|
|
3921
|
-
ase(await eq(async x => x, 'hey')('hey'), true)
|
|
3922
|
-
ase(await eq('ho', async x => x)(1), false)
|
|
3923
|
-
})
|
|
3924
|
-
it('[sync] eq(valueA, valueB) == (valueA == valueB)', () => {
|
|
3925
|
-
ase(eq('hey', 'hey'), true)
|
|
3926
|
-
ase(eq('hey', 'ho'), false)
|
|
3927
|
-
})
|
|
3928
|
-
it('false for eq(string,)', () => {
|
|
3929
|
-
assert.strictEqual(eq('hey',), false)
|
|
3930
|
-
})
|
|
3931
|
-
})
|
|
3932
|
-
|
|
3933
|
-
describe('gt', () => {
|
|
3934
|
-
it('API coverage', async () => {
|
|
3935
|
-
aok(
|
|
3936
|
-
gt(1, number => number, number => number * 0)
|
|
3937
|
-
)
|
|
3938
|
-
aok(
|
|
3939
|
-
await gt(Promise.resolve(1), number => number, number => number * 0)
|
|
3940
|
-
)
|
|
3941
|
-
aok(gt(1, number => number, 0))
|
|
3942
|
-
aok(await gt(Promise.resolve(1), number => number, 0))
|
|
3943
|
-
aok(gt(1, 3, number => number * 0))
|
|
3944
|
-
aok(await gt(Promise.resolve(1), 3, number => number * 0))
|
|
3945
|
-
})
|
|
3946
|
-
it('[sync] gt(f, g)(x) === (f(x) > g(x))', async () => {
|
|
3947
|
-
ase(gt(x => x + 1, x => x)(1), true)
|
|
3948
|
-
ase(gt(x => x, x => x)(1), false)
|
|
3949
|
-
ase(gt(x => x, x => x + 1)(1), false)
|
|
3950
|
-
})
|
|
3951
|
-
it('[async] gt(f, g)(x) === (f(x) > g(x))', async () => {
|
|
3952
|
-
aok(gt(x => x + 1, async x => x)(1) instanceof Promise)
|
|
3953
|
-
ase(await gt(x => x + 1, async x => x)(1), true)
|
|
3954
|
-
ase(await gt(async x => x, x => x)(1), false)
|
|
3955
|
-
ase(await gt(async x => x, async x => x + 1)(1), false)
|
|
3956
|
-
})
|
|
3957
|
-
it('[sync] gt(f, value)(x) === (valueA > valueB)', async () => {
|
|
3958
|
-
ase(gt(() => 1, 0)('abc'), true)
|
|
3959
|
-
ase(gt(() => 1, 1)('abc'), false)
|
|
3960
|
-
ase(gt(() => 0, 1)('abc'), false)
|
|
3961
|
-
})
|
|
3962
|
-
it('[sync] gt(value, g)(x) === (valueA > valueB)', async () => {
|
|
3963
|
-
ase(gt(1, () => 0)('abc'), true)
|
|
3964
|
-
ase(gt(1, () => 1)('abc'), false)
|
|
3965
|
-
ase(gt(0, () => 1)('abc'), false)
|
|
3966
|
-
})
|
|
3967
|
-
it('[async] gt(value, g)(x) === (value > g(x))', async () => {
|
|
3968
|
-
aok(gt(1, async x => x)(2) instanceof Promise)
|
|
3969
|
-
ase(await gt(1, async x => x)(0), true)
|
|
3970
|
-
ase(await gt(1, async x => x)(1), false)
|
|
3971
|
-
ase(await gt(1, async x => x)(2), false)
|
|
3972
|
-
ase(await gt(async x => x, 0)(1), true)
|
|
3973
|
-
ase(await gt(async x => x, 1)(1), false)
|
|
3974
|
-
ase(await gt(async x => x, 2)(1), false)
|
|
3975
|
-
})
|
|
3976
|
-
it('[sync] gt(valueA, valueB) === (valueA > valueB)', () => {
|
|
3977
|
-
ase(gt(1, 0), true)
|
|
3978
|
-
ase(gt(1, 1), false)
|
|
3979
|
-
ase(gt(0, 1), false)
|
|
3980
|
-
})
|
|
3981
|
-
it('throws RangeError on not enough arguments', async () => {
|
|
3982
|
-
assert.strictEqual(gt('hey'), false)
|
|
3983
|
-
})
|
|
3984
|
-
})
|
|
3985
|
-
|
|
3986
|
-
describe('lt', () => {
|
|
3987
|
-
it('API coverage', async () => {
|
|
3988
|
-
aok(
|
|
3989
|
-
lt(5, number => number - 3, number => number + 3)
|
|
3990
|
-
)
|
|
3991
|
-
aok(
|
|
3992
|
-
await lt(Promise.resolve(5), number => number - 3, number => number + 3)
|
|
3993
|
-
)
|
|
3994
|
-
aok(lt(5, 2, number => number + 3))
|
|
3995
|
-
aok(await lt(Promise.resolve(5), 2, number => number + 3))
|
|
3996
|
-
aok(lt(5, number => number - 3, 5))
|
|
3997
|
-
aok(await lt(Promise.resolve(5), number => number - 3, 5))
|
|
3998
|
-
})
|
|
3999
|
-
it('[sync] lt(f, g)(x) === (f(x) < g(x))', async () => {
|
|
4000
|
-
ase(lt(x => x + 1, x => x)(1), false)
|
|
4001
|
-
ase(lt(x => x, x => x)(1), false)
|
|
4002
|
-
ase(lt(x => x, x => x + 1)(1), true)
|
|
4003
|
-
})
|
|
4004
|
-
it('[async] lt(f, g)(x) === (f(x) < g(x))', async () => {
|
|
4005
|
-
aok(lt(x => x + 1, async x => x)(1) instanceof Promise)
|
|
4006
|
-
ase(await lt(x => x + 1, async x => x)(1), false)
|
|
4007
|
-
ase(await lt(async x => x, x => x)(1), false)
|
|
4008
|
-
ase(await lt(async x => x, async x => x + 1)(1), true)
|
|
4009
|
-
})
|
|
4010
|
-
it('[sync] lt(f, value)(x) === (valueA < valueB)', async () => {
|
|
4011
|
-
ase(lt(() => 1, 0)('abc'), false)
|
|
4012
|
-
ase(lt(() => 1, 1)('abc'), false)
|
|
4013
|
-
ase(lt(() => 0, 1)('abc'), true)
|
|
4014
|
-
})
|
|
4015
|
-
it('[sync] lt(value, g)(x) === (valueA < valueB)', async () => {
|
|
4016
|
-
ase(lt(1, () => 0)('abc'), false)
|
|
4017
|
-
ase(lt(1, () => 1)('abc'), false)
|
|
4018
|
-
ase(lt(0, () => 1)('abc'), true)
|
|
4019
|
-
})
|
|
4020
|
-
it('[async] lt(value, g)(x) === (value < g(x))', async () => {
|
|
4021
|
-
aok(lt(1, async x => x)(2) instanceof Promise)
|
|
4022
|
-
ase(await lt(1, async x => x)(0), false)
|
|
4023
|
-
ase(await lt(1, async x => x)(1), false)
|
|
4024
|
-
ase(await lt(1, async x => x)(2), true)
|
|
4025
|
-
ase(await lt(async x => x, 0)(1), false)
|
|
4026
|
-
ase(await lt(async x => x, 1)(1), false)
|
|
4027
|
-
ase(await lt(async x => x, 2)(1), true)
|
|
4028
|
-
})
|
|
4029
|
-
it('[sync] lt(valueA, valueB) === (valueA < valueB)', () => {
|
|
4030
|
-
ase(lt(1, 0), false)
|
|
4031
|
-
ase(lt(1, 1), false)
|
|
4032
|
-
ase(lt(0, 1), true)
|
|
4033
|
-
})
|
|
4034
|
-
it('throws RangeError on not enough arguments', () => {
|
|
4035
|
-
assert.strictEqual(lt('hey'), false)
|
|
4036
|
-
})
|
|
4037
|
-
})
|
|
4038
|
-
|
|
4039
|
-
describe('gte', () => {
|
|
4040
|
-
it('API coverage', async () => {
|
|
4041
|
-
aok(
|
|
4042
|
-
gte(5, number => number + 3, number => number + 3)
|
|
4043
|
-
)
|
|
4044
|
-
aok(
|
|
4045
|
-
await gte(Promise.resolve(5), number => number + 3, number => number + 3)
|
|
4046
|
-
)
|
|
4047
|
-
aok(gte(5, 10, number => number + 3))
|
|
4048
|
-
aok(await gte(Promise.resolve(5), 10, number => number + 3))
|
|
4049
|
-
aok(gte(5, number => number + 3, 5))
|
|
4050
|
-
aok(await gte(Promise.resolve(5), number => number + 3, 5))
|
|
4051
|
-
})
|
|
4052
|
-
it('[sync] gte(f, g)(x) === (f(x) >= g(x))', async () => {
|
|
4053
|
-
ase(gte(x => x + 1, x => x)(1), true)
|
|
4054
|
-
ase(gte(x => x, x => x)(1), true)
|
|
4055
|
-
ase(gte(x => x, x => x + 1)(1), false)
|
|
4056
|
-
})
|
|
4057
|
-
it('[async] gte(f, g)(x) === (f(x) >= g(x))', async () => {
|
|
4058
|
-
aok(gte(x => x + 1, async x => x)(1) instanceof Promise)
|
|
4059
|
-
ase(await gte(x => x + 1, async x => x)(1), true)
|
|
4060
|
-
ase(await gte(async x => x, x => x)(1), true)
|
|
4061
|
-
ase(await gte(async x => x, async x => x + 1)(1), false)
|
|
4062
|
-
})
|
|
4063
|
-
it('[sync] gte(f, value)(x) === (valueA >= valueB)', async () => {
|
|
4064
|
-
ase(gte(() => 1, 0)('abc'), true)
|
|
4065
|
-
ase(gte(() => 1, 1)('abc'), true)
|
|
4066
|
-
ase(gte(() => 0, 1)('abc'), false)
|
|
4067
|
-
})
|
|
4068
|
-
it('[sync] gte(value, g)(x) === (valueA >= valueB)', async () => {
|
|
4069
|
-
ase(gte(1, () => 0)('abc'), true)
|
|
4070
|
-
ase(gte(1, () => 1)('abc'), true)
|
|
4071
|
-
ase(gte(0, () => 1)('abc'), false)
|
|
4072
|
-
})
|
|
4073
|
-
it('[async] gte(value, g)(x) === (value >= g(x))', async () => {
|
|
4074
|
-
aok(gte(1, async x => x)(2) instanceof Promise)
|
|
4075
|
-
ase(await gte(1, async x => x)(0), true)
|
|
4076
|
-
ase(await gte(1, async x => x)(1), true)
|
|
4077
|
-
ase(await gte(1, async x => x)(2), false)
|
|
4078
|
-
ase(await gte(async x => x, 0)(1), true)
|
|
4079
|
-
ase(await gte(async x => x, 1)(1), true)
|
|
4080
|
-
ase(await gte(async x => x, 2)(1), false)
|
|
4081
|
-
})
|
|
4082
|
-
it('[sync] gte(valueA, valueB)(x) === (valueA >= valueB)', () => {
|
|
4083
|
-
ase(gte(1, 0), true)
|
|
4084
|
-
ase(gte(1, 1), true)
|
|
4085
|
-
ase(gte(0, 1), false)
|
|
4086
|
-
})
|
|
4087
|
-
it('throws RangeError on not enough arguments', () => {
|
|
4088
|
-
assert.strictEqual(gte('hey'), false)
|
|
4089
|
-
})
|
|
4090
|
-
})
|
|
4091
|
-
|
|
4092
|
-
describe('lte', () => {
|
|
4093
|
-
it('API coverage', async () => {
|
|
4094
|
-
aok(
|
|
4095
|
-
lte(5, number => number + 3, number => number + 3)
|
|
4096
|
-
)
|
|
4097
|
-
aok(
|
|
4098
|
-
await lte(Promise.resolve(5), number => number + 3, number => number + 3)
|
|
4099
|
-
)
|
|
4100
|
-
aok(lte(5, 0, number => number + 3))
|
|
4101
|
-
aok(await lte(Promise.resolve(5), 0, number => number + 3))
|
|
4102
|
-
aok(lte(5, number => number + 3, 10))
|
|
4103
|
-
aok(await lte(Promise.resolve(5), number => number + 3, 10))
|
|
4104
|
-
})
|
|
4105
|
-
it('[sync] lte(f, g)(x) === (f(x) <= g(x))', async () => {
|
|
4106
|
-
ase(lte(x => x + 1, x => x)(1), false)
|
|
4107
|
-
ase(lte(x => x, x => x)(1), true)
|
|
4108
|
-
ase(lte(x => x, x => x + 1)(1), true)
|
|
4109
|
-
})
|
|
4110
|
-
it('[async] lte(f, g)(x) === (f(x) <= g(x))', async () => {
|
|
4111
|
-
aok(lte(x => x + 1, async x => x)(1) instanceof Promise)
|
|
4112
|
-
ase(await lte(x => x + 1, async x => x)(1), false)
|
|
4113
|
-
ase(await lte(async x => x, x => x)(1), true)
|
|
4114
|
-
ase(await lte(async x => x, async x => x + 1)(1), true)
|
|
4115
|
-
})
|
|
4116
|
-
it('[sync] lte(f, value)(x) === (valueA <= valueB)', async () => {
|
|
4117
|
-
ase(lte(() => 1, 0)('abc'), false)
|
|
4118
|
-
ase(lte(() => 1, 1)('abc'), true)
|
|
4119
|
-
ase(lte(() => 0, 1)('abc'), true)
|
|
4120
|
-
})
|
|
4121
|
-
it('[sync] lte(value, g)(x) === (valueA <= valueB)', async () => {
|
|
4122
|
-
ase(lte(1, () => 0)('abc'), false)
|
|
4123
|
-
ase(lte(1, () => 1)('abc'), true)
|
|
4124
|
-
ase(lte(0, () => 1)('abc'), true)
|
|
4125
|
-
})
|
|
4126
|
-
it('[async] lte(value, g)(x) === (value <= g(x))', async () => {
|
|
4127
|
-
aok(lte(1, async x => x)(2) instanceof Promise)
|
|
4128
|
-
ase(await lte(1, async x => x)(0), false)
|
|
4129
|
-
ase(await lte(1, async x => x)(1), true)
|
|
4130
|
-
ase(await lte(1, async x => x)(2), true)
|
|
4131
|
-
ase(await lte(async x => x, 0)(1), false)
|
|
4132
|
-
ase(await lte(async x => x, 1)(1), true)
|
|
4133
|
-
ase(await lte(async x => x, 2)(1), true)
|
|
4134
|
-
})
|
|
4135
|
-
it('[sync] lte(valueA, valueB)(x) === (valueA <= valueB)', () => {
|
|
4136
|
-
ase(lte(1, 0), false)
|
|
4137
|
-
ase(lte(1, 1), true)
|
|
4138
|
-
ase(lte(0, 1), true)
|
|
4139
|
-
})
|
|
4140
|
-
it('throws RangeError on not enough arguments', () => {
|
|
4141
|
-
assert.strictEqual(lte('hey'), false)
|
|
4142
|
-
})
|
|
4143
|
-
})
|
|
4144
|
-
|
|
4145
|
-
describe('thunkify', () => {
|
|
4146
|
-
const add2 = (a, b) => a + b
|
|
4147
|
-
const thunkAdd212 = thunkify(add2, 1, 2)
|
|
4148
|
-
it('creates a thunk', async () => {
|
|
4149
|
-
assert.strictEqual(thunkAdd212.length, 0)
|
|
4150
|
-
assert.strictEqual(thunkAdd212(), 3)
|
|
4151
|
-
})
|
|
4152
|
-
|
|
4153
|
-
const asyncThunkAdd212 = thunkify(add2, Promise.resolve(1), 2)
|
|
4154
|
-
it('creates a thunk that resolves any promise arguments', async () => {
|
|
4155
|
-
assert.strictEqual(asyncThunkAdd212.length, 0)
|
|
4156
|
-
assert.strictEqual(await asyncThunkAdd212(), 3)
|
|
4157
|
-
})
|
|
4158
|
-
})
|
|
4159
|
-
|
|
4160
|
-
describe('always', () => {
|
|
4161
|
-
it('always returns a value', async () => {
|
|
4162
|
-
assert.strictEqual(always(1)(), 1)
|
|
4163
|
-
assert.strictEqual(always(null)(), null)
|
|
4164
|
-
assert.strictEqual(always(NaN)(), NaN)
|
|
4165
|
-
})
|
|
4166
|
-
})
|
|
4167
|
-
|
|
4168
|
-
describe('curry', () => {
|
|
4169
|
-
const add3 = (a, b, c) => a + b + c
|
|
4170
|
-
const curriedAdd3 = curry(add3)
|
|
4171
|
-
it('round robin', async () => {
|
|
4172
|
-
assert.strictEqual(curry(add3, 'a', 'b', 'c'), 'abc')
|
|
4173
|
-
assert.strictEqual(curry(add3)('a', 'b', 'c'), 'abc')
|
|
4174
|
-
assert.strictEqual(curry(add3, 'a')('b', 'c'), 'abc')
|
|
4175
|
-
assert.strictEqual(curry(add3, 'a', 'b')('c'), 'abc')
|
|
4176
|
-
assert.strictEqual(curry(add3)('a', 'b')('c'), 'abc')
|
|
4177
|
-
assert.strictEqual(curry(add3)('a')('b', 'c'), 'abc')
|
|
4178
|
-
assert.strictEqual(curry(add3)('a')('b')('c'), 'abc')
|
|
4179
|
-
})
|
|
4180
|
-
|
|
4181
|
-
it('with placeholder __', async () => {
|
|
4182
|
-
assert.strictEqual(curry(add3, __, 'b', 'c')('a'), 'abc')
|
|
4183
|
-
assert.strictEqual(curry(add3, 'a', __, 'c')('b'), 'abc')
|
|
4184
|
-
assert.strictEqual(curry(add3, 'a', 'b', __)('c'), 'abc')
|
|
4185
|
-
assert.strictEqual(curry(add3, 'a', __, __)('b', 'c'), 'abc')
|
|
4186
|
-
assert.strictEqual(curry(add3, __, 'b', __)('a', 'c'), 'abc')
|
|
4187
|
-
assert.strictEqual(curry(add3, __, __, 'c')('a', 'b'), 'abc')
|
|
4188
|
-
assert.strictEqual(curry(add3, __, __, __)('a', 'b', 'c'), 'abc')
|
|
4189
|
-
assert.strictEqual(curry(add3, __, __, __)(__, __, __)('a', 'b', 'c'), 'abc')
|
|
4190
|
-
assert.strictEqual(curry(add3, __, __, __)(__, 'b', 'c')('a'), 'abc')
|
|
4191
|
-
assert.strictEqual(curry(add3, __, __, __)('a', __, 'c')('b'), 'abc')
|
|
4192
|
-
assert.strictEqual(curry(add3, __, __, __)('a', 'b', __)('c'), 'abc')
|
|
4193
|
-
assert.strictEqual(curry(add3)(__, __)(__)('a', 'b', __)('c'), 'abc')
|
|
4194
|
-
assert.strictEqual(curry(add3)(__)(__, __)('a')('b')(__)('c'), 'abc')
|
|
4195
|
-
assert.strictEqual(curry(add3)(__)(__)(__)('a', 'b', __)('c'), 'abc')
|
|
4196
|
-
assert.strictEqual(curry(add3)(__)(__)(__)('a')('b')(__)('c'), 'abc')
|
|
4197
|
-
})
|
|
4198
|
-
})
|
|
4199
|
-
|
|
4200
|
-
describe('curry.arity', () => {
|
|
4201
|
-
const add3 = (a, b, c) => a + b + c
|
|
4202
|
-
const curriedAdd3 = curry.arity(3, add3)
|
|
4203
|
-
it('round robin', async () => {
|
|
4204
|
-
assert.strictEqual(curry.arity(3, add3, 'a', 'b', 'c'), 'abc')
|
|
4205
|
-
assert.strictEqual(curry.arity(3, add3)('a', 'b', 'c'), 'abc')
|
|
4206
|
-
assert.strictEqual(curry.arity(3, add3, 'a')('b', 'c'), 'abc')
|
|
4207
|
-
assert.strictEqual(curry.arity(3, add3, 'a', 'b')('c'), 'abc')
|
|
4208
|
-
assert.strictEqual(curry.arity(3, add3)('a', 'b')('c'), 'abc')
|
|
4209
|
-
assert.strictEqual(curry.arity(3, add3)('a')('b', 'c'), 'abc')
|
|
4210
|
-
assert.strictEqual(curry.arity(3, add3)('a')('b')('c'), 'abc')
|
|
4211
|
-
})
|
|
4212
|
-
|
|
4213
|
-
it('with placeholder __', async () => {
|
|
4214
|
-
assert.strictEqual(curry.arity(3, add3, __, 'b', 'c')('a'), 'abc')
|
|
4215
|
-
assert.strictEqual(curry.arity(3, add3, 'a', __, 'c')('b'), 'abc')
|
|
4216
|
-
assert.strictEqual(curry.arity(3, add3, 'a', 'b', __)('c'), 'abc')
|
|
4217
|
-
assert.strictEqual(curry.arity(3, add3, 'a', __, __)('b', 'c'), 'abc')
|
|
4218
|
-
assert.strictEqual(curry.arity(3, add3, __, 'b', __)('a', 'c'), 'abc')
|
|
4219
|
-
assert.strictEqual(curry.arity(3, add3, __, __, 'c')('a', 'b'), 'abc')
|
|
4220
|
-
assert.strictEqual(curry.arity(3, add3, __, __, __)('a', 'b', 'c'), 'abc')
|
|
4221
|
-
assert.strictEqual(curry.arity(3, add3, __, __, __)(__, __, __)('a', 'b', 'c'), 'abc')
|
|
4222
|
-
assert.strictEqual(curry.arity(3, add3, __, __, __)(__, 'b', 'c')('a'), 'abc')
|
|
4223
|
-
assert.strictEqual(curry.arity(3, add3, __, __, __)('a', __, 'c')('b'), 'abc')
|
|
4224
|
-
assert.strictEqual(curry.arity(3, add3, __, __, __)('a', 'b', __)('c'), 'abc')
|
|
4225
|
-
assert.strictEqual(curry.arity(3, add3)(__, __)(__)('a', 'b', __)('c'), 'abc')
|
|
4226
|
-
assert.strictEqual(curry.arity(3, add3)(__)(__, __)('a')('b')(__)('c'), 'abc')
|
|
4227
|
-
assert.strictEqual(curry.arity(3, add3)(__)(__)(__)('a', 'b', __)('c'), 'abc')
|
|
4228
|
-
assert.strictEqual(curry.arity(3, add3)(__)(__)(__)('a', 'b', __)('c'), 'abc')
|
|
4229
|
-
assert.strictEqual(curry.arity(3, add3)(__)(__)(__)('a')('b')(__)('c'), 'abc')
|
|
4230
|
-
})
|
|
4231
|
-
})
|
|
4232
|
-
|
|
4233
|
-
describe('__', () => {
|
|
4234
|
-
it('is Symbol(placeholder)', async () => {
|
|
4235
|
-
assert.strictEqual(typeof __, 'symbol')
|
|
4236
|
-
})
|
|
4237
|
-
})
|
|
4238
|
-
|
|
4239
|
-
it('exports 31 functions', async () => {
|
|
4240
|
-
ase(Object.keys(rubico).length, 31)
|
|
4241
|
-
})
|
|
4242
|
-
})
|