froebel
Version:
TypeScript utility library
30 lines (26 loc) • 844 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
/**
* Takes a `list` and returns a pair of lists containing: the elements that
* match the `predicate` and those that don't, respectively.
*
* Think of it as `filter`, but the elements that don't pass the filter aren't
* discarded but returned in a separate list instead.
*
* @example
* ```
* const [strings, numbers] = partition(
* ['a', 'b', 1, 'c', 2, 3],
* (el): el is string => typeof el === 'string'
* )
* // strings: ["a", "b", "c"]
* // numbers: [1, 2, 3]
* ```
*/
const partition = (list, predicate) => list.reduce(([t, f], c) => predicate(c) ? [[...t, c], f] : [t, [...f, c]], [[], []]);
var _default = partition;
exports.default = _default;
module.exports = Object.assign(exports.default || {}, exports);