Skip to content

Commit

Permalink
Add a transform option (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
searls authored and sindresorhus committed Mar 15, 2017
1 parent 5b7c2bd commit a4948ad
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ module.exports = (val, opts, pad) => {

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;
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 + ']';

seen.pop(val);
Expand All @@ -102,7 +106,11 @@ module.exports = (val, opts, pad) => {
const isSymbol = typeof el === 'symbol';
const isClassic = !isSymbol && /^[a-z$_][a-z$_0-9]*$/i.test(el);
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 + '}';

seen.pop(val);
Expand Down
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,41 @@ Type: `Function`

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

Type: `number`
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ it('considering filter option to stringify an object', () => {
assert.equal(actual, '{\n\tbar: {\n\t\tval: 10\n\t}\n}');
});

it('allows an object to be transformed', () => {
const obj = {
foo: {
val: 10
},
bar: 9,
baz: [8]
};

const actual = stringifyObject(obj, {
transform: (obj, prop, result) => {
if (prop === 'val') {
return String(obj[prop] + 1);
} else if (prop === 'bar') {
return '\'' + result + 'L\'';
} else if (obj[prop] === 8) {
return 'LOL';
}
return result;
}
});

assert.equal(actual, '{\n\tfoo: {\n\t\tval: 11\n\t},\n\tbar: \'9L\',\n\tbaz: [\n\t\tLOL\n\t]\n}');
});

it('should not crash with circular references in arrays', () => {
const array = [];
array.push(array);
Expand Down

0 comments on commit a4948ad

Please sign in to comment.