Skip to content

Commit 96431aa

Browse files
victorporofokuryu
andauthored
Support sparse arrays (#95)
Signed-off-by: Victor Porof <victor.porof@gmail.com> Co-authored-by: Ryuichi Okumura <okuryu@okuryu.com>
1 parent d4bed9c commit 96431aa

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var randomBytes = require('randombytes');
1111
// Generate an internal UID to make the regexp pattern harder to guess.
1212
var UID_LENGTH = 16;
1313
var UID = generateUID();
14-
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|U|I|B)-' + UID + '-(\\d+)__@"', 'g');
14+
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B)-' + UID + '-(\\d+)__@"', 'g');
1515

1616
var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
1717
var IS_PURE_FUNCTION = /function.*?\(/;
@@ -68,6 +68,7 @@ module.exports = function serialize(obj, options) {
6868
var dates = [];
6969
var maps = [];
7070
var sets = [];
71+
var arrays = [];
7172
var undefs = [];
7273
var infinities= [];
7374
var bigInts = [];
@@ -106,6 +107,13 @@ module.exports = function serialize(obj, options) {
106107
if(origValue instanceof Set) {
107108
return '@__S-' + UID + '-' + (sets.push(origValue) - 1) + '__@';
108109
}
110+
111+
if(origValue instanceof Array) {
112+
var isSparse = origValue.filter(function(){return true}).length !== origValue.length;
113+
if (isSparse) {
114+
return '@__A-' + UID + '-' + (arrays.push(origValue) - 1) + '__@';
115+
}
116+
}
109117
}
110118

111119
if (type === 'function') {
@@ -197,7 +205,7 @@ module.exports = function serialize(obj, options) {
197205
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
198206
}
199207

200-
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0) {
208+
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && arrays.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0) {
201209
return str;
202210
}
203211

@@ -228,6 +236,10 @@ module.exports = function serialize(obj, options) {
228236
return "new Set(" + serialize(Array.from(sets[valueIndex].values()), options) + ")";
229237
}
230238

239+
if (type === 'A') {
240+
return "Array.prototype.slice.call(" + serialize(Object.assign({ length: arrays[valueIndex].length }, arrays[valueIndex]), options) + ")";
241+
}
242+
231243
if (type === 'U') {
232244
return 'undefined'
233245
}

test/unit/serialize.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,26 @@ describe('serialize( obj )', function () {
392392
});
393393
});
394394

395+
describe('sparse arrays', function () {
396+
it('should serialize sparse arrays', function () {
397+
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
398+
delete a[0];
399+
a.length = 3;
400+
a[5] = "wat"
401+
expect(serialize(a)).to.be.a('string').equal('Array.prototype.slice.call({"1":2,"2":3,"5":"wat","length":6})');
402+
expect(serialize({t: [a]})).to.be.a('string').equal('{"t":[Array.prototype.slice.call({"1":2,"2":3,"5":"wat","length":6})]}');
403+
});
404+
405+
it('should deserialize a sparse array', function () {
406+
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
407+
delete a[0];
408+
a.length = 3;
409+
a[5] = "wat"
410+
var b = eval(serialize(a));
411+
expect(b).to.be.a('Array').deep.equal([ , 2, 3, , , 'wat' ]);
412+
});
413+
});
414+
395415
describe('Infinity', function () {
396416
it('should serialize Infinity', function () {
397417
expect(serialize(Infinity)).to.equal('Infinity');

0 commit comments

Comments
 (0)