From b328934b6c5ae8cd723ff719ea7645d381826011 Mon Sep 17 00:00:00 2001 From: Jeremy Nagel Date: Wed, 27 Nov 2019 14:07:19 +1100 Subject: [PATCH] [BUGFIX] Fix issue with with array constructor - You can do new Array(4) to generate an array with 4 empty items - Doing the same with list caused an error - This broke lodash.deepclone - This PR restores compatibility with the array constructor Fixes #1798 --- lib/list.js | 5 +++++ test/list.test.js | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/list.js b/lib/list.js index 4c14bd1f..4f4dfed7 100644 --- a/lib/list.js +++ b/lib/list.js @@ -27,6 +27,11 @@ function List(items, itemType, parent) { } } + if (typeof items === 'number') { + // trying to initialise empty array with a length + items = [...new Array(items)]; + } + const arr = []; arr.__proto__ = List.prototype; diff --git a/test/list.test.js b/test/list.test.js index cdc608a3..9a9e834f 100644 --- a/test/list.test.js +++ b/test/list.test.js @@ -38,6 +38,16 @@ function PhoneCtor(label, num) { this.num = num; } +describe('Does not break default Array functionality', function() { + it('allows creating an empty length with a specified length', function() { + const list = new List(4); + list.should.be.an.instanceOf(Array); + list.length.should.be.eql(4); + should.not.exist(list.itemType); + list.toJSON().should.eql([undefined, undefined, undefined, undefined]); + }); +}); + describe('list of items typed by a class', function() { it('allows itemType to be a class', function() { const phones = givenPhones();