Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

utils.ts

211 lines5.6 KB

1/**
2 * Simple assertion function for runtime checks.
3 * Throws an error if the condition is false.
4 */
5export function assert(
6 condition: unknown,
7 message?: string,
8): asserts condition {
9 if (!condition) {
10 throw new Error(message || `Assertion failed`)
11 }
13​
14/**
15 * A map that returns a default value for keys that are not present.
16 */
17export class DefaultMap<K, V> extends Map<K, V> {
18 constructor(
19 private defaultValue: () => V,
20 entries?: Iterable<[K, V]>,
21 ) {
22 super(entries)
23 }
24​
25 get(key: K): V {
26 if (!this.has(key)) {
27 // this.set(key, this.defaultValue())
28 return this.defaultValue()
29 }
30 return super.get(key)!
31 }
32​
33 /**
34 * Update the value for a key using a function.
35 */
36 update(key: K, updater: (value: V) => V): V {
37 const value = this.get(key)
38 const newValue = updater(value)
39 this.set(key, newValue)
40 return newValue
41 }
43​
44// JS engines have various limits on how many args can be passed to a function
45// with a spread operator, so we need to split the operation into chunks
46// 32767 is the max for Chrome 14, all others are higher
47// TODO: investigate the performance of this and other approaches
48const chunkSize = 30000
49export function chunkedArrayPush(array: Array<unknown>, other: Array<unknown>) {
50 if (other.length <= chunkSize) {
51 array.push(...other)
52 } else {
53 for (let i = 0; i < other.length; i += chunkSize) {
54 const chunk = other.slice(i, i + chunkSize)
55 array.push(...chunk)
56 }
57 }
59​
60export function binarySearch<T>(
61 array: Array<T>,
62 value: T,
63 comparator: (a: T, b: T) => number,
64): number {
65 let low = 0
66 let high = array.length
67 while (low < high) {
68 const mid = Math.floor((low + high) / 2)
69 const comparison = comparator(array[mid]!, value)
70 if (comparison < 0) {
71 low = mid + 1
72 } else if (comparison > 0) {
73 high = mid
74 } else {
75 return mid
76 }
77 }
78 return low
80​
81/**
82 * Utility for generating unique IDs for objects and values.
83 * Uses WeakMap for object reference tracking and consistent hashing for primitives.
84 */
85export class ObjectIdGenerator {
86 private objectIds = new WeakMap<object, number>()
87 private nextId = 0
88​
89 /**
90 * Get a unique identifier for any value.
91 * - Objects: Uses WeakMap for reference-based identity
92 * - Primitives: Uses consistent string-based hashing
93 */
94 getId(value: any): number {
95 // For primitives, use a simple hash of their string representation
96 if (typeof value !== `object` || value === null) {
97 const str = String(value)
98 let hashValue = 0
99 for (let i = 0; i < str.length; i++) {
100 const char = str.charCodeAt(i)
101 hashValue = (hashValue << 5) - hashValue + char
102 hashValue = hashValue & hashValue // Convert to 32-bit integer
103 }
104 return hashValue
105 }
106​
107 // For objects, use WeakMap to assign unique IDs
108 if (!this.objectIds.has(value)) {
109 this.objectIds.set(value, this.nextId++)
110 }
111 return this.objectIds.get(value)!
112 }
113​
114 /**
115 * Get a string representation of the ID for use in composite keys.
116 */
117 getStringId(value: any): string {
118 if (value === null) return `null`
119 if (value === undefined) return `undefined`
120 if (typeof value !== `object`) return `str_${String(value)}`
121​
122 return `obj_${this.getId(value)}`
123 }
125​
126/**
127 * Global instance for cases where a shared object ID space is needed.
128 */
129export const globalObjectIdGenerator = new ObjectIdGenerator()
130​
131export function* concatIterable<T>(
132 ...iterables: Array<Iterable<T>>
133): Iterable<T> {
134 for (const iterable of iterables) {
135 yield* iterable
136 }
138​
139export function* mapIterable<T, U>(
140 it: Iterable<T>,
141 fn: (t: T) => U,
142): Iterable<U> {
143 for (const t of it) {
144 yield fn(t)
145 }
147​
148export type HRange = [number, number] // half-open [start, end[ i.e. end is exclusive
149​
150/**
151 * Computes the difference between two half-open ranges.
152 * @param a - The first half-open range
153 * @param b - The second half-open range
154 * @returns The difference between the two ranges
155 */
156export function diffHalfOpen(a: HRange, b: HRange) {
157 const [a1, a2] = a
158 const [b1, b2] = b
159​
160 // A \ B can be up to two segments (left and right of the overlap)
161 const onlyInA: Array<number> = [
162 ...range(a1, Math.min(a2, b1)), // left side of A outside B
163 ...range(Math.max(a1, b2), a2), // right side of A outside B
164 ]
165​
166 // B \ A similarly
167 const onlyInB: Array<number> = [
168 ...range(b1, Math.min(b2, a1)),
169 ...range(Math.max(b1, a2), b2),
170 ]
171​
172 return { onlyInA, onlyInB }
174​
175function range(start: number, end: number): Array<number> {
176 const out: Array<number> = []
177 for (let i = start; i < end; i++) out.push(i)
178 return out
180​
181/**
182 * Compares two keys (string | number) in a consistent, deterministic way.
183 * Handles mixed types by ordering strings before numbers.
184 */
185export function compareKeys(a: string | number, b: string | number): number {
186 // Same type: compare directly
187 if (typeof a === typeof b) {
188 if (a < b) return -1
189 if (a > b) return 1
190 return 0
191 }
192 // Different types: strings come before numbers
193 return typeof a === `string` ? -1 : 1
195​
196/**
197 * Serializes a value for use as a key, handling BigInt and Date values that JSON.stringify cannot handle.
198 * Uses JSON.stringify with a replacer function to convert BigInt values to strings and Date values to ISO strings.
199 * This is used for creating string keys in groupBy operations.
200 */
201export function serializeValue(value: unknown): string {
202 return JSON.stringify(value, (_, val) => {
203 if (typeof val === 'bigint') {
204 return val.toString()
205 }
206 if (val instanceof Date) {
207 return val.toISOString()
208 }
209 return val
210 })