[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
This commit is contained in:
Jeremy Nagel 2019-11-27 14:07:19 +11:00
parent c555dedfeb
commit b328934b6c
2 changed files with 15 additions and 0 deletions

View File

@ -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;

View File

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