UNPKG

@electric-sql/d2mini

Version:

D2Mini is a minimal implementation of Differential Dataflow for performing in-memory incremental view maintenance.

107 lines 4.24 kB
import { topK, topKWithIndex } from './topK.js'; import { topKWithFractionalIndex } from './topKWithFractionalIndex.js'; import { map } from './map.js'; import { innerJoin } from './join.js'; import { consolidate } from './consolidate.js'; /** * Orders the elements and limits the number of results, with optional offset * This requires a keyed stream, and uses the `topK` operator to order all the elements. * * @param valueExtractor - A function that extracts the value to order by from the element * @param options - An optional object containing comparator, limit and offset properties * @returns A piped operator that orders the elements and limits the number of results */ export function orderBy(valueExtractor, options) { const limit = options?.limit ?? Infinity; const offset = options?.offset ?? 0; const comparator = options?.comparator ?? ((a, b) => { // Default to JS like ordering if (a === b) return 0; if (a < b) return -1; return 1; }); return (stream) => { return stream.pipe(map(([key, value]) => [ null, [ key, valueExtractor(value), ], ]), topK((a, b) => comparator(a[1], b[1]), { limit, offset }), map(([_, [key]]) => [key, null]), innerJoin(stream), map(([key, value]) => { return [key, value[1]]; }), consolidate()); }; } /** * Orders the elements and limits the number of results, with optional offset and * annotates the value with the index. * This requires a keyed stream, and uses the `topKWithIndex` operator to order all the elements. * * @param valueExtractor - A function that extracts the value to order by from the element * @param options - An optional object containing comparator, limit and offset properties * @returns A piped operator that orders the elements and limits the number of results */ export function orderByWithIndex(valueExtractor, options) { const limit = options?.limit ?? Infinity; const offset = options?.offset ?? 0; const comparator = options?.comparator ?? ((a, b) => { // Default to JS like ordering if (a === b) return 0; if (a < b) return -1; return 1; }); return (stream) => { return stream.pipe(map(([key, value]) => [ null, [ key, valueExtractor(value), ], ]), topKWithIndex((a, b) => comparator(a[1], b[1]), { limit, offset }), map(([_, [[key], index]]) => [key, index]), innerJoin(stream), map(([key, [index, value]]) => { return [key, [value, index]]; }), consolidate()); }; } /** * Orders the elements and limits the number of results, with optional offset and * annotates the value with a fractional index. * This requires a keyed stream, and uses the `topKWithFractionalIndex` operator to order all the elements. * * @param valueExtractor - A function that extracts the value to order by from the element * @param options - An optional object containing comparator, limit and offset properties * @returns A piped operator that orders the elements and limits the number of results */ export function orderByWithFractionalIndex(valueExtractor, options) { const limit = options?.limit ?? Infinity; const offset = options?.offset ?? 0; const comparator = options?.comparator ?? ((a, b) => { // Default to JS like ordering if (a === b) return 0; if (a < b) return -1; return 1; }); return (stream) => { return stream.pipe(map(([key, value]) => [ null, [ key, valueExtractor(value), ], ]), topKWithFractionalIndex((a, b) => comparator(a[1], b[1]), { limit, offset, }), map(([_, [[key], index]]) => [key, index]), innerJoin(stream), map(([key, [index, value]]) => { return [key, [value, index]]; }), consolidate()); }; } //# sourceMappingURL=orderBy.js.map