Skip to content

Commit

Permalink
refactor(utils): refactor bisectLeft
Browse files Browse the repository at this point in the history
  • Loading branch information
benji6 committed Sep 13, 2024
1 parent 9ef26c3 commit ffd8e92
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion client/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("utils", () => {

const testArray = [
"2015",
"2016",
"2017",
"2017",
"2018",
"2019",
Expand All @@ -53,6 +53,8 @@ describe("utils", () => {

expect(bisectLeft(testArray, "2014")).toBe(0);
expect(bisectLeft(testArray, "2015")).toBe(0);
expect(bisectLeft(testArray, "2016")).toBe(1);
expect(bisectLeft(testArray, "2017")).toBe(1);
expect(bisectLeft(testArray, "2020")).toBe(5);
expect(bisectLeft(testArray, "2025")).toBe(10);
expect(bisectLeft(testArray, "2026")).toBe(11);
Expand Down
6 changes: 3 additions & 3 deletions client/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { interpolateHcl } from "d3-interpolate";
import { removeStopwords } from "stopword";

export const bisectLeft = (xs: string[], x: string, left = 0) => {
let right = xs.length - 1;
while (left <= right) {
let right = xs.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (xs[mid] < x) left = mid + 1;
else right = mid - 1;
else right = mid;
}
return left;
};
Expand Down

0 comments on commit ffd8e92

Please sign in to comment.