Skip to content

Commit

Permalink
Breaking change: json: true option now filters undefined properties
Browse files Browse the repository at this point in the history
Similarily to JSON.stringify()
  • Loading branch information
Tim committed Jul 19, 2019
1 parent aedc617 commit 1613a09
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ jsesc([ 'foo\x00bar', [1, '©', { 'foo': true, 'qux': null }], 42 ], {
'json': true
});
// → '["foo\\u0000bar",[1,"\\u00A9",{"foo":true,"qux":null}],42]'
// Undefined object properties are skipped, similarily to `JSON.stringify()`:
jsesc({ foo: undefined, bar: null }, {
'json': true
});
// → '{"bar": null}'
// Values that aren’t allowed in JSON are run through `JSON.stringify()`:
jsesc([ undefined, -Infinity ], {
'json': true
Expand Down
8 changes: 5 additions & 3 deletions jsesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

const object = {};
const hasOwnProperty = object.hasOwnProperty;
const forOwn = (object, callback) => {
const forOwn = (object, callback, noUndefined) => {
for (const key in object) {
if (hasOwnProperty.call(object, key)) {
if (hasOwnProperty.call(object, key) &&
(!noUndefined || (typeof object[key] !== 'undefined'))
) {
callback(key, object[key]);
}
}
Expand Down Expand Up @@ -225,7 +227,7 @@ const jsesc = (argument, options) => {
(compact ? '' : ' ') +
jsesc(value, options)
);
});
}, !!json);
if (isEmpty) {
return '{}';
}
Expand Down
8 changes: 5 additions & 3 deletions src/jsesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

const object = {};
const hasOwnProperty = object.hasOwnProperty;
const forOwn = (object, callback) => {
const forOwn = (object, callback, noUndefined) => {
for (const key in object) {
if (hasOwnProperty.call(object, key)) {
if (hasOwnProperty.call(object, key) &&
(!noUndefined || (typeof object[key] !== 'undefined'))
) {
callback(key, object[key]);
}
}
Expand Down Expand Up @@ -225,7 +227,7 @@ const jsesc = (argument, options) => {
(compact ? '' : ' ') +
jsesc(value, options)
);
});
}, !!json);
if (isEmpty) {
return '{}';
}
Expand Down

0 comments on commit 1613a09

Please sign in to comment.