Skip to content

Commit 69ed397

Browse files
committed
feat: add new function recursiveUntypeArrays
1 parent 2dbf39c commit 69ed397

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ exports[`test existence of exported functions 1`] = `
170170
"isPowerOfTwo",
171171
"nextPowerOfTwo",
172172
"recursiveResolve",
173+
"recursiveUntypeArrays",
173174
"stringify",
174175
"calculateAdaptiveWeights",
175176
]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { expect, test } from 'vitest';
2+
3+
import { recursiveUntypeArrays } from '../recursiveUntypeArrays';
4+
5+
test('basic cases', () => {
6+
expect(recursiveUntypeArrays(1)).toStrictEqual(1);
7+
expect(recursiveUntypeArrays('a')).toStrictEqual('a');
8+
expect(recursiveUntypeArrays({})).toStrictEqual({});
9+
expect(recursiveUntypeArrays([])).toStrictEqual([]);
10+
expect(recursiveUntypeArrays([1, 2, 3])).toStrictEqual([1, 2, 3]);
11+
expect(recursiveUntypeArrays(Float32Array.from([1, 2, 3]))).toStrictEqual([
12+
1, 2, 3,
13+
]);
14+
});
15+
16+
test('simple object', async () => {
17+
const object = {
18+
a: {
19+
b: {
20+
c: Int32Array.from([1, 2, 3]),
21+
},
22+
},
23+
d: [new Int32Array([1, 2, 3]), new Float32Array([3, 4, 5]), 2],
24+
};
25+
26+
expect(recursiveUntypeArrays(object)).toStrictEqual({
27+
a: {
28+
b: {
29+
c: [1, 2, 3],
30+
},
31+
},
32+
d: [[1, 2, 3], [3, 4, 5], 2],
33+
});
34+
});

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export * from './getRescaler';
66
export * from './isPowerOfTwo';
77
export * from './nextPowerOfTwo';
88
export * from './recursiveResolve';
9+
export * from './recursiveUntypeArrays';
910
export * from './stringify';
1011
export * from './calculateAdaptiveWeights';

src/utils/recursiveUntypeArrays.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { NumberArray } from 'cheminfo-types';
2+
3+
/**
4+
* Recursively change the typed arrays to normal arrays
5+
* The changes are done in-place !
6+
* @param object
7+
* @returns
8+
*/
9+
export function recursiveUntypeArrays(object: unknown) {
10+
if (typeof object !== 'object') return object;
11+
object = modifier(object);
12+
return object;
13+
}
14+
15+
function modifier(object: any) {
16+
if (typeof object !== 'object') return object;
17+
if (ArrayBuffer.isView(object)) {
18+
return Array.from(object as NumberArray);
19+
}
20+
for (const key in object) {
21+
if (ArrayBuffer.isView(object[key])) {
22+
object[key] = Array.from(object[key] as NumberArray);
23+
} else if (typeof object[key] === 'object') {
24+
modifier(object[key]);
25+
}
26+
}
27+
return object;
28+
}

0 commit comments

Comments
 (0)