compote-ui
Version:
An opinionated UI component library for Svelte, built on top of [Ark UI](https://ark-ui.com) with additional components and features not available in the core Ark UI library.
24 lines (23 loc) • 1.15 kB
JavaScript
/**
* Value handling for numeric range filters (`inNumberRange`): the filter value is a
* `[min, max]` tuple where either bound may be `undefined`.
*/
/** The `[min, max]` bounds of a range filter value, or both undefined when there is none. */
export function rangeBounds(filterValue) {
if (!Array.isArray(filterValue))
return [undefined, undefined];
const [min, max] = filterValue;
return [typeof min === 'number' ? min : undefined, typeof max === 'number' ? max : undefined];
}
/**
* The filter value after committing `pending` edits onto the current filter value. A bound
* that wasn't edited keeps its current (live) value, so a stale bound can't come back after
* the filter was changed elsewhere. Returns `undefined` when both bounds are empty, which
* removes the filter.
*/
export function mergeRangeFilter(current, pending) {
const [currentMin, currentMax] = rangeBounds(current);
const min = 'min' in pending ? (pending.min ?? undefined) : currentMin;
const max = 'max' in pending ? (pending.max ?? undefined) : currentMax;
return min === undefined && max === undefined ? undefined : [min, max];
}