Merge pull request #1625 from strongloop/fix-list-of-class-instances

Allow List to take items as instances of a class
This commit is contained in:
Raymond Feng 2018-09-17 09:02:16 -07:00 committed by GitHub
commit 242fd1bb56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 143 additions and 2 deletions

View File

@ -63,9 +63,13 @@ function List(items, itemType, parent) {
});
}
List.prototype.toItem = function(item) {
return isClass(this.itemType) ? new this.itemType(item) : this.itemType(item);
};
items.forEach(function(item, i) {
if (itemType && !(item instanceof itemType)) {
arr[i] = itemType(item);
arr[i] = arr.toItem(item);
} else {
arr[i] = item;
}
@ -79,7 +83,7 @@ util.inherits(List, Array);
var _push = List.prototype.push;
List.prototype.push = function(obj) {
var item = this.itemType && (obj instanceof this.itemType) ? obj : this.itemType(obj);
var item = this.itemType && (obj instanceof this.itemType) ? obj : this.toItem(obj);
_push.call(this, item);
return item;
};
@ -117,3 +121,6 @@ List.prototype.toString = function() {
return JSON.stringify(this.toJSON());
};
function isClass(fn) {
return fn && fn.toString().indexOf('class ') === 0;
}

View File

@ -1,3 +1,8 @@
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var kvMemory = require('../lib/connectors/kv-memory');
var DataSource = require('..').DataSource;

View File

@ -1,3 +1,8 @@
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var debug = require('debug')('test');

124
test/list.test.js Normal file
View File

@ -0,0 +1,124 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var should = require('./init.js');
var List = require('../lib/list');
/**
* Phone as a class
*/
class Phone {
constructor(label, num) {
this.label = label;
this.num = num;
}
toJSON() {
return {
label: this.label,
num: this.num,
};
}
}
/**
* Phone as a constructor function
* @param {string} label
* @param {number} num
*/
function PhoneCtor(label, num) {
if (!(this instanceof PhoneCtor)) {
return new PhoneCtor(label, num);
}
this.label = label;
this.num = num;
}
describe('list of items typed by a class', function() {
it('allows itemType to be a class', function() {
var phones = givenPhones();
var list = new List(phones, Phone);
list.should.be.an.instanceOf(Array);
list.toJSON().should.be.eql(phones);
});
it('converts items of plain json to the itemType', function() {
var phones = givenPhonesAsJSON();
var list = new List(phones, Phone);
list[0].should.be.an.instanceOf(Phone);
});
it('converts stringified items to the itemType', function() {
var phones = givenPhonesAsJSON();
var list = new List(JSON.stringify(phones), Phone);
list[0].should.be.an.instanceOf(Phone);
});
it('converts items of plain json to the itemType with push', function() {
var phones = givenPhonesAsJSON();
var list = new List([], Phone);
list.push(phones[0]);
list[0].should.be.an.instanceOf(Phone);
});
});
describe('list of items typed by a ctor', function() {
it('allows itemType to be a ctor', function() {
var phones = givenPhonesWithCtor();
var list = new List(phones, PhoneCtor);
list.should.be.an.instanceOf(Array);
list.toJSON().should.be.eql(phones);
});
it('converts items of plain json to the itemType', function() {
var phones = givenPhonesAsJSON();
var list = new List(phones, PhoneCtor);
list[0].should.be.an.instanceOf(PhoneCtor);
});
it('converts stringified items to the itemType', function() {
var phones = givenPhonesAsJSON();
var list = new List(JSON.stringify(phones), PhoneCtor);
list[0].should.be.an.instanceOf(PhoneCtor);
});
it('converts items of plain json to the itemType with push', function() {
var phones = givenPhonesAsJSON();
var list = new List([], PhoneCtor);
list.push(phones[0]);
list[0].should.be.an.instanceOf(PhoneCtor);
});
});
function givenPhones() {
return [
new Phone('home', '111-222-3333'),
new Phone('work', '111-222-5555'),
];
}
function givenPhonesWithCtor() {
return [
new PhoneCtor('home', '111-222-3333'),
PhoneCtor('work', '111-222-5555'),
];
}
function givenPhonesAsJSON() {
return [
{label: 'home', num: '111-222-3333'},
{label: 'work', num: '111-222-5555'},
];
}