Skip to content

Commit

Permalink
fix(xySortX): avoid NaN results when X is a typedArray (#249)
Browse files Browse the repository at this point in the history
* chore: add failing test case

* fix: avoid NaN results when X is a typedArray

* use callback in Array.from

Co-authored-by: Michaël Zasso <[email protected]>

* chore: move test case to __tests__ folder

---------

Co-authored-by: Michaël Zasso <[email protected]>
  • Loading branch information
jobo322 and targos authored Jun 20, 2024
1 parent 566bafd commit af7ca47
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/xy/__tests__/xySortX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ test('sorted reverse', () => {
y: Float64Array.from([3, 2, 1]),
});
});

test('typed XY arrays', () => {
const data = {
x: Float64Array.from([
1.557, 1.265, 1.535, 1.622, 2, 1.426, 1.094, 1.201, 1.825,
]),
y: Float64Array.from([0, 1, 2, 5, 6, 7, 8, 9, 10]),
};
const result = xySortX(data);
expect(result).toStrictEqual({
x: Float64Array.from([
1.094, 1.201, 1.265, 1.426, 1.535, 1.557, 1.622, 1.825, 2,
]),
y: Float64Array.from([8, 9, 1, 7, 2, 0, 5, 10, 6]),
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest';

import integral from './integral';
import integral from '../integral';

test('should compute expect(integral with a,b, x0 and x1', () => {
expect(integral(0, 1, 1, 0)).toBe(0.5);
Expand Down
10 changes: 4 additions & 6 deletions src/xy/xySortX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ export function xySortX(data: DataXY): DataXY<Float64Array> {
}
}

const xyObject = (x as number[])
.map((val, index) => ({
x: val,
y: y[index],
}))
.sort((a, b) => a.x - b.x);
const xyObject = Array.from(x, (val, index) => ({
x: val,
y: y[index],
})).sort((a, b) => a.x - b.x);

const response = {
x: new Float64Array(x.length),
Expand Down

0 comments on commit af7ca47

Please sign in to comment.