3.1.0 • Published 6 years ago

reduss v3.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Reduss

Build Status codecov Greenkeeper badge Commitizen friendly JavaScript Style Guide david-dm

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

ParamTypeDefaultDescription
pathstringThe path on which to set a new value
data*The data to set
baseObjectObjectObjectThe 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)

ParamTypeDefaultDescription
objobjectThe object on which the leafs are selected
previousPatharrayarrayThe 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

ParamTypeDescription
mappersObject.<string, function()>Object with keys corresponding to paths and value corresponding to maping function taking the value at objectToMap path
objectToMapObjectThe 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

ParamTypeDescription
mappersObject.<string, function()>Object with keys corresponding to paths (or simple keys) and value corresponding to maping function taking the value at objectToMap path
objectToMapObjectThe 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
ParamTypeDescription
pathstringA string representing the path to access (e.g. 'some.prop.nested')
objToAccessObjectThe 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
ParamTypeDescription
indexesArray.<number>An array of indexes to pick (the order matter)
arrayOfElementsArray.<*>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

ParamTypeDefaultDescription
conditionfunctionTake as first argment the current value and next all the reducer argument (acumulator, index, array) and return true if the reducer should be called
reducerfunctionTake all the reducer argument (acumulator, value, index, array), return the new accumuulator
arrayArrayAn array to reduce
initAcc*ArrayThe 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

ParamTypeDescription
conditionfunctionRetun true if the number can be summed
numbersArray.<number>The array of numbers to sum

Example

// returns 6
sumOnly(x => x <= 3, [1,2,3,4])
3.1.0

6 years ago

3.0.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago