From 894405be8a269434eb8fbbe0802858837bf6e048 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Tue, 16 Aug 2022 19:14:48 +0300 Subject: [PATCH] make _all$ protected in entities service --- src/lib/listing/services/entities.service.ts | 12 +++++------ src/lib/utils/functions.ts | 21 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/lib/listing/services/entities.service.ts b/src/lib/listing/services/entities.service.ts index 294e2b0..0d674c5 100644 --- a/src/lib/listing/services/entities.service.ts +++ b/src/lib/listing/services/entities.service.ts @@ -22,17 +22,17 @@ export class EntitiesService< protected readonly _entityClass?: new (entityInterface: Interface, ...args: unknown[]) => Class; protected readonly _entityChanged$ = new Subject(); protected readonly _entityDeleted$ = new Subject(); - readonly #all$ = new BehaviorSubject([]); + protected readonly _all$ = new BehaviorSubject([]); constructor() { super(); - this.all$ = this.#all$.asObservable().pipe(shareDistinctLast()); - this.allLength$ = this.#all$.pipe(getLength, shareDistinctLast()); + this.all$ = this._all$.asObservable().pipe(shareDistinctLast()); + this.allLength$ = this._all$.pipe(getLength, shareDistinctLast()); this.noData$ = this.#noData$; } get all(): Class[] { - return Object.values(this.#all$.getValue()); + return Object.values(this._all$.getValue()); } get #noData$(): Observable { @@ -78,7 +78,7 @@ export class EntitiesService< return entity; }); - this.#all$.next(newEntities); + this._all$.next(newEntities); // Emit observables only after entities have been updated @@ -94,7 +94,7 @@ export class EntitiesService< remove(id: Id) { const entity = this.all.find(item => item.id === id); if (entity) { - this.#all$.next(this.all.filter(item => item.id !== id)); + this._all$.next(this.all.filter(item => item.id !== id)); this._entityDeleted$.next(entity); } } diff --git a/src/lib/utils/functions.ts b/src/lib/utils/functions.ts index a920067..948a20c 100644 --- a/src/lib/utils/functions.ts +++ b/src/lib/utils/functions.ts @@ -137,6 +137,21 @@ export function bool(value: unknown): boolean { return Boolean(_value); } +export function groupBy(array: T[], predicate: (value: T, index: number, items: T[]) => Q) { + return array.reduce((dict, value, index, items) => { + const key = predicate(value, index, items); + if (dict.has(key)) { + const group = dict.get(key); + if (!group) { + throw new Error(`Oh, why, group ${key} is undefined`); + } + group.push(value); + return dict; + } + return dict.set(key, [value]); + }, new Map()); +} + declare global { interface String { capitalize(): string; @@ -150,6 +165,8 @@ declare global { * The value returned from the function determines whether the element is kept or removed. */ filterTruthy(and?: (value: T) => boolean): T[]; + + groupBy(condition: (value: T) => Key): Map; } interface Console { @@ -172,6 +189,10 @@ Array.prototype.filterTruthy = function (this: T[], predicate: (value: T) => return this.filter(value => !!value && predicate(value)); }; +Array.prototype.groupBy = function (this: T[], condition: (value: T) => Key): Map { + return groupBy(this, condition); +}; + /** * Use this in field initialization or in constructor of a service / component * @param param