bit-bin
Version:
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b
26 lines (24 loc) • 695 B
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = splitBy;
/**
* splits an array to two chunks using a conditional predicate function.
* @name splitBy
* @param {[*]} array array to split.
* @param {() => boolean} fn predicate function to test each element of the array.
* @returns {[[], []]} two new arrays with the elements that failed the test from the left.
* @example
* ```js
* splitBy([1, 2, 3, 4, 5], isEven) // => [[1, 3, 5], [2, 4]]
* ```
*/
function splitBy(array, fn) {
const truthy = [];
const falsy = [];
array.forEach(elm => {
if (fn(elm)) truthy.push(elm);else falsy.push(elm);
});
return [falsy, truthy];
}
;