reduss
v3.1.0
Published
A collection of reduce utilities
Downloads
2
Readme
Reduss
A passionate love letter to Array.prototype.reduce()
Functions
atPathSet(path, data, [baseObject]) ⇒ Object
Kind: global function
Returns: Object - A new object with the path provided equal to data, and optionnaly any values present in baseObject
| Param | Type | Default | Description | | ------------ | ------------------- | ------------------- | ------------------------------------------------------------- | | path | string | | The path on which to set a new value | | data | * | | The data to set | | [baseObject] | Object | Object | The base object, serve as the initial accumulator, default {} |
Example
//returns { some: { prop: { one: 1, two: 2 } } }
atPathSet('some.prop.one', 1, atPathSet('some.prop.two', 2))
leafs(obj, [previousPath]) ⇒ Array.<PathValueObject>
Kind: global function
Returns: Array.<PathValueObject> - An array with object containing the leafs information (path/value)
| Param | Type | Default | Description | | -------------- | ------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | obj | object | | The object on which the leafs are selected | | [previousPath] | array | array | The accumulated path while iterating on obj properties, default to empty array, needed only for recursion, should not be a passed argument when calling the function |
Example
// returns [
// { path: ['a', 'b', 'c'], value: 4 },
// { path: ['a', 'b', 'e'], value: 6 },
// { path: ['a', 'd', 'l'], value: 2 }
// ]
leafs({
a: {
b: { c: 4, e: 6 },
d: { l: 2 }
}
})
mapKeys(mappers, objectToMap) ⇒ Object
Kind: global function
Returns: Object - A new object with the mapped keys
| Param | Type | Description | | ----------- | ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | | mappers | Object.<string, function()> | Object with keys corresponding to paths and value corresponding to maping function taking the value at objectToMap path | | objectToMap | Object | The source object for the mapping |
Example
// returns { str: 'SOMEVALUE', num: 4 }
mapKeys(
{ str: x => x.toUpperCase(), num: n => n + 3 },
{ str: 'someValue',
num: 1,
}
)
mapKeysDeep(mappers, objectToMap) ⇒ Object
Kind: global function
Returns: Object - A new object with the mapped paths
| Param | Type | Description | | ----------- | ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | | mappers | Object.<string, function()> | Object with keys corresponding to paths (or simple keys) and value corresponding to maping function taking the value at objectToMap path | | objectToMap | Object | The source object for the mapping |
Example
// returns {
// obj: {
// prop: {
// last: 8
// }
// }
// }
mapKeysDeep(
{
'obj.prop.last': x => x * 2
},
{
obj: {
prop: { last: 4 }
}
}
)
path(path, objToAccess) ⇒ *
Kind: global function
Returns: * - The value corresponding to the path
Throws:
- If we try to access a non existing property on objToAccess
| Param | Type | Description | | ----------- | ------------------- | ------------------------------------------------------------------ | | path | string | A string representing the path to access (e.g. 'some.prop.nested') | | objToAccess | Object | The accessed object |
Example
// returns 6
path('a.b.c', { a: { b: { c: 6 } } })
pickIndexes(indexes, arrayOfElements) ⇒ Array.<*>
Kind: global function
Returns: Array.<*> - The picked elements in the order of the indexes argument
Throws:
- Can throw an error if oyu try to pick an index which does not exist on arrayOfElements
| Param | Type | Description | | --------------- | --------------------------------- | ---------------------------------------------- | | indexes | Array.<number> | An array of indexes to pick (the order matter) | | arrayOfElements | Array.<*> | The array to pick values from |
Example
// returns [6, 6, 2]
pickIndexes([2, 4, 1], [1, 2, 6, 1, 6])
reduceIf(condition, reducer, array, [initAcc]) ⇒ *
Kind: global function
Returns: * - The reduced value
| Param | Type | Default | Description | | --------- | --------------------- | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | | condition | function | | Take as first argment the current value and next all the reducer argument (acumulator, index, array) and return true if the reducer should be called | | reducer | function | | Take all the reducer argument (acumulator, value, index, array), return the new accumuulator | | array | Array | | An array to reduce | | [initAcc] | * | Array | The initial accumulator, default empty array |
Example
// returns 6
reduceIf(x => x <= 3, (acc, v) => acc + v, [1,2,3,4], 0)
sumOnly(condition, numbers) ⇒ number
Kind: global function
Returns: number - The sum of matching numbers
| Param | Type | Description | | --------- | --------------------------------- | -------------------------------------- | | condition | function | Retun true if the number can be summed | | numbers | Array.<number> | The array of numbers to sum |
Example
// returns 6
sumOnly(x => x <= 3, [1,2,3,4])