
serialize-as-code
This serializer is intended for serializing ANY Javascript objects into
its source code representation. It is cyclomatic-save extension and alternative
to e.g. JSON.stringify
. It can even be used to perform deep comparisions
for most cases. ATTENTION: There is no functionality to parse the string back
to the provided object. (one-direction-flow)
Some aspects
Installation
With yarn
yarn add serialize-as-code # if you want to use it for prod
yarn add --dev serialize-as-code # if only used in tests
Usage
You may import the named import Serializer
and call run with ANY object.
It prints the object as its source code. So you should be able to copy-paste
the printed result and most deep comparisons should work.
import { Serializer } from 'serialize-as-code';
console.log(Serializer.run({foo: 'bar'}));
console.log(Serializer.run([1, 2]));
console.log(Serializer.run(Symbol.for('my-key')));
console.log(Serializer.run(<Fragment key="foo"><div>Test that</div></Fragment>));
If you try to serialize cyclomatic structures, you will see within the result
were they were detected.
Custom serialization
You have the possibility to apply ANY custom serialization just on top.
import { Serializer } from 'serialize-as-code';
const someObjectToMatch = { foo: 'foo' };
const CustomSerializer = (o: any): string | void => {
if (o === someObjectToMatch) return 'FOO';
};
const YourSerializer = Serializer.create(CustomSerializer);
console.log({canBe: 'nested', here: someObjectToMatch});