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

28 lines
514 B
JavaScript

import {fromJSON as behaviorItemFromJSON} from './registry';
export function Collection(type) {
const plural = `${type}s`;
return class Collection {
constructor() {
this[plural] = [];
}
count() {
return this[plural].length;
}
fromJSON(json) {
this[plural] = [];
for (const item of json) {
this[plural].push(behaviorItemFromJSON(item));
}
return this;
}
toJSON() {
return this[plural].map((item) => item.toJSON());
}
};
}