パッケージの詳細

serialize-as-code

fdc-viktor-luft8.2kMIT2.0.2

Serialize any Javascript object as its source code

serialize, cyclomatic, stringify, representation

readme

GitHub license npm package Travis Coverage Status

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

  • TypeScript support included
  • ~ 1 kB (gzipped) (see bundlephobia)

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';

// Arrays or Objects
console.log(Serializer.run({foo: 'bar'})); // prints: {foo: 'bar'}
console.log(Serializer.run([1, 2])); // prints: [1, 2]

// Symbols (not suppoerted on JSON.stringify)
console.log(Serializer.run(Symbol.for('my-key')));
// prints: Symbol.for('my-key')

// JSX (but react is not needed and their is no dependency)
console.log(Serializer.run(<Fragment key="foo"><div>Test that</div></Fragment>));
// prints: <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';
    // return undefined to proceed with regular serialization
};

const YourSerializer = Serializer.create(CustomSerializer);

console.log({ canBe: 'nested', here: someObjectToMatch });
// prints: {canBe: 'nested', here: FOO}

更新履歴

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[2.0.2] - 2022-03-29

Fixed

  • Prevent accessing property before checking its existence. This was causing auto-growing Proxy to run into infinite loop.
  • Generating type interface instead of shipping real types to prevent TS errors for consumers.

[2.0.1] - 2021-11-24

Changed

  • Serializing window and document as globals

Fixed

  • Serializing some properties of the document where causing errors by accessing the constructor which some of them didn't have.

[2.0.0] - 2021-08-26

Fixed

  • Serializing of HTMLElements created by jest-dom caused infinite loops and now serialized only by their constructor names.

[1.2.0] - 2019-08-15

Added

  • BigInt and AsyncFunction serialization

[1.1.0] - 2019-03-26

Added

  • Native Map and Set serialization

[1.0.3] - 2018-08-20

Changed

  • Improved Symbol serialization