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