From 94bac893feb6617d61cf79f4321407c215224733 Mon Sep 17 00:00:00 2001 From: Victor Porof Date: Wed, 9 Sep 2020 14:27:08 +0200 Subject: [PATCH] Support sparse arrays (#95) Signed-off-by: Victor Porof Co-authored-by: Ryuichi Okumura --- index.js | 16 ++++++++++++++-- test/unit/serialize.js | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index cf14df4..640db8f 100644 --- a/index.js +++ b/index.js @@ -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.*?\(/; @@ -68,6 +68,7 @@ module.exports = function serialize(obj, options) { var dates = []; var maps = []; var sets = []; + var arrays = []; var undefs = []; var infinities= []; var bigInts = []; @@ -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(function(){return true}).length !== origValue.length; + if (isSparse) { + return '@__A-' + UID + '-' + (arrays.push(origValue) - 1) + '__@'; + } + } } if (type === 'function') { @@ -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; } @@ -228,6 +236,10 @@ module.exports = function serialize(obj, options) { return "new Set(" + serialize(Array.from(sets[valueIndex].values()), options) + ")"; } + if (type === 'A') { + return "Array.prototype.slice.call(" + serialize(Object.assign({ length: arrays[valueIndex].length }, arrays[valueIndex]), options) + ")"; + } + if (type === 'U') { return 'undefined' } diff --git a/test/unit/serialize.js b/test/unit/serialize.js index a618dae..bd24070 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -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('Array.prototype.slice.call({"1":2,"2":3,"5":"wat","length":6})'); + expect(serialize({t: [a]})).to.be.a('string').equal('{"t":[Array.prototype.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');