@carto/ps-utils 1.3.0 → 1.5.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 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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("@turf/meta");function R(t,e=1){if(!t){console.warn(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);return}if(e<0||e>1){console.warn(`hexToRgbaDeckGL: ${e} 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),r=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,r,i,e*255]}function T(t,{withPrefix:e=!0}={}){if(!t||!t.length){console.warn("rgbaDeckGLToHex: invalid array or empty values");return}if(t.some(r=>r<0||r>255)){console.warn(`rgbaDeckGLToHex: [${t.toString()}] invalid array value format`);return}const o=t.map(r=>r.toString(16).padStart(2,"0")).join("");return`${e?"#":""}${o}`}function I(t,e){return(...r)=>{clearTimeout(0),setTimeout(()=>t(...r),e)}}const m=63710088e-1;function h(t){return b.geomReduce(t,function(e,n){return e+A(n)},0)}function A(t){let e=0,n;switch(t.type){case"Polygon":return p(t.coordinates);case"MultiPolygon":for(n=0;n<t.coordinates.length;n++)e+=p(t.coordinates[n]);return e;case"Point":case"MultiPoint":case"LineString":case"MultiLineString":return 0}return 0}function p(t){let e=0;if(t&&t.length>0){e+=Math.abs(y(t[0]));for(let n=1;n<t.length;n++)e-=Math.abs(y(t[n]))}return e}function y(t){let e,n,o,r,i,a,c,s=0;const u=t.length;if(u>2){for(c=0;c<u;c++)c===u-2?(r=u-2,i=u-1,a=0):c===u-1?(r=u-1,i=0,a=1):(r=c,i=c+1,a=c+2),e=t[r],n=t[i],o=t[a],s+=(f(o[0])-f(e[0]))*Math.sin(f(n[1]));s=s*m*m/2}return s}function f(t){return t*Math.PI/180}const g={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 L(t,e,n){return Intl.DateTimeFormat(e,n).format(t)}function D(t,e,n){return Intl.NumberFormat(e,n).format(t)}function w(t,e,n={}){return D(t,e,{...g.NUMBER,...n})}function F(t,e,n={}){return D(t,e,{...g.CURRENCY,...n})}const G=/:+[a-z]+/gi;function k(t,e=[]){let n=t;const o=n.match(G)||[],r=Array.isArray(e);return o.forEach((i,a)=>{let c;if(r)c=e[a];else{const s=i.substring(1);c=e[s]}n=n.replace(i,c?.toString())}),n}function l(t,{format:e="NFD",replaceUnicode:n="[̀-ͯ]"}={}){const o=new RegExp(n,"g");return t.normalize(e).replace(o,"")}function M(t,e,{matchFn:n,normalizeOptions:o}={}){const r=new RegExp(l(t.toLowerCase(),o),"gi");return Array.isArray(e)?e.filter(i=>{const a=n?n(i):i;return l(a).match(r)}):l(e).match(r)?e:null}exports.CONFIG=g;exports.area=h;exports.debounce=I;exports.formatCurrency=F;exports.formatDate=L;exports.formatNumber=w;exports.hexToRgbaDeckGL=R;exports.matchText=M;exports.normalize=l;exports.replaceRoute=k;exports.rgbaDeckGLToHex=T;
package/dist/index.js CHANGED
@@ -1,62 +1,100 @@
1
- function m(t, r = 1) {
1
+ import { geomReduce as R } from "@turf/meta";
2
+ function T(t, n = 1) {
2
3
  if (!t) {
3
4
  console.warn(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);
4
5
  return;
5
6
  }
6
- if (r < 0 || r > 1) {
7
- console.warn(`hexToRgbaDeckGL: ${r} invalid alpha format`);
7
+ if (n < 0 || n > 1) {
8
+ console.warn(`hexToRgbaDeckGL: ${n} invalid alpha format`);
8
9
  return;
9
10
  }
10
11
  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
12
  console.warn(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);
12
13
  return;
13
14
  }
14
- const n = t.length;
15
- n < 6 && (t = t.slice(0, 6 - n).repeat(6 / n));
16
- const e = parseInt(
15
+ const e = t.length;
16
+ e < 6 && (t = t.slice(0, 6 - e).repeat(6 / e));
17
+ const o = parseInt(
17
18
  t.length === 3 ? t.slice(0, 1).repeat(2) : t.slice(0, 2),
18
19
  16
19
- ), o = parseInt(
20
+ ), r = parseInt(
20
21
  t.length === 3 ? t.slice(1, 2).repeat(2) : t.slice(2, 4),
21
22
  16
22
23
  ), i = parseInt(
23
24
  t.length === 3 ? t.slice(2, 3).repeat(2) : t.slice(4, 6),
24
25
  16
25
26
  );
26
- return [e, o, i, r * 255];
27
+ return [o, r, i, n * 255];
27
28
  }
28
- function p(t, { withPrefix: r = !0 } = {}) {
29
+ function h(t, { withPrefix: n = !0 } = {}) {
29
30
  if (!t || !t.length) {
30
31
  console.warn("rgbaDeckGLToHex: invalid array or empty values");
31
32
  return;
32
33
  }
33
- if (t.some((o) => o < 0 || o > 255)) {
34
+ if (t.some((r) => r < 0 || r > 255)) {
34
35
  console.warn(
35
36
  `rgbaDeckGLToHex: [${t.toString()}] invalid array value format`
36
37
  );
37
38
  return;
38
39
  }
39
- const e = t.map((o) => o.toString(16).padStart(2, "0")).join("");
40
- return `${r ? "#" : ""}${e}`;
40
+ const o = t.map((r) => r.toString(16).padStart(2, "0")).join("");
41
+ return `${n ? "#" : ""}${o}`;
41
42
  }
42
- function u(t, { format: r = "NFD", replaceUnicode: n = "[̀-ͯ]" } = {}) {
43
- const e = new RegExp(n, "g");
44
- return t.normalize(r).replace(e, "");
43
+ function w(t, n) {
44
+ return (...r) => {
45
+ clearTimeout(0), setTimeout(() => t(...r), n);
46
+ };
45
47
  }
46
- function D(t, r, { matchFn: n, normalizeOptions: e } = {}) {
47
- const o = new RegExp(
48
- u(t.toLowerCase(), e),
49
- "gi"
48
+ const g = 63710088e-1;
49
+ function L(t) {
50
+ return R(
51
+ t,
52
+ function(n, e) {
53
+ return n + A(e);
54
+ },
55
+ 0
50
56
  );
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
57
  }
56
- function y(t, r, n) {
57
- return Intl.DateTimeFormat(r, n).format(t);
58
+ function A(t) {
59
+ let n = 0, e;
60
+ switch (t.type) {
61
+ case "Polygon":
62
+ return m(t.coordinates);
63
+ case "MultiPolygon":
64
+ for (e = 0; e < t.coordinates.length; e++)
65
+ n += m(t.coordinates[e]);
66
+ return n;
67
+ case "Point":
68
+ case "MultiPoint":
69
+ case "LineString":
70
+ case "MultiLineString":
71
+ return 0;
72
+ }
73
+ return 0;
74
+ }
75
+ function m(t) {
76
+ let n = 0;
77
+ if (t && t.length > 0) {
78
+ n += Math.abs(p(t[0]));
79
+ for (let e = 1; e < t.length; e++)
80
+ n -= Math.abs(p(t[e]));
81
+ }
82
+ return n;
83
+ }
84
+ function p(t) {
85
+ let n, e, o, r, i, u, c, s = 0;
86
+ const a = t.length;
87
+ if (a > 2) {
88
+ for (c = 0; c < a; c++)
89
+ c === a - 2 ? (r = a - 2, i = a - 1, u = 0) : c === a - 1 ? (r = a - 1, i = 0, u = 1) : (r = c, i = c + 1, u = c + 2), n = t[r], e = t[i], o = t[u], s += (l(o[0]) - l(n[0])) * Math.sin(l(e[1]));
90
+ s = s * g * g / 2;
91
+ }
92
+ return s;
93
+ }
94
+ function l(t) {
95
+ return t * Math.PI / 180;
58
96
  }
59
- const a = {
97
+ const y = {
60
98
  NUMBER: {
61
99
  style: "decimal",
62
100
  maximumFractionDigits: 1,
@@ -78,38 +116,57 @@ const a = {
78
116
  day: "2-digit"
79
117
  }
80
118
  };
81
- function f(t, r, n) {
82
- return Intl.NumberFormat(r, n).format(t);
119
+ function F(t, n, e) {
120
+ return Intl.DateTimeFormat(n, e).format(t);
121
+ }
122
+ function D(t, n, e) {
123
+ return Intl.NumberFormat(n, e).format(t);
83
124
  }
84
- function R(t, r, n = {}) {
85
- return f(t, r, { ...a.NUMBER, ...n });
125
+ function M(t, n, e = {}) {
126
+ return D(t, n, { ...y.NUMBER, ...e });
86
127
  }
87
- function F(t, r, n = {}) {
88
- return f(t, r, { ...a.CURRENCY, ...n });
128
+ function k(t, n, e = {}) {
129
+ return D(t, n, { ...y.CURRENCY, ...e });
89
130
  }
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) => {
131
+ const I = /:+[a-z]+/gi;
132
+ function E(t, n = []) {
133
+ let e = t;
134
+ const o = e.match(I) || [], r = Array.isArray(n);
135
+ return o.forEach((i, u) => {
95
136
  let c;
96
- if (o)
97
- c = r[s];
137
+ if (r)
138
+ c = n[u];
98
139
  else {
99
- const g = i.substring(1);
100
- c = r[g];
140
+ const s = i.substring(1);
141
+ c = n[s];
101
142
  }
102
- n = n.replace(i, c?.toString());
103
- }), n;
143
+ e = e.replace(i, c?.toString());
144
+ }), e;
145
+ }
146
+ function f(t, { format: n = "NFD", replaceUnicode: e = "[̀-ͯ]" } = {}) {
147
+ const o = new RegExp(e, "g");
148
+ return t.normalize(n).replace(o, "");
149
+ }
150
+ function G(t, n, { matchFn: e, normalizeOptions: o } = {}) {
151
+ const r = new RegExp(
152
+ f(t.toLowerCase(), o),
153
+ "gi"
154
+ );
155
+ return Array.isArray(n) ? n.filter((i) => {
156
+ const u = e ? e(i) : i;
157
+ return f(u).match(r);
158
+ }) : f(n).match(r) ? n : null;
104
159
  }
105
160
  export {
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
161
+ y as CONFIG,
162
+ L as area,
163
+ w as debounce,
164
+ k as formatCurrency,
165
+ F as formatDate,
166
+ M as formatNumber,
167
+ T as hexToRgbaDeckGL,
168
+ G as matchText,
169
+ f as normalize,
170
+ E as replaceRoute,
171
+ h as rgbaDeckGLToHex
115
172
  };
@@ -0,0 +1,3 @@
1
+ export { hexToRgbaDeckGL } from './hex-to-gba-deckgl/hex-to-gba-deckgl';
2
+ export { rgbaDeckGLToHex } from './rgba-deck-gl-to-hex/rgba-deck-gl-to-hex';
3
+ export * from './rgba-deck-gl-to-hex/types';
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns a debounced version of the provided function.
3
+ * @param func - The function to debounce.
4
+ * @param {number} waitFor - The number of milliseconds to wait before invoking the debounced function.
5
+ * @returns A debounced version of the provided function.
6
+ */
7
+ export declare function debounce<F extends (...args: any[]) => any>(func: F, waitFor: number): (...args: Parameters<F>) => ReturnType<F>;
@@ -0,0 +1,17 @@
1
+ import type { GeojsonAreaProps } from './types';
2
+ /**
3
+ * Takes one or more features and returns their area in square meters paying attention to earth radius.
4
+ * It's fixed in turf.js v7.0.0+ for previous versions of turf use this function instead turf.area
5
+ * @name area
6
+ * @param {GeojsonAreaProps>} geojson {GeoJSON} input GeoJSON feature(s)
7
+ * @returns {number} area in square meters
8
+ * @example
9
+ * let polygon = turf.polygon([[[125, -15], [113, -22], [154, -27], [144, -15], [125, -15]]]);
10
+ *
11
+ * let area = turf.area(polygon);
12
+ *
13
+ * //addToMap
14
+ * let addToMap = [polygon]
15
+ * polygon.properties.area = area
16
+ */
17
+ export declare function area(geojson: GeojsonAreaProps): any;
@@ -0,0 +1,4 @@
1
+ import type { Geometries } from '@turf/helpers';
2
+ import type { Feature, FeatureCollection, LineString, GeoJsonProperties, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon } from 'geojson';
3
+ export type GeomAreaProps = Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon;
4
+ export type GeojsonAreaProps = Geometries | FeatureCollection<Geometries, GeoJsonProperties> | Feature<Geometries, GeoJsonProperties>;
@@ -1,12 +1,13 @@
1
- export { hexToRgbaDeckGL } from './colors/hex-to-gba-deckgl/hex-to-gba-deckgl';
2
- export { rgbaDeckGLToHex } from './colors/rgba-deck-gl-to-hex/rgba-deck-gl-to-hex';
3
- export * from './colors/rgba-deck-gl-to-hex/types';
4
- export { matchText } from './strings/match-text/match-text';
5
- export * from './strings/match-text/types';
6
- export { normalize } from './strings/normalize/normalize';
7
- export * from './strings/normalize/types';
1
+ export * from './colors';
2
+ export * from './debounce/debounce';
3
+ export { area } from './features/area';
4
+ export * from './features/types';
5
+ export * from './formatters/constants';
8
6
  export { formatDate } from './formatters/date/format-date';
9
7
  export { formatNumber, formatCurrency } from './formatters/number/format-number';
10
- export * from './formatters/constants';
11
8
  export * from './routes/replace-route';
12
9
  export * from './routes/types';
10
+ export { matchText } from './strings/match-text/match-text';
11
+ export { normalize } from './strings/normalize/normalize';
12
+ export * from './strings/match-text/types';
13
+ export * from './strings/normalize/types';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carto/ps-utils",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "CARTO's Professional Service Utils library",
5
5
  "files": [
6
6
  "dist"
@@ -25,5 +25,14 @@
25
25
  "test:related": "vitest --run --passWithNoTests",
26
26
  "test:coverage": "vitest run --coverage"
27
27
  },
28
- "dependencies": {}
28
+ "peerDependencies": {
29
+ "@turf/meta": ">= 6.x < 7.x"
30
+ },
31
+ "dependencies": {
32
+ "@turf/meta": "6.5.0"
33
+ },
34
+ "devDependencies": {
35
+ "@turf/helpers": "6.5.0",
36
+ "@types/geojson": "7946.0.11"
37
+ }
29
38
  }