MCPcopy Create free account
hub / github.com/TanStack/db / findInsertPosition

Function findInsertPosition

packages/db/src/utils/array-utils.ts:37–57  ·  view source on GitHub ↗
(
  sortedArray: Array<[T, any]>,
  value: T,
  compareFn: (a: T, b: T) => number,
)

Source from the content-addressed store, hash-verified

35 * @returns The index where the value should be inserted to maintain order
36 */
37export function findInsertPosition<T>(
38 sortedArray: Array<[T, any]>,
39 value: T,
40 compareFn: (a: T, b: T) => number,
41): number {
42 let left = 0
43 let right = sortedArray.length
44
45 while (left < right) {
46 const mid = Math.floor((left + right) / 2)
47 const comparison = compareFn(sortedArray[mid]![0], value)
48
49 if (comparison < 0) {
50 left = mid + 1
51 } else {
52 right = mid
53 }
54 }
55
56 return left
57}
58
59/**
60 * Deletes a value from a sorted array while maintaining sort order

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected