Merge pull request #1701 from strongloop/3x/list-format

feat: support array within array in 3.x
This commit is contained in:
Hage Yaapa 2019-03-19 19:06:14 +05:30 committed by GitHub
commit 2f093ce254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -64,7 +64,12 @@ function List(items, itemType, parent) {
} }
List.prototype.toItem = function(item) { List.prototype.toItem = function(item) {
return isClass(this.itemType) ? new this.itemType(item) : this.itemType(item); if (isClass(this.itemType)) {
return new this.itemType(item);
} else {
if (Array.isArray(item)) return item;
else return this.itemType(item);
}
}; };
items.forEach(function(item, i) { items.forEach(function(item, i) {
@ -91,7 +96,10 @@ List.prototype.push = function(obj) {
List.prototype.toObject = function(onlySchema, removeHidden, removeProtected) { List.prototype.toObject = function(onlySchema, removeHidden, removeProtected) {
const items = []; const items = [];
this.forEach(function(item) { this.forEach(function(item) {
if (item && typeof item === 'object' && item.toObject) { if (item && Array.isArray(item) && item.toArray) {
const subArray = item.toArray();
items.push(subArray);
} else if (item && typeof item === 'object' && item.toObject) {
items.push(item.toObject(onlySchema, removeHidden, removeProtected)); items.push(item.toObject(onlySchema, removeHidden, removeProtected));
} else { } else {
items.push(item); items.push(item);

View File

@ -27,6 +27,9 @@ const json = {
{label: 'work', id: 'x@sample.com'}, {label: 'work', id: 'x@sample.com'},
{label: 'home', id: 'x@home.com'}, {label: 'home', id: 'x@home.com'},
], ],
nestedArray: [
[{msg: 'Hi'}],
],
tags: [], tags: [],
}; };