Skip to content

Commit

Permalink
refactor!: split also returns empty sets for sectors
Browse files Browse the repository at this point in the history
makes it easier to reason about the result in relation to the sectors
  • Loading branch information
tabcat committed Apr 29, 2024
1 parent a8e6502 commit e58af4d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/split.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pairwiseTraversal } from "./util";

/**
* Splits a set into multiple sets at sector points
* Splits a set into multiple sets at sector points.
*
* @param source - Set to split at each sector point (inclusive)
* @param sectors - Set of sector points
Expand All @@ -15,8 +15,8 @@ export function* split <T>(source: T[], sectors: T[], comparator: (a: T, b: T) =
section.push(a)
}

// only split if b is not null and section has elements OR source is exausted
if ((b !== null && section.length > 0) || a === source[source.length - 1]) {
// only split if b is not null OR source is exausted
if (b !== null || a === source[source.length - 1]) {
yield section
section = []

Expand Down
4 changes: 2 additions & 2 deletions test/split.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("split", () => {
});

test(testNames.firstEmpty, () => {
expect([...split([], numbers, comparator)]).toEqual([]);
expect([...split([], numbers, comparator)]).toEqual(numbers.map(_ => []));
});

test(testNames.secondEmpty, () => {
Expand Down Expand Up @@ -49,7 +49,7 @@ describe("split", () => {

test(testNames.noOverlap, () => {
expect([...split(even, odd, comparator)]).toEqual(even.map((n) => [n]));
expect([...split(odd, even, comparator)]).toEqual(odd.map((n) => [n]));
expect([...split(odd, even, comparator)]).toEqual([[], ...odd.map((n) => [n])]);
});
});
});

0 comments on commit e58af4d

Please sign in to comment.