-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add utility stringify that converts typed array to nomal array
- Loading branch information
Showing
5 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
replacer?: (this: any, key: string, value: any) => any, | ||
Check warning on line 12 in src/utils/stringify.ts GitHub Actions / nodejs / lint-eslint
Check warning on line 12 in src/utils/stringify.ts GitHub Actions / nodejs / lint-eslint
|
||
space?: string | number, | ||
): string { | ||
const internalReplacer = (key: string, value: any) => { | ||
if (ArrayBuffer.isView(value)) { | ||
value = Array.from(value as NumberArray); | ||
} | ||
if (replacer) { | ||
return replacer(key, value); | ||
} | ||
return value; | ||
}; | ||
|
||
return JSON.stringify(object, internalReplacer, space); | ||
} |