-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from giraugh/feat/split-array-fn
`splitArray` utility
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@giraugh/tools": minor | ||
--- | ||
|
||
Add splitArray util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Split an array when a predicate function determines an element is a predicate | ||
* (similiar to String.prototype.split but for arrays) | ||
* @param arr the array to split | ||
* @param splitFn a function over elements in the array that determines whether an element is a delimeter. | ||
* @returns the array of groups split as per the `splitFn` | ||
* | ||
* @example | ||
* const groups = splitArray([1, 3, 5, 6, 7, 9, 11], (x) => x % 2 === 0) // [[1, 3, 5], [7, 9, 11]] | ||
*/ | ||
export const splitArray = <T>(arr: T[], splitFn: (t: T) => boolean): T[][] => { | ||
// Handle special case of empty array | ||
if (arr.length === 0) return [] | ||
|
||
let groups: T[][] = [[]] | ||
while (true) { | ||
// Shift one element and peak the next element | ||
if (arr.length === 0) break | ||
const next = arr.shift()! | ||
|
||
// Split? | ||
if (splitFn(next)) { | ||
groups.push([]) | ||
} else { | ||
// Add element to current group | ||
groups[groups.length - 1]!.push(next) | ||
} | ||
} | ||
|
||
return groups | ||
} | ||
|
||
if (import.meta.vitest) { | ||
const { expect, it } = import.meta.vitest | ||
|
||
it('works for example 1', () => { | ||
const runs = splitArray([1, 3, 5, 6, 7, 9, 11], (x) => x % 2 === 0) | ||
expect(runs).toEqual([[1, 3, 5], [7, 9, 11]]) | ||
}) | ||
|
||
it('handles an empty array', () => { | ||
const runs = splitArray([], (x) => x === 0) | ||
expect(runs).toEqual([]) | ||
}) | ||
|
||
it('works if predicate is always true', () => { | ||
const runs = splitArray(['a', 'b', 'c'], () => true) | ||
expect(runs).toEqual([[], [], [], []]) | ||
}) | ||
|
||
} |