Open History to see the last commit
1import { groupedTopKWithFractionalIndex } from './groupedTopKWithFractionalIndex.js'
2import { consolidate } from './consolidate.js'
3import type { IStreamBuilder, KeyValue } from '../types.js'
4
5export interface GroupedOrderByOptions<Ve> {
6 comparator?: (a: Ve, b: Ve) => number
7 limit?: number
8 offset?: number
9}
10
11export interface GroupedOrderByWithFractionalIndexOptions<
12 Ve,
13 KeyType = unknown,
14 ValueType = unknown,
15> extends GroupedOrderByOptions<Ve> {
16 setSizeCallback?: (getSize: () => number) => void
17 setWindowFn?: (
18 windowFn: (options: { offset?: number; limit?: number }) => void,
19 ) => void
20 /**
21 * Function to extract a group key from the element's key and value.
22 * Elements with the same group key will be sorted and limited together.
23 */
24 groupKeyFn: (key: KeyType, value: ValueType) => unknown
25}
26
27/**
28 * Orders the elements per group and limits the number of results per group, with optional offset and
29 * annotates the value with a fractional index.
30 * This requires a keyed stream, and uses the `groupedTopKWithFractionalIndex` operator to order elements within each group.
31 *
32 * Elements are grouped by the provided groupKeyFn, and each group maintains its own sorted collection
33 * with independent limit/offset.
34 *
35 * @param valueExtractor - A function that extracts the value to order by from the element
36 * @param options - Configuration including groupKeyFn, comparator, limit, and offset
37 * @returns A piped operator that orders the elements per group and limits the number of results per group
38 */
39export function groupedOrderByWithFractionalIndex<
40 T extends KeyValue<unknown, unknown>,
41 Ve = unknown,
42>(
43 valueExtractor: (
44 value: T extends KeyValue<unknown, infer V> ? V : never,
45 ) => Ve,
46 options: GroupedOrderByWithFractionalIndexOptions<
47 Ve,
48 T extends KeyValue<infer K, unknown> ? K : never,
49 T extends KeyValue<unknown, infer V> ? V : never
50 >,
51) {
52 type KeyType = T extends KeyValue<infer K, unknown> ? K : never
53 type ValueType = T extends KeyValue<unknown, infer V> ? V : never
54
55 const limit = options.limit ?? Infinity
56 const offset = options.offset ?? 0
57 const setSizeCallback = options.setSizeCallback
58 const setWindowFn = options.setWindowFn
59 const groupKeyFn = options.groupKeyFn
60 const comparator =
61 options.comparator ??
62 ((a, b) => {
63 // Default to JS like ordering
64 if (a === b) return 0
65 if (a < b) return -1
66 return 1
67 })
68
69 return (
70 stream: IStreamBuilder<T>,
71 ): IStreamBuilder<[KeyType, [ValueType, string]]> => {
72 // Cast to the expected key type for groupedTopKWithFractionalIndex
73 type StreamKey = KeyType extends string | number ? KeyType : string | number
74
75 return stream.pipe(
76 groupedTopKWithFractionalIndex<StreamKey, ValueType>(
77 (a: ValueType, b: ValueType) =>
78 comparator(valueExtractor(a), valueExtractor(b)),
79 {
80 limit,
81 offset,
82 setSizeCallback,
83 setWindowFn,
84 groupKeyFn: groupKeyFn as (
85 key: StreamKey,
86 value: ValueType,
87 ) => unknown,
88 },
89 ) as (
90 stream: IStreamBuilder<T>,
91 ) => IStreamBuilder<[KeyType, [ValueType, string]]>,
92 consolidate(),
93 )
94 }
95}