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