Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/libs/observable-api/append-to-frozen-array.function.ts

28 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-01-23 14:42:24 +01:00
/**
* @export
* @method appendToFrozenArray
* @param {Observable<T>} source - RxJS Subject to use for this Observable.
* @param {(mappable: T) => R} mappingFunction - Method to return the part for this Observable to return.
* @param {(previousResult: R, currentResult: R) => boolean} [memoizationFunction] - Method to Compare if the data has changed. Should return true when data is different.
* @description - Creates a RxJS Observable from RxJS Subject.
2023-01-24 13:44:04 +01:00
* @example <caption>Example append new entry for a ArrayState or a part of DeepState/ObjectState it which is an array. Where the key is unique and the item will be updated if matched with existing.</caption>
2023-01-23 14:42:24 +01:00
* const entry = {key: 'myKey', value: 'myValue'};
* const newDataSet = appendToFrozenArray(mySubject.getValue(), entry, x => x.key === key);
* mySubject.next(newDataSet);
*/
export function appendToFrozenArray<T>(data: T[], entry: T, getUniqueMethod?: (entry: T) => unknown): T[] {
const unFrozenDataSet = [...data];
if (getUniqueMethod) {
const unique = getUniqueMethod(entry);
const indexToReplace = unFrozenDataSet.findIndex((x) => getUniqueMethod(x) === unique);
if (indexToReplace !== -1) {
unFrozenDataSet[indexToReplace] = entry;
} else {
unFrozenDataSet.push(entry);
}
} else {
unFrozenDataSet.push(entry);
}
return unFrozenDataSet;
}