Skip to content

Commit af7ca47

Browse files
jobo322targos
andauthored
fix(xySortX): avoid NaN results when X is a typedArray (#249)
* 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]>
1 parent 566bafd commit af7ca47

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/xy/__tests__/xySortX.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ test('sorted reverse', () => {
4343
y: Float64Array.from([3, 2, 1]),
4444
});
4545
});
46+
47+
test('typed XY arrays', () => {
48+
const data = {
49+
x: Float64Array.from([
50+
1.557, 1.265, 1.535, 1.622, 2, 1.426, 1.094, 1.201, 1.825,
51+
]),
52+
y: Float64Array.from([0, 1, 2, 5, 6, 7, 8, 9, 10]),
53+
};
54+
const result = xySortX(data);
55+
expect(result).toStrictEqual({
56+
x: Float64Array.from([
57+
1.094, 1.201, 1.265, 1.426, 1.535, 1.557, 1.622, 1.825, 2,
58+
]),
59+
y: Float64Array.from([8, 9, 1, 7, 2, 0, 5, 10, 6]),
60+
});
61+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from 'vitest';
22

3-
import integral from './integral';
3+
import integral from '../integral';
44

55
test('should compute expect(integral with a,b, x0 and x1', () => {
66
expect(integral(0, 1, 1, 0)).toBe(0.5);

src/xy/xySortX.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ export function xySortX(data: DataXY): DataXY<Float64Array> {
2424
}
2525
}
2626

27-
const xyObject = (x as number[])
28-
.map((val, index) => ({
29-
x: val,
30-
y: y[index],
31-
}))
32-
.sort((a, b) => a.x - b.x);
27+
const xyObject = Array.from(x, (val, index) => ({
28+
x: val,
29+
y: y[index],
30+
})).sort((a, b) => a.x - b.x);
3331

3432
const response = {
3533
x: new Float64Array(x.length),

0 commit comments

Comments
 (0)