UNPKG

dtable-utils

Version:

dtable common utils

77 lines (72 loc) 3.11 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var collaborator = require('../../cell-value-get/collaborator.js'); var cellValue = require('../../cell-value-get/cell-value.js'); var checkbox = require('./checkbox.js'); var collaborator$1 = require('./collaborator.js'); var date = require('./date.js'); var number = require('./number.js'); var text = require('./text.js'); var cellType = require('../../constants/cell-type.js'); var column = require('../../constants/column.js'); var formula = require('../../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 (column.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 number.sortNumber(leftNumber, rightNumber, sortType); } if (column.DATE_COLUMN_OPTIONS.includes(array_type)) { var leftDate = Array.isArray(leftArray) ? leftArray[0] : leftArray; var rightDate = Array.isArray(rightArray) ? rightArray[0] : rightArray; return date.sortDate(leftDate, rightDate, sortType); } if (array_type === cellType.CellType.CHECKBOX || array_type === formula.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 checkbox.sortCheckbox(leftBool, rightBool, sortType); } if (array_type === cellType.CellType.COLLABORATOR) { var collaborators = value.collaborators; var leftCollaborators = Array.isArray(leftArray) ? leftArray : [leftArray]; var rightCollaborators = Array.isArray(rightArray) ? rightArray : [rightArray]; if (collaborators) { leftCollaborators = collaborator.getCollaboratorsNames(leftCollaborators, collaborators); rightCollaborators = collaborator.getCollaboratorsNames(rightCollaborators, collaborators); } return collaborator$1.sortCollaborator(leftCollaborators, rightCollaborators, sortType); } var leftText = cellValue.getFormulaDisplayString(leftArray, columnData); var rightText = cellValue.getFormulaDisplayString(rightArray, columnData); return text.sortText(leftText, rightText, sortType); }; exports.sortByArrayType = sortByArrayType;