Skip to content

Commit

Permalink
fix: handle empty nested data when trying to find a sample (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
venikx authored Jun 8, 2023
1 parent 74b8e09 commit 96a5d01
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-pianos-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@equinor/esv-intersection': patch
---

handle empty nested data when trying to find a sample
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.storybook/src/esv-intersection-data
docs_out
dist
.direnv
30 changes: 19 additions & 11 deletions src/datautils/findsample.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
export function findIndexOfSample(data: number[][], pos: number): number {
if (data.length < 2) {
return -1;
}

const linearSearchLimit = 20;
let a = 0;
let b = data.length - 1;
const linearSearchLimit = 20;
let aPos = data[a][0];
let bPos = data[b][0];
let aPos = data[a]?.[0];
let bPos = data[b]?.[0];

while (b - a > linearSearchLimit) {
const splitAt = Math.floor((b + a) / 2);
const splitPos = data[splitAt][0];
const splitPos = data[splitAt]?.[0];

if (aPos == null || bPos == null || splitPos == null) {
return -1;
}

if (pos >= aPos && pos < splitPos) {
b = splitAt;
bPos = data[b][0];
bPos = data[b]?.[0];
} else if (pos >= splitPos && pos <= bPos) {
a = splitAt;
aPos = data[a][0];
aPos = data[a]?.[0];
} else if (pos <= aPos && pos > splitPos) {
b = splitAt;
bPos = data[b][0];
bPos = data[b]?.[0];
} else if (pos <= splitPos && pos >= bPos) {
a = splitAt;
aPos = data[a][0];
aPos = data[a]?.[0];
} else {
return -1;
}
}

let index = -1;
for (let i = a; i < b; i++) {
const v1 = data[i][0];
const v2 = data[i + 1][0];
if (Math.min(v1, v2) <= pos && pos <= Math.max(v1, v2)) {
const v1 = data[i]?.[0];
const v2 = data[i + 1]?.[0];
if (v1 != null && v2 != null && Math.min(v1, v2) <= pos && pos <= Math.max(v1, v2)) {
index = i;
break;
}
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default mergeConfig(
viteConfig,
defineConfig({
test: {
include: ['(src|test)/**/__tests__/**/*.?(c|m)[jt]s?(x)', '(src|test)/**/?(*.){test,spec}.?(c|m)[jt]s?(x)'],
environment: 'jsdom',
watch: false,
setupFiles: ['./__mocks__/pixi.js.ts', '__mocks__/canvas.ts'],
Expand Down

0 comments on commit 96a5d01

Please sign in to comment.