dtable-utils
Version:
dtable common utils
73 lines (70 loc) • 2.99 kB
JavaScript
import { getCollaboratorsNames } from '../../cell-value-get/collaborator.js';
import { getFormulaDisplayString } from '../../cell-value-get/cell-value.js';
import { sortCheckbox } from './checkbox.js';
import { sortCollaborator } from './collaborator.js';
import { sortDate } from './date.js';
import { sortNumber } from './number.js';
import { sortText } from './text.js';
import { CellType } from '../../constants/cell-type.js';
import { NUMERIC_COLUMNS_TYPES, DATE_COLUMN_OPTIONS } from '../../constants/column.js';
import { FORMULA_RESULT_TYPE } from '../../constants/formula.js';
/**
* Sort by array type
* @param {array} leftArray
* @param {array} rightArray
* @param {string} sortType e.g. 'up' | 'down
* @param {object} columnData e.g. { result_type, array_type, array_data, ... }
* @param {object} value e.g. { collaborators, ... }
* @returns number
*/
var sortByArrayType = function sortByArrayType(leftArray, rightArray, sortType, _ref) {
var columnData = _ref.columnData,
value = _ref.value;
var _ref2 = columnData || {},
array_type = _ref2.array_type;
if (NUMERIC_COLUMNS_TYPES.includes(array_type)) {
var leftNumber = leftArray;
var rightNumber = rightArray;
if (Array.isArray(leftArray)) {
leftNumber = leftArray[0];
}
if (Array.isArray(rightArray)) {
rightNumber = rightArray[0];
}
leftNumber = leftNumber || leftNumber === 0 ? leftNumber : null;
rightNumber = rightNumber || rightNumber === 0 ? rightNumber : null;
return sortNumber(leftNumber, rightNumber, sortType);
}
if (DATE_COLUMN_OPTIONS.includes(array_type)) {
var leftDate = Array.isArray(leftArray) ? leftArray[0] : leftArray;
var rightDate = Array.isArray(rightArray) ? rightArray[0] : rightArray;
return sortDate(leftDate, rightDate, sortType);
}
if (array_type === CellType.CHECKBOX || array_type === FORMULA_RESULT_TYPE.BOOL) {
var leftBool = leftArray;
var rightBool = rightArray;
if (Array.isArray(leftArray)) {
leftBool = leftArray[0];
}
if (Array.isArray(rightArray)) {
rightBool = rightArray[0];
}
leftBool = leftBool || false;
rightBool = rightBool || false;
return sortCheckbox(leftBool, rightBool, sortType);
}
if (array_type === CellType.COLLABORATOR) {
var collaborators = value.collaborators;
var leftCollaborators = Array.isArray(leftArray) ? leftArray : [leftArray];
var rightCollaborators = Array.isArray(rightArray) ? rightArray : [rightArray];
if (collaborators) {
leftCollaborators = getCollaboratorsNames(leftCollaborators, collaborators);
rightCollaborators = getCollaboratorsNames(rightCollaborators, collaborators);
}
return sortCollaborator(leftCollaborators, rightCollaborators, sortType);
}
var leftText = getFormulaDisplayString(leftArray, columnData);
var rightText = getFormulaDisplayString(rightArray, columnData);
return sortText(leftText, rightText, sortType);
};
export { sortByArrayType };