npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serialise-js

v0.2.1

Published

An extensible library for serialising JavaScript data

Downloads

3

Readme

serialise-js

npm version build status coverage status dependencies status

An extensible library for serialising JavaScript data

Installation

npm install serialise-js --save

Usage

Serialise any variable:

var serialise = require('serialise-js');
serialise({
  foo: [1, 2, 3],
  bar: { key: 'value' },
  "the key": "the value"
});

Returns:

{
  foo: [
    1,
    2,
    3
  ],
  bar: {
    key: 'value'
  },
  'the key': 'the value'
}

Options

You can pass in an options object as a second argument to serialise:

  • indent - Set the characters used for a single indent, default 2 spaces
  • doubleQuotes - Set this to true to serialise strings with double quotes
  • inline - Set this to true to serialise object and arrays inline, set to a number to serialise inline below lines of this length

Example:

var input = { foo: ['bar', { zim: 'gir' }] };
serialise(input, { indent: ' ', doubleQuotes: true, inline: 25 });

Returns:

{
 foo: [
  "bar",
  { zim: "gir" }
 ]
}

Caveats

  • Recursive instances of objects and arrays are replaced by the string '[Circular]'
  • Functions are serialised, but this may not be valid javascript if they are native functions or use variables from another scope, you can prevent this by overriding serialise.function e.g. serialise.function = function() { return 'null'; }

Extending

It's easy to extend and modify the library.

Say you want to support serialisation of the popular moment.js library. By default moment will be serialised as a normal object, which is pretty gross. We can allow it to serialise as it would be defined (or close enough).

  1. The first thing we need to do is create a serialisation method, these are all exposed on the serialise function (e.g. serialise.string). We can just add another one:

    serialise.moment = function(moment, options) {
      return 'moment(' +serialise.string(moment.format(), options) + ')';
    };

    This uses serialise.string to serialise the formatted date string.

  2. Next we need to create a type matching function to detect this type, and add it to serialise.types:

    serialise.types.moment = function(val) {
      return val && val._isAMomentObject;
    };
  3. Finally we need to add this type to the typeOrder array, which defines the order in this the types are checked, in our case we need it to be checked before object, so we can just add it to the start of the array:

    serialise.typeOrder.unshift('moment');

Now we can use it:

console.log(serialise({ time: moment('2015-01-01') }));

Which outputs:

{
  time: moment('2015-01-01T00:00:00+00:00')
}

This could be neater, still, but you get the idea.