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 🙏

© 2025 – Pkg Stats / Ryan Hefner

obj-stringify

v1.0.0

Published

Performs deep serialization of object to string. With configurable depth and indent size.

Downloads

27

Readme

obj-stringify

This small library helps you to deeply serialize your objects perserving inner functions and objects.

How is it different from well-known JSON.stringify method? So it

  • Avoids double quotes around properties and values.
  • Correctly shows inner RegExp, Functions and so on.
  • Allows you to choose single or double quotes for string values.

Installation

$ npm install obj-stringify --save

Usage

You can import it using CommonJS format

const objStringify = require('obj-stringify')

If you work with RequireJS module format:

define([
  'node_modules/obj-stringify/index'
], function(objStringify) {
  ...
})

Finally, if you don't use any module systems you can insert it via script tag and objStringify function would be accessible in global namespace.

// index.html
<script src="node_modules/obj-strinfigy/index.js"></script>
<script src="src/app.js"></script>

// src/app.js
console.log(typeof objStringify === 'function') // true

Params

objStringify(obj [, options])

obj

Object you want to stringify.

Type: Object Array

Required: true

Note: for other argument types will be returned serialized value!

options

Formatted configuration. Use it to specify required format and depth of transformation

Type: Object

Required: false

You can configure:

options.indent

If it is a number then such amount of spaces will be used to make string look more readable. In case it is a string, it will be used instead of spaces.

Type: Number String

Default: 2

options.depth

The level of accurately transformation. Use it when you don't want to stringify very deep objects

Type: Number

Default: 1000

options.inline

Allows you to return one line string.

Type: Boolean

Default: false

options.singleQuotes

It defines whether string values inside object should be wrapped in single quotes. Set it to false when you want to get double quotes for string values.

Type: Boolean

Default: true

Examples

const obj = {
  module: {
    rules: [
      {
        test: /test\.js$/,
        exclude: 'libs/js/angular'
      }
    ]
  }
}

const options = {
  indent: '  ', // use 2 spaces as indent
  singleQuotes: false // use double quotes
};

const res = objStringify(obj, options)

console.log(res)
/*
{
  module: {
    rules: [
      {
        test: /test\.js$/,
        exclude: "libs/js/angular"
      }
    ]
  }
}
*/

Show functions correctly:

const helper = {
  methods: [
    {
      sum: function(a, b) {
        return a + b;
      }
    },

    {
      reverse: (str) => str.split('').reverse().join(''),
    }
  ]
}

// convert using default options
const res = objStringify(helper)

console.log(res)
/*
{
  methods: [
    {
      sum: function (a, b) {
          return a + b;
        }
    },
    {
      reverse: (str) => str.split('').reverse().join('')
    }
  ]
}
*/

Using depth:

const status = {
  tasks: {
    inProgress: [
      {id: 22, name: 'create header'}
    ],

    completed: [
      {id: 3, name: 'setup environment'}
    ]
  },

  backlog: [
    {id: 6, name: 'customize player'}
  ]
}

const options = {
  depth: 2 // restrict accurate serialization to 2 levels. Deeper this level would be called toString method.
}

const res = objStringify(status, options)

console.log(res)
/*
{
  tasks: {
    inProgress: [object Object],
    completed: [object Object]
  },
  backlog: [
    [object Object]
  ]
}
*/

Serialize dates as well:

const carHistory = {
  issued: new Date(2012, 3, 22),

  beginUsedIn: new Date(2012, 8, 3),

  info: {
    accidents: [
      {
        date: new Date(2014, 6, 6),
        damages: []
      }
    ]
  }
}

const res = objStringify(carHistory);

console.log(res)
// It prints Belarus Standard Time as I am staying there.
/*
{
  issued: 'Sun Apr 22 2012 00:00:00 GMT+0300 (Belarus Standard Time)',
  beginUsedIn: 'Mon Sep 03 2012 00:00:00 GMT+0300 (Belarus Standard Time)',
  info: {
    accidents: [
      {
        date: 'Sun Jul 06 2014 00:00:00 GMT+0300 (Belarus Standard Time)',
        damages: []
      }
    ]
  }
}
*/

Pass array as argument:

const cbs = [
  function(val) {
    return val * 2;
  },

  function(val) {
    return !val;
  }
]

const res = objStringify(cbs)

console.log(res)
/*
[
  function (val) {
    return val * 2;
  },
  function (val) {
    return !val;
  }
]
*/

Using inline option:

const container = {
  name: 'list',
  children: {
    name: 'popup',
    type: 3
  }
}

const options = {
  inline: true // returns inline string
}

const res = objStringify(container, options)

console.log(res)

/*
{  name: 'list',  children: {    name: 'popup',    type: 3  }}
*/

Using sign as indent:

const phone = {
  manufacturer: 'apple'
}

const options = {
  indent: '$'
}

const res = objStringify(phone, options)

console.log(res)

/*
{
$manufacturer: 'apple'
}
*/

Keep in mind:

  • It doesn't support circular references, non-enumerable properties, symbol properties.

  • It travers through passed object and gradually serializes its value calling toString method. So you can define custom toString method on your object and it will be called during serialization.

  • It doesn't support object's subtypes like Math, Navigator, history etc.

  • It can't serialize very deep objects (approximately 10000 levels deep) as browsers have some restrictions for recursion.