import { AbsentCellValue, CellValue, ColumnType, MaybeAbsentCellValue } from '../types'; import isEqual from 'lodash/isEqual'; export const isAbsentCellValue = ( cellValue: MaybeAbsentCellValue ): cellValue is AbsentCellValue => { return cellValue === null || cellValue === undefined; }; /** * Check if a cell value can be treated as if it was absent. This includes * _actual_ absence (null, undefined) as well as the empty string, the empty * array, and NaN. Note that this isn't type guarded because NaN has type * `number` which means that we can't correctly "split" the cell value type. */ export const isEmptyCellValue = (cellValue: MaybeAbsentCellValue) => { return ( isAbsentCellValue(cellValue) || cellValue === '' || // Custom case. Array types, we will auto convert them to empty arrays when they are null. So basically null and [] are the same. Please help write this better. (Array.isArray(cellValue) && cellValue.length === 0) || Number.isNaN(cellValue as any) ); }; // Similar to isEmptyCellValue but both are used everywhere // @TODO combine with isEmptyCellValue? export const getIsEmpty = (value: any): boolean => { if (Array.isArray(value)) { if (isEqual(value, [])) return true; return value.some((v: any) => { if (!v && v !== 0) return true; if (typeof v !== 'object') return false; return getIsEmpty(v.fileId) && getIsEmpty(v.visibleName); }); } return value === null || typeof value === 'undefined' || value === ''; }; export const convertMaybeCollaboratorCellValueToGroupIdArray = ( value: MaybeAbsentCellValue ): CellValue => { if (isAbsentCellValue(value)) return []; if (Array.isArray(value)) return value; return [value]; };