JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 788508
  • Score
    100M100P100Q180048F
  • License MIT

serialize objects to javascript

Package Exports

  • serialize-to-js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (serialize-to-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

serialize-to-js

serialize objects to javascript

NPM version Build Status

Serialize objects into a require-able module while checking circular structures and respecting references.

Table of Contents

Methods

serialize

serialize(source, opts, opts.ignoreCircular, opts.reference)

serializes an object to javascript

Example - serializing regex, date, buffer, ...

var serialize = require('serialize-to-js').serialize;
var obj = { object: {
        regexp: /^test?$/,
        date: new Date(),
        buffer: new Buffer('data'),
        number: 3.1415,
        string: "test" } };
console.log(serialize(obj));
//> {object: {regexp: /^test?$/, date: new Date('2015-04-18T20:01:51.903Z'), buffer: new Buffer('ZGF0YQ==', 'base64'), number: 3.1415, string: 'test'}}

Example - serializing while respecting references

var serialize = require('serialize-to-js').serialize;
var obj = { object: { regexp: /^test?$/ } };
obj.reference = obj.object;
var opts = { reference: true };
console.log(serialize(obj, opts));
//> {object: {regexp: /^test?$/}}
console.log(opts.references);
//> [ [ 'reference', 'object' ] ]

Parameters

source: Object | Array | function | Any, source to serialize

opts: Object, options

opts.ignoreCircular: Boolean, ignore circular objects

opts.reference: Boolean, reference instead of a copy (requires post-processing of opts.references)

Returns: String, serialized representation of source

serializeToModule

serializeToModule(source, opts, opts.ignoreCircular, opts.reference, opts.beautify)

serialize to a module which can be requireed.

Example - serializing while respecting references

var serialTM = require('serialize-to-js').serializeToModule;
var obj = { object: { regexp: /^test?$/ } };
obj.reference = obj.object;
console.log(serialTM(obj, { reference: true }));
//> var m = module.exports = {
//>     object: {
//>         regexp: /^test?$/
//>     }
//> };
//> m.reference = m.object;

Parameters

source: Object | Array | function | Any, source to serialize

opts: Object, options

opts.ignoreCircular: Boolean, ignore circular objects

opts.reference: Boolean, reference instead of a copy (requires post-processing of opts.references)

opts.beautify: Boolean | Object, beautify output - default is false. If Object then use je-beautify options.

Returns: String, serialized representation of source as module

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work or correctly attributed with the source of its origin and licence.

License

Copyright (c) 2015 commenthol (MIT License)

See LICENSE for more info.