stringify-object
Advanced tools
Comparing version 2.4.0 to 3.0.0
72
index.js
'use strict'; | ||
var isRegexp = require('is-regexp'); | ||
var isPlainObj = require('is-plain-obj'); | ||
const isRegexp = require('is-regexp'); | ||
const isObj = require('is-obj'); | ||
module.exports = function (val, opts, pad) { | ||
var seen = []; | ||
module.exports = (val, opts, pad) => { | ||
const seen = []; | ||
@@ -12,8 +12,10 @@ return (function stringify(val, opts, pad) { | ||
pad = pad || ''; | ||
var tokens; | ||
if(opts.inlineCharacterLimit == void 0) { | ||
let tokens; | ||
if (opts.inlineCharacterLimit === undefined) { | ||
tokens = { | ||
newLine: '\n', | ||
newLineOrSpace: '\n', | ||
pad: pad, | ||
pad, | ||
indent: pad + opts.indent | ||
@@ -27,19 +29,23 @@ }; | ||
indent: '@@__STRINGIFY_OBJECT_INDENT__@@' | ||
} | ||
}; | ||
} | ||
var expandWhiteSpace = function(string) { | ||
if (opts.inlineCharacterLimit == void 0) { return string; } | ||
var oneLined = string. | ||
replace(new RegExp(tokens.newLine, 'g'), ''). | ||
replace(new RegExp(tokens.newLineOrSpace, 'g'), ' '). | ||
replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), ''); | ||
if(oneLined.length <= opts.inlineCharacterLimit) { | ||
const expandWhiteSpace = string => { | ||
if (opts.inlineCharacterLimit === undefined) { | ||
return string; | ||
} | ||
const oneLined = string | ||
.replace(new RegExp(tokens.newLine, 'g'), '') | ||
.replace(new RegExp(tokens.newLineOrSpace, 'g'), ' ') | ||
.replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), ''); | ||
if (oneLined.length <= opts.inlineCharacterLimit) { | ||
return oneLined; | ||
} else { | ||
return string. | ||
replace(new RegExp(tokens.newLine + '|' + tokens.newLineOrSpace, 'g'), '\n'). | ||
replace(new RegExp(tokens.pad, 'g'), pad). | ||
replace(new RegExp(tokens.indent, 'g'), pad + opts.indent); | ||
} | ||
return string | ||
.replace(new RegExp(tokens.newLine + '|' + tokens.newLineOrSpace, 'g'), '\n') | ||
.replace(new RegExp(tokens.pad, 'g'), pad) | ||
.replace(new RegExp(tokens.indent, 'g'), pad + opts.indent); | ||
}; | ||
@@ -61,3 +67,3 @@ | ||
if (val instanceof Date) { | ||
return 'new Date(\'' + val.toISOString() + '\')'; | ||
return `new Date('${val.toISOString()}')`; | ||
} | ||
@@ -72,4 +78,4 @@ | ||
var ret = '[' + tokens.newLine + val.map(function (el, i) { | ||
var eol = val.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; | ||
const ret = '[' + tokens.newLine + val.map((el, i) => { | ||
const eol = val.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; | ||
return tokens.indent + stringify(el, opts, pad + opts.indent) + eol; | ||
@@ -83,4 +89,4 @@ }).join('') + tokens.pad + ']'; | ||
if (isPlainObj(val)) { | ||
var objKeys = Object.keys(val); | ||
if (isObj(val)) { | ||
const objKeys = Object.keys(val); | ||
@@ -93,3 +99,3 @@ if (objKeys.length === 0) { | ||
var ret = '{' + tokens.newLine + objKeys.map(function (el, i) { | ||
const ret = '{' + tokens.newLine + objKeys.map((el, i) => { | ||
if (opts.filter && !opts.filter(val, el)) { | ||
@@ -99,4 +105,4 @@ return ''; | ||
var eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; | ||
var key = /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts); | ||
const eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; | ||
const key = /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts); | ||
return tokens.indent + key + ': ' + stringify(val[el], opts, pad + opts.indent) + eol; | ||
@@ -110,12 +116,12 @@ }).join('') + tokens.pad + '}'; | ||
val = String(val).replace(/[\r\n]/g, function (x) { | ||
return x === '\n' ? '\\n' : '\\r'; | ||
}); | ||
val = String(val).replace(/[\r\n]/g, x => x === '\n' ? '\\n' : '\\r'); | ||
if (opts.singleQuotes === false) { | ||
return '"' + val.replace(/"/g, '\\\"') + '"'; | ||
val = val.replace(/"/g, '\\"'); | ||
return `"${val}"`; | ||
} | ||
return '\'' + val.replace(/'/g, '\\\'') + '\''; | ||
val = val.replace(/'/g, '\\\''); | ||
return `'${val}'`; | ||
})(val, opts, pad); | ||
}; |
{ | ||
"name": "stringify-object", | ||
"version": "2.4.0", | ||
"version": "3.0.0", | ||
"description": "Stringify an object/array like JSON.stringify just without all the double-quotes", | ||
@@ -13,6 +13,6 @@ "license": "BSD-2-Clause", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "xo && mocha" | ||
}, | ||
@@ -33,8 +33,12 @@ "files": [ | ||
"dependencies": { | ||
"is-plain-obj": "^1.0.0", | ||
"is-obj": "^1.0.1", | ||
"is-regexp": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
"mocha": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
# stringify-object [![Build Status](https://secure.travis-ci.org/yeoman/stringify-object.svg?branch=master)](http://travis-ci.org/yeoman/stringify-object) | ||
> Stringify an object/array like JSON.stringify just without all the double-quotes. | ||
> Stringify an object/array like JSON.stringify just without all the double-quotes | ||
@@ -20,3 +20,3 @@ Useful for when you want to get the string representation of an object in a formatted way. | ||
```js | ||
var obj = { | ||
const obj = { | ||
foo: 'bar', | ||
@@ -27,3 +27,3 @@ 'arr': [1, 2, 3], | ||
var pretty = stringifyObject(obj, { | ||
const pretty = stringifyObject(obj, { | ||
indent: ' ', | ||
@@ -58,4 +58,3 @@ singleQuotes: false | ||
*Required* | ||
Type: `object`, `array` | ||
Type: `Object` `Array` | ||
@@ -66,10 +65,10 @@ #### options | ||
Type: `string` | ||
Type: `string`<br> | ||
Default: `'\t'` | ||
Choose the indentation you prefer. | ||
Preferred indentation. | ||
##### singleQuotes | ||
Type: `boolean` | ||
Type: `boolean`<br> | ||
Default: `true` | ||
@@ -81,5 +80,5 @@ | ||
Type: `function` | ||
Type: `Function` | ||
Expected to return a boolean of whether to keep the object. | ||
Expected to return a `boolean` of whether to keep the object. | ||
@@ -89,6 +88,4 @@ ##### inlineCharacterLimit | ||
Type: `number` | ||
Default: undefined | ||
When set, will inline values up to `inlineCharacterLimit` length for the sake | ||
of more terse output. | ||
When set, will inline values up to `inlineCharacterLimit` length for the sake of more terse output. | ||
@@ -98,3 +95,3 @@ For example, given the example at the top of the README: | ||
```js | ||
var obj = { | ||
const obj = { | ||
foo: 'bar', | ||
@@ -105,3 +102,3 @@ 'arr': [1, 2, 3], | ||
var pretty = stringifyObject(obj, { | ||
const pretty = stringifyObject(obj, { | ||
indent: ' ', | ||
@@ -124,7 +121,7 @@ singleQuotes: false, | ||
As you can see, `arr` was printed as a one-liner because its string was shorter | ||
than 12 characters. | ||
As you can see, `arr` was printed as a one-liner because its string was shorter than 12 characters. | ||
## License | ||
[BSD license](http://opensource.org/licenses/bsd-license.php) © Yeoman Team |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
93
0
6820
2
119
+ Addedis-obj@^1.0.1
+ Addedis-obj@1.0.1(transitive)
- Removedis-plain-obj@^1.0.0
- Removedis-plain-obj@1.1.0(transitive)