@tanstack/db
Version:
A reactive client store for building super fast apps on sync
53 lines (52 loc) • 2.04 kB
JavaScript
import { and, lt, gte, eq, or, isUndefined, isNull, not, gt } from "../query/builder/functions.js";
import { Value } from "../query/ir.js";
function isNullish(expression) {
return or(isNull(expression), isUndefined(expression));
}
function followsBoundary(clause, value) {
const nullish = isNullish(clause.expression);
if (value == null) {
return clause.compareOptions.nulls === `first` ? not(nullish) : new Value(false);
}
const operator = clause.compareOptions.direction === `asc` ? gt : lt;
const comparison = operator(clause.expression, new Value(value));
return clause.compareOptions.nulls === `last` ? or(comparison, nullish) : comparison;
}
function buildCursor(orderBy, values) {
if (values.length === 0) return void 0;
if (orderBy.length !== 1 || values.length !== 1) {
throw new Error(`Only single-column cursors are supported`);
}
return followsBoundary(orderBy[0], values[0]);
}
function buildCursorCurrent(orderBy, values) {
const { expression } = orderBy[0] ?? {};
if (!expression || values.length === 0) return void 0;
const value = values[0];
if (value == null) return isNullish(expression);
if (value instanceof Date) {
if (!Number.isFinite(value.getTime())) return void 0;
return and(
gte(expression, new Value(value)),
lt(expression, new Value(new Date(value.getTime() + 1)))
);
}
if (typeof value === `object`) return void 0;
return eq(expression, new Value(value));
}
function canExpressCursorOrder(orderBy, values) {
if (orderBy.length !== 1 || values.length !== 1) return false;
const value = values[0];
if (value == null) return false;
if (value instanceof Date) return Number.isFinite(value.getTime());
if (typeof value === `string`) {
return orderBy[0].compareOptions.stringSort === `lexical`;
}
return typeof value === `number` && Number.isFinite(value) || typeof value === `bigint` || typeof value === `boolean`;
}
export {
buildCursor,
buildCursorCurrent,
canExpressCursorOrder
};
//# sourceMappingURL=cursor.js.map