@nexcodepl/store
Advanced tools
| export declare function compare(a: unknown, b: unknown, compareDepth?: number): boolean; | ||
| export declare function getCompare(depth?: number): (a: unknown, b: unknown) => boolean; |
| export function compare(a, b, compareDepth) { | ||
| if (typeof a !== typeof b) | ||
| return false; | ||
| const t = typeof a; | ||
| if (t === "boolean" || t === "number" || t === "string" || t === "bigint" || t === "symbol") { | ||
| return a === b; | ||
| } | ||
| if (a instanceof Map && b instanceof Map) { | ||
| if (a.size !== b.size) | ||
| return false; | ||
| for (const [key, value] of a) { | ||
| if (compareDepth === 0) { | ||
| if (!Object.is(value, b.get(key))) { | ||
| return false; | ||
| } | ||
| } | ||
| else if (!compare(value, b.get(key), getCompareDepth())) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| if (a instanceof Set && b instanceof Set) { | ||
| if (a.size !== b.size) | ||
| return false; | ||
| for (const value of a) { | ||
| if (!b.has(value)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| if (Array.isArray(a) && Array.isArray(b)) { | ||
| if (a.length !== b.length) | ||
| return false; | ||
| for (let i = 0; i < a.length; i++) { | ||
| if (compareDepth === 0) { | ||
| if (!Object.is(a[i], b[i])) { | ||
| return false; | ||
| } | ||
| } | ||
| else if (!compare(a[i], b[i], getCompareDepth())) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| if (isObject(a) && isObject(b)) { | ||
| const keysA = Object.keys(a); | ||
| const keysB = Object.keys(b); | ||
| if (keysA.length !== keysB.length) | ||
| return false; | ||
| for (const key of keysA) { | ||
| const va = a[key]; | ||
| const vb = b[key]; | ||
| if (compareDepth === 0) { | ||
| if (!Object.is(va, vb)) { | ||
| return false; | ||
| } | ||
| } | ||
| else if (!compare(va, vb, getCompareDepth())) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| return false; | ||
| function getCompareDepth() { | ||
| if (compareDepth === undefined) | ||
| return undefined; | ||
| if (compareDepth === 0) | ||
| return 0; | ||
| return compareDepth - 1; | ||
| } | ||
| } | ||
| export function getCompare(depth) { | ||
| return (a, b) => compare(a, b, depth); | ||
| } | ||
| function isObject(v) { | ||
| return typeof v === "object"; | ||
| } |
+1
-0
@@ -5,1 +5,2 @@ export * from "./computed.js"; | ||
| export * from "./types.js"; | ||
| export * from "./compare.js"; |
+1
-0
@@ -5,1 +5,2 @@ export * from "./computed.js"; | ||
| export * from "./types.js"; | ||
| export * from "./compare.js"; |
+8
-2
@@ -0,1 +1,2 @@ | ||
| import { compare } from "./compare.js"; | ||
| export class Store { | ||
@@ -24,4 +25,9 @@ _state; | ||
| const newState = isStoreSetStateFunction(setState) ? setState(newStateBase) : setState; | ||
| if (!!this._config?.shouldStateUpdate && !this._config.shouldStateUpdate(this._state, newState)) { | ||
| return; | ||
| if (this._config?.shouldStateUpdate !== null) { | ||
| if (!!this._config?.shouldStateUpdate && !this._config.shouldStateUpdate(this._state, newState)) { | ||
| return; | ||
| } | ||
| else if (!compare(this._state, newState, 2)) { | ||
| return; | ||
| } | ||
| } | ||
@@ -28,0 +34,0 @@ this._state = newState; |
+1
-1
@@ -12,3 +12,3 @@ export type Primitive = string | number | boolean | bigint | symbol | undefined | null; | ||
| export interface StoreConfig<T> { | ||
| shouldStateUpdate?: StoreStateShouldUpdate<T>; | ||
| shouldStateUpdate?: StoreStateShouldUpdate<T> | null; | ||
| stateCopyFunction?: (state: T) => T; | ||
@@ -15,0 +15,0 @@ } |
+1
-1
| { | ||
| "name": "@nexcodepl/store", | ||
| "version": "1.0.7", | ||
| "version": "1.0.8", | ||
| "description": "State management library", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
10663
35.44%15
15.38%247
58.33%