Skip to content

Commit

Permalink
feat: add utility stringify that converts typed array to nomal array
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Mar 4, 2024
1 parent 6fdd7de commit 5cac645
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/utils/__tests__/__snapshots__/stringify.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`stringify 1`] = `
"{
\\"a\\": [
1,
2,
3
],
\\"b\\": [
4,
5,
6
],
\\"c\\": [
1,
2,
3
],
\\"d\\": \\"Hello\\"
}"
`;
File renamed without changes.
18 changes: 18 additions & 0 deletions src/utils/__tests__/stringify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { stringify } from '../stringify';

test('stringify', () => {
const object = {
a: new Float32Array([1, 2, 3]),
b: new Uint8Array([4, 5, 6]),
c: [1, 2, 3],
d: 'Hello',
};
const result = stringify(object);
expect(result).toBe('{"a":[1,2,3],"b":[4,5,6],"c":[1,2,3],"d":"Hello"}');

const result2 = stringify(object, (key, value) => (value === 2 ? 22 : value));
expect(result2).toBe('{"a":[1,22,3],"b":[4,5,6],"c":[1,22,3],"d":"Hello"}');

const result3 = stringify(object, undefined, 2);
expect(result3).toMatchSnapshot();
});
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './createStepArray';
export * from './getRescaler';
export * from './isPowerOfTwo';
export * from './nextPowerOfTwo';
export * from './stringify';
26 changes: 26 additions & 0 deletions src/utils/stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NumberArray } from 'cheminfo-types';

/**
* Stringify an object and convert all typed arrays to arrays
* @param object
* @param replacer
* @param space
* @returns
*/
export function stringify(
object: any,

Check warning on line 11 in src/utils/stringify.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected any. Specify a different type
replacer?: (this: any, key: string, value: any) => any,

Check warning on line 12 in src/utils/stringify.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected any. Specify a different type

Check warning on line 12 in src/utils/stringify.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected any. Specify a different type

Check warning on line 12 in src/utils/stringify.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected any. Specify a different type
space?: string | number,
): string {
const internalReplacer = (key: string, value: any) => {

Check warning on line 15 in src/utils/stringify.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected any. Specify a different type
if (ArrayBuffer.isView(value)) {
value = Array.from(value as NumberArray);
}
if (replacer) {
return replacer(key, value);
}
return value;
};

return JSON.stringify(object, internalReplacer, space);
}

0 comments on commit 5cac645

Please sign in to comment.