@carto/ps-utils 1.2.0 → 1.3.0

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 CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function f(t,r=1){if(!t){console.warn(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);return}if(r<0||r>1){console.warn(`hexToRgbaDeckGL: ${r} invalid alpha format`);return}if(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(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);return}const n=t.length;n<6&&(t=t.slice(0,6-n).repeat(6/n));const o=parseInt(t.length===3?t.slice(0,1).repeat(2):t.slice(0,2),16),e=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[o,e,i,r*255]}function g(t,{withPrefix:r=!0}={}){if(!t||!t.length){console.warn("rgbaDeckGLToHex: invalid array or empty values");return}if(t.some(e=>e<0||e>255)){console.warn(`rgbaDeckGLToHex: [${t.toString()}] invalid array value format`);return}const o=t.map(e=>e.toString(16).padStart(2,"0")).join("");return`${r?"#":""}${o}`}function c(t,{format:r="NFD",replaceUnicode:n="[̀-ͯ]"}={}){const o=new RegExp(n,"g");return t.normalize(r).replace(o,"")}function m(t,r,{matchFn:n,normalizeOptions:o}={}){const e=new RegExp(c(t.toLowerCase(),o),"gi");return Array.isArray(r)?r.filter(i=>{const a=n?n(i):i;return c(a).match(e)}):c(r).match(e)?r:null}function l(t,r,n){return Intl.DateTimeFormat(r,n).format(t)}const s={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 u(t,r,n){return Intl.NumberFormat(r,n).format(t)}function D(t,r,n={}){return u(t,r,{...s.NUMBER,...n})}function p(t,r,n={}){return u(t,r,{...s.CURRENCY,...n})}exports.CONFIG=s;exports.formatCurrency=p;exports.formatDate=l;exports.formatNumber=D;exports.hexToRgbaDeckGL=f;exports.matchText=m;exports.normalize=c;exports.rgbaDeckGLToHex=g;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function l(t,r=1){if(!t){console.warn(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);return}if(r<0||r>1){console.warn(`hexToRgbaDeckGL: ${r} invalid alpha format`);return}if(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(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);return}const n=t.length;n<6&&(t=t.slice(0,6-n).repeat(6/n));const o=parseInt(t.length===3?t.slice(0,1).repeat(2):t.slice(0,2),16),e=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[o,e,i,r*255]}function m(t,{withPrefix:r=!0}={}){if(!t||!t.length){console.warn("rgbaDeckGLToHex: invalid array or empty values");return}if(t.some(e=>e<0||e>255)){console.warn(`rgbaDeckGLToHex: [${t.toString()}] invalid array value format`);return}const o=t.map(e=>e.toString(16).padStart(2,"0")).join("");return`${r?"#":""}${o}`}function c(t,{format:r="NFD",replaceUnicode:n="[̀-ͯ]"}={}){const o=new RegExp(n,"g");return t.normalize(r).replace(o,"")}function D(t,r,{matchFn:n,normalizeOptions:o}={}){const e=new RegExp(c(t.toLowerCase(),o),"gi");return Array.isArray(r)?r.filter(i=>{const s=n?n(i):i;return c(s).match(e)}):c(r).match(e)?r:null}function p(t,r,n){return Intl.DateTimeFormat(r,n).format(t)}const u={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 f(t,r,n){return Intl.NumberFormat(r,n).format(t)}function y(t,r,n={}){return f(t,r,{...u.NUMBER,...n})}function R(t,r,n={}){return f(t,r,{...u.CURRENCY,...n})}const T=/:+[a-z]+/gi;function b(t,r=[]){let n=t;const o=n.match(T)||[],e=Array.isArray(r);return o.forEach((i,s)=>{let a;if(e)a=r[s];else{const g=i.substring(1);a=r[g]}n=n.replace(i,a?.toString())}),n}exports.CONFIG=u;exports.formatCurrency=R;exports.formatDate=p;exports.formatNumber=y;exports.hexToRgbaDeckGL=l;exports.matchText=D;exports.normalize=c;exports.replaceRoute=b;exports.rgbaDeckGLToHex=m;
package/dist/index.js CHANGED
@@ -1,18 +1,18 @@
1
- function f(t, n = 1) {
1
+ function m(t, r = 1) {
2
2
  if (!t) {
3
3
  console.warn(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);
4
4
  return;
5
5
  }
6
- if (n < 0 || n > 1) {
7
- console.warn(`hexToRgbaDeckGL: ${n} invalid alpha format`);
6
+ if (r < 0 || r > 1) {
7
+ console.warn(`hexToRgbaDeckGL: ${r} invalid alpha format`);
8
8
  return;
9
9
  }
10
10
  if (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)) {
11
11
  console.warn(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);
12
12
  return;
13
13
  }
14
- const r = t.length;
15
- r < 6 && (t = t.slice(0, 6 - r).repeat(6 / r));
14
+ const n = t.length;
15
+ n < 6 && (t = t.slice(0, 6 - n).repeat(6 / n));
16
16
  const e = parseInt(
17
17
  t.length === 3 ? t.slice(0, 1).repeat(2) : t.slice(0, 2),
18
18
  16
@@ -23,9 +23,9 @@ function f(t, n = 1) {
23
23
  t.length === 3 ? t.slice(2, 3).repeat(2) : t.slice(4, 6),
24
24
  16
25
25
  );
26
- return [e, o, i, n * 255];
26
+ return [e, o, i, r * 255];
27
27
  }
28
- function g(t, { withPrefix: n = !0 } = {}) {
28
+ function p(t, { withPrefix: r = !0 } = {}) {
29
29
  if (!t || !t.length) {
30
30
  console.warn("rgbaDeckGLToHex: invalid array or empty values");
31
31
  return;
@@ -37,26 +37,26 @@ function g(t, { withPrefix: n = !0 } = {}) {
37
37
  return;
38
38
  }
39
39
  const e = t.map((o) => o.toString(16).padStart(2, "0")).join("");
40
- return `${n ? "#" : ""}${e}`;
40
+ return `${r ? "#" : ""}${e}`;
41
41
  }
42
- function s(t, { format: n = "NFD", replaceUnicode: r = "[̀-ͯ]" } = {}) {
43
- const e = new RegExp(r, "g");
44
- return t.normalize(n).replace(e, "");
42
+ function u(t, { format: r = "NFD", replaceUnicode: n = "[̀-ͯ]" } = {}) {
43
+ const e = new RegExp(n, "g");
44
+ return t.normalize(r).replace(e, "");
45
45
  }
46
- function l(t, n, { matchFn: r, normalizeOptions: e } = {}) {
46
+ function D(t, r, { matchFn: n, normalizeOptions: e } = {}) {
47
47
  const o = new RegExp(
48
- s(t.toLowerCase(), e),
48
+ u(t.toLowerCase(), e),
49
49
  "gi"
50
50
  );
51
- return Array.isArray(n) ? n.filter((i) => {
52
- const a = r ? r(i) : i;
53
- return s(a).match(o);
54
- }) : s(n).match(o) ? n : null;
51
+ return Array.isArray(r) ? r.filter((i) => {
52
+ const s = n ? n(i) : i;
53
+ return u(s).match(o);
54
+ }) : u(r).match(o) ? r : null;
55
55
  }
56
- function m(t, n, r) {
57
- return Intl.DateTimeFormat(n, r).format(t);
56
+ function y(t, r, n) {
57
+ return Intl.DateTimeFormat(r, n).format(t);
58
58
  }
59
- const c = {
59
+ const a = {
60
60
  NUMBER: {
61
61
  style: "decimal",
62
62
  maximumFractionDigits: 1,
@@ -78,22 +78,38 @@ const c = {
78
78
  day: "2-digit"
79
79
  }
80
80
  };
81
- function u(t, n, r) {
82
- return Intl.NumberFormat(n, r).format(t);
81
+ function f(t, r, n) {
82
+ return Intl.NumberFormat(r, n).format(t);
83
83
  }
84
- function D(t, n, r = {}) {
85
- return u(t, n, { ...c.NUMBER, ...r });
84
+ function R(t, r, n = {}) {
85
+ return f(t, r, { ...a.NUMBER, ...n });
86
86
  }
87
- function p(t, n, r = {}) {
88
- return u(t, n, { ...c.CURRENCY, ...r });
87
+ function F(t, r, n = {}) {
88
+ return f(t, r, { ...a.CURRENCY, ...n });
89
+ }
90
+ const l = /:+[a-z]+/gi;
91
+ function T(t, r = []) {
92
+ let n = t;
93
+ const e = n.match(l) || [], o = Array.isArray(r);
94
+ return e.forEach((i, s) => {
95
+ let c;
96
+ if (o)
97
+ c = r[s];
98
+ else {
99
+ const g = i.substring(1);
100
+ c = r[g];
101
+ }
102
+ n = n.replace(i, c?.toString());
103
+ }), n;
89
104
  }
90
105
  export {
91
- c as CONFIG,
92
- p as formatCurrency,
93
- m as formatDate,
94
- D as formatNumber,
95
- f as hexToRgbaDeckGL,
96
- l as matchText,
97
- s as normalize,
98
- g as rgbaDeckGLToHex
106
+ a as CONFIG,
107
+ F as formatCurrency,
108
+ y as formatDate,
109
+ R as formatNumber,
110
+ m as hexToRgbaDeckGL,
111
+ D as matchText,
112
+ u as normalize,
113
+ T as replaceRoute,
114
+ p as rgbaDeckGLToHex
99
115
  };
@@ -1 +1,16 @@
1
+ /**
2
+ * This function is a helper function to copy text to the clipboard.
3
+ *
4
+ * @param {string} text - The text to copy to the clipboard.
5
+ *
6
+ * @returns {void}
7
+ *
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { copy } from '@carto/ps-utils'
12
+ *
13
+ * copy('Hello world!')
14
+ * ```
15
+ */
1
16
  export declare function copy(text: string): void;
@@ -21,6 +21,6 @@
21
21
  * hexToRgbaDeckGL('#d1', 1)
22
22
  * ```
23
23
  *
24
- * @alpha
24
+ * @deprecated use `hexToRgb` from `@deck.gl/carto/typed/style/palette` instead
25
25
  */
26
26
  export declare function hexToRgbaDeckGL(hexadecimal: string, alpha?: number): number[] | undefined;
@@ -24,7 +24,5 @@ import { RgbaDeckGLToHexOptions } from './types';
24
24
  * rgbaDeckGLToHex([0, 0, 0], { withPrefix: false }) // => '000000'
25
25
  * ```
26
26
  *
27
- *
28
- * @alpha
29
27
  */
30
28
  export declare function rgbaDeckGLToHex(rgba: number[], { withPrefix }?: RgbaDeckGLToHexOptions): string | undefined;
@@ -8,3 +8,5 @@ export * from './strings/normalize/types';
8
8
  export { formatDate } from './formatters/date/format-date';
9
9
  export { formatNumber, formatCurrency } from './formatters/number/format-number';
10
10
  export * from './formatters/constants';
11
+ export * from './routes/replace-route';
12
+ export * from './routes/types';
@@ -0,0 +1,23 @@
1
+ import { Route, Values } from './types';
2
+ /**
3
+ * Replace route with values from array or object.
4
+ * @param {Route} data
5
+ * @param {Values} values
6
+ *
7
+ * @returns {Route}
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * replaceRoute('/user/:id', { id: 1 }) // /user/1
12
+ * replaceRoute('/user/:id/:name', [1, 'John']) // /user/1/John
13
+ * ```
14
+ *
15
+ * @remarks
16
+ * 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`.
17
+ *
18
+ * ```ts
19
+ * replaceRoute('/user/:id/:name', [1]) // /user/1/undefined
20
+ * ```
21
+ *
22
+ **/
23
+ export declare function replaceRoute(data: Route, values?: Values): string;
@@ -0,0 +1,2 @@
1
+ export type Route = string;
2
+ export type Values = Record<string, string | number> | (string | number)[];
@@ -8,6 +8,5 @@ import { MatchTextOptions } from './types';
8
8
  *
9
9
  * @returns {T[] | string | null} - The matched data or null.
10
10
  *
11
- * @alpha
12
11
  */
13
12
  export declare function matchText<T>(text: string, data: T[] | string, { matchFn, normalizeOptions }?: MatchTextOptions<T>): T[] | string | null;
@@ -14,6 +14,5 @@ import { NormalizeOptions } from './types';
14
14
  * normalize('foo bar')
15
15
  * ```
16
16
  *
17
- * @alpha
18
17
  */
19
18
  export declare function normalize(data: string, { format, replaceUnicode }?: NormalizeOptions): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carto/ps-utils",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "CARTO's Professional Service Utils library",
5
5
  "files": [
6
6
  "dist"
@@ -17,16 +17,13 @@
17
17
  }
18
18
  },
19
19
  "scripts": {
20
- "build": "vite build && tsc",
20
+ "prebuild": "npm exec rimraf dist",
21
+ "build": "tsc && vite build",
21
22
  "lint": "eslint src --ext ts,tsx,js,jsx,json --ignore-path ../../.gitignore --fix",
22
23
  "format": "prettier --write . --ignore-path ../../.gitignore",
23
24
  "test:watch": "vitest",
24
25
  "test:related": "vitest --run --passWithNoTests",
25
26
  "test:coverage": "vitest run --coverage"
26
27
  },
27
- "typedoc": {
28
- "entryPoint": "./src/index.ts",
29
- "readmeFile": "./README.md"
30
- },
31
28
  "dependencies": {}
32
29
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};