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

28 lines
514 B
JavaScript
Raw Normal View History

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