handsontable
Version:
Handsontable is a JavaScript Data Grid available for React, Angular and Vue.
61 lines (60 loc) • 2.28 kB
JavaScript
import { isEmpty } from "../../../helpers/mixed.mjs";
import { DO_NOT_SWAP, FIRST_BEFORE_SECOND, FIRST_AFTER_SECOND } from "../sortService/index.mjs";
/**
* Default sorting compare function factory. Method get as parameters `sortOrder` and `columnMeta` and return compare function.
*
* @param {string} sortOrder Sort order (`asc` for ascending, `desc` for descending).
* @param {object} columnMeta Column meta object.
* @param {object} columnPluginSettings Plugin settings for the column.
* @returns {Function} The compare function.
*/
export function compareFunctionFactory(sortOrder, columnMeta, columnPluginSettings) {
const locale = columnMeta.locale;
return function (value, nextValue) {
const {
sortEmptyCells
} = columnPluginSettings;
if (typeof value === 'string') {
value = value.toLocaleLowerCase(locale);
}
if (typeof nextValue === 'string') {
nextValue = nextValue.toLocaleLowerCase(locale);
}
if (value === nextValue) {
return DO_NOT_SWAP;
}
if (isEmpty(value)) {
if (isEmpty(nextValue)) {
return DO_NOT_SWAP;
}
// Just fist value is empty and `sortEmptyCells` option was set
if (sortEmptyCells) {
return sortOrder === 'asc' ? FIRST_BEFORE_SECOND : FIRST_AFTER_SECOND;
}
return FIRST_AFTER_SECOND;
}
if (isEmpty(nextValue)) {
// Just second value is empty and `sortEmptyCells` option was set
if (sortEmptyCells) {
return sortOrder === 'asc' ? FIRST_AFTER_SECOND : FIRST_BEFORE_SECOND;
}
return FIRST_BEFORE_SECOND;
}
if (isNaN(value) && !isNaN(nextValue)) {
return sortOrder === 'asc' ? FIRST_AFTER_SECOND : FIRST_BEFORE_SECOND;
} else if (!isNaN(value) && isNaN(nextValue)) {
return sortOrder === 'asc' ? FIRST_BEFORE_SECOND : FIRST_AFTER_SECOND;
} else if (!(isNaN(value) || isNaN(nextValue))) {
value = parseFloat(value);
nextValue = parseFloat(nextValue);
}
if (value < nextValue) {
return sortOrder === 'asc' ? FIRST_BEFORE_SECOND : FIRST_AFTER_SECOND;
}
if (value > nextValue) {
return sortOrder === 'asc' ? FIRST_AFTER_SECOND : FIRST_BEFORE_SECOND;
}
return DO_NOT_SWAP;
};
}
export const COLUMN_DATA_TYPE = 'default';