37
loading...
This website collects cookies to deliver better user experience
type PartitionWith = <T>(items: T[], predicates: ((item: T) => boolean)[]): T[][]
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const isLessOrEqualThanFive = (number: number) => number <= 5;
const isGreaterThanFive = (number) => number > 5;
const results = partitionWith(array, [isLessOrEqualThanFive, isGreaterThanFive ]);
console.log(results); // [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
it('creates an array of partitions with a length that is equal to the predicates array length', () => {
const predicateOne = (n: number) => n < 5;
const predicateTwo = (n: number) => n >= 5;
const array = [1, 2, 4, 5, 6];
const results = partitionWith(array, [predicateOne, predicateTwo]);
expect(results.length).toBe(2);
})
const partitionWith: PartitionWith = <T>(items: T[], predicates: ((item: T) => boolean)[]) => {
const results: T[][] = [...Array(predicates.length)].map(x => []);
return results;
}
const predicateIndex = predicates.findIndex(predicate => predicate(item));
it('create partitions based on the provided predicates', () => {
const arrayToPartition = [0, 1, '1', 2, 3, 4, '12', 5, 6, 7, 8, 9, , '11', 10];
const isLessThanFive = (maybeNumber: number | string) => typeof maybeNumber === 'number' && maybeNumber < 5;
const isGreaterOrEqualThanFive = (maybeNumber: number | string) => typeof maybeNumber === 'number' && maybeNumber >= 5;
const isString = (maybeString: number | string) => typeof maybeString === 'string';
const results = partitionWith(arrayToPartition, [isLessThanFive, isGreaterOrEqualThanFive, isString]);
expect(results).toEqual([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10], ['1', '12', '11']]);
});
type PartitionWith = <T>(items: T[], predicates: ((item: T) => boolean)[]) => T[][];
export const partitionWith: PartitionWith = <T>(items: T[], predicates: ((item: T) => boolean)[]) => {
const results: T[][] = [...Array(predicates.length)].map(x => []);
items.forEach((item) => {
const predicateIndex = predicates.findIndex(predicate => predicate(item));
if(predicateIndex !== -1) {
results[predicateIndex].push(item);
}
})
return results;
}
it('returns an extra array of items that did not satisfy any condition', () => {
const items = [0, 1, '1', 2, 3, 4, '12', 5, 6, 7, 8, 9, , '11', 10];
const isLessThanFive = (maybeNumber: number | string) => typeof maybeNumber === 'number' && maybeNumber < 5;
const isGreaterOrEqualThanFive = (maybeNumber: number | string) => typeof maybeNumber === 'number' && maybeNumber >= 5;
const results = partitionWith(items, [isLessThanFive, isGreaterOrEqualThanFive]);
expect(results).toEqual([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10], ['1', '12', '11']])
})
results.push([])
export const partitionWith: PartitionWith = <T>(items: T[], predicates: ((item: T) => boolean)[]) => {
const results: T[][] = [...Array(predicates.length)].map(x => []);
results.push([])
items.forEach((item) => {
const predicateIndex = predicates.findIndex(predicate => predicate(item));
if(predicateIndex !== -1) {
results[predicateIndex].push(item);
} else {
const falsyResultsArrayIndex = predicates.length;
results[falsyResultsArrayIndex].push(item);
}
})
return results;
}