@carto/ps-utils 2.0.0-canary.5 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/package.json +9 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});async function m(t){try{await navigator.clipboard.writeText(t)}catch(r){throw new Error(r)}}function l(t,r=1){if(!t){console.warn(`hexToRgba: ${t} invalid hexadecimal format`);return}if(r<0||r>1){console.warn(`hexToRgba: ${r} invalid alpha format`);return}if(t!=null&&t.includes("#")&&(t=t.slice(1)),!/[0-9A-Fa-f]{6}/g.test(t)&&!/[0-9A-Fa-f]{3}/g.test(t)&&!/[0-9A-Fa-f]{2}/g.test(t)){console.warn(`hexToRgba: ${t} invalid hexadecimal format`);return}const n=t.length;n<6&&(t=t.slice(0,6-n).repeat(6/n));const e=parseInt(t.length===3?t.slice(0,1).repeat(2):t.slice(0,2),16),o=parseInt(t.length===3?t.slice(1,2).repeat(2):t.slice(2,4),16),i=parseInt(t.length===3?t.slice(2,3).repeat(2):t.slice(4,6),16);return[e,o,i,r*255]}function y(t,{withPrefix:r=!0}={}){if(!(t!=null&&t.length)){console.warn("rgbaToHex: invalid array or empty values");return}if(t.some(o=>o<0||o>255)){console.warn(`rgbaToHex: [${t.toString()}] invalid array value format`);return}const e=t.map(o=>o.toString(16).padStart(2,"0")).join("");return`${r?"#":""}${e}`}function p(t,r){return(...o)=>{clearTimeout(0),setTimeout(()=>t(...o),r)}}const a={NUMBER:{style:"decimal",maximumFractionDigits:1,minimumFractionDigits:0,notation:"compact",compactDisplay:"short"},CURRENCY:{style:"currency",currency:"USD",maximumFractionDigits:2,minimumFractionDigits:2,notation:"compact",compactDisplay:"short"},DATE:{year:"numeric",month:"2-digit",day:"2-digit"}};function T(t,r,n){return Intl.DateTimeFormat(r,n).format(t)}function f(t,r,n){return Intl.NumberFormat(r,n).format(t)}function R(t,r,n={}){return f(t,r,{...a.NUMBER,...n})}function D(t,r,n={}){return f(t,r,{...a.CURRENCY,...n})}function s(t,{format:r="NFD",replaceUnicode:n="[̀-ͯ]"}={}){const e=new RegExp(n,"g");return t.normalize(r).replace(e,"")}function F(t,r,{matchFn:n,normalizeOptions:e}={}){let o=t;return typeof t=="string"&&(o=new RegExp(s(t.toLowerCase(),e),"gi")),Array.isArray(r)?r.filter(i=>{const u=n?n(i):i;return s(u).match(o)}):s(r).match(o)?r:null}const b=/:+[a-z]+/gi;function w(t,r=[]){let n=t;const e=n.match(b)??[],o=Array.isArray(r);return e.forEach((i,u)=>{let c;if(o)c=r[u];else{const g=i.substring(1);c=r[g]}n=n.replace(i,(c==null?void 0:c.toString())??"")}),n}exports.DEFAULT_FORMATTERS_CONFIG=a;exports.copy=m;exports.debounce=p;exports.formatCurrency=D;exports.formatDate=T;exports.formatNumber=R;exports.hexToRgba=l;exports.matchText=F;exports.normalize=s;exports.replaceRoute=w;exports.rgbaToHex=y;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/clipboard/copy.ts","../src/colors/hex-to-rgba/hex-to-rgba.ts","../src/colors/rgba-to-hex/rgba-to-hex.ts","../src/debounce/debounce.ts","../src/formatters/constants.ts","../src/formatters/date/format-date.ts","../src/formatters/number/format-number.ts","../src/strings/normalize/normalize.ts","../src/strings/match-text/match-text.ts","../src/strings/routes/replace-route.ts"],"sourcesContent":["/**\n * This function is a helper function to copy text to the clipboard.\n *\n * @param {string} text - The text to copy to the clipboard.\n *\n * @returns {void}\n *\n *\n * @example\n * ```ts\n * copy('Hello world!')\n * ```\n */\nexport async function copy(text: string): Promise<void> {\n try {\n await navigator.clipboard.writeText(text)\n } catch (e: unknown) {\n throw new Error(e as string) // copy failed.\n }\n}\n","/**\n * It converts a hexadecimal color to an array of numbers\n *\n * @param {string} hexadecimal - hexadecimal string, including # or not\n * @param {number} alpha - opacity of final color, value must be between 0-1\n *\n * @returns An array of numbers.\n *\n * @example\n *\n * 6 digits hexadecimal:\n * ```ts\n * hexToRgba('#d1d1d1', 1)\n * ```\n * 3 digits hexadecimal:\n * ```ts\n * hexToRgba('#d1d', 1)\n * ```\n * 2 digits hexadecimal:\n * ```ts\n * hexToRgba('#d1', 1)\n * ```\n */\nexport function hexToRgba(\n hexadecimal: string,\n alpha = 1,\n): [number, number, number, number] | undefined {\n if (!hexadecimal) {\n // eslint-disable-next-line no-console\n console.warn(`hexToRgba: ${hexadecimal} invalid hexadecimal format`)\n return\n }\n if (alpha < 0 || alpha > 1) {\n // eslint-disable-next-line no-console\n console.warn(`hexToRgba: ${alpha} invalid alpha format`)\n return\n }\n if (hexadecimal?.includes('#')) {\n hexadecimal = hexadecimal.slice(1)\n }\n if (\n !/[0-9A-Fa-f]{6}/g.test(hexadecimal) &&\n !/[0-9A-Fa-f]{3}/g.test(hexadecimal) &&\n !/[0-9A-Fa-f]{2}/g.test(hexadecimal)\n ) {\n // eslint-disable-next-line no-console\n console.warn(`hexToRgba: ${hexadecimal} invalid hexadecimal format`)\n return\n }\n\n const length = hexadecimal.length\n\n if (length < 6) {\n hexadecimal = hexadecimal.slice(0, 6 - length).repeat(6 / length)\n }\n\n const r = parseInt(\n hexadecimal.length === 3\n ? hexadecimal.slice(0, 1).repeat(2)\n : hexadecimal.slice(0, 2),\n 16,\n )\n const g = parseInt(\n hexadecimal.length === 3\n ? hexadecimal.slice(1, 2).repeat(2)\n : hexadecimal.slice(2, 4),\n 16,\n )\n const b = parseInt(\n hexadecimal.length === 3\n ? hexadecimal.slice(2, 3).repeat(2)\n : hexadecimal.slice(4, 6),\n 16,\n )\n return [r, g, b, alpha * 255]\n}\n","import type { RgbaToHexOptions } from './types'\n\n/**\n * It converts an array of RGBA values to a hexadecimal string.\n *\n * @param {number[]} rgba - rgba array, including alpha or not\n * @param {RgbaToHexOptions} options - options to parse\n *\n * @returns A string\n *\n * @example\n *\n * Without alpha value:\n * ```ts\n * rgbaDeckGLToHex([0, 0, 0]) // => '#000000'\n * ```\n *\n * With alpha value:\n * ```ts\n * rgbaDeckGLToHex([0, 0, 0, 1]) // => '#000000FF'\n * ```\n *\n * Without prefix:\n * ```ts\n * rgbaDeckGLToHex([0, 0, 0], { withPrefix: false }) // => '000000'\n * ```\n *\n */\nexport function rgbaToHex(\n rgba: number[] | undefined,\n { withPrefix = true }: RgbaToHexOptions = {},\n): string | undefined {\n if (!rgba?.length) {\n // eslint-disable-next-line no-console\n console.warn(`rgbaToHex: invalid array or empty values`)\n return\n }\n\n const hasInvalidValue = rgba.some((value) => value < 0 || value > 255)\n if (hasInvalidValue) {\n // eslint-disable-next-line no-console\n console.warn(`rgbaToHex: [${rgba.toString()}] invalid array value format`)\n return\n }\n\n const hexadecimal = rgba\n .map((value) => {\n const hex = value.toString(16)\n return hex.padStart(2, '0')\n })\n .join('')\n\n return `${withPrefix ? '#' : ''}${hexadecimal}`\n}\n","/**\n * Returns a debounced version of the provided function.\n * @param func - The function to debounce.\n * @param {number} waitFor - The number of milliseconds to wait before invoking the debounced function.\n * @returns A debounced version of the provided function.\n */\nexport function debounce<F extends (...args: unknown[]) => unknown>(\n func: F,\n waitFor: number,\n): (...args: Parameters<F>) => ReturnType<F> {\n const timeout = 0\n\n const debounced = (...args: Parameters<F>) => {\n clearTimeout(timeout)\n setTimeout(() => func(...args), waitFor)\n }\n\n return debounced as (...args: Parameters<F>) => ReturnType<F>\n}\n","export const DEFAULT_FORMATTERS_CONFIG: Record<\n string,\n Intl.NumberFormatOptions | Intl.DateTimeFormatOptions\n> = {\n NUMBER: {\n style: 'decimal',\n maximumFractionDigits: 1,\n minimumFractionDigits: 0,\n notation: 'compact',\n compactDisplay: 'short',\n },\n\n CURRENCY: {\n style: 'currency',\n currency: 'USD',\n maximumFractionDigits: 2,\n minimumFractionDigits: 2,\n notation: 'compact',\n compactDisplay: 'short',\n },\n DATE: {\n year: 'numeric',\n month: '2-digit',\n day: '2-digit',\n },\n} as const\n","/**\n * Formats a Date or number to a string based on the given options.\n * This function could be used to format a Date, in Date format or miliseconds, in specific locale and options.\n * @public\n *\n * @export\n * @param {number | Date} value\n * @param {string | string[]} locales\n * @param {Intl.DateTimeFormatOptions} [options]\n * @return {string}\n */\nexport function formatDate(\n value: number | Date,\n locales: string | string[],\n options?: Intl.DateTimeFormatOptions,\n): string {\n return Intl.DateTimeFormat(locales, options).format(value)\n}\n","import { DEFAULT_FORMATTERS_CONFIG } from '../constants'\n\n/**\n * Formats a number to a string based on the given options.\n * This function could be used to format a number as a percentage, currency or unit in specific locale.\n * @public\n *\n * @example\n *\n * Without specific locale:\n * ```ts\n * format(3001) // => '3,001'\n * ```\n *\n * With specific locale:\n * ```ts\n * format(3001, 'es-ES') // => '3001'\n * ```\n *\n * With specific style:\n * ```ts\n * format(45, 'es-ES', {\n * style: 'unit',\n * unit: 'liter',\n * unitDisplay: 'long',\n * }) // => '45 litros'\n * ```\n *\n * @export\n * @param {number} value - The number to be formatted.\n * @param {string} [locale] - The locale to be used.\n * @param {Intl.NumberFormatOptions} [options] - The options to be used.\n * @return {string}\n */\nfunction format(\n value: number,\n locale: string,\n options?: Intl.NumberFormatOptions,\n): string {\n return Intl.NumberFormat(locale, options).format(value)\n}\n\n/**\n * Formats a number to a string.\n * @public\n *\n * @example\n *\n * ```ts\n * formatNumber(3001, 'es-ES') // => '3 mil'\n * ```\n *\n * @export\n * @param {number} value - The number to be formatted.\n * @param {string} locale - The locale to be used.\n * @param {Intl.NumberFormatOptions} [options] - The options to be used.\n * @return {string}\n */\nexport function formatNumber(\n value: number,\n locale: string,\n options: Intl.NumberFormatOptions = {},\n): string {\n return format(value, locale, {\n ...DEFAULT_FORMATTERS_CONFIG.NUMBER,\n ...options,\n })\n}\n\n/**\n * Formats a number to a currency string.\n * @public\n *\n * @example\n *\n * ```ts\n * formatCurrency(3001, 'en-US') // => '$3.00K'\n * ```\n *\n * @export\n * @param {number} value - The number to be formatted.\n * @param {string} locale - The locale to be used.\n * @param {Intl.NumberFormatOptions} [options] - The options to be used.\n * @return {string}\n */\nexport function formatCurrency(\n value: number,\n locale: string,\n options: Intl.NumberFormatOptions = {},\n): string {\n return format(value, locale, {\n ...DEFAULT_FORMATTERS_CONFIG.CURRENCY,\n ...options,\n })\n}\n","import type { NormalizeOptions } from './types'\n\n/**\n * It takes a string and returns a string normalized.\n * @param {string} data - The data to be normalized.\n * @param {NormalizeOptions} options - data - The string to be normalized.\n * @returns A string\n *\n * @remarks\n * The normalization by default is NFD\n *\n * @example\n *\n * ```ts\n * normalize('foo bar')\n * ```\n *\n */\nexport function normalize(\n data: string,\n { format = 'NFD', replaceUnicode = '[\\u0300-\\u036f]' }: NormalizeOptions = {},\n): string {\n const unicode = new RegExp(replaceUnicode, 'g')\n return data.normalize(format).replace(unicode, '')\n}\n","import type { MatchTextOptions } from './types'\nimport { normalize } from '../normalize/normalize'\n\n/**\n * It takes a string, an array of strings, and an optional object of options, and returns an\n * array of strings that match the input string\n * @param {string | RegExp} text - The text to match against.\n * @param {T[] | string} data - The data to match against.\n * @param {MatchTextOptions} options - The options to use.\n *\n * @returns {T[] | string | null} - The matched data or null.\n *\n */\nexport function matchText<T>(\n value: string | RegExp,\n data: T[] | string,\n { matchFn, normalizeOptions }: MatchTextOptions<T> = {},\n): T[] | string | null {\n let regex = value\n if (typeof value === 'string') {\n regex = new RegExp(normalize(value.toLowerCase(), normalizeOptions), 'gi')\n }\n\n if (!Array.isArray(data)) {\n return normalize(data).match(regex) ? data : null\n }\n\n return data.filter((match) => {\n const newData = (matchFn ? matchFn(match) : match) as string\n return normalize(newData).match(regex)\n })\n}\n","import type { Route, Values } from './types'\n\nconst regex = /:+[a-z]+/gi\n\n/**\n * Replace route with values from array or object.\n * @param {Route} data\n * @param {Values} values\n *\n * @returns {Route}\n *\n * @example\n * ```ts\n * replaceRoute('/user/:id', { id: 1 }) // /user/1\n * replaceRoute('/user/:id/:name', [1, 'John']) // /user/1/John\n * ```\n *\n * @remarks\n * This function is used to replace the route with the values from the array or object. If the values are not found, it will replace it with `undefined`.\n *\n * ```ts\n * replaceRoute('/user/:id/:name', [1]) // /user/1/undefined\n * ```\n *\n **/\nexport function replaceRoute(data: Route, values: Values = []): Route {\n let _data = data\n const templates = _data.match(regex) ?? []\n const isArray = Array.isArray(values)\n\n templates.forEach((template, index) => {\n let value\n if (!isArray) {\n const key = template.substring(1)\n value = values[key]\n } else {\n value = values[index]\n }\n _data = _data.replace(template, value?.toString() ?? '')\n })\n\n return _data\n}\n"],"names":["copy","text","e","hexToRgba","hexadecimal","alpha","length","r","g","b","rgbaToHex","rgba","withPrefix","value","debounce","func","waitFor","args","DEFAULT_FORMATTERS_CONFIG","formatDate","locales","options","format","locale","formatNumber","formatCurrency","normalize","data","replaceUnicode","unicode","matchText","matchFn","normalizeOptions","regex","match","newData","replaceRoute","values","_data","templates","isArray","template","index","key"],"mappings":"gFAaA,eAAsBA,EAAKC,EAA6B,CAClD,GAAA,CACI,MAAA,UAAU,UAAU,UAAUA,CAAI,QACjCC,EAAY,CACb,MAAA,IAAI,MAAMA,CAAW,CAAA,CAE/B,CCIgB,SAAAC,EACdC,EACAC,EAAQ,EACsC,CAC9C,GAAI,CAACD,EAAa,CAER,QAAA,KAAK,cAAcA,CAAW,6BAA6B,EACnE,MAAA,CAEE,GAAAC,EAAQ,GAAKA,EAAQ,EAAG,CAElB,QAAA,KAAK,cAAcA,CAAK,uBAAuB,EACvD,MAAA,CAKF,GAHID,GAAA,MAAAA,EAAa,SAAS,OACVA,EAAAA,EAAY,MAAM,CAAC,GAGjC,CAAC,kBAAkB,KAAKA,CAAW,GACnC,CAAC,kBAAkB,KAAKA,CAAW,GACnC,CAAC,kBAAkB,KAAKA,CAAW,EACnC,CAEQ,QAAA,KAAK,cAAcA,CAAW,6BAA6B,EACnE,MAAA,CAGF,MAAME,EAASF,EAAY,OAEvBE,EAAS,IAC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,SAASG,EAAaT,EAAaU,EAAiB,GAAW,CACpE,IAAIC,EAAQX,EACZ,MAAMY,EAAYD,EAAM,MAAML,CAAK,GAAK,CAAC,EACnCO,EAAU,MAAM,QAAQH,CAAM,EAE1B,OAAAE,EAAA,QAAQ,CAACE,EAAUC,IAAU,CACjC,IAAA7B,EACJ,GAAK2B,EAIH3B,EAAQwB,EAAOK,CAAK,MAJR,CACN,MAAAC,EAAMF,EAAS,UAAU,CAAC,EAChC5B,EAAQwB,EAAOM,CAAG,CAAA,CAIpBL,EAAQA,EAAM,QAAQG,GAAU5B,GAAA,YAAAA,EAAO,aAAc,EAAE,CAAA,CACxD,EAEMyB,CACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carto/ps-utils",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "CARTO's Professional Service Helpers library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"dependencies": {},
|
|
@@ -8,8 +8,14 @@
|
|
|
8
8
|
"peerDependencies": {},
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"import":
|
|
12
|
-
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/types/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/types/index.d.ts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
},
|
|
15
21
|
"publishConfig": {
|