Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create partitionBy function #5796

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/partitionBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import baseAssignValue from './.internal/baseAssignValue';
import reduce from './reduce';

/** Used to check objects for own properties. */
const hasOwnProperty = Object.prototype.hasOwnProperty;

/**
* Partitions a collection into two arrays: one for elements that satisfy
* the predicate and one for elements that do not.
*
* @since 0.1.0
* @category Collection
* @param {Array|Object} collection The collection to iterate over.
* @param {Function} predicate The predicate function to determine the grouping.
* @returns {Object} Returns an object with two properties: 'true' and 'false'.
* Each property holds an array of elements that satisfy or do not satisfy the predicate.
* @example
*
* partitionBy([1, 2, 3, 4, 5], n => n % 2 === 0);
* => { 'true': [2, 4], 'false': [1, 3, 5] }
*/

type Predicate<T> = (value: T) => boolean;

type PartitionResult<T> = {
true: T[];
false: T[];
};

function partitionBy<T>(collection: T[], predicate: Predicate<T>): PartitionResult<T> {
return reduce(
collection,
(result: { [x: string]: any[] }, value: T) => {
const key = predicate(value) ? 'true' : 'false';
if (hasOwnProperty.call(result, key)) {
result[key].push(value);
} else {
baseAssignValue(result, key, [value]);
}
return result;
},
{} as PartitionResult<T>,
);
}

export default partitionBy;
50 changes: 50 additions & 0 deletions test/partitionBy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import partitionBy from '../src/partitionBy';
import { identity } from './utils';

describe('partitionBy', () => {
const array = [1, 0, 1];

it('should split elements into two groups by the predicate', () => {
expect(partitionBy([], identity)).toEqual({ true: [], false: [] });
expect(partitionBy(array, value => value === 1)).toEqual({ true: [1, 1], false: [0] });
expect(partitionBy(array, value => value === 0)).toEqual({ true: [0], false: [1, 1] });
});

it('should use the identity function when the predicate is nullish', () => {
const values = [, null, undefined];
const expected = [{ true: [1, 1], false: [0] }, { true: [1, 1], false: [0] }, { true: [1, 1], false: [0] }];

const actual = values.map((value, index) =>
index ? partitionBy(array, value) : partitionBy(array),
);

expect(actual).toEqual(expected);
});

it('should work with a custom predicate function', () => {
const isEven = value => value % 2 === 0;
const numbers = [1, 2, 3, 4, 5];

expect(partitionBy(numbers, isEven)).toEqual({ true: [2, 4], false: [1, 3, 5] });
});

it('should work with a number for the predicate', () => {
const array = [
[1, 0],
[0, 1],
[1, 0],
];

expect(partitionBy(array, 0)).toEqual({ true: [array[0], array[2]], false: [array[1]] });
expect(partitionBy(array, 1)).toEqual({ true: [array[1]], false: [array[0], array[2]] });
});

it('should work with an object for the collection', () => {
const collection = { a: 1.1, b: 0.2, c: 1.3 };
const expected = { true: [1.1, 1.3], false: [0.2] };

const actual = partitionBy(collection, Math.floor);

expect(actual).toEqual(expected);
});
});