Skip to content

Commit

Permalink
feat: add xyEnsureFloat64 function (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 authored Mar 6, 2024
1 parent db7c801 commit 467a03a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/xy/__tests__/xyEnsureFloat64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { xyEnsureFloat64 } from '../xyEnsureFloat64';

test('normal array', () => {
const data = {
x: [-1, 2, -3, 4],
y: [-1, 2, -3, 4],
};
const float64 = xyEnsureFloat64(data);
float64.x[0] = 0;
expect(float64.x).toBeInstanceOf(Float64Array);
expect(float64.y).toBeInstanceOf(Float64Array);
expect(data.x).toStrictEqual([-1, 2, -3, 4]);
expect(float64.x).toStrictEqual(Float64Array.from([0, 2, -3, 4]));
expect(float64.y).toStrictEqual(Float64Array.from([-1, 2, -3, 4]));
});

test('typed array', () => {
const data = {
x: Float64Array.from([-1, 2, -3, 4]),
y: Float64Array.from([-1, 2, -3, 4]),
};
const float64 = xyEnsureFloat64(data);
float64.x[0] = 0;
expect(float64.x).toBeInstanceOf(Float64Array);
expect(float64.y).toBeInstanceOf(Float64Array);
expect(data.x).toStrictEqual(Float64Array.from([-1, 2, -3, 4]));
expect(float64.x).toStrictEqual(Float64Array.from([0, 2, -3, 4]));
expect(float64.y).toStrictEqual(Float64Array.from([-1, 2, -3, 4]));
});
14 changes: 14 additions & 0 deletions src/xy/xyEnsureFloat64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DataXY, NumberArray } from 'cheminfo-types';

import { xEnsureFloat64 } from '../x/xEnsureFloat64';

interface DataXYNumberArray {
x: NumberArray;
y: NumberArray;
}
export function xyEnsureFloat64(data: DataXYNumberArray): DataXY<Float64Array> {
return {
x: xEnsureFloat64(data.x),
y: xEnsureFloat64(data.y),
};
}

0 comments on commit 467a03a

Please sign in to comment.