reduce-to
convert and modify Arrays, Objects, Maps, Sets and Strings. A powerful utility belt well under 1kb gzip, no dependencies
example
var x = // CONVERSIONSvar source = 'a' 'b' 'c' map = // Map of [0,'a'], [1, 'b'], [2, 'c'] obj = // {'0': 'a', '1': 'b', '2': 'c'} set = // Set of 'a', 'b', 'c' txt = // 'abc' arr = // same as source // LAZY TRANSFORMS WITH TRANSDUCERSvar zAC = // 'zAC
Features
- apply multiple transforms in a lazy and efficient way
- convert to any javascript key-value data types
var myMap = x(JSON.parse(json), new Map)
var json = x(set, [])
var noDuplicates = x(x(arr, new Set), [])
var shallowClone = x(obj, {})
var plusOne = x(new Int8Array([1,2,3]), x.map(v=>v+1))
converted to an Array
- works seamlesly in ES5
supported types
- Input:
Array
,Strings
,Object
,Map
,Set
,Iterators
- Output:
Array
,Strings
,Object
,Map
,Set
,WeakMap
,WeakSet
key value conversions
Array
: index, valueString
: index, valueObject
: key, valueMap
: key.toString(), valueSet
: index, valueIterators
: index, value
transducer and reducer functions
To keep things light and simple, only 3 reducer types are provided
.map((val, key, source) => newValue)
.filter((val, key, source) => Boolean)
.index((val, index, source) => Boolean)
index
is similar to filter
but with a counter index instead of the key (same as Array.filter
).
Most common other reducers can be emulated with these 3 base reducers:
take(N)
: useindex((val, idx) => idx < N)
drop(N)
: useindex((val, idx) => idx >= N)
- ...
reject
,dropWhile
, ...