Skip to content

Commit 4dfd9e5

Browse files
committed
Revert "support serialize undefined (#54)"
This reverts commit 356987f.
1 parent b7273f8 commit 4dfd9e5

File tree

3 files changed

+4
-27
lines changed

3 files changed

+4
-27
lines changed

Diff for: README.md

+1-1
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,"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}'
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}'
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.

Diff for: index.js

+3-19
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|U)-' + UID + '-(\\d+)__@"', 'g');
11+
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S)-' + UID + '-(\\d+)__@"', 'g');
1212

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

4948
// Returns placeholders for functions and regexps (identified by index)
5049
// which are later replaced by their string representation.
5150
function replacer(key, value) {
52-
53-
if (!value && value !== undefined) {
51+
if (!value) {
5452
return value;
5553
}
5654

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

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

@@ -125,12 +119,6 @@ module.exports = function serialize(obj, options) {
125119
return serializedFn;
126120
}
127121

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-
134122
var str;
135123

136124
// Creates a JSON string representation of the value.
@@ -154,7 +142,7 @@ module.exports = function serialize(obj, options) {
154142
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
155143
}
156144

157-
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0) {
145+
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0) {
158146
return str;
159147
}
160148

@@ -178,10 +166,6 @@ module.exports = function serialize(obj, options) {
178166
return "new Set(" + serialize(Array.from(sets[valueIndex].values()), options) + ")";
179167
}
180168

181-
if (type === 'U') {
182-
return 'undefined'
183-
}
184-
185169
var fn = functions[valueIndex];
186170

187171
return serializeFunc(fn);

Diff for: test/unit/serialize.js

-7
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ describe('serialize( obj )', function () {
5757
var ws = String.fromCharCode(8232);
5858
expect(eval(serialize(ws))).to.equal(ws);
5959
});
60-
61-
it('should serialize undefined correctly', function () {
62-
var obj;
63-
var str = '{"undef":undefined,"nest":{"undef":undefined}}';
64-
eval('obj = ' + str);
65-
expect(serialize(obj)).to.equal(str);
66-
});
6760
});
6861

6962
describe('functions', function () {

0 commit comments

Comments
 (0)