es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
30 lines (25 loc) • 845 B
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const identity = require('../../function/identity.js');
const isArrayLike = require('../predicate/isArrayLike.js');
const iteratee = require('../util/iteratee.js');
function partition(source, predicate = identity.identity) {
if (!source) {
return [[], []];
}
const collection = isArrayLike.isArrayLike(source) ? source : Object.values(source);
predicate = iteratee.iteratee(predicate);
const matched = [];
const unmatched = [];
for (let i = 0; i < collection.length; i++) {
const value = collection[i];
if (predicate(value)) {
matched.push(value);
}
else {
unmatched.push(value);
}
}
return [matched, unmatched];
}
exports.partition = partition;