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

smart-stringify-object

v0.1.0

Published

Stringify object with code chunks insertion

Downloads

9

Readme

This is fork of stringify-object

Stringify object with code chunks insertion

Install

$ yarn add smart-stringify-object

Usage

var obj = {
	foo: 'bar',
	'arr': [1, 2, 3],
	nested: { hello: "world" },
	method: () => 'function () { return this.foo; }'
};

var pretty = stringifyObject(obj, {
	indent: '  ',
	singleQuotes: false,
	functions: 'exec'
});

console.log(pretty);
/*
{
	foo: "bar",
	arr: [
		1,
		2,
		3
	],
	nested: {
		hello: "world"
	},
	method: function () { return this.foo; }
}
*/

Stringify functions

You can choose how exactly will be stringified object's properties of function type.

By default, the functions will be stringified as they were described in a code but you can use the functions as a tool to generate javascript code. There are two variants to use the functions in such a way:

Exec

Option: `function: "exec"

Execute a function and insert the result as a code chunk.

const obj = {
	name: path.basename(__filename),
	module: () => `require(${JSON.stringify(__filename)})`
}

stringify(obj, {
	functions: "exec"
})
/*
{
	name: 'example.js',
	module: require("path/to/example.js")
}
*/

Extract

Option: function: "extract"

Get function's body and insert it as code chunk.

var obj = {
	key: function() { Symbol() }
}

var pretty = stringifyObject(obj, {
	functions: "extract"
}
console.log(pretty);
/*
{
	key:  Symbol()
}
*/

API

stringifyObject(input, [options])

Circular references will be replaced with "[Circular]".

input

Required
Type: object, array

options

indent

Type: string
Default: '\t'

Choose the indentation you prefer.

singleQuotes

Type: boolean
Default: true

Set to false to get double-quoted strings.

filter(obj, prop)

Type: function

Expected to return a boolean of whether to keep the object.

inlineCharacterLimit

Type: number Default: undefined

When set, will inline values up to inlineCharacterLimit length for the sake of more terse output.

For example, given the example at the top of the README:

var obj = {
	foo: 'bar',
	'arr': [1, 2, 3],
	nested: { hello: "world" }
};

var pretty = stringifyObject(obj, {
	indent: '  ',
	singleQuotes: false,
	inlineCharacterLimit: 12
});

console.log(pretty);
/*
{
	foo: "bar",
	arr: [1, 2, 3],
	nested: {
		hello: "world"
	}
}
*/

As you can see, arr was printed as a one-liner because its string was shorter than 12 characters.

functions

Type: "extract" | "exec" | false
Default: false

Choose method to stringify functions

  • extract Extract function body as raw code
  • exec Execute function and use its result as raw code

Exctract example:

var obj = {
	key: function() { Symbol() }
}

var pretty = stringifyObject(obj, {
	functions: "extract"
}
console.log(pretty);
/*
{
	key:  Symbol()
}
*/

Exec example:

var moduleName = "path/to/module.js"
var obj = {
	myModule: () => `require(${JSON.stringify(moduleName)})`
}

var pretty = stringifyObject(obj, {
	functions: "exec"
}
console.log(pretty);
/*
{
	myModule:  require("path/to/module.js")
}
*/

License

BSD license © Vladimir Kalmykov, Yeoman Team