Skip to content

Commit

Permalink
Support sparse arrays
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Porof <victor.porof@gmail.com>
  • Loading branch information
victorporof committed Aug 28, 2020
1 parent d4bed9c commit 1ac90a8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 14 additions & 2 deletions index.js
Expand Up @@ -11,7 +11,7 @@ var randomBytes = require('randombytes');
// Generate an internal UID to make the regexp pattern harder to guess.
var UID_LENGTH = 16;
var UID = generateUID();
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|U|I|B)-' + UID + '-(\\d+)__@"', 'g');
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B)-' + UID + '-(\\d+)__@"', 'g');

var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
var IS_PURE_FUNCTION = /function.*?\(/;
Expand Down Expand Up @@ -68,6 +68,7 @@ module.exports = function serialize(obj, options) {
var dates = [];
var maps = [];
var sets = [];
var arrays = [];
var undefs = [];
var infinities= [];
var bigInts = [];
Expand Down Expand Up @@ -106,6 +107,13 @@ module.exports = function serialize(obj, options) {
if(origValue instanceof Set) {
return '@__S-' + UID + '-' + (sets.push(origValue) - 1) + '__@';
}

if(origValue instanceof Array) {
var isSparse = origValue.filter(() => true).length != origValue.length;
if (isSparse) {
return '@__A-' + UID + '-' + (arrays.push(origValue) - 1) + '__@';
}
}
}

if (type === 'function') {
Expand Down Expand Up @@ -197,7 +205,7 @@ module.exports = function serialize(obj, options) {
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
}

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) {
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) {
return str;
}

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

if (type === 'A') {
return "[].slice.call(" + serialize(Object.assign({ length: arrays[valueIndex].length }, arrays[valueIndex]), options) + ")";
}

if (type === 'U') {
return 'undefined'
}
Expand Down
20 changes: 20 additions & 0 deletions test/unit/serialize.js
Expand Up @@ -392,6 +392,26 @@ describe('serialize( obj )', function () {
});
});

describe('sparse arrays', function () {
it('should serialize sparse arrays', function () {
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
delete a[0];
a.length = 3;
a[5] = "wat"
expect(serialize(a)).to.be.a('string').equal('[].slice.call({"1":2,"2":3,"5":"wat","length":6})');
expect(serialize({t: [a]})).to.be.a('string').equal('{"t":[[].slice.call({"1":2,"2":3,"5":"wat","length":6})]}');
});

it('should deserialize a sparse array', function () {
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
delete a[0];
a.length = 3;
a[5] = "wat"
var b = eval(serialize(a));
expect(b).to.be.a('Array').deep.equal([ , 2, 3, , , 'wat' ]);
});
});

describe('Infinity', function () {
it('should serialize Infinity', function () {
expect(serialize(Infinity)).to.equal('Infinity');
Expand Down

0 comments on commit 1ac90a8

Please sign in to comment.