rubico 2.2.0 → 2.2.2
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 +1 -3
- 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 +1 -1
- package/dist/rubico.es.min.js +1 -1
- package/dist/rubico.global.js +1 -1
- package/dist/rubico.global.min.js +1 -1
- package/dist/rubico.js +1 -1
- package/dist/rubico.min.js +1 -1
- package/dist/rubico.mjs +1 -1
- package/dist/set.es.js +1 -1
- package/dist/set.es.min.js +1 -1
- package/dist/set.js +1 -1
- package/dist/set.min.js +1 -1
- package/dist/set.mjs +1 -1
- package/dist/some.es.js +1 -1
- package/dist/some.es.min.js +1 -1
- package/dist/some.js +1 -1
- package/dist/some.min.js +1 -1
- package/dist/some.mjs +1 -1
- package/dist/switchCase.es.js +1 -1
- package/dist/switchCase.es.min.js +1 -1
- package/dist/switchCase.js +1 -1
- package/dist/switchCase.min.js +1 -1
- package/dist/switchCase.mjs +1 -1
- package/dist/tap.es.js +1 -1
- package/dist/tap.es.min.js +1 -1
- package/dist/tap.js +1 -1
- package/dist/tap.min.js +1 -1
- package/dist/tap.mjs +1 -1
- package/dist/thunkify.es.js +1 -1
- package/dist/thunkify.es.min.js +1 -1
- package/dist/thunkify.js +1 -1
- package/dist/thunkify.min.js +1 -1
- package/dist/thunkify.mjs +1 -1
- package/dist/transform.es.js +1 -1
- package/dist/transform.es.min.js +1 -1
- package/dist/transform.js +1 -1
- package/dist/transform.min.js +1 -1
- package/dist/transform.mjs +1 -1
- package/dist/tryCatch.es.js +1 -1
- package/dist/tryCatch.es.min.js +1 -1
- package/dist/tryCatch.js +1 -1
- package/dist/tryCatch.min.js +1 -1
- package/dist/tryCatch.mjs +1 -1
- package/dist/x/append.es.js +1 -1
- package/dist/x/append.es.min.js +1 -1
- package/dist/x/append.js +1 -1
- package/dist/x/append.min.js +1 -1
- package/dist/x/append.mjs +1 -1
- package/dist/x/callProp.es.js +1 -1
- package/dist/x/callProp.es.min.js +1 -1
- package/dist/x/callProp.js +1 -1
- package/dist/x/callProp.min.js +1 -1
- package/dist/x/callProp.mjs +1 -1
- package/dist/x/defaultsDeep.es.js +1 -1
- package/dist/x/defaultsDeep.es.min.js +1 -1
- package/dist/x/defaultsDeep.js +1 -1
- package/dist/x/defaultsDeep.min.js +1 -1
- package/dist/x/defaultsDeep.mjs +1 -1
- package/dist/x/differenceWith.es.js +1 -1
- package/dist/x/differenceWith.es.min.js +1 -1
- package/dist/x/differenceWith.js +1 -1
- package/dist/x/differenceWith.min.js +1 -1
- package/dist/x/differenceWith.mjs +1 -1
- package/dist/x/filterOut.es.js +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 +1 -1
- package/dist/x/trace.es.min.js +1 -1
- package/dist/x/trace.js +1 -1
- package/dist/x/trace.min.js +1 -1
- package/dist/x/trace.mjs +1 -1
- package/dist/x/unionWith.es.js +1 -1
- package/dist/x/unionWith.es.min.js +1 -1
- package/dist/x/unionWith.js +1 -1
- package/dist/x/unionWith.min.js +1 -1
- package/dist/x/unionWith.mjs +1 -1
- package/dist/x/uniq.es.js +1 -1
- package/dist/x/uniq.es.min.js +1 -1
- package/dist/x/uniq.js +1 -1
- package/dist/x/uniq.min.js +1 -1
- package/dist/x/uniq.mjs +1 -1
- package/dist/x/unless.es.js +1 -1
- package/dist/x/unless.es.min.js +1 -1
- package/dist/x/unless.js +1 -1
- package/dist/x/unless.min.js +1 -1
- package/dist/x/unless.mjs +1 -1
- package/dist/x/values.es.js +1 -1
- package/dist/x/values.es.min.js +1 -1
- package/dist/x/values.js +1 -1
- package/dist/x/values.min.js +1 -1
- package/dist/x/values.mjs +1 -1
- package/dist/x/when.es.js +1 -1
- package/dist/x/when.es.min.js +1 -1
- package/dist/x/when.js +1 -1
- package/dist/x/when.min.js +1 -1
- package/dist/x/when.mjs +1 -1
- package/eq.js +9 -0
- package/es.js +1 -1
- package/every.js +12 -3
- package/filter.js +3 -7
- package/flatMap.js +8 -7
- package/get.js +16 -0
- package/gt.js +9 -0
- package/gte.js +9 -0
- package/index.js +1 -1
- package/lt.js +9 -0
- package/lte.js +9 -0
- package/map.js +7 -14
- package/monad/AsyncPool.js +0 -0
- package/monad/BrokenPromise.js +63 -0
- package/monad/BrokenPromise.test.js +22 -0
- package/monad/Cancellable.js +84 -0
- package/monad/Cancellable.memoryUsage.js +60 -0
- package/monad/Cancellable.test.js +20 -0
- package/monad/Function.js +0 -0
- package/monad/Instance.benchmark.js +378 -0
- package/monad/Instance.js +351 -0
- package/monad/Instance.test.js +445 -0
- package/monad/Mux.benchmark.js +29 -0
- package/monad/Mux.js +589 -0
- package/monad/Mux.test.js +512 -0
- package/monad/PossiblePromise.benchmark.js +87 -0
- package/monad/PossiblePromise.js +144 -0
- package/monad/PossiblePromise.test.js +89 -0
- package/monad/README.md +75 -0
- package/monad/Reducer.js +0 -0
- package/monad/Sequence.benchmark.js +42 -0
- package/monad/Sequence.js +36 -0
- package/monad/SpecScript.js +0 -0
- package/monad/Stack.js +0 -0
- package/monad/Struct.benchmark.js +154 -0
- package/monad/Struct.js +249 -0
- package/monad/Struct.test.js +237 -0
- package/package.json +2 -1
- package/reduce.js +1 -0
- package/set.js +13 -4
- package/some.js +15 -4
- package/switchCase.js +21 -2
- package/transform.js +8 -8
package/monad/Struct.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/* rubico v1.5.0
|
|
2
|
+
* https://github.com/a-synchronous/rubico
|
|
3
|
+
* (c) 2019-2020 Richard Tong
|
|
4
|
+
* rubico may be freely distributed under the MIT license.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict'
|
|
8
|
+
|
|
9
|
+
const Instance = require('./Instance')
|
|
10
|
+
|
|
11
|
+
const { isArray, isObject, isSet, isMap } = Instance
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @synopsis
|
|
15
|
+
* isStruct(x any) -> boolean
|
|
16
|
+
*/
|
|
17
|
+
const isStruct = x => isArray(x) || isObject(x) || isSet(x) || isMap(x)
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @name Struct
|
|
21
|
+
*
|
|
22
|
+
* @synopsis
|
|
23
|
+
* new Struct(x Array|Object|Set|Map) -> Struct
|
|
24
|
+
*
|
|
25
|
+
* @catchphrase
|
|
26
|
+
* Finite data structure
|
|
27
|
+
*/
|
|
28
|
+
const Struct = function(x) {
|
|
29
|
+
if (!isStruct(x)) {
|
|
30
|
+
throw new TypeError(`cannot convert ${x} to Struct`)
|
|
31
|
+
}
|
|
32
|
+
this.value = x
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @name Struct.isStruct
|
|
37
|
+
*
|
|
38
|
+
* @synopsis
|
|
39
|
+
* Struct.isStruct(x any) -> boolean
|
|
40
|
+
*
|
|
41
|
+
* @catchphrase
|
|
42
|
+
* Tell if struct
|
|
43
|
+
*/
|
|
44
|
+
Struct.isStruct = isStruct
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @synopsis
|
|
48
|
+
* <T>objectEntriesGenerator(x Object<T>) -> Iterator<[key string, T]>
|
|
49
|
+
*/
|
|
50
|
+
const objectEntriesGenerator = function*(x) {
|
|
51
|
+
for (const k in x) {
|
|
52
|
+
yield [k, x[k]]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @name Struct.entries
|
|
58
|
+
*
|
|
59
|
+
* @synopsis
|
|
60
|
+
* <T any>Struct.entries(x Array<T>) -> Iterator<[index number, T]>
|
|
61
|
+
*
|
|
62
|
+
* <T any>Struct.entries(x Object<T>) -> Iterator<[key string, T]>
|
|
63
|
+
*
|
|
64
|
+
* <T any>Struct.entries(x Set<T>) -> Iterator<[T, T]>
|
|
65
|
+
*
|
|
66
|
+
* <A any, B any>Struct.entries(x Map<A, B>) -> Iterator<[A, B]>
|
|
67
|
+
*
|
|
68
|
+
* @catchphrase
|
|
69
|
+
* Get an iterator of key value pairs
|
|
70
|
+
*/
|
|
71
|
+
Struct.entries = x => isObject(x) ? objectEntriesGenerator(x) : x.entries()
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @synopsis
|
|
75
|
+
* <T>objectValuesGenerator(x Object<T>) -> Iterator<T>
|
|
76
|
+
*/
|
|
77
|
+
const objectValuesGenerator = function*(x) {
|
|
78
|
+
for (const k in x) {
|
|
79
|
+
yield x[k]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @name Struct.values
|
|
85
|
+
*
|
|
86
|
+
* @synopsis
|
|
87
|
+
* <T any>Struct.values(
|
|
88
|
+
* x Array<T>|Object<T>|Set<T>|Map<any, T>
|
|
89
|
+
* ) -> Iterator<T>
|
|
90
|
+
*
|
|
91
|
+
* @catchphrase
|
|
92
|
+
* Get an iterator of values
|
|
93
|
+
*/
|
|
94
|
+
Struct.values = x => isObject(x) ? objectValuesGenerator(x) : x.values()
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @name Struct.get
|
|
98
|
+
*
|
|
99
|
+
* @synopsis
|
|
100
|
+
* <T any>Struct.get(x Array<T>, index number) -> T|undefined
|
|
101
|
+
*
|
|
102
|
+
* <T any>Struct.get(x Object<T>, index string) -> T|undefined
|
|
103
|
+
*
|
|
104
|
+
* <T any>Struct.get(x Set<T>, index T) -> T|undefined
|
|
105
|
+
*
|
|
106
|
+
* <A any, B any>Struct.get(x Map<A, B>, index A) -> B|undefined
|
|
107
|
+
*
|
|
108
|
+
* @catchphrase
|
|
109
|
+
* Get an item by index
|
|
110
|
+
*/
|
|
111
|
+
Struct.get = (x, index) => {
|
|
112
|
+
if (typeof x.get == 'function') return x.get(index)
|
|
113
|
+
if (typeof x.has == 'function') return x.has(index) ? index : undefined
|
|
114
|
+
return x[index]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
Struct.get.ternary = (x, index) => (typeof x.get == 'function'
|
|
119
|
+
? x.get(index) : typeof x.has == 'function'
|
|
120
|
+
? x.has(index) ? index : undefined : x[index])
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @name Struct.set
|
|
125
|
+
*
|
|
126
|
+
* @synopsis
|
|
127
|
+
* Struct.set(x Array, value any, index number) -> mutated Array
|
|
128
|
+
*
|
|
129
|
+
* Struct.set(x Object, value any, index string) -> mutated Object
|
|
130
|
+
*
|
|
131
|
+
* Struct.set(x Set, value any) -> mutated Set
|
|
132
|
+
*
|
|
133
|
+
* Struct.set(x Map, value any, index any) -> mutated Map
|
|
134
|
+
*
|
|
135
|
+
* @catchphrase
|
|
136
|
+
* Set a value
|
|
137
|
+
*/
|
|
138
|
+
Struct.set = (x, value, index) => {
|
|
139
|
+
if (typeof x.set == 'function') return x.set(index, value)
|
|
140
|
+
if (typeof x.add == 'function') return x.add(value)
|
|
141
|
+
x[index] = value
|
|
142
|
+
return x
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @synopsis
|
|
147
|
+
* objectKeysCount(obj object) -> ct number
|
|
148
|
+
*/
|
|
149
|
+
const objectKeysCount = obj => {
|
|
150
|
+
let ct = 0
|
|
151
|
+
for (const _ in obj) ct += 1
|
|
152
|
+
return ct
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @name Struct.size
|
|
157
|
+
*
|
|
158
|
+
* @synopsis
|
|
159
|
+
* Struct.size(x Array|Object|Set|Map) -> number
|
|
160
|
+
*
|
|
161
|
+
* @catchphrase
|
|
162
|
+
* Count values
|
|
163
|
+
*/
|
|
164
|
+
Struct.size = x => {
|
|
165
|
+
if (isObject(x)) return objectKeysCount(x)
|
|
166
|
+
return 'size' in x ? x.size : x.length
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/*
|
|
170
|
+
Struct.size.objectKeys = x => {
|
|
171
|
+
if (isObject(x)) return Object.keys(x).length
|
|
172
|
+
return 'size' in x ? x.size : x.length
|
|
173
|
+
}
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @synopsis
|
|
178
|
+
* copySet(x Set) -> y Set
|
|
179
|
+
*/
|
|
180
|
+
const copySet = x => {
|
|
181
|
+
const y = new Set()
|
|
182
|
+
for (const item of x) y.add(item)
|
|
183
|
+
return y
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @synopsis
|
|
189
|
+
* copyMap(x Map) -> y Map
|
|
190
|
+
*/
|
|
191
|
+
const copyMap = x => {
|
|
192
|
+
const y = new Map()
|
|
193
|
+
for (const entry of x) y.set(...entry)
|
|
194
|
+
return y
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @name Struct.copy
|
|
199
|
+
*
|
|
200
|
+
* @synopsis
|
|
201
|
+
* Struct.copy(x Array) -> copied Array
|
|
202
|
+
*
|
|
203
|
+
* Struct.copy(x Object) -> copied Object
|
|
204
|
+
*
|
|
205
|
+
* Struct.copy(x Set) -> copied Set
|
|
206
|
+
*
|
|
207
|
+
* Struct.copy(x Map) -> copied Map
|
|
208
|
+
*
|
|
209
|
+
* @catchphrase
|
|
210
|
+
* Shallow copy a struct
|
|
211
|
+
*/
|
|
212
|
+
Struct.copy = x => {
|
|
213
|
+
if (isArray(x)) return x.slice()
|
|
214
|
+
if (isObject(x)) return { ...x }
|
|
215
|
+
return isSet(x) ? copySet(x) : copyMap(x)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const { entries: structEntries, set: structSet } = Struct
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @name Struct.copyDeep
|
|
222
|
+
*
|
|
223
|
+
* @synopsis
|
|
224
|
+
* Struct.copyDeep(x Array) -> deeplyCopied Array
|
|
225
|
+
*
|
|
226
|
+
* Struct.copyDeep(x Object) -> deeplyCopied Object
|
|
227
|
+
*
|
|
228
|
+
* Struct.copyDeep(x Set) -> deeplyCopied Set
|
|
229
|
+
*
|
|
230
|
+
* Struct.copyDeep(x Map) -> deeplyCopied Map
|
|
231
|
+
*
|
|
232
|
+
* @catchphrase
|
|
233
|
+
* Deep copy a struct
|
|
234
|
+
*/
|
|
235
|
+
const structCopyDeep = x => {
|
|
236
|
+
const y = new x.constructor()
|
|
237
|
+
for (const [index, value] of structEntries(x)) {
|
|
238
|
+
structSet(
|
|
239
|
+
y,
|
|
240
|
+
isStruct(value) ? structCopyDeep(value) : value,
|
|
241
|
+
index,
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
return y
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
Struct.copyDeep = structCopyDeep
|
|
248
|
+
|
|
249
|
+
module.exports = Struct
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
const assert = require('assert')
|
|
2
|
+
const Struct = require('./Struct')
|
|
3
|
+
|
|
4
|
+
describe('Struct', () => {
|
|
5
|
+
describe('new Struct(x Array|Object|Set|Map) -> Struct', () => {
|
|
6
|
+
it('x Array; Struct', async () => {
|
|
7
|
+
assert.equal(new Struct([1, 2, 3]).constructor, Struct)
|
|
8
|
+
})
|
|
9
|
+
it('x Object; Struct', async () => {
|
|
10
|
+
assert.equal(new Struct({ a: 1, b: 2, c: 3 }).constructor, Struct)
|
|
11
|
+
})
|
|
12
|
+
it('x Set; Struct', async () => {
|
|
13
|
+
assert.equal(new Struct(new Set([1, 2, 3])).constructor, Struct)
|
|
14
|
+
})
|
|
15
|
+
it('x Map; Struct', async () => {
|
|
16
|
+
assert.equal(new Struct(new Map([['a', 1], ['b', 2], ['c', 3]])).constructor, Struct)
|
|
17
|
+
})
|
|
18
|
+
it('x string; TypeError', async () => {
|
|
19
|
+
assert.throws(
|
|
20
|
+
() => new Struct('hey'),
|
|
21
|
+
new TypeError('cannot convert hey to Struct')
|
|
22
|
+
)
|
|
23
|
+
})
|
|
24
|
+
it('x null; TypeError', async () => {
|
|
25
|
+
assert.throws(
|
|
26
|
+
() => new Struct(null),
|
|
27
|
+
new TypeError('cannot convert null to Struct')
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
it('x undefined; TypeError', async () => {
|
|
31
|
+
assert.throws(
|
|
32
|
+
() => new Struct(undefined),
|
|
33
|
+
new TypeError('cannot convert undefined to Struct')
|
|
34
|
+
)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
describe('Struct.isStruct(x any) -> boolean', () => {
|
|
39
|
+
it('x Array; true', async () => {
|
|
40
|
+
assert.equal(Struct.isStruct([]), true)
|
|
41
|
+
})
|
|
42
|
+
it('x Object; true', async () => {
|
|
43
|
+
assert.equal(Struct.isStruct({}), true)
|
|
44
|
+
})
|
|
45
|
+
it('x Set; true', async () => {
|
|
46
|
+
assert.equal(Struct.isStruct(new Set()), true)
|
|
47
|
+
})
|
|
48
|
+
it('x Map; true', async () => {
|
|
49
|
+
assert.equal(Struct.isStruct(new Map()), true)
|
|
50
|
+
})
|
|
51
|
+
it('x ArrayBuffer; false', async () => {
|
|
52
|
+
assert.equal(Struct.isStruct(new ArrayBuffer()), false)
|
|
53
|
+
})
|
|
54
|
+
it('x Number; false', async () => {
|
|
55
|
+
assert.equal(Struct.isStruct(1), false)
|
|
56
|
+
})
|
|
57
|
+
it('x String; false', async () => {
|
|
58
|
+
assert.equal(Struct.isStruct('hey'), false)
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const nativeObjectToString = Object.prototype.toString
|
|
63
|
+
|
|
64
|
+
const objectToString = x => nativeObjectToString.call(x)
|
|
65
|
+
|
|
66
|
+
it('<T any>Struct.entries(x Array<T>) -> Iterator<[index number, T]>', async () => {
|
|
67
|
+
assert.equal(objectToString(Struct.entries([1, 2, 3])), '[object Array Iterator]')
|
|
68
|
+
assert.deepEqual([...Struct.entries([1, 2, 3])], [[0, 1], [1, 2], [2, 3]])
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('<T any>Struct.entries(x Object<T>) -> Iterator<[key string, T]>', async () => {
|
|
72
|
+
assert.equal(objectToString(Struct.entries({ a: 1, b: 2, c: 3 })), '[object Generator]')
|
|
73
|
+
assert.deepEqual([...Struct.entries({ a: 1, b: 2, c: 3 })], [['a', 1], ['b', 2], ['c', 3]])
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('<T any>Struct.entries(x Set<T>) -> Iterator<[T, T]>', async () => {
|
|
77
|
+
assert.equal(objectToString(Struct.entries(new Set([1, 2, 3]))), '[object Set Iterator]')
|
|
78
|
+
assert.deepEqual([...Struct.entries(new Set([1, 2, 3]))], [[1, 1], [2, 2], [3, 3]])
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('<A any, B any>Struct.entries(x Map<A, B>) -> Iterator<[A, B]>', async () => {
|
|
82
|
+
assert.equal(objectToString(Struct.entries(new Map([[1, 'a'], [2, 'b'], [3, 'c']]))), '[object Map Iterator]')
|
|
83
|
+
assert.deepEqual([...Struct.entries(new Map([[1, 'a'], [2, 'b'], [3, 'c']]))], [[1, 'a'], [2, 'b'], [3, 'c']])
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
describe('<T any>Struct.values(x Array<T>|Object<T>|Set<T>|Map<any, T>) -> Iterator<T>', () => {
|
|
87
|
+
it('x Array<T>; Iterator<T>', async () => {
|
|
88
|
+
assert.equal(objectToString(Struct.values([1, 2, 3])), '[object Array Iterator]')
|
|
89
|
+
assert.deepEqual([...Struct.values([1, 2, 3])], [1, 2, 3])
|
|
90
|
+
})
|
|
91
|
+
it('x Object<T>; Iterator<T>', async () => {
|
|
92
|
+
assert.equal(objectToString(Struct.values({ a: 1, b: 2, c: 3 })), '[object Generator]')
|
|
93
|
+
assert.deepEqual([...Struct.values({ a: 1, b: 2, c: 3 })], [1, 2, 3])
|
|
94
|
+
})
|
|
95
|
+
it('x Set<T>; Iterator<T>', async () => {
|
|
96
|
+
assert.equal(objectToString(Struct.values(new Set([1, 2, 3]))), '[object Set Iterator]')
|
|
97
|
+
assert.deepEqual([...Struct.values(new Set([1, 2, 3]))], [1, 2, 3])
|
|
98
|
+
})
|
|
99
|
+
it('x Map<T>; Iterator<T>', async () => {
|
|
100
|
+
assert.equal(objectToString(Struct.values(new Map([[1, 'a'], [2, 'b'], [3, 'c']]))), '[object Map Iterator]')
|
|
101
|
+
assert.deepEqual([...Struct.values(new Map([[1, 'a'], [2, 'b'], [3, 'c']]))], ['a', 'b', 'c'])
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('<T any>Struct.get(x Array<T>, index number) -> T|undefined', async () => {
|
|
106
|
+
assert.equal(Struct.get([1, 2, 3], 0), 1)
|
|
107
|
+
assert.strictEqual(Struct.get([1, 2, 3], 4), undefined)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('<T any>Struct.get(x Object<T>, index string) -> T|undefined', async () => {
|
|
111
|
+
assert.equal(Struct.get({ a: 1, b: 2, c: 3 }, 'a'), 1)
|
|
112
|
+
assert.strictEqual(Struct.get({ a: 1, b: 2, c: 3 }, 'e'), undefined)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('<T any>Struct.get(x Set<T>, index T) -> T|undefined', async () => {
|
|
116
|
+
assert.equal(Struct.get(new Set([1, 2, 3]), 1), 1)
|
|
117
|
+
assert.strictEqual(Struct.get(new Set([1, 2, 3]), 5), undefined)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('<A any, B any>Struct.get(x Map<A, B>, index A) -> B|undefined', async () => {
|
|
121
|
+
const m = new Map([[1, 'a'], [2, 'b'], [3, 'c']])
|
|
122
|
+
assert.equal(Struct.get(m, 1), 'a')
|
|
123
|
+
assert.strictEqual(Struct.get(m, 5), undefined)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('Struct.set(x Array, value any, index number) -> mutated Array', async () => {
|
|
127
|
+
const arr = [1, 2]
|
|
128
|
+
assert.deepEqual(Struct.set(arr, 3, 2), [1, 2, 3])
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('Struct.set(x Object, value any, index string) -> mutated Object', async () => {
|
|
132
|
+
const obj = { a: 1, b: 2 }
|
|
133
|
+
assert.deepEqual(Struct.set(obj, 3, 'c'), { a: 1, b: 2, c: 3 })
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('Struct.set(x Set, value any) -> mutated Set', async () => {
|
|
137
|
+
const set = new Set([1, 2])
|
|
138
|
+
assert.deepEqual(Struct.set(set, 3), new Set([1, 2, 3]))
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('Struct.set(x Map, value any, index any) -> mutated Map', async () => {
|
|
142
|
+
const map = new Map([['a', 1], ['b', 2]])
|
|
143
|
+
assert.deepEqual(Struct.set(map, 3, 'c'), new Map([['a', 1], ['b', 2], ['c', 3]]))
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
describe('Struct.size(x Array|Object|Set|Map) -> y number', () => {
|
|
147
|
+
it('x [1, 2, 3]; y 3', async () => {
|
|
148
|
+
assert.strictEqual(Struct.size([1, 2, 3]), 3)
|
|
149
|
+
})
|
|
150
|
+
it('x []; y 0', async () => {
|
|
151
|
+
assert.strictEqual(Struct.size([]), 0)
|
|
152
|
+
})
|
|
153
|
+
it('x { a: 1, b: 2, c: 3 }; y 3', async () => {
|
|
154
|
+
assert.strictEqual(Struct.size({ a: 1, b: 2, c: 3 }), 3)
|
|
155
|
+
})
|
|
156
|
+
it('x {}; y 0', async () => {
|
|
157
|
+
assert.strictEqual(Struct.size({}), 0)
|
|
158
|
+
})
|
|
159
|
+
it('x Set<[1, 2, 3]>; y 3', async () => {
|
|
160
|
+
assert.strictEqual(Struct.size(new Set([1, 2, 3])), 3)
|
|
161
|
+
})
|
|
162
|
+
it('x Set<[]>; y 0', async () => {
|
|
163
|
+
assert.strictEqual(Struct.size(new Set()), 0)
|
|
164
|
+
})
|
|
165
|
+
it('x Map<[[\'a\', 1], [\'b\', 2], [\'c\', 3]]>; y 3', async () => {
|
|
166
|
+
assert.strictEqual(Struct.size(new Map([['a', 1], ['b', 2], ['c', 3]])), 3)
|
|
167
|
+
})
|
|
168
|
+
it('x Map<[]>; y 0', async () => {
|
|
169
|
+
assert.strictEqual(Struct.size(new Map()), 0)
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('Struct.copy(x Array) -> copied Array', async () => {
|
|
174
|
+
const arr = [1, 2, 3]
|
|
175
|
+
const copied = Struct.copy(arr)
|
|
176
|
+
assert(copied !== arr)
|
|
177
|
+
assert.deepEqual(arr, copied)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it('Struct.copy(x Object) -> copied Object', async () => {
|
|
181
|
+
const obj = { a: 1, b: 2, c: 3 }
|
|
182
|
+
const copied = Struct.copy(obj)
|
|
183
|
+
assert(copied !== obj)
|
|
184
|
+
assert.deepEqual(obj, copied)
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('Struct.copy(x Set) -> copied Set', async () => {
|
|
188
|
+
const set = new Set([1, 2, 3])
|
|
189
|
+
const copied = Struct.copy(set)
|
|
190
|
+
assert(copied !== set)
|
|
191
|
+
assert.deepEqual(set, copied)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('Struct.copy(x Map) -> copied Map', async () => {
|
|
195
|
+
const map = new Map([['a', 1], ['b', 2], ['c', 3]])
|
|
196
|
+
const copied = Struct.copy(map)
|
|
197
|
+
assert(copied !== map)
|
|
198
|
+
assert.deepEqual(map, copied)
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it('Struct.copyDeep(x Array) -> deeplyCopied Array', async () => {
|
|
202
|
+
const arr = [1, [2], [[3], 'hey', new Uint8Array([1, 2, 3])]]
|
|
203
|
+
const copied = Struct.copyDeep(arr)
|
|
204
|
+
assert(copied !== arr)
|
|
205
|
+
assert.deepEqual(arr, copied)
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('Struct.copyDeep(x Object) -> deeplyCopied Object', async () => {
|
|
209
|
+
const obj = { a: 1, b: [2], c: [[3]], d: new Set([NaN, 'hey', { a: null }]) }
|
|
210
|
+
const copied = Struct.copy(obj)
|
|
211
|
+
assert(copied !== obj)
|
|
212
|
+
assert.deepEqual(obj, copied)
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('Struct.copyDeep(x Set) -> deeplyCopied Set', async () => {
|
|
216
|
+
const set = new Set([1, [2], [[[[3]]]], {}, [], undefined])
|
|
217
|
+
const copied = Struct.copy(set)
|
|
218
|
+
assert(copied !== set)
|
|
219
|
+
assert.deepEqual(set, copied)
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
it('Struct.copyDeep(x Map) -> deeplyCopied Map', async () => {
|
|
223
|
+
const map = new Map([
|
|
224
|
+
['a', 1], ['b', 2], ['c', 3],
|
|
225
|
+
[Symbol.for('ayo'), {}], ['hey', {
|
|
226
|
+
a: [1, [2], [[3]]],
|
|
227
|
+
b: new Set([undefined, null, {}, [], new RegExp()]),
|
|
228
|
+
c: { d: { e: new BigInt64Array([1n, 2n, 3n]) } },
|
|
229
|
+
}],
|
|
230
|
+
])
|
|
231
|
+
const copied = Struct.copy(map)
|
|
232
|
+
assert(copied !== map)
|
|
233
|
+
assert.deepEqual(map, copied)
|
|
234
|
+
assert.strictEqual(map.get('hey').c.d.e, copied.get('hey').c.d.e)
|
|
235
|
+
// anything not a struct is a reference. This is good for performance but could be dangerous
|
|
236
|
+
})
|
|
237
|
+
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rubico",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.2",
|
|
4
4
|
"description": "[a]synchronous functional programming",
|
|
5
5
|
"author": "Richard Tong",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"_internal",
|
|
15
15
|
"dist",
|
|
16
16
|
"x",
|
|
17
|
+
"monad",
|
|
17
18
|
"index.js",
|
|
18
19
|
"es.js",
|
|
19
20
|
"global.js",
|
package/reduce.js
CHANGED
package/set.js
CHANGED
|
@@ -39,7 +39,7 @@ const _set = function (obj, path, value) {
|
|
|
39
39
|
* @synopsis
|
|
40
40
|
* ```coffeescript [specscript]
|
|
41
41
|
* set(
|
|
42
|
-
* object Object,
|
|
42
|
+
* object Promise|Object,
|
|
43
43
|
* path string|Array<string|number>,
|
|
44
44
|
* value function|any,
|
|
45
45
|
* ) -> result Promise|Object
|
|
@@ -60,15 +60,15 @@ const _set = function (obj, path, value) {
|
|
|
60
60
|
* * an array of keys or indices - `['a', 0, 'value']`
|
|
61
61
|
*
|
|
62
62
|
* ```javascript [playground]
|
|
63
|
-
* console.log(set(
|
|
63
|
+
* console.log(set({ b: 2 }, 'a', 1)) // { a: 1, b: 2 }
|
|
64
64
|
*
|
|
65
65
|
* const nestedAC2 = { a: { c: 2 } }
|
|
66
66
|
*
|
|
67
|
-
* console.log(set('a.b', 1)
|
|
67
|
+
* console.log(set(nestedAC2, 'a.b', 1)) // { a : { b: 1, c: 2 }}
|
|
68
68
|
*
|
|
69
69
|
* const nestedA0BC3 = { a: [{ b: { c: 3 } }] }
|
|
70
70
|
*
|
|
71
|
-
* console.log(set('a[0].b.c', 4)
|
|
71
|
+
* console.log(set(nestedA0BC3, 'a[0].b.c', 4)) // { a: [{ b: { c: 4 } }] }
|
|
72
72
|
* ```
|
|
73
73
|
*
|
|
74
74
|
* The property value may be a function, in which case it is treated as a resolver and provided the argument object to resolve the value to set.
|
|
@@ -81,6 +81,15 @@ const _set = function (obj, path, value) {
|
|
|
81
81
|
* console.log(myNewObj) // { a: 1, b: 3 }
|
|
82
82
|
* ```
|
|
83
83
|
*
|
|
84
|
+
* `set` supports a tacit API for composability.
|
|
85
|
+
*
|
|
86
|
+
* ```javascript [playground]
|
|
87
|
+
* pipe({ a: 1 }, [
|
|
88
|
+
* set('b', 2),
|
|
89
|
+
* console.log, // { a: 1, b: 2 }
|
|
90
|
+
* ])
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
84
93
|
* @since 1.7.0
|
|
85
94
|
*/
|
|
86
95
|
|
package/some.js
CHANGED
|
@@ -75,10 +75,21 @@ const _some = function (collection, predicate) {
|
|
|
75
75
|
* yield 1; yield 2; yield 3; yield 4; yield 5
|
|
76
76
|
* }
|
|
77
77
|
*
|
|
78
|
-
* some(
|
|
79
|
-
* fetchTodo
|
|
80
|
-
*
|
|
81
|
-
*
|
|
78
|
+
* const promise = some(todoIDsGenerator(), async id => {
|
|
79
|
+
* const todo = await fetchTodo(id)
|
|
80
|
+
* return todo.title.startsWith('fugiat')
|
|
81
|
+
* })
|
|
82
|
+
*
|
|
83
|
+
* promise.then(console.log) // true
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* `some` supports a tacit API for composability.
|
|
87
|
+
*
|
|
88
|
+
* ```javascript [playground]
|
|
89
|
+
* pipe([1, 2, 3], [
|
|
90
|
+
* some(number => number < 5),
|
|
91
|
+
* console.log, // true
|
|
92
|
+
* ])
|
|
82
93
|
* ```
|
|
83
94
|
*
|
|
84
95
|
* @execution concurrent
|
package/switchCase.js
CHANGED
|
@@ -15,12 +15,31 @@ const curryArgs3 = require('./_internal/curryArgs3')
|
|
|
15
15
|
* switchCase(conditionalValues Array<boolean|any>) -> Promise|any
|
|
16
16
|
*
|
|
17
17
|
* switchCase(
|
|
18
|
+
* ...args,
|
|
19
|
+
* conditionalFuncsOrValues Array<function|boolean|any>
|
|
20
|
+
* ) -> Promise|any
|
|
21
|
+
*
|
|
22
|
+
* switchCase(
|
|
18
23
|
* conditionalFuncsOrValues Array<function|boolean|any>
|
|
19
24
|
* )(...args) -> Promise|any
|
|
20
25
|
* ```
|
|
21
26
|
*
|
|
22
27
|
* @description
|
|
23
|
-
* Functional equivalent to the [Conditional (ternary) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator). Accepts an array of conditional functions that specifies cases as pairings of `predicate` and `resolver` functions with the exception of the last, default resolver. All functions are provided with the same arguments and executed in series. The result of a `switchCase`
|
|
28
|
+
* Functional equivalent to the [Conditional (ternary) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator). Accepts an array of conditional functions that specifies cases as pairings of `predicate` and `resolver` functions with the exception of the last, default resolver. All functions are provided with the same arguments and executed in series. The result of a `switchCase` operation is either the result of the execution the last default resolver, or the result of the execution of the first resolver where the associated predicate tested true.
|
|
29
|
+
*
|
|
30
|
+
* ```javascript [playground]
|
|
31
|
+
* const fruitIsYellow = fruit => fruit.color == 'yellow'
|
|
32
|
+
*
|
|
33
|
+
* console.log(
|
|
34
|
+
* switchCase({ name: 'plantain', color: 'yellow' }, [
|
|
35
|
+
* fruitIsYellow,
|
|
36
|
+
* fruit => fruit.name + ' is possibly a banana',
|
|
37
|
+
* fruit => fruit.name + ' is probably not a banana',
|
|
38
|
+
* ])
|
|
39
|
+
* ) // plantain is possibly a banana
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* For composability `switchCase` supports a tacit API.
|
|
24
43
|
*
|
|
25
44
|
* ```javascript [playground]
|
|
26
45
|
* const fruitIsYellow = fruit => fruit.color == 'yellow'
|
|
@@ -38,7 +57,7 @@ const curryArgs3 = require('./_internal/curryArgs3')
|
|
|
38
57
|
* // apple is probably not a banana
|
|
39
58
|
* ```
|
|
40
59
|
*
|
|
41
|
-
* Any function can be replaced with a nonfunction (object or primitive) value
|
|
60
|
+
* Any function can be replaced with a nonfunction (object or primitive) value to be used directly in the operation.
|
|
42
61
|
*
|
|
43
62
|
* ```javascript [playground]
|
|
44
63
|
* switchCase([
|
package/transform.js
CHANGED
|
@@ -26,8 +26,6 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
26
26
|
* type Transformable = Array|String|Set|TypedArray|{ concat: function }|{ write: function }|Object
|
|
27
27
|
* type Foldable = Iterable|AsyncIterable|Object<value any>
|
|
28
28
|
*
|
|
29
|
-
* initialValue Transformable|((foldable Foldable)=>Promise|Transformable)
|
|
30
|
-
*
|
|
31
29
|
* transform(
|
|
32
30
|
* foldable Foldable,
|
|
33
31
|
* transducer Transducer,
|
|
@@ -58,9 +56,9 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
58
56
|
*
|
|
59
57
|
* const isOdd = number => number % 2 == 1
|
|
60
58
|
*
|
|
61
|
-
* const squaredOdds =
|
|
62
|
-
* filter(isOdd),
|
|
63
|
-
* map(square),
|
|
59
|
+
* const squaredOdds = compose([
|
|
60
|
+
* Transducer.filter(isOdd),
|
|
61
|
+
* Transducer.map(square),
|
|
64
62
|
* ])
|
|
65
63
|
*
|
|
66
64
|
* // transform arrays into arrays
|
|
@@ -96,7 +94,7 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
96
94
|
* },
|
|
97
95
|
* }
|
|
98
96
|
*
|
|
99
|
-
* transform(map(square), Stdout)([1, 2, 3, 4, 5])
|
|
97
|
+
* transform(Transducer.map(square), Stdout)([1, 2, 3, 4, 5])
|
|
100
98
|
* // 1
|
|
101
99
|
* // 4
|
|
102
100
|
* // 9
|
|
@@ -124,8 +122,10 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
124
122
|
* }
|
|
125
123
|
*
|
|
126
124
|
* transform(
|
|
127
|
-
*
|
|
128
|
-
*
|
|
125
|
+
* streamRandomInts(),
|
|
126
|
+
* Transducer.map(pipe([square, toString])),
|
|
127
|
+
* process.stdout,
|
|
128
|
+
* ) // 9216576529289484980147613249169774446246768649...
|
|
129
129
|
* ```
|
|
130
130
|
*
|
|
131
131
|
* @execution series
|