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

Function assertSorted

packages/db-collection-e2e/src/utils/assertions.ts:108–134  ·  view source on GitHub ↗
(
  items: Array<T>,
  field: K,
  direction: `asc` | `desc` = `asc`,
  message?: string,
)

Source from the content-addressed store, hash-verified

106 * Assert that items are sorted correctly
107 */
108export function assertSorted<T, K extends keyof T>(
109 items: Array<T>,
110 field: K,
111 direction: `asc` | `desc` = `asc`,
112 message?: string,
113) {
114 for (let i = 1; i < items.length; i++) {
115 const prev = items[i - 1]![field]
116 const curr = items[i]![field]
117
118 if (direction === `asc`) {
119 if (prev > curr) {
120 throw new Error(
121 message ??
122 `Items not sorted ascending by ${String(field)}: ${prev} > ${curr} at index ${i}`,
123 )
124 }
125 } else {
126 if (prev < curr) {
127 throw new Error(
128 message ??
129 `Items not sorted descending by ${String(field)}: ${prev} < ${curr} at index ${i}`,
130 )
131 }
132 }
133 }
134}
135
136/**
137 * Assert that predicate pushdown occurred (no over-fetching)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected