gristlabs_grist-core/app/common/ColumnFilterFunc.ts
Alex Hall 251d79704b (core) Migrate Attachments columns from marshalled blobs to JSON
Summary: Adds a migration in preparation for future work on tracking and deleting attachments. This includes a `_grist_Attachments.timeDeleted` column which isn't used yet, and changing the storage format of user columns of type `Attachments`. DocStorage now treats Attachments like RefList in general (since they use JSON), which also prompted a tiny bit of refactoring.

Test Plan: Added a migration test case showing the change in format.

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D3352
2022-04-06 13:28:47 +02:00

30 lines
1.4 KiB
TypeScript

import {CellValue} from "app/common/DocActions";
import {FilterState, makeFilterState} from "app/common/FilterState";
import {decodeObject} from "app/plugin/objtypes";
import {isList, isListType} from "./gristTypes";
export type ColumnFilterFunc = (value: CellValue) => boolean;
// Returns a filter function for a particular column: the function takes a cell value and returns
// whether it's accepted according to the given FilterState.
export function makeFilterFunc({ include, values }: FilterState,
columnType?: string): ColumnFilterFunc {
// NOTE: This logic results in complex values and their stringified JSON representations as equivalent.
// For example, a TypeError in the formula column and the string '["E","TypeError"]' would be seen as the same.
// TODO: This narrow corner case seems acceptable for now, but may be worth revisiting.
return (val: CellValue) => {
if (isList(val) && columnType && isListType(columnType)) {
const list = decodeObject(val) as unknown[];
return list.some(item => values.has(item as any) === include);
}
return (values.has(Array.isArray(val) ? JSON.stringify(val) : val) === include);
};
}
// Given a JSON string, returns a ColumnFilterFunc
export function buildColFilter(filterJson: string | undefined,
columnType?: string): ColumnFilterFunc | null {
return filterJson ? makeFilterFunc(makeFilterState(filterJson), columnType) : null;
}