(
topKFunction: typeof topKWithFractionalIndex,
valueExtractor: (
value: T extends KeyValue<unknown, infer V> ? V : never,
) => Ve,
options?: OrderByWithFractionalIndexOptions<Ve>,
)
| 135 | } |
| 136 | |
| 137 | export 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 |
no test coverage detected