UNPKG

dtable-utils

Version:

dtable common utils

97 lines (93 loc) 3.45 kB
import { getFormulaDisplayString } from '../../cell-value-get/cell-value.js'; import { isNumericColumn } from '../../column/number.js'; import { checkboxFilter } from './checkbox.js'; import { collaboratorFilter } from './collaborator.js'; import { dateFilter } from './date.js'; import { textFilter } from './text.js'; import { multipleSelectFilter } from './multiple-select.js'; import { departmentMultipleSelectFilter } from './department-multiple-select.js'; import { numberFilter } from './number.js'; import { CellType } from '../../constants/cell-type.js'; import { DATE_COLUMN_OPTIONS, COLLABORATOR_COLUMN_TYPES } from '../../constants/column.js'; import { FILTER_PREDICATE_TYPE } from '../../constants/filter/filter-predicate.js'; var TRUE_VALUE = [true, 'true']; /** * Filter by array type * @param {array} array e.g. [ cellValue, ... ] * @param {object} filter e.g. { filter_predicate, column, ... } * @param {string} username * @param {string} userId * @returns bool */ var filterByArrayType = function filterByArrayType(array, filter, _ref) { var username = _ref.username, userId = _ref.userId, userDepartmentIdsMap = _ref.userDepartmentIdsMap; var column = filter.column, linked_column = filter.linked_column; var filter_predicate = filter.filter_predicate; if (Array.isArray(array)) { if (filter_predicate === FILTER_PREDICATE_TYPE.EMPTY) { return array.length === 0; } if (filter_predicate === FILTER_PREDICATE_TYPE.NOT_EMPTY) { return array.length > 0; } } if (!linked_column) { return textFilter('', filter, userId); } var linkedColumnType = linked_column.type; if (linkedColumnType === CellType.BOOL || linkedColumnType === CellType.CHECKBOX) { var normalizedArray = array; if (Array.isArray(array)) { normalizedArray = array[0]; } normalizedArray = TRUE_VALUE.includes(normalizedArray); return checkboxFilter(normalizedArray, filter); } if (linkedColumnType === CellType.SINGLE_SELECT) { var _normalizedArray = array; if (array && !Array.isArray(array)) { _normalizedArray = [array]; } return multipleSelectFilter(_normalizedArray, filter); } if (linkedColumnType === CellType.MULTIPLE_SELECT) { return multipleSelectFilter(array, filter); } if (linkedColumnType === CellType.DEPARTMENT_SINGLE_SELECT) { var _normalizedArray2 = array; if (array && !Array.isArray(array)) { _normalizedArray2 = [array]; } return departmentMultipleSelectFilter(_normalizedArray2, filter, userDepartmentIdsMap); } if (isNumericColumn({ type: linkedColumnType })) { var _normalizedArray3 = array; if (Array.isArray(array)) { var cellValueLength = array.length; if (cellValueLength === 0) { _normalizedArray3 = ''; } else if (cellValueLength === 1) { _normalizedArray3 = array[0]; } } return numberFilter(_normalizedArray3, filter); } if (DATE_COLUMN_OPTIONS.includes(linkedColumnType)) { var newCellValue = array; if (Array.isArray(array)) { newCellValue = String(array); } return dateFilter(newCellValue, filter); } if (COLLABORATOR_COLUMN_TYPES.includes(linkedColumnType)) { return collaboratorFilter(array, filter, username); } var displayString = getFormulaDisplayString(array, column.data) || ''; return textFilter(displayString, filter, userId); }; export { filterByArrayType };