redux-devtools/packages/redux-devtools-app-core/src/utils/stringifyJSON.ts
Matt Oakes 96ac1f291a
Move the logic from @redux-devtools/app into @redux-devtools/app-core (#1655)
This change splits out the main logic from the Redux Devtools App into a new
core package but keeps the socket connection management in @redux-devtools/app.
The aim is to allow for easier reuse of the rest of the app in other envioronments
with their own transport methods, such as React Native or Electron.
2024-06-12 09:18:46 -04:00

22 lines
788 B
TypeScript

import jsan from 'jsan';
import { DATA_TYPE_KEY, DATA_REF_KEY } from '../constants/dataTypes';
function replacer(key: string, value: unknown) {
if (typeof value === 'object' && value !== null && DATA_TYPE_KEY in value) {
const __serializedType__ = (value as any)[DATA_TYPE_KEY];
const clone = { ...value };
delete (clone as any)[DATA_TYPE_KEY]; // eslint-disable-line no-param-reassign
const r = { data: clone, __serializedType__ };
if (DATA_REF_KEY in value)
(r as any).__serializedRef__ = (clone as any)[DATA_REF_KEY];
return r;
}
return value;
}
export function stringifyJSON(data: unknown, serialize: boolean | undefined) {
return serialize
? jsan.stringify(data, replacer, null as unknown as undefined, true)
: jsan.stringify(data);
}