Merge pull request #1799 from jeznag/fix-issue-with-new-array

Fix issue with broken compatibility with array constructor
This commit is contained in:
Miroslav Bajtoš 2019-12-06 10:18:17 +01:00 committed by GitHub
commit 002137dd5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 = []; const arr = [];
arr.__proto__ = List.prototype; arr.__proto__ = List.prototype;

View File

@ -38,6 +38,16 @@ function PhoneCtor(label, num) {
this.num = 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() { describe('list of items typed by a class', function() {
it('allows itemType to be a class', function() { it('allows itemType to be a class', function() {
const phones = givenPhones(); const phones = givenPhones();