stringify-object
Advanced tools
Comparing version 2.3.1 to 2.4.0
64
index.js
@@ -12,3 +12,39 @@ 'use strict'; | ||
pad = pad || ''; | ||
var tokens; | ||
if(opts.inlineCharacterLimit == void 0) { | ||
tokens = { | ||
newLine: '\n', | ||
newLineOrSpace: '\n', | ||
pad: pad, | ||
indent: pad + opts.indent | ||
}; | ||
} else { | ||
tokens = { | ||
newLine: '@@__STRINGIFY_OBJECT_NEW_LINE__@@', | ||
newLineOrSpace: '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@', | ||
pad: '@@__STRINGIFY_OBJECT_PAD__@@', | ||
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) { | ||
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); | ||
} | ||
}; | ||
if (seen.indexOf(val) !== -1) { | ||
return '"[Circular]"'; | ||
} | ||
if (val === null || | ||
@@ -32,13 +68,15 @@ val === undefined || | ||
return '[\n' + val.map(function (el, i) { | ||
var eol = val.length - 1 === i ? '\n' : ',\n'; | ||
return pad + opts.indent + stringify(el, opts, pad + opts.indent) + eol; | ||
}).join('') + pad + ']'; | ||
seen.push(val); | ||
var ret = '[' + tokens.newLine + val.map(function (el, i) { | ||
var eol = val.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; | ||
return tokens.indent + stringify(el, opts, pad + opts.indent) + eol; | ||
}).join('') + tokens.pad + ']'; | ||
seen.pop(val); | ||
return expandWhiteSpace(ret); | ||
} | ||
if (isPlainObj(val)) { | ||
if (seen.indexOf(val) !== -1) { | ||
return '"[Circular]"'; | ||
} | ||
var objKeys = Object.keys(val); | ||
@@ -52,3 +90,3 @@ | ||
var ret = '{\n' + objKeys.map(function (el, i) { | ||
var ret = '{' + tokens.newLine + objKeys.map(function (el, i) { | ||
if (opts.filter && !opts.filter(val, el)) { | ||
@@ -58,10 +96,10 @@ return ''; | ||
var eol = objKeys.length - 1 === i ? '\n' : ',\n'; | ||
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); | ||
return pad + opts.indent + key + ': ' + stringify(val[el], opts, pad + opts.indent) + eol; | ||
}).join('') + pad + '}'; | ||
return tokens.indent + key + ': ' + stringify(val[el], opts, pad + opts.indent) + eol; | ||
}).join('') + tokens.pad + '}'; | ||
seen.pop(val); | ||
return ret; | ||
return expandWhiteSpace(ret); | ||
} | ||
@@ -68,0 +106,0 @@ |
{ | ||
"name": "stringify-object", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"description": "Stringify an object/array like JSON.stringify just without all the double-quotes", | ||
@@ -5,0 +5,0 @@ "license": "BSD-2-Clause", |
@@ -81,5 +81,42 @@ # stringify-object [![Build Status](https://secure.travis-ci.org/yeoman/stringify-object.svg?branch=master)](http://travis-ci.org/yeoman/stringify-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: | ||
```js | ||
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. | ||
## License | ||
[BSD license](http://opensource.org/licenses/bsd-license.php) and copyright Google | ||
[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
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
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
6842
4
92
122
1