UNPKG

@techmely/utils

Version:

Collection of helpful JavaScript / TypeScript utils

73 lines (68 loc) 1.92 kB
/*! * @techmely/utils * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com> * MIT Licensed */ import { isNumber } from "./chunk-BD32CMSL.mjs"; // src/date.ts var isSameDay = (date1, date2) => { if (!(date1 && date2)) { return false; } return date1.getDate() === date2.getDate() && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear(); }; var isSameMonth = (date1, date2) => { if (!(date1 && date2)) { return false; } return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth(); }; var isToday = (date) => isSameDay(date, /* @__PURE__ */ new Date()); var SortDirection = /* @__PURE__ */ ((SortDirection2) => { SortDirection2["ASC"] = "ASC"; SortDirection2["DESC"] = "DESC"; return SortDirection2; })(SortDirection || {}); function sortByDate(a, b, key, direction = "ASC" /* ASC */) { if (a[key] < b[key]) { return direction === "ASC" /* ASC */ ? 1 : -1; } if (a[key] > b[key]) { return direction === "ASC" /* ASC */ ? -1 : 1; } return 0; } 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; } var suffixAmPm = (h) => `${h % 12 === 0 ? 12 : h % 12}${h < 12 ? "am" : "pm"}`; var getQuarter = (d = /* @__PURE__ */ new Date()) => Math.ceil((d.getMonth() + 1) / 3); export { isSameMonth, isToday, SortDirection, sortByDate, sortData, suffixAmPm, getQuarter };