Skip to content

Commit 48d1cf4

Browse files
Merge pull request #1 from jadejs/undefined
Support stringifying 'undefined'
2 parents bc708aa + 7b002ba commit 48d1cf4

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ function stringify(obj) {
55
if (obj instanceof Date) {
66
return 'new Date(' + stringify(obj.toISOString()) + ')';
77
}
8+
if (obj === undefined) {
9+
return 'undefined';
10+
}
811
return JSON.stringify(obj)
912
.replace(/\u2028/g, '\\u2028')
1013
.replace(/\u2029/g, '\\u2029');
11-
}
14+
}

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ assert(stringify('foo') === '"foo"');
77
assert(stringify('foo\u2028bar\u2029baz') === '"foo\\u2028bar\\u2029baz"');
88
assert(stringify(new Date('2014-12-19T03:42:00.000Z')) === 'new Date("2014-12-19T03:42:00.000Z")');
99
assert(stringify({foo: 'bar'}) === '{"foo":"bar"}');
10+
assert(stringify(undefined) === 'undefined');
11+
assert(stringify(null) === 'null');
1012

1113
console.log('tests passed');

0 commit comments

Comments
 (0)