2021-05-27 11:06:26 +00:00
|
|
|
import { CellValue } from "app/common/DocActions";
|
2022-09-14 09:04:20 +00:00
|
|
|
import { IRelativeDateSpec, isEquivalentRelativeDate, isRelativeBound } from "app/common/RelativeDates";
|
|
|
|
|
|
|
|
export type { IRelativeDateSpec } from "app/common/RelativeDates";
|
|
|
|
export { isRelativeBound } from "app/common/RelativeDates";
|
2021-05-27 11:06:26 +00:00
|
|
|
|
|
|
|
// Filter object as stored in the db
|
|
|
|
export interface FilterSpec {
|
|
|
|
included?: CellValue[];
|
|
|
|
excluded?: CellValue[];
|
2022-09-14 09:04:20 +00:00
|
|
|
min?: number|IRelativeDateSpec;
|
|
|
|
max?: number|IRelativeDateSpec;
|
2021-05-27 11:06:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 09:04:20 +00:00
|
|
|
export type IRangeBoundType = undefined|number|IRelativeDateSpec;
|
2022-05-24 14:59:12 +00:00
|
|
|
|
|
|
|
export type FilterState = ByValueFilterState | RangeFilterState
|
|
|
|
|
2021-05-27 11:06:26 +00:00
|
|
|
// A more efficient representation of filter state for a column than FilterSpec.
|
2022-05-24 14:59:12 +00:00
|
|
|
interface ByValueFilterState {
|
2021-05-27 11:06:26 +00:00
|
|
|
include: boolean;
|
|
|
|
values: Set<CellValue>;
|
|
|
|
}
|
|
|
|
|
2022-05-24 14:59:12 +00:00
|
|
|
interface RangeFilterState {
|
2022-09-14 09:04:20 +00:00
|
|
|
min?: number|IRelativeDateSpec;
|
|
|
|
max?: number|IRelativeDateSpec;
|
2022-05-24 14:59:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 11:06:26 +00:00
|
|
|
// Creates a FilterState. Accepts spec as a json string or a FilterSpec.
|
|
|
|
export function makeFilterState(spec: string | FilterSpec): FilterState {
|
|
|
|
if (typeof (spec) === 'string') {
|
|
|
|
return makeFilterState((spec && JSON.parse(spec)) || {});
|
|
|
|
}
|
2022-05-24 14:59:12 +00:00
|
|
|
if (spec.min !== undefined || spec.max !== undefined) {
|
|
|
|
return {min: spec.min, max: spec.max};
|
|
|
|
}
|
2021-05-27 11:06:26 +00:00
|
|
|
return {
|
|
|
|
include: Boolean(spec.included),
|
|
|
|
values: new Set(spec.included || spec.excluded || []),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if state and spec are equivalent, false otherwise.
|
|
|
|
export function isEquivalentFilter(state: FilterState, spec: FilterSpec): boolean {
|
|
|
|
const other = makeFilterState(spec);
|
2022-05-24 14:59:12 +00:00
|
|
|
if (!isRangeFilter(state) && !isRangeFilter(other)) {
|
|
|
|
if (state.include !== other.include) { return false; }
|
|
|
|
if (state.values.size !== other.values.size) { return false; }
|
|
|
|
if (other.values) {
|
|
|
|
for (const val of other.values) { if (!state.values.has(val)) { return false; } }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isRangeFilter(state) && isRangeFilter(other)) {
|
|
|
|
if (state.min !== other.min || state.max !== other.max) { return false; }
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 11:06:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-05-24 14:59:12 +00:00
|
|
|
|
|
|
|
export function isRangeFilter(state: FilterState): state is RangeFilterState {
|
|
|
|
const {min, max} = state as any;
|
|
|
|
return min !== undefined || max !== undefined;
|
|
|
|
}
|
2022-09-14 09:04:20 +00:00
|
|
|
|
|
|
|
export function isEquivalentBound(a: IRangeBoundType, b: IRangeBoundType) {
|
|
|
|
if (isRelativeBound(a) && isRelativeBound(b)) {
|
|
|
|
return isEquivalentRelativeDate(a, b);
|
|
|
|
}
|
|
|
|
if (isRelativeBound(a) || isRelativeBound(b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return a === b;
|
|
|
|
}
|