Skip to content

Commit 65d9f91

Browse files
committed
Print functions correctly
1 parent abda14d commit 65d9f91

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

lib/prettyjson.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ exports.version = require('../package.json').version;
88

99
// Helper function to detect if an object can be directly serializable
1010
var isSerializable = function(input, onlyPrimitives, options) {
11-
if (typeof input === 'boolean' ||
12-
typeof input === 'number' || input === null ||
13-
input instanceof Date) {
11+
if (
12+
typeof input === 'boolean' ||
13+
typeof input === 'number' ||
14+
typeof input === 'function' ||
15+
input === null ||
16+
input instanceof Date
17+
) {
1418
return true;
1519
}
1620
if (typeof input === 'string' && input.indexOf('\n') === -1) {
@@ -50,6 +54,10 @@ var addColorToData = function(input, options) {
5054
if (typeof input === 'number') {
5155
return colors[options.numberColor](sInput);
5256
}
57+
if (typeof input === 'function') {
58+
return 'function() {}';
59+
}
60+
5361
if (Array.isArray(input)) {
5462
return input.join(', ');
5563
}
@@ -70,7 +78,7 @@ var renderToArray = function(data, options, indentation) {
7078
return [Utils.indent(indentation) + addColorToData(data, options)];
7179
}
7280

73-
//unserializable string means it's multiline
81+
// Unserializable string means it's multiline
7482
if (typeof data === 'string') {
7583
return [
7684
Utils.indent(indentation) + '"""',

test/prettyjson_spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ describe('prettyjson general tests', function() {
4040
].join('\n'));
4141
});
4242

43+
it('should output a function', function() {
44+
var input = ['first string', function(a) { return a;}];
45+
var output = prettyjson.render(input);
46+
47+
output.should.equal([
48+
colors.green('- ') + input[0],
49+
colors.green('- ') + 'function() {}',
50+
].join('\n'));
51+
});
52+
4353
it('should output an array of arrays', function() {
4454
var input = ['first string', ['nested 1', 'nested 2'], 'second string'];
4555
var output = prettyjson.render(input);

0 commit comments

Comments
 (0)