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

orderBy.ts

201 lines6.0 KB

1import { topK, topKWithIndex } from "./topK.js"
2import { topKWithFractionalIndex } from "./topKWithFractionalIndex.js"
3import { map } from "./map.js"
4import { innerJoin } from "./join.js"
5import { consolidate } from "./consolidate.js"
6import type { KeyValue } from "../types.js"
7import type { IStreamBuilder } from "../types"
8​
9export interface OrderByOptions<Ve> {
10 comparator?: (a: Ve, b: Ve) => number
11 limit?: number
12 offset?: number
14​
15type OrderByWithFractionalIndexOptions<Ve> = OrderByOptions<Ve> & {
16 setSizeCallback?: (getSize: () => number) => void
18​
19/**
20 * Orders the elements and limits the number of results, with optional offset
21 * This requires a keyed stream, and uses the `topK` operator to order all the elements.
22 *
23 * @param valueExtractor - A function that extracts the value to order by from the element
24 * @param options - An optional object containing comparator, limit and offset properties
25 * @returns A piped operator that orders the elements and limits the number of results
26 */
27export function orderBy<T extends KeyValue<unknown, unknown>, Ve = unknown>(
28 valueExtractor: (
29 value: T extends KeyValue<unknown, infer V> ? V : never
30 ) => Ve,
31 options?: OrderByOptions<Ve>
32) {
33 const limit = options?.limit ?? Infinity
34 const offset = options?.offset ?? 0
35 const comparator =
36 options?.comparator ??
37 ((a, b) => {
38 // Default to JS like ordering
39 if (a === b) return 0
40 if (a < b) return -1
41 return 1
42 })
43​
44 return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {
45 type KeyType = T extends KeyValue<infer K, unknown> ? K : never
46​
47 return stream.pipe(
48 map(
49 ([key, value]) =>
50 [
51 null,
52 [
53 key,
54 valueExtractor(
55 value as T extends KeyValue<unknown, infer V> ? V : never
56 ),
57 ],
58 ] as KeyValue<null, [KeyType, Ve]>
59 ),
60 topK((a, b) => comparator(a[1], b[1]), { limit, offset }),
61 map(([_, [key]]) => [key, null] as KeyValue<KeyType, null>),
62 innerJoin(stream),
63 map(([key, value]) => {
64 return [key, value[1]] as T
65 }),
66 consolidate()
67 )
68 }
70​
71/**
72 * Orders the elements and limits the number of results, with optional offset and
73 * annotates the value with the index.
74 * This requires a keyed stream, and uses the `topKWithIndex` operator to order all the elements.
75 *
76 * @param valueExtractor - A function that extracts the value to order by from the element
77 * @param options - An optional object containing comparator, limit and offset properties
78 * @returns A piped operator that orders the elements and limits the number of results
79 */
80export function orderByWithIndex<
81 T extends KeyValue<unknown, unknown>,
82 Ve = unknown,
83>(
84 valueExtractor: (
85 value: T extends KeyValue<unknown, infer V> ? V : never
86 ) => Ve,
87 options?: OrderByOptions<Ve>
88) {
89 const limit = options?.limit ?? Infinity
90 const offset = options?.offset ?? 0
91 const comparator =
92 options?.comparator ??
93 ((a, b) => {
94 // Default to JS like ordering
95 if (a === b) return 0
96 if (a < b) return -1
97 return 1
98 })
99​
100 return (
101 stream: IStreamBuilder<T>
102 ): IStreamBuilder<
103 KeyValue<
104 T extends KeyValue<infer K, unknown> ? K : never,
105 [T extends KeyValue<unknown, infer V> ? V : never, number]
106 >
107 > => {
108 type KeyType = T extends KeyValue<infer K, unknown> ? K : never
109 type ValueType = T extends KeyValue<unknown, infer V> ? V : never
110​
111 return stream.pipe(
112 map(
113 ([key, value]) =>
114 [
115 null,
116 [
117 key,
118 valueExtractor(
119 value as T extends KeyValue<unknown, infer V> ? V : never
120 ),
121 ],
122 ] as KeyValue<null, [KeyType, Ve]>
123 ),
124 topKWithIndex((a, b) => comparator(a[1], b[1]), { limit, offset }),
125 map(([_, [[key], index]]) => [key, index] as KeyValue<KeyType, number>),
126 innerJoin(stream),
127 map(([key, [index, value]]) => {
128 return [key, [value, index]] as KeyValue<KeyType, [ValueType, number]>
129 }),
130 consolidate()
131 )
132 }
134​
135export function orderByWithFractionalIndexBase<
136 T extends KeyValue<unknown, unknown>,
137 Ve = unknown,
138>(
139 topKFunction: typeof topKWithFractionalIndex,
140 valueExtractor: (
141 value: T extends KeyValue<unknown, infer V> ? V : never
142 ) => Ve,
143 options?: OrderByWithFractionalIndexOptions<Ve>
144) {
145 type KeyType = T extends KeyValue<infer K, unknown> ? K : never
146 type ValueType = T extends KeyValue<unknown, infer V> ? V : never
147​
148 const limit = options?.limit ?? Infinity
149 const offset = options?.offset ?? 0
150 const setSizeCallback = options?.setSizeCallback
151 const comparator =
152 options?.comparator ??
153 ((a, b) => {
154 // Default to JS like ordering
155 if (a === b) return 0
156 if (a < b) return -1
157 return 1
158 })
159​
160 return (
161 stream: IStreamBuilder<T>
162 ): IStreamBuilder<[KeyType, [ValueType, string]]> => {
163 return stream.pipe(
164 topKFunction(
165 (a: ValueType, b: ValueType) =>
166 comparator(valueExtractor(a), valueExtractor(b)),
167 {
168 limit,
169 offset,
170 setSizeCallback,
171 }
172 ),
173 consolidate()
174 )
175 }
177​
178/**
179 * Orders the elements and limits the number of results, with optional offset and
180 * annotates the value with a fractional index.
181 * This requires a keyed stream, and uses the `topKWithFractionalIndex` operator to order all the elements.
182 *
183 * @param valueExtractor - A function that extracts the value to order by from the element
184 * @param options - An optional object containing comparator, limit and offset properties
185 * @returns A piped operator that orders the elements and limits the number of results
186 */
187export function orderByWithFractionalIndex<
188 T extends KeyValue<unknown, unknown>,
189 Ve = unknown,
190>(
191 valueExtractor: (
192 value: T extends KeyValue<unknown, infer V> ? V : never
193 ) => Ve,
194 options?: OrderByWithFractionalIndexOptions<Ve>
195) {
196 return orderByWithFractionalIndexBase(
197 topKWithFractionalIndex,
198 valueExtractor,
199 options
200 )