Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 91x 91x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 37x 37x 35x 35x 35x 35x 37x 34x 37x 38x 38x 38x 25x 25x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 90x 60x 60x 56x 56x 56x 90x 90x 56x 90x 7x 7x 12x 12x 4x 4x 49x 90x 49x 49x 49x 49x 90x 49x 49x 49x 49x 49x 49x 49x 49x 42x 42x 42x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Equality utilities for comparing values.
*/
import type { AnyFunc, Equality, EqualityShorthand } from "./types";
/**
* Strict equality (Object.is).
*/
export function strictEqual<T>(a: T, b: T): boolean {
return Object.is(a, b);
}
/**
* Shallow equality for objects/arrays.
* Compares by reference for each top-level key/index.
*
* @param itemEqual - Optional comparator for each item/value (defaults to Object.is)
*/
export function shallowEqual<T>(
a: T,
b: T,
itemEqual: (a: unknown, b: unknown) => boolean = Object.is
): boolean {
if (Object.is(a, b)) return true;
if (typeof a !== "object" || a === null) return false;
if (typeof b !== "object" || b === null) return false;
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
for (const key of keysA) {
if (!Object.prototype.hasOwnProperty.call(b, key)) return false;
if (!itemEqual((a as any)[key], (b as any)[key])) return false;
}
return true;
}
/**
* 2-level shallow equality.
* Compares keys/length, then shallow compares each item/value.
*
* @example
* [{ id: 1, data: obj }] vs [{ id: 1, data: obj }] // true (same obj ref)
*/
export function shallow2Equal<T>(a: T, b: T): boolean {
return shallowEqual(a, b, shallowEqual);
}
/**
* 3-level shallow equality.
* Compares keys/length, then shallow2 compares each item/value.
*
* @example
* [{ id: 1, nested: { data: obj } }] vs [{ id: 1, nested: { data: obj } }] // true
*/
export function shallow3Equal<T>(a: T, b: T): boolean {
return shallowEqual(a, b, shallow2Equal);
}
/**
* Deep equality.
*/
export function deepEqual(a: any, b: any): boolean {
if (Object.is(a, b)) return true;
if (
typeof a !== "object" ||
a === null ||
typeof b !== "object" ||
b === null
)
return false;
if (Array.isArray(a)) {
if (!Array.isArray(b) || a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) return false;
}
return true;
}
if (Array.isArray(b)) return false;
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
for (const key of keysA) {
if (
!Object.prototype.hasOwnProperty.call(b, key) ||
!deepEqual(a[key], b[key])
)
return false;
}
return true;
}
/**
* Resolve equality strategy to a function.
*/
export function resolveEquality<T>(
e: Equality<T> | undefined
): (a: T, b: T) => boolean {
if (!e || e === "strict") return strictEqual;
if (e === "shallow") return shallowEqual;
if (e === "shallow2") return shallow2Equal;
if (e === "shallow3") return shallow3Equal;
if (e === "deep") return deepEqual;
return e;
}
export function equality(shorthand: EqualityShorthand) {
return resolveEquality(shorthand);
}
// =============================================================================
// Value Stabilization
// =============================================================================
export type StableFn<TArgs extends any[], TResult> = {
getOriginal: () => (...args: TArgs) => TResult;
getCurrent: () => (...args: TArgs) => TResult;
setCurrent: (newFn: (...args: TArgs) => TResult) => void;
};
export function createStableFn<TArgs extends any[], TResult>(
fn: (...args: TArgs) => TResult
): StableFn<TArgs, TResult> {
const originalFn = fn;
let currentFn = fn;
return Object.assign(
(...args: TArgs) => {
return currentFn(...args);
},
{
getOriginal: () => originalFn,
getCurrent: () => currentFn,
setCurrent(newFn: (...args: TArgs) => TResult) {
currentFn = newFn;
},
}
);
}
/**
* Check if a value is a stable function wrapper.
*/
export function isStableFn<TArgs extends any[], TResult>(
value: unknown
): value is StableFn<TArgs, TResult> {
return (
typeof value === "function" &&
"getOriginal" in value &&
"getCurrent" in value &&
"setCurrent" in value
);
}
/**
* Stabilize a value with automatic function wrapper support.
*
* - Functions: Creates/updates stable wrapper (reference never changes)
* - Date objects: Compared by timestamp (uses deepEqual)
* - Other values: Returns previous if equal per equalityFn
*
* @param prev - Previous value container (or undefined for first call)
* @param next - New value
* @param equalityFn - Equality function for non-function/non-date values
* @returns Tuple of [stabilized value, wasStable]
*/
export function tryStabilize<T>(
prev: { value: T } | undefined,
next: T,
equalityFn: (a: T, b: T) => boolean
): [T, boolean] {
// First time - no previous value
if (!prev) {
if (typeof next === "function") {
return [createStableFn(next as AnyFunc) as T, false];
}
return [next, false];
}
// Handle functions with stable wrapper pattern
if (typeof next === "function") {
if (isStableFn(prev.value)) {
// Update existing stable wrapper with new function
prev.value.setCurrent(next as AnyFunc);
return [prev.value as T, true];
}
// Previous wasn't a stable fn, create new wrapper
return [createStableFn(next as AnyFunc) as T, false];
}
if (next && next instanceof Date) {
if (prev.value && prev.value instanceof Date) {
if (next.getTime() === prev.value.getTime()) {
return [prev.value, true];
}
}
return [next, false];
}
// Non-functions: use equality comparison
if (equalityFn(prev.value, next)) {
return [prev.value, true];
}
return [next, false];
}
|