UNPKG

@bscotch/stitch

Version:

Stitch: The GameMaker Studio 2 Asset Pipeline Development Kit.

61 lines 1.88 kB
import { Yy } from '@bscotch/yy'; import { difference, uniqBy } from 'lodash-es'; import { warn } from '../../utility/log.js'; import { dehydrateArray } from '../hydrate.js'; export class Gms2ComponentArrayBase { items = []; /** Get shallow-copy array of all item instances */ list() { return [...this.items]; } filter(matchFunction) { return this.items.filter(matchFunction); } filterByField(field, value) { return this.items.filter((item) => item[field] == value); } find(matchFunction) { return this.items.find(matchFunction); } findByField(field, value) { return this.items.find((item) => item[field] == value); } removeByField(field, value) { const itemIdx = this.items.findIndex((item) => item[field] == value); if (itemIdx > -1) { this.items.splice(itemIdx, 1); } return this; } push(...items) { this.items.push(...items); return this; } prepend(...items) { this.items.unshift(...items); return this; } /** * Create a new component instance if one doesn't already exist * matching the given uniqueField:uniqueValue pair. */ addIfNew(data, uniqueField, uniqueFieldValue) { const existing = this.findByField(uniqueField, uniqueFieldValue); if (!existing) { return this.addNew(data); } return false; } toJSON() { return dehydrateArray(this.items); } uniqueYypDataEntries(data) { const uniqueData = uniqBy(data, Yy.stringify); const removedItems = difference(data, uniqueData); if (removedItems.length) { warn(`Duplicate entries found`, { count: removedItems.length }); } return uniqueData; } } //# sourceMappingURL=Gms2ComponentArrayBase.js.map