Socket
Socket
Sign inDemoInstall

universal-serialize

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    universal-serialize

Javascript module template.


Version published
Weekly downloads
24K
decreased by-3.38%
Maintainers
1
Install size
98.1 kB
Created
Weekly downloads
 

Changelog

Source

2.0.0 (2022-02-28)

⚠ BREAKING CHANGES

  • move to krakenjs scope

  • add tooling for code coverage, changelogs, and conventional commits (f676fbc)

  • commitlint and standard version setup (bec929f)

  • move to krakenjs scope (9097947)

  • setup codecov and badges (6bcd1b2)

  • update license (bf24cc0)

  • upgrade to grumbler-scripts v5 (e56e1d7)

Readme

Source

Universal Serialize

Universal serializer and deserializer, which supports many build-in javascript types, and any number of custom types you want to support

Simple serialization

import { serialize, deserialize } from 'universal-serialize';


// Define a complex object
const originalObject = {
  foo: 'bar',
  date: new Date(),
  error: new Error('world')
};

// Serialize the complex object
const jsonString = serialize(originalObject);

// Deserialize the object
const deserializedObject = deserialize(jsonString);

// Make use of the deserialized data and objects
console.log(
  deserializedObject.foo,
  deserializedObject.date.toUTCString(),
  deserializedObject.error.stack
);

By default universal serialize will serialize:

  • Errors
  • Regexes
  • Dates
  • Arrays
  • Objects
  • Strings
  • Numbers
  • Booleans
  • Nulls

Custom serialization

import { serialize, deserialize, serializeType, TYPE } from 'universal-serialize';

// Define a new serialization type
const SERIALIZED_FUNCTION = 'SERIALIZED_FUNCTION';

// Define a complex object containing a function
const originalObject = {
    sayHello: () => {
        console.log('Hello world!');
    };
};

// Serialize the object with a special handler to serialize function types
const jsonString = serialize(originalObject, {
    [ TYPE.FUNCTION ]: (val) => {
        // Serialize the function as a 'serialized function'
        return serializeType(SERIALIZED_FUNCTION, val.toString());
    }
});

// Deserialize any `SERIALIZED_FUNCTION` types from the serialized object
const deserializedObject = deserialize(jsonString, {
    [ SERIALIZED_FUNCTION ]: (fnString) => {
      return eval(fnString);
    }
});

// Call the deserialized functionn
deserializedObject.sayHello();

Quick Start

Getting Started
  • Fork the module
  • Run setup: npm install
  • Start editing code in ./src and writing tests in ./tests
  • npm run build
Building
npm run build
Tests
  • Edit tests in ./test/tests

  • Run the tests:

    npm run test
    
Testing with different/multiple browsers
npm run karma -- --browser=PhantomJS
npm run karma -- --browser=Chrome
npm run karma -- --browser=Safari
npm run karma -- --browser=Firefox
npm run karma -- --browser=PhantomJS,Chrome,Safari,Firefox
Keeping the browser open after tests
npm run karma -- --browser=Chrome --keep-open
Publishing
Before you publish for the first time:
  • Delete the example code in ./src, ./test/tests and ./demo
  • Edit the module name in package.json
  • Edit README.md and CONTRIBUTING.md
Then:
  • Publish your code: npm run release to add a patch
    • Or npm run release:path, npm run release:minor, npm run release:major

Keywords

FAQs

Last updated on 20 Jul 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc