@veracity/vui
Version:
Veracity UI is a React component library crafted for use within Veracity applications and pages. Based on Styled Components and @xstyled.
22 lines (21 loc) • 842 B
JavaScript
//#region src/utils/array.ts
/** Comparator for Array.sort when sorting arrays of objects by given key with optional order. */
function compareBy(key, order = "asc") {
const ord = order === "asc" ? 1 : -1;
return (item1, item2) => {
if (order === null) return 0;
const a = item1[key];
const b = item2[key];
if (typeof a === "string" && typeof b === "string") return a.localeCompare(b) * ord;
else if (typeof a === "number" && typeof b === "number") return a > b ? ord : a < b ? -ord : 0;
return 0;
};
}
/** Adds given item to the array if it wasn't there yet, or removes it otherwise. */
function toggleItem(array, item) {
return array.includes(item) ? array.filter((it) => it !== item) : [...array, item];
}
//#endregion
export { compareBy, toggleItem };
globalThis.__vuiVersion__ = "5.3.1"
//# sourceMappingURL=array.js.map