@carto/ps-utils 1.3.0 → 1.4.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 R=require("@turf/meta");function b(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 i=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),o=parseInt(t.length===3?t.slice(2,3).repeat(2):t.slice(4,6),16);return[i,r,o,e*255]}function I(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 i=t.map(r=>r.toString(16).padStart(2,"0")).join("");return`${e?"#":""}${i}`}function l(t,{format:e="NFD",replaceUnicode:n="[̀-ͯ]"}={}){const i=new RegExp(n,"g");return t.normalize(e).replace(i,"")}function T(t,e,{matchFn:n,normalizeOptions:i}={}){const r=new RegExp(l(t.toLowerCase(),i),"gi");return Array.isArray(e)?e.filter(o=>{const c=n?n(o):o;return l(c).match(r)}):l(e).match(r)?e:null}function h(t,e,n){return Intl.DateTimeFormat(e,n).format(t)}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 D(t,e,n){return Intl.NumberFormat(e,n).format(t)}function A(t,e,n={}){return D(t,e,{...g.NUMBER,...n})}function L(t,e,n={}){return D(t,e,{...g.CURRENCY,...n})}const w=/:+[a-z]+/gi;function F(t,e=[]){let n=t;const i=n.match(w)||[],r=Array.isArray(e);return i.forEach((o,c)=>{let a;if(r)a=e[c];else{const s=o.substring(1);a=e[s]}n=n.replace(o,a?.toString())}),n}const m=63710088e-1;function G(t){return R.geomReduce(t,function(e,n){return e+k(n)},0)}function k(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,i,r,o,c,a,s=0;const u=t.length;if(u>2){for(a=0;a<u;a++)a===u-2?(r=u-2,o=u-1,c=0):a===u-1?(r=u-1,o=0,c=1):(r=a,o=a+1,c=a+2),e=t[r],n=t[o],i=t[c],s+=(f(i[0])-f(e[0]))*Math.sin(f(n[1]));s=s*m*m/2}return s}function f(t){return t*Math.PI/180}exports.CONFIG=g;exports.area=G;exports.formatCurrency=L;exports.formatDate=h;exports.formatNumber=A;exports.hexToRgbaDeckGL=b;exports.matchText=T;exports.normalize=l;exports.replaceRoute=F;exports.rgbaDeckGLToHex=I;
package/dist/index.js CHANGED
@@ -1,62 +1,63 @@
1
- function m(t, r = 1) {
1
+ import { geomReduce as R } from "@turf/meta";
2
+ function w(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 r = t.length;
16
+ r < 6 && (t = t.slice(0, 6 - r).repeat(6 / r));
17
+ const i = parseInt(
17
18
  t.length === 3 ? t.slice(0, 1).repeat(2) : t.slice(0, 2),
18
19
  16
19
- ), o = parseInt(
20
+ ), e = parseInt(
20
21
  t.length === 3 ? t.slice(1, 2).repeat(2) : t.slice(2, 4),
21
22
  16
22
- ), i = parseInt(
23
+ ), o = 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 [i, e, o, n * 255];
27
28
  }
28
- function p(t, { withPrefix: r = !0 } = {}) {
29
+ function L(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((e) => e < 0 || e > 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 i = t.map((e) => e.toString(16).padStart(2, "0")).join("");
41
+ return `${n ? "#" : ""}${i}`;
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 l(t, { format: n = "NFD", replaceUnicode: r = "[̀-ͯ]" } = {}) {
44
+ const i = new RegExp(r, "g");
45
+ return t.normalize(n).replace(i, "");
45
46
  }
46
- function D(t, r, { matchFn: n, normalizeOptions: e } = {}) {
47
- const o = new RegExp(
48
- u(t.toLowerCase(), e),
47
+ function F(t, n, { matchFn: r, normalizeOptions: i } = {}) {
48
+ const e = new RegExp(
49
+ l(t.toLowerCase(), i),
49
50
  "gi"
50
51
  );
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;
52
+ return Array.isArray(n) ? n.filter((o) => {
53
+ const a = r ? r(o) : o;
54
+ return l(a).match(e);
55
+ }) : l(n).match(e) ? n : null;
55
56
  }
56
- function y(t, r, n) {
57
- return Intl.DateTimeFormat(r, n).format(t);
57
+ function T(t, n, r) {
58
+ return Intl.DateTimeFormat(n, r).format(t);
58
59
  }
59
- const a = {
60
+ const y = {
60
61
  NUMBER: {
61
62
  style: "decimal",
62
63
  maximumFractionDigits: 1,
@@ -78,38 +79,88 @@ const a = {
78
79
  day: "2-digit"
79
80
  }
80
81
  };
81
- function f(t, r, n) {
82
- return Intl.NumberFormat(r, n).format(t);
82
+ function D(t, n, r) {
83
+ return Intl.NumberFormat(n, r).format(t);
83
84
  }
84
- function R(t, r, n = {}) {
85
- return f(t, r, { ...a.NUMBER, ...n });
85
+ function b(t, n, r = {}) {
86
+ return D(t, n, { ...y.NUMBER, ...r });
86
87
  }
87
- function F(t, r, n = {}) {
88
- return f(t, r, { ...a.CURRENCY, ...n });
88
+ function M(t, n, r = {}) {
89
+ return D(t, n, { ...y.CURRENCY, ...r });
89
90
  }
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) => {
91
+ const A = /:+[a-z]+/gi;
92
+ function k(t, n = []) {
93
+ let r = t;
94
+ const i = r.match(A) || [], e = Array.isArray(n);
95
+ return i.forEach((o, a) => {
95
96
  let c;
96
- if (o)
97
- c = r[s];
97
+ if (e)
98
+ c = n[a];
98
99
  else {
99
- const g = i.substring(1);
100
- c = r[g];
100
+ const u = o.substring(1);
101
+ c = n[u];
101
102
  }
102
- n = n.replace(i, c?.toString());
103
- }), n;
103
+ r = r.replace(o, c?.toString());
104
+ }), r;
105
+ }
106
+ const g = 63710088e-1;
107
+ function E(t) {
108
+ return R(
109
+ t,
110
+ function(n, r) {
111
+ return n + I(r);
112
+ },
113
+ 0
114
+ );
115
+ }
116
+ function I(t) {
117
+ let n = 0, r;
118
+ switch (t.type) {
119
+ case "Polygon":
120
+ return p(t.coordinates);
121
+ case "MultiPolygon":
122
+ for (r = 0; r < t.coordinates.length; r++)
123
+ n += p(t.coordinates[r]);
124
+ return n;
125
+ case "Point":
126
+ case "MultiPoint":
127
+ case "LineString":
128
+ case "MultiLineString":
129
+ return 0;
130
+ }
131
+ return 0;
132
+ }
133
+ function p(t) {
134
+ let n = 0;
135
+ if (t && t.length > 0) {
136
+ n += Math.abs(m(t[0]));
137
+ for (let r = 1; r < t.length; r++)
138
+ n -= Math.abs(m(t[r]));
139
+ }
140
+ return n;
141
+ }
142
+ function m(t) {
143
+ let n, r, i, e, o, a, c, u = 0;
144
+ const s = t.length;
145
+ if (s > 2) {
146
+ for (c = 0; c < s; c++)
147
+ c === s - 2 ? (e = s - 2, o = s - 1, a = 0) : c === s - 1 ? (e = s - 1, o = 0, a = 1) : (e = c, o = c + 1, a = c + 2), n = t[e], r = t[o], i = t[a], u += (f(i[0]) - f(n[0])) * Math.sin(f(r[1]));
148
+ u = u * g * g / 2;
149
+ }
150
+ return u;
151
+ }
152
+ function f(t) {
153
+ return t * Math.PI / 180;
104
154
  }
105
155
  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
156
+ y as CONFIG,
157
+ E as area,
158
+ M as formatCurrency,
159
+ T as formatDate,
160
+ b as formatNumber,
161
+ w as hexToRgbaDeckGL,
162
+ F as matchText,
163
+ l as normalize,
164
+ k as replaceRoute,
165
+ L as rgbaDeckGLToHex
115
166
  };
@@ -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>;
@@ -10,3 +10,5 @@ export { formatNumber, formatCurrency } from './formatters/number/format-number'
10
10
  export * from './formatters/constants';
11
11
  export * from './routes/replace-route';
12
12
  export * from './routes/types';
13
+ export { area } from './features/area';
14
+ export * from './features/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.4.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
  }