diff --git a/.changeset/large-pianos-appear.md b/.changeset/large-pianos-appear.md new file mode 100644 index 00000000..7fe4ba40 --- /dev/null +++ b/.changeset/large-pianos-appear.md @@ -0,0 +1,5 @@ +--- +'@equinor/esv-intersection': patch +--- + +handle empty nested data when trying to find a sample diff --git a/.prettierignore b/.prettierignore index 3e14ff53..f9f41fa9 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,4 @@ .storybook/src/esv-intersection-data docs_out dist +.direnv diff --git a/src/datautils/findsample.ts b/src/datautils/findsample.ts index 02b17ef8..d93dc5cc 100644 --- a/src/datautils/findsample.ts +++ b/src/datautils/findsample.ts @@ -1,26 +1,34 @@ 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; } @@ -28,9 +36,9 @@ export function findIndexOfSample(data: number[][], pos: number): number { 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; } diff --git a/vitest.config.ts b/vitest.config.ts index 1f6b5ff0..d81dea09 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -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'],