getUniqueMethod

This commit is contained in:
Niels Lyngsø
2023-11-27 11:56:45 +01:00
parent 6344ad136b
commit b65c02c90f
2 changed files with 15 additions and 15 deletions

View File

@@ -12,12 +12,12 @@ import { UmbDeepState } from './deep-state.js';
* The ArrayState provides methods to append data when the data is an Object.
*/
export class UmbArrayState<T> extends UmbDeepState<T[]> {
readonly getUnique: (entry: T) => unknown;
readonly getUniqueMethod: (entry: T) => unknown;
#sortMethod?: (a: T, b: T) => number;
constructor(initialData: T[], getUniqueOfEntryMethod: (entry: T) => unknown) {
super(initialData);
this.getUnique = getUniqueOfEntryMethod;
this.getUniqueMethod = getUniqueOfEntryMethod;
}
/**
@@ -60,12 +60,12 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
*/
remove(uniques: unknown[]) {
let next = this.getValue();
if (this.getUnique) {
if (this.getUniqueMethod) {
uniques.forEach((unique) => {
next = next.filter((x) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.getUnique(x) !== unique;
return this.getUniqueMethod(x) !== unique;
});
});
@@ -89,11 +89,11 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
*/
removeOne(unique: unknown) {
let next = this.getValue();
if (this.getUnique) {
if (this.getUniqueMethod) {
next = next.filter((x) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.getUnique(x) !== unique;
return this.getUniqueMethod(x) !== unique;
});
this.next(next);
@@ -142,8 +142,8 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
*/
appendOne(entry: T) {
const next = [...this.getValue()];
if (this.getUnique) {
pushToUniqueArray(next, entry, this.getUnique);
if (this.getUniqueMethod) {
pushToUniqueArray(next, entry, this.getUniqueMethod);
} else {
next.push(entry);
}
@@ -168,10 +168,10 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
* ]);
*/
append(entries: T[]) {
if (this.getUnique) {
if (this.getUniqueMethod) {
const next = [...this.getValue()];
entries.forEach((entry) => {
pushToUniqueArray(next, entry, this.getUnique!);
pushToUniqueArray(next, entry, this.getUniqueMethod!);
});
this.next(next);
} else {
@@ -195,10 +195,10 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
* myState.updateOne(2, {value: 'updated-bar'});
*/
updateOne(unique: unknown, entry: Partial<T>) {
if (!this.getUnique) {
if (!this.getUniqueMethod) {
throw new Error("Can't partial update an ArrayState without a getUnique method provided when constructed.");
}
this.next(partialUpdateFrozenArray(this.getValue(), entry, (x) => unique === this.getUnique!(x)));
this.next(partialUpdateFrozenArray(this.getValue(), entry, (x) => unique === this.getUniqueMethod!(x)));
return this;
}
}

View File

@@ -31,7 +31,7 @@ export class UmbStoreBase<StoreItemType = any> extends EventTarget implements Um
*/
append(item: StoreItemType) {
this._data.append([item]);
const unique = this._data.getUnique(item) as string;
const unique = this._data.getUniqueMethod(item) as string;
this.dispatchEvent(new UmbStoreAppendEvent([unique]));
}
@@ -42,7 +42,7 @@ export class UmbStoreBase<StoreItemType = any> extends EventTarget implements Um
*/
appendItems(items: Array<StoreItemType>) {
this._data.append(items);
const uniques = items.map((item) => this._data.getUnique(item)) as Array<string>;
const uniques = items.map((item) => this._data.getUniqueMethod(item)) as Array<string>;
this.dispatchEvent(new UmbStoreAppendEvent(uniques));
}
@@ -84,7 +84,7 @@ export class UmbStoreBase<StoreItemType = any> extends EventTarget implements Um
* @memberof UmbStoreBase
*/
getItems(uniques: Array<string>) {
return this._data.getValue().filter((item) => uniques.includes(this._data.getUnique(item) as string));
return this._data.getValue().filter((item) => uniques.includes(this._data.getUniqueMethod(item) as string));
}
/**