@techmely/utils
Version:
Collection of helpful JavaScript / TypeScript utils
39 lines (33 loc) • 850 B
JavaScript
/*!
* @techmely/utils
* Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com>
* MIT Licensed
*/
// src/isNumber.ts
function isNumber(val) {
return toString.call(val) === "[object Number]";
}
// src/sortData.ts
function sortData(a, b, direction = "ASC" /* ASC */, options) {
let result = 0;
const { locale = "vi", shouldIgnoreZero = false } = options || {};
if (shouldIgnoreZero) {
if (a === b) {
return 0;
}
if (a === 0) {
return 1;
}
if (b === 0) {
return -1;
}
}
if (isNumber(a) && isNumber(b)) {
const aParsed = a?.toString() ?? "";
const bParsed = b?.toString() ?? "";
result = isNumber(a) && isNumber(b) ? a - b : aParsed.localeCompare(bParsed, locale);
}
return direction === "ASC" /* ASC */ ? result : -result;
}
exports.sortData = sortData;
;