Skip to content

Commit

Permalink
fix undefined values
Browse files Browse the repository at this point in the history
closes #44
  • Loading branch information
rafeca committed Jan 10, 2022
1 parent 6d0286f commit 25a00a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/prettyjson.js
Expand Up @@ -13,6 +13,7 @@ var isSerializable = function(input, onlyPrimitives, options) {
typeof input === 'number' ||
typeof input === 'function' ||
input === null ||
input === undefined ||
input instanceof Date
) {
return true;
Expand Down Expand Up @@ -48,7 +49,7 @@ var addColorToData = function(input, options) {
if (input === false) {
return colors.red(sInput);
}
if (input === null) {
if (input === null || input === undefined) {
return colors.grey(sInput);
}
if (typeof input === 'number') {
Expand Down Expand Up @@ -158,11 +159,6 @@ var renderToArray = function(data, options, indentation) {
}
key = Utils.indent(indentation) + key;

// Skip `undefined`, it's not a valid JSON value.
if (data[i] === undefined) {
return;
}

// If the value is serializable, render it in the same line
if (isSerializable(data[i], false, options)) {
var nextIndentation = options.noAlign ? 0 : maxIndexLength - i.length;
Expand Down
23 changes: 23 additions & 0 deletions test/prettyjson_spec.js
Expand Up @@ -292,6 +292,29 @@ describe('Printing numbers, booleans and other objects', function() {
output.should.equal(' ' + colors.grey('null'));
});

it('should print a undefined object correctly ', function() {
var input;
var output = prettyjson.render(input, {}, 4);

output.should.equal(' ' + colors.grey('undefined'));
});

it('should print undefined keys ', function() {
var input = {
foo: undefined,
bar: [1, undefined, 2],
};
var output = prettyjson.render(input, {}, 4);

output.should.equal(([
' ' + colors.green('foo: ') + colors.grey('undefined'),
' ' + colors.green('bar: '),
' ' + colors.green('- ') + colors.blue(1),
' ' + colors.green('- ') + colors.grey('undefined'),
' ' + colors.green('- ') + colors.blue(2)
].join('\n')));
});

it('should print an Error correctly ', function() {
Error.stackTraceLimit = 1;
var input = new Error('foo');
Expand Down

0 comments on commit 25a00a1

Please sign in to comment.