Skip to content

Commit

Permalink
Merge pull request #1701 from strongloop/3x/list-format
Browse files Browse the repository at this point in the history
feat: support array within array in 3.x
  • Loading branch information
hacksparrow committed Mar 19, 2019
2 parents 16ffd42 + d8e1c2c commit 2f093ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ function List(items, itemType, parent) {
}

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) {
Expand All @@ -91,7 +96,10 @@ List.prototype.push = function(obj) {
List.prototype.toObject = function(onlySchema, removeHidden, removeProtected) {
const items = [];
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));
} else {
items.push(item);
Expand Down
3 changes: 3 additions & 0 deletions test/introspection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const json = {
{label: 'work', id: 'x@sample.com'},
{label: 'home', id: 'x@home.com'},
],
nestedArray: [
[{msg: 'Hi'}],
],
tags: [],
};

Expand Down

0 comments on commit 2f093ce

Please sign in to comment.