more jsDocs + strict equality for strings

This commit is contained in:
Niels Lyngsø
2024-02-28 21:00:56 +01:00
parent e1fdadac43
commit c2bdfde140
5 changed files with 27 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
import type { MappingFunction, MemoizationFunction } from '../types/index.js';
import { createObservablePart } from '../utils/index.js';
import { createObservablePart, strictEqualityMemoization } from '../utils/index.js';
import { UmbBasicState } from './basic-state.js';
/**
@@ -17,6 +17,6 @@ export class UmbStringState<T> extends UmbBasicState<T | string> {
mappingFunction: MappingFunction<T | string, ReturnType>,
memoizationFunction?: MemoizationFunction<ReturnType>,
) {
return createObservablePart(this._subject, mappingFunction, memoizationFunction);
return createObservablePart(this._subject, mappingFunction, memoizationFunction ?? strictEqualityMemoization);
}
}

View File

@@ -1,3 +1,14 @@
/**
* Deep freeze an object and all its properties.
* @param {T} inObj - The object to deep freeze.
* @returns {T} - The deep frozen object.
* @description - Deep freezes an object and all its properties.
* @example <caption>Example of deep freezing an object.</caption>
* const myObject = {a: 1, b: {c: 2}};
* const frozenObject = deepFreeze(myObject);
* frozenObject.a = 3; // Throws an error.
* frozenObject.b.c = 4; // Throws an error.
*/
export const deepFreeze = Object.freeze(function deepFreezeImpl<T>(inObj: T): T {
if (inObj != null && typeof inObj === 'object') {
Object.freeze(inObj);

View File

@@ -3,9 +3,10 @@ export * from './create-observable-part.function.js';
export * from './deep-freeze.function.js';
export * from './default-memoization.function.js';
export * from './filter-frozen-array.function.js';
export * from './merge-observables.function.js';
export * from './json-string-comparison.function.js';
export * from './merge-observables.function.js';
export * from './observe-multiple.function.js';
export * from './partial-update-frozen-array.function.js';
export * from './push-to-unique-array.function.js';
export * from './simple-hash-code.function.js';
export * from './strict-equality-memoization.function.js';

View File

@@ -37,7 +37,7 @@ export function mergeObservables<
): Observable<R> {
return combineLatest(sources).pipe(
map(mergeFunction as MappingFunction<unknown[], R>),
distinctUntilChanged(memoizationFunction || defaultMemoization),
distinctUntilChanged(memoizationFunction ?? defaultMemoization),
shareReplay(1),
);
}

View File

@@ -0,0 +1,11 @@
/**
* @export
* @method strictEqualityMemoization
* @param {unknown} previousValue - The previous value to compare.
* @param {unknown} currentValue - The current value to compare.
* @returns {boolean} - Returns true if the values are identical.
* @description - Compares the two values using strict equality.
*/
export function strictEqualityMemoization(previousValue: unknown, currentValue: unknown): boolean {
return previousValue === currentValue;
}