stringify-object
Advanced tools
Comparing version 3.1.0 to 3.2.0
14
index.js
@@ -79,3 +79,7 @@ 'use strict'; | ||
const eol = val.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; | ||
return tokens.indent + stringify(el, opts, pad + opts.indent) + eol; | ||
let value = stringify(el, opts, pad + opts.indent); | ||
if (opts.transform) { | ||
value = opts.transform(val, i, value); | ||
} | ||
return tokens.indent + value + eol; | ||
}).join('') + tokens.pad + ']'; | ||
@@ -106,3 +110,7 @@ | ||
const key = isSymbol || isClassic ? el : stringify(el, opts); | ||
return tokens.indent + String(key) + ': ' + stringify(val[el], opts, pad + opts.indent) + eol; | ||
let value = stringify(val[el], opts, pad + opts.indent); | ||
if (opts.transform) { | ||
value = opts.transform(val, el, value); | ||
} | ||
return tokens.indent + String(key) + ': ' + value + eol; | ||
}).join('') + tokens.pad + '}'; | ||
@@ -122,5 +130,5 @@ | ||
val = val.replace(/'/g, '\\\''); | ||
val = val.replace(/\\?'/g, '\\\''); | ||
return `'${val}'`; | ||
})(val, opts, pad); | ||
}; |
{ | ||
"name": "stringify-object", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Stringify an object/array like JSON.stringify just without all the double-quotes", | ||
@@ -5,0 +5,0 @@ "license": "BSD-2-Clause", |
@@ -20,2 +20,4 @@ # stringify-object [![Build Status](https://secure.travis-ci.org/yeoman/stringify-object.svg?branch=master)](http://travis-ci.org/yeoman/stringify-object) | ||
```js | ||
const stringifyObject = require('stringify-object'); | ||
const obj = { | ||
@@ -79,4 +81,39 @@ foo: 'bar', | ||
Expected to return a `boolean` of whether to keep the object. | ||
Expected to return a `boolean` of whether to include the property `prop` of the object `obj` in the output. | ||
##### transform(obj, prop, originalResult) | ||
Type: `Function`<br> | ||
Default: `undefined` | ||
Expected to return a `string` that transforms the string that resulted from stringifying `obj[prop]`. This can be used to detect special types of objects that need to be stringified in a particular way. The `transform` function might return an alternate string in this case, otherwise returning the `originalResult`. | ||
Here's an example that uses the `transform` option to mask fields named "password": | ||
```js | ||
const obj = { | ||
user: 'becky', | ||
password: 'secret' | ||
} | ||
const pretty = stringifyObject(obj, { | ||
transform: (obj, prop, originalResult) => { | ||
if (prop === 'password') { | ||
return originalResult.replace(/\w/g, '*'); | ||
} else { | ||
return originalResult; | ||
} | ||
} | ||
}); | ||
console.log(pretty); | ||
/* | ||
{ | ||
user: 'becky', | ||
password: '******' | ||
} | ||
*/ | ||
``` | ||
##### inlineCharacterLimit | ||
@@ -83,0 +120,0 @@ |
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
8258
105
156