New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

code-stringify

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

code-stringify

code-stringify is node.js module that converts JavaScript variables into source codes. Unlike JSON.stringify, code-stringify converts things into strings of code, not JSON.

  • 1.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
decreased by-7.57%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status

code-stringify

code-stringify is the node.js module that converts JavaScript variables into source codes.

Unlike JSON.stringify, code-stringify also deals with reference(object) types of variables, and it converts JavaScript variables into strings of codes, not JSON.

Installation

npm install code-stringify --save

Usage

const code = require('code-stringify')
const obj = {
  0: 1,
  a: function(n){return n;},
  b: 1,
  'c-d': 3
}

const fs = require('fs')

// So you can use code-stringify to save your javascript variables into a file:
fs.writeFileSync(
  'output.js',
  'module.exports = ' + code(a, null, 2)
)

Then 'output.js' will look like:

module.exports = {
  0: 1,
  a: function (n){return n;},
  b: 1,
  "c-d": 3
}

code(subject, replacer, space)

subject mixed

The subject to be converted

replacer function(key, value)|Array

The replacer argument acts just like the second parameter of JSON.stringify.

code({
  a: 1,
  b: 2
}, function (key, value) {
  return key === 'b'
    ? undefined
    : value
})

// '{a:1}'
space number|string

The space argument acts just like the third parameter of JSON.stringify.

code.Code(string)

If an object obj has a prototype property toCode and obj.toCode is an function, then code(obj) will be equal to obj.toCode().

We could use new code.Code(code_string) to define an already-stringified property.

So, see the example below:

let output = 'module.exports = ' + code({
  a: 1,
  'foo-bar': 2,
  foo: new code.Code('(function(a){return a})(3)')
})

saveFile(output, 'output.js')

And the output.js will be:

module.exports = {
  a: 1,
  'foo-bar': 2,
  foo: (function(a){return a})(3)
}

Versus JSON.stringify()

valueJSON.stringify(value)code(value)comment
1'1''1'
'1'"1""'1'"you can change quote style by code.QUOTE = '"'
undefinedundefined'undefined'
null'null''null'
[undefined][null][undefined]
[null][null][null]
  • JSON.stringify makes JSON.
  • code-stringify makes JavaScript code.

Known Issues

  • Can't deal with recursive objects or arrays SO FAR.
  • space parameter could not affect the code indent inside functions.
  • Could not deal with variable scope so far.

Those issues or tasks which should be done to enhance the module might be fixed in the future. Or there will be a million thanks if you fork and contribute ~~

Keywords

FAQs

Package last updated on 27 Jul 2016

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc