Partly Document Type Workspace (#636)
* move container property logic into a manager * restructure of property structure helpers * simpler styling * migrate structural code to helper classes * rename to helper * re arrange * rename + transfer structural views * styling * context observers * refactor container observables * _observeOwnerProperties * clean up * creation of base structure * add property method * move style * view and edit properties * use ! * import ordering * correct typing * fixes
This commit is contained in:
@@ -12,11 +12,37 @@ import { pushToUniqueArray } from './push-to-unique-array.function';
|
||||
* The ArrayState provides methods to append data when the data is an Object.
|
||||
*/
|
||||
export class ArrayState<T> extends DeepState<T[]> {
|
||||
private _getUnique?: (entry: T) => unknown;
|
||||
#getUnique?: (entry: T) => unknown;
|
||||
#sortMethod?: (a: T, b: T) => number;
|
||||
|
||||
constructor(initialData: T[], getUniqueMethod?: (entry: T) => unknown) {
|
||||
super(initialData);
|
||||
this._getUnique = getUniqueMethod;
|
||||
this.#getUnique = getUniqueMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* @method sortBy
|
||||
* @param {(a: T, b: T) => number} sortMethod - A method to be used for sorting everytime data is set.
|
||||
* @description - A sort method to this Subject.
|
||||
* @example <caption>Example add sort method</caption>
|
||||
* const data = [
|
||||
* { key: 1, value: 'foo'},
|
||||
* { key: 2, value: 'bar'}
|
||||
* ];
|
||||
* const myState = new ArrayState(data, (x) => x.key);
|
||||
* myState.sortBy((a, b) => (a.sortOrder || 0) - (b.sortOrder || 0));
|
||||
*/
|
||||
sortBy(sortMethod?: (a: T, b: T) => number) {
|
||||
this.#sortMethod = sortMethod;
|
||||
return this;
|
||||
}
|
||||
|
||||
next(value: T[]) {
|
||||
if (this.#sortMethod) {
|
||||
super.next(value.sort(this.#sortMethod));
|
||||
} else {
|
||||
super.next(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,12 +60,12 @@ export class ArrayState<T> extends DeepState<T[]> {
|
||||
*/
|
||||
remove(uniques: unknown[]) {
|
||||
let next = this.getValue();
|
||||
if (this._getUnique) {
|
||||
if (this.#getUnique) {
|
||||
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.#getUnique(x) !== unique;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -63,11 +89,11 @@ export class ArrayState<T> extends DeepState<T[]> {
|
||||
*/
|
||||
removeOne(unique: unknown) {
|
||||
let next = this.getValue();
|
||||
if (this._getUnique) {
|
||||
if (this.#getUnique) {
|
||||
next = next.filter((x) => {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
return this._getUnique(x) !== unique;
|
||||
return this.#getUnique(x) !== unique;
|
||||
});
|
||||
|
||||
this.next(next);
|
||||
@@ -116,8 +142,8 @@ export class ArrayState<T> extends DeepState<T[]> {
|
||||
*/
|
||||
appendOne(entry: T) {
|
||||
const next = [...this.getValue()];
|
||||
if (this._getUnique) {
|
||||
pushToUniqueArray(next, entry, this._getUnique);
|
||||
if (this.#getUnique) {
|
||||
pushToUniqueArray(next, entry, this.#getUnique);
|
||||
} else {
|
||||
next.push(entry);
|
||||
}
|
||||
@@ -142,10 +168,10 @@ export class ArrayState<T> extends DeepState<T[]> {
|
||||
* ]);
|
||||
*/
|
||||
append(entries: T[]) {
|
||||
if (this._getUnique) {
|
||||
if (this.#getUnique) {
|
||||
const next = [...this.getValue()];
|
||||
entries.forEach((entry) => {
|
||||
pushToUniqueArray(next, entry, this._getUnique!);
|
||||
pushToUniqueArray(next, entry, this.#getUnique!);
|
||||
});
|
||||
this.next(next);
|
||||
} else {
|
||||
@@ -169,16 +195,10 @@ export class ArrayState<T> extends DeepState<T[]> {
|
||||
* myState.updateOne(2, {value: 'updated-bar'});
|
||||
*/
|
||||
updateOne(unique: unknown, entry: Partial<T>) {
|
||||
if (!this._getUnique) {
|
||||
if (!this.#getUnique) {
|
||||
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 as Exclude<typeof this._getUnique, undefined>)(x)
|
||||
)
|
||||
);
|
||||
this.next(partialUpdateFrozenArray(this.getValue(), entry, (x) => unique === this.#getUnique!(x)));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user