avocado-old/packages/behavior/item/collection.js

36 lines
648 B
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
import {fromJSON as behaviorItemFromJSON} from './registry';
export function Collection(type) {
return class Collection {
static type() {
return `${type}s`;
}
constructor() {
this[`${type}s`] = [];
}
count() {
return this[`${type}s`].length;
}
fromJSON(json) {
this[`${type}s`] = [];
for (const i in json.items) {
const item = json.items[i];
this[`${type}s`].push(behaviorItemFromJSON(item.type, item));
}
return this;
}
toJSON() {
return {
type,
items: this[`${type}s`].map((item) => item.toJSON()),
};
}
};
}