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();