-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update "map" to allow curried and un-curried calling.
BREAKING CHANGE: Multiple functions can be passed if wrapped in array. ```js // old map(inc, inc)([1, 2]) // new map([inc, inc], [1, 2]) ```
- Loading branch information
Showing
2 changed files
with
56 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,56 @@ | ||
/** | ||
* Iterate over an input list, calling `fn` for each element, return a new | ||
* array | ||
* | ||
* @param {Function} fn The function | ||
* @param {[]} list Array | ||
* | ||
* @return {Array} | ||
*/ | ||
const map = (...fn) => source => { | ||
const result = [] | ||
const sourceArray = Array.isArray(source) ? source : [source] | ||
import { pipe } from "../pipe/pipe" | ||
|
||
for (let i = 0, valuesCount = sourceArray.length; i < valuesCount; i++) { | ||
let value = sourceArray[i] | ||
|
||
// pipe functions through each value | ||
for (let j = 0, fnCount = fn.length; j < fnCount; j++) { | ||
value = fn[j].call(null, value, i, sourceArray) | ||
} | ||
const _map = (fn, _source) => { | ||
const result = [] | ||
const source = Array.isArray(_source) ? _source : [_source] | ||
|
||
result.push(value) | ||
for (let i = 0, valuesCount = source.length; i < valuesCount; ++i) { | ||
result.push( | ||
Array.isArray(fn) ? pipe(...fn)(source[i]) : fn(source[i], i, source) | ||
) | ||
} | ||
|
||
return result | ||
} | ||
|
||
export { map } | ||
/** | ||
* Iterates over an array and applies a function on each element, returning a | ||
* new array with the transformed elements. | ||
* | ||
* @param {Fn|Fn[]} fn Transform function called on all elements | ||
* @param {[]} source Array to iterate over | ||
* | ||
* @return {Array} Returns new instance | ||
* | ||
* @tag Array | ||
* @signature (fn: Fn|Fn[]) => (source: []) => [] | ||
* @signature (fn: Fn|Fn[], source: []) => [] | ||
* | ||
* @example | ||
* const inc = x => x + 1 | ||
* | ||
* map(inc, [1, 2]) | ||
* // => [2, 3] | ||
* | ||
* map([inc, inc], [1, 2]) | ||
* // => [3, 4] | ||
*/ | ||
export const map = (...params) => { | ||
/* | ||
* @signature (fn: Fn|Fn[]) => (source: []): [] | ||
* | ||
* map(inc)([1, 2]) | ||
* // => [2, 3] | ||
*/ | ||
if (params.length === 1) { | ||
return source => _map(params[0], source) | ||
} | ||
|
||
/* | ||
* @signature (fn: Fn|Fn[], source: []): [] | ||
* | ||
* map(inc, [1, 2]) | ||
* // => [2, 3] | ||
*/ | ||
return _map(...params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters