@pillar-ui/utils
Version:
A collection of utility functions and tools to assist with common tasks and improve the efficiency of web development. These utilities are carefully crafted to enhance productivity and streamline the development process.
1 lines • 8.21 kB
Source Map (JSON)
{"version":3,"sources":["../src/clamp/index.ts","../src/composeEventHandlers/index.ts","../src/formatPrice/index.ts","../src/getURLParameters/index.ts","../src/isInRange/index.ts","../src/isLeapYear/index.ts","../src/isValidDate/index.ts","../src/isValidLocale/index.ts","../src/randomNumber/index.ts","../src/toCapitalize/index.ts","../src/toKebabCase/index.ts","../src/toSlug/index.ts"],"names":["clamp","value","min","max","composeEventHandlers","handlers","event","handler","formatPrice","price","locale","currency","getURLParameters","url","params","obj","key","isInRange","isLeapYear","y","isValidDate","dateString","date","isValidLocale","canonicalLocales","randomNumber","rounded","toCapitalize","str","toKebabCase","toSlug"],"mappings":"AAMO,SAASA,CAAMC,CAAAA,CAAAA,CAAe,CAACC,CAAAA,CAAKC,CAAG,CAAA,CAA6B,CACzE,OAAO,IAAK,CAAA,GAAA,CAAIA,CAAK,CAAA,IAAA,CAAK,IAAID,CAAKD,CAAAA,CAAK,CAAC,CAC3C,CCAO,IAAMG,CACX,CAAA,CAAA,GAAIC,CACHC,GAAAA,CAAAA,EACCD,CAAS,CAAA,OAAA,CAASE,CAAY,EAAA,OAAOA,CAAY,EAAA,UAAA,EAAcA,EAAQD,CAAK,CAAC,ECH1E,IAAME,CAAc,CAAA,CAACC,CAAeC,CAAAA,CAAAA,CAAS,OAASC,CAAAA,CAAAA,CAAW,KACtE,GAAA,IAAI,IAAK,CAAA,YAAA,CAAaD,CAAQ,CAAA,CAAE,MAAO,UAAY,CAAA,QAAA,CAAAC,CAAS,CAAC,CAAE,CAAA,MAAA,CAAOF,CAAK,ECTtE,SAASG,CAAAA,CAAiBC,CAAmD,CAAA,CAClF,IAAMC,CAAAA,CAAS,IAAI,eAAA,CAAgB,IAAI,GAAID,CAAAA,CAAG,CAAE,CAAA,MAAM,CAChDE,CAAAA,CAAAA,CAA4C,EAAC,CACnD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AA,MAAA,IAAYtB,CAAMD,CAAAA,CAAAA,CAAAA,CAAOA,CACnD,CAAA,OAAOwB,CAAU,CAAA,IAAA,CAAK,KAAMD,CAAAA,CAAY,EAAIA,CAC9C,ECjBO,SAASE,CAAAA,CAAaC,CAAqB,CAAA,CAChD,OAAAA,CAAAA,CAAMA,CAAI,CAAA,IAAA,EACHA,CAAAA,CAAAA,CAAI,CAAC,CAAA,CAAE,WAAY,EAAA,CAAIA,EAAI,KAAM,CAAA,CAAC,CAC3C,CCHaC,IAAAA,CAAAA,CAAeD,CAC1BA,EAAAA,CAAAA,CACG,IAAK,EAAA,CACL,OAAQ,CAAA,iBAAA,CAAmB,OAAO,CAAA,CAClC,OAAQ,CAAA,MAAA,CAAQ,GAAG,CACnB,CAAA,WAAA,GCCE,IAAME,CAAUF,CAAAA,CAAAA,EACdA,CAAI,CAAA,IAAA,EAAO,CAAA,MAAA,GAAW,CACzB,CAAA,EAAA,CACAA,CACG,CAAA,WAAA,EACA,CAAA,IAAA,GACA,OAAQ,CAAA,MAAA,CAAQ,OAAO,CAAA,CACvB,OAAQ,CAAA,OAAA,CAAS,GAAG,CAAA,CACpB,QAAQ,OAAS,CAAA,GAAG,CACpB,CAAA,OAAA,CAAQ,cAAgB,CAAA,EAAE,CAC1B,CAAA,OAAA,CAAQ,SAAU,EAAE","file":"index.mjs","sourcesContent":["/**\n * Clamps a value between a minimum and maximum range.\n * @param {number} value - The value to clamp.\n * @param {[number, number]} range - The minimum and maximum values defining the range.\n * @returns {number} - The clamped value.\n */\nexport function clamp(value: number, [min, max]: [number, number]): number {\n return Math.min(max, Math.max(min, value))\n}\n","/**\n * Composes multiple event handlers into a single handler.\n *\n * @template T - The type of the event object.\n * @param {...((event: T) => void)} handlers - The event handlers to compose.\n * @returns {(event: T) => void} - The composed event handler.\n */\n\nexport const composeEventHandlers =\n (...handlers: ((event: Event) => void)[]) =>\n (event: Event) =>\n handlers.forEach((handler) => typeof handler === 'function' && handler(event))\n","/**\n * Formats a price value as a currency string using the specified locale and currency.\n * @param {number} price - The price value to format.\n * @param {string} [locale='en-US'] - The locale to use for formatting the currency.\n * @param {string} [currency='USD'] - The currency code to use for formatting the price.\n * @returns {string} - The formatted price as a currency string.\n */\n\nexport const formatPrice = (price: number, locale = 'en-US', currency = 'USD') =>\n new Intl.NumberFormat(locale, { style: 'currency', currency }).format(price)\n","export function getURLParameters(url: string): { [key: string]: string | string[] } {\n const params = new URLSearchParams(new URL(url).search)\n const obj: { [key: string]: string | string[] } = {}\n for (const [key, value] of params.entries()) {\n obj[key] = value\n }\n return obj\n}\n","/**\n * Checks if a number is within a specified range (inclusive).\n * @param {object} params - The parameters for the range check.\n * @param {number} params.value - The number to check.\n * @param {number} params.min - The minimum value of the range.\n * @param {number} params.max - The maximum value of the range.\n * @returns {boolean} Returns true if the number is within the range, false otherwise.\n */\n\ninterface Range {\n value: number\n min: number\n max: number\n}\n\nexport const isInRange = ({ value, min, max }: Range) => value >= min && value <= max\n","// Years Is a short for Year\nexport const isLeapYear = (y: number) =>\n Number.isNaN(y) || y <= 0 ? false : y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0)\n","/**\n * Checks if a string is a valid date.\n * @param {string} dateString - The string to check.\n * @returns {boolean} - Returns true if the string is a valid date, false otherwise.\n */\nexport function isValidDate(dateString: Date | string): boolean {\n const date = new Date(dateString)\n if (date.toString() === 'Invalid Date' || isNaN(date.getTime())) return false\n\n return true\n}\n","/**\n * Checks if a given locale is a valid locale string.\n *\n * @param {string} locale - The locale string to validate.\n * @returns {boolean} Returns true if the locale is valid, false otherwise.\n *\n * @example\n * isValidLocale('en-US'); // Output: true\n * isValidLocale('fr-FR'); // Output: true\n * isValidLocale('invalid'); // Output: false\n */\nexport function isValidLocale(locale: string): boolean {\n const localeRegex = /^[a-zA-Z]{2,3}(?:-[a-zA-Z]{2,3}(?:-[a-zA-Z]{4})?)?$/i\n if (!localeRegex.test(locale)) return false\n const canonicalLocales = Intl.getCanonicalLocales(locale)\n return canonicalLocales.length > 0 && canonicalLocales[0].toLowerCase() === locale.toLowerCase()\n}\n","export interface RandomNumber {\n min?: number\n max: number\n rounded?: boolean\n}\n\n/**\n * Generates a random number within a specified range.\n * @param {{ min?: number, max: number, rounded?: boolean }} options - The options object.\n * @param {number} options.min - The minimum value of the range (inclusive). Default: 0.\n * @param {number} options.max - The maximum value of the range (exclusive).\n * @param {boolean} options.rounded - Whether to round the generated number to the nearest integer. Default: false.\n * @returns {number} - The generated random number.\n */\nexport const randomNumber = ({ min = 0, max, rounded = false }: RandomNumber) => {\n const randomNumber = Math.random() * (max - min) + min\n return rounded ? Math.floor(randomNumber) : randomNumber\n}\n","export function toCapitalize(str: string): string {\n str = str.trim()\n return str[0].toUpperCase() + str.slice(1)\n}\n","export const toKebabCase = (str: string) =>\n str\n .trim()\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .replace(/\\s+/g, '-')\n .toLowerCase()\n","/**\n * Return a slugified copy of a string.\n *\n * @param {string} str The string to be slugified\n * @return {string} The slugified string.\n */\nexport const toSlug = (str: string) => {\n return str.trim().length === 0\n ? ''\n : str\n .toLowerCase()\n .trim()\n .replace(/ & /g, ' and ')\n .replace(/[ ]+/g, '-')\n .replace(/[-]+/g, '-')\n .replace(/[^a-z0-9-]+/g, '')\n .replace(/^-|-$/g, '')\n}\n"]}