Skip to content

Commit eed510c

Browse files
authored
Introduce support for Infinity (#72)
1 parent 82bb2d2 commit eed510c

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

index.js

+11-2
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|U|I)-' + UID + '-(\\d+)__@"', 'g');
1212

1313
var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
1414
var IS_PURE_FUNCTION = /function.*?\(/;
@@ -57,6 +57,7 @@ module.exports = function serialize(obj, options) {
5757
var maps = [];
5858
var sets = [];
5959
var undefs = [];
60+
var infinities= [];
6061

6162
// Returns placeholders for functions and regexps (identified by index)
6263
// which are later replaced by their string representation.
@@ -102,6 +103,10 @@ module.exports = function serialize(obj, options) {
102103
return '@__U-' + UID + '-' + (undefs.push(origValue) - 1) + '__@';
103104
}
104105

106+
if (type === 'number' && !isNaN(origValue) && !isFinite(origValue)) {
107+
return '@__I-' + UID + '-' + (infinities.push(origValue) - 1) + '__@';
108+
}
109+
105110
return value;
106111
}
107112

@@ -175,7 +180,7 @@ module.exports = function serialize(obj, options) {
175180
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
176181
}
177182

178-
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0) {
183+
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0 && infinities.length === 0) {
179184
return str;
180185
}
181186

@@ -203,6 +208,10 @@ module.exports = function serialize(obj, options) {
203208
return 'undefined'
204209
}
205210

211+
if (type === 'I') {
212+
return infinities[valueIndex];
213+
}
214+
206215
var fn = functions[valueIndex];
207216

208217
return serializeFunc(fn);

test/unit/serialize.js

+34-8
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,18 @@ describe('serialize( obj )', function () {
335335
var regexKey = /.*/;
336336
var m = new Map([
337337
['a', 123],
338-
[regexKey, 456]
338+
[regexKey, 456],
339+
[Infinity, 789]
339340
]);
340-
expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[new RegExp(".*", ""),456]])');
341-
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[new RegExp(".*", ""),456]])]}');
341+
expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,789]])');
342+
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,789]])]}');
342343
});
343344

344345
it('should deserialize a map', function () {
345346
var m = eval(serialize(new Map([
346347
['a', 123],
347-
[null, 456]
348+
[null, 456],
349+
[Infinity, 789]
348350
])));
349351
expect(m).to.be.a('Map');
350352
expect(m.get(null)).to.equal(456);
@@ -357,23 +359,47 @@ describe('serialize( obj )', function () {
357359
var m = new Set([
358360
'a',
359361
123,
360-
regex
362+
regex,
363+
Infinity
361364
]);
362-
expect(serialize(m)).to.be.a('string').equal('new Set(["a",123,new RegExp(".*", "")])');
363-
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Set(["a",123,new RegExp(".*", "")])]}');
365+
expect(serialize(m)).to.be.a('string').equal('new Set(["a",123,new RegExp(".*", ""),Infinity])');
366+
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Set(["a",123,new RegExp(".*", ""),Infinity])]}');
364367
});
365368

366369
it('should deserialize a set', function () {
367370
var m = eval(serialize(new Set([
368371
'a',
369372
123,
370-
null
373+
null,
374+
Infinity
371375
])));
372376
expect(m).to.be.a('Set');
373377
expect(m.has(null)).to.equal(true);
374378
});
375379
});
376380

381+
describe('Infinity', function () {
382+
it('should serialize Infinity', function () {
383+
expect(serialize(Infinity)).to.equal('Infinity');
384+
expect(serialize({t: [Infinity]})).to.be.a('string').equal('{"t":[Infinity]}');
385+
});
386+
387+
it('should deserialize Infinity', function () {
388+
var d = eval(serialize(Infinity));
389+
expect(d).to.equal(Infinity);
390+
});
391+
392+
it('should serialize -Infinity', function () {
393+
expect(serialize(-Infinity)).to.equal('-Infinity');
394+
expect(serialize({t: [-Infinity]})).to.be.a('string').equal('{"t":[-Infinity]}');
395+
});
396+
397+
it('should deserialize -Infinity', function () {
398+
var d = eval(serialize(-Infinity));
399+
expect(d).to.equal(-Infinity);
400+
});
401+
});
402+
377403
describe('XSS', function () {
378404
it('should encode unsafe HTML chars to Unicode', function () {
379405
expect(serialize('</script>')).to.equal('"\\u003C\\u002Fscript\\u003E"');

0 commit comments

Comments
 (0)