8000 support serialize undefined · nqdy666/serialize-javascript@1fef1f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fef1f4

Browse files
committed
support serialize undefined
1 parent c812a45 commit 1fef1f4

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ serialize({
5252
The above will produce the following string output:
5353

5454
```js
55-
'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,date:new Date("2016-04-28T22:02:17.156Z"),new Map([["hello", "world"]]),new Set([123,456]),"fn":function echo(arg) { return arg; },"re":/([^\\s]+)/g}'
55+
'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,"undef":undefined,"date":new Date("2016-04-28T22:02:17.000Z"),"map":new Map([["hello","world"]]),"set":new Set([123,456]),"fn":function echo(arg) { return arg; },"re":/([^\s]+)/g}'
5656
```
5757

5858
Note: to produced a beautified string, you can pass an optional second argument to `serialize()` to define the number of spaces to be used for the indentation.

index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ See the accompanying LICENSE file for terms.
88

99
// Generate an internal UID to make the regexp pattern harder to guess.
1010
var UID = Math.floor(Math.random() * 0x10000000000).toString(16);
11-
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S)-' + UID + '-(\\d+)__@"', 'g');
11+
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S|U)-' + UID + '-(\\d+)__@"', 'g');
1212

1313
var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
1414
var IS_PURE_FUNCTION = /function.*?\(/;
@@ -44,11 +44,13 @@ module.exports = function serialize(obj, options) {
4444
var dates = [];
4545
var maps = [];
4646
var sets = [];
47+
var undefs = [];
4748

4849
// Returns placeholders for functions and regexps (identified by index)
4950
// which are later replaced by their string representation.
5051
function replacer(key, value) {
51-
if (!value) {
52+
53+
if (!value && value !== undefined) {
5254
return value;
5355
}
5456

@@ -79,6 +81,10 @@ module.exports = function serialize(obj, options) {
7981
return '@__F-' + UID + '-' + (functions.push(origValue) - 1) + '__@';
8082
}
8183

84+
if (type === 'undefined') {
85+
return '@__U-' + UID + '-' + (undefs.push(origValue) - 1) + '__@';
86+
}
87+
8288
return value;
8389
}
8490

@@ -119,6 +125,12 @@ module.exports = function serialize(obj, options) {
119125
return serializedFn;
120126
}
121127

128+
// Protects against `JSON.stringify()` returning `undefined`, by serializing
129+
// to the literal string: "undefined".
130+
if (obj === undefined) {
131+
return String(obj);
132+
}
133+
122134
var str;
123135

124136
// Creates a JSON string representation of the value.
@@ -134,7 +146,7 @@ module.exports = function serialize(obj, options) {
134146
if (typeof str !== 'string') {
135147
return String(str);
136148
}
137-
149+
138150
// Replace unsafe HTML and invalid JavaScript line terminator chars with
139151
// their safe Unicode char counterpart. This _must_ happen before the
140152
// regexps and functions are serialized and added back to the string.
@@ -166,6 +178,10 @@ module.exports = function serialize(obj, options) {
166178
return "new Set(" + serialize(Array.from(sets[valueIndex].values()), options) + ")";
167179
}
168180

181+
if (type === 'U') {
182+
return 'undefined'
183+
}
184+
169185
var fn = functions[valueIndex];
170186

171187
return serializeFunc(fn);

0 commit comments

Comments
 (0)
0