UNPKG

generator-begcode

Version:

Spring Boot + Angular/React/Vue in one handy generator

41 lines (40 loc) 1.27 kB
export class LazyArray { items; constructor(items) { this.items = items; } then(onfulfilled, onrejected) { const promise = Array.isArray(this.items) ? Promise.resolve(this.items) : this.items; return promise.then(onfulfilled, onrejected); } map(selector) { if (Array.isArray(this.items)) { return new LazyArray(this.items.map(selector)); } return new LazyArray(this.items.then(x => x.map(selector))); } unique() { if (Array.isArray(this.items)) { return new LazyArray(filterDuplicates(this.items, x => x)); } return new LazyArray(this.items.then(x => filterDuplicates(x, x => x))); } uniqueBy(compareBy) { if (Array.isArray(this.items)) { return new LazyArray(filterDuplicates(this.items, compareBy)); } return new LazyArray(this.items.then(x => filterDuplicates(x, compareBy))); } } export const filterDuplicates = (items, compareBy) => { const set = new Set(); const uniqueItems = []; for (const item of items) { if (set.has(compareBy(item))) { continue; } uniqueItems.push(item); set.add(compareBy(item)); } return uniqueItems; };