Skip to content

Commit

Permalink
feat: Add "converge" functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Aug 28, 2020
1 parent afae8e5 commit ebd03ee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
46 changes: 46 additions & 0 deletions src/converge/converge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { pipe } from "../pipe/pipe"

const _converge = (_accFn, _extractFn, source) => {
const accFn = Array.isArray(_accFn) ? pipe(..._accFn) : _accFn
const extractFn = Array.isArray(_extractFn) ? _extractFn : [_extractFn]
const extractValues = []

for (let i = 0; i < extractFn.length; ++i) {
extractValues.push(extractFn[i](source))
}

return accFn(...extractValues)
}

/**
* Apply a list of function, extract functions, on the same input and use
* those results as parameters for another accumulator function.
*
* @param {Fn|Fn[]} accFn Accumulator or final aggreate function
* @param {Fn|Fn[]} extractFn List of functions to be applied on input
* @param {Any} source Source input
*
* @return {Any}
*
* @name converge
* @tag Core
* @signature (accFn: Fn|Fn[], extractFn: Fn|Fn[]) => (source: Array): Any
* @signature (accFn: Fn|Fn[], extractFn: Fn|Fn[], source: Array): Any
*
* @example
* const divide = () => ...
* const sum = () => ...
* const count = () => ...
*
* converge(divide, [sum, count], [1, 2, 3, 4, 5, 6, 7])
* // => 4
*/
export const converge = (...params) => {
// @signature (accFn, extractFn) => (source)
if (params.length <= 2) {
return source => _converge(params[0], params[1], source)
}

// @signature (accFn, extractFn, source)
return _converge(...params)
}
27 changes: 27 additions & 0 deletions src/converge/converge.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import test from "tape"
import { converge, reduce, read } from ".."

test("converge", t => {
const obj = { a: 1, b: 2 }
const sum = (...params) => reduce((acc, item) => acc + item, 0, params)

t.equals(
converge(sum, [read("a"), read("b")])(obj),
3,
"Extract object properties + sum (curried)"
)

t.equals(
converge(sum, read("b"), obj),
2,
"Extract object property + sum (uncurried)"
)

t.equals(
converge([sum], [read("a"), read("b")], obj),
3,
"Extract object properties + sum (uncurried)"
)

t.end()
})
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
export { curry } from "./curry/curry"
export { zipToObj } from "./zip-to-obj/zip-to-obj"
export { isDeepEqual, isDeepEqual as deepEqual } from "./deep-equal/deep-equal"
export { elapsedTime } from "./elapsed-time/elapsed-time"
export { groupBy } from "./group-by/group-by"
export { indexBy } from "./index-by/index-by"
export { hist } from "./hist/hist"
export { isMatch } from "./is-match/is-match"
export { protoChain } from "./proto-chain/proto-chain"
export { renameFile } from "./rename-file/rename-file"
export { sequence, sequenceWhile } from "./sequence/sequence"
Expand All @@ -16,9 +13,11 @@ export { throttle } from "./throttle/throttle"
export { debounce } from "./debounce/debounce"

// Core
export { curry } from "./curry/curry"
export { map } from "./map/map"
export { mapMatrix } from "./map-matrix/map-matrix"
export { reduce } from "./reduce/reduce"
export { converge } from "./converge/converge"
export { forEach } from "./for-each/for-each"
export { pipe } from "./pipe/pipe"
export { pipeP } from "./pipeP/pipeP"
Expand All @@ -37,12 +36,14 @@ export { any, anyWith } from "./any/any"
export { all, allWith } from "./all/all"
export { when } from "./when/when"
export { cases } from "./cases/cases"
export { isMatch } from "./is-match/is-match"

// Object
export { pick } from "./pick/pick"
export { pluck } from "./pluck/pluck"
export { merge } from "./merge/merge"
export { keys } from "./keys/keys"
export { zipToObj } from "./zip-to-obj/zip-to-obj"

// Array
export { top } from "./top/top"
Expand Down

0 comments on commit ebd03ee

Please sign in to comment.