@carto/ps-utils 1.2.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 +1 -1
- package/dist/index.js +101 -34
- package/dist/types/clipboard/copy.d.ts +15 -0
- package/dist/types/colors/hex-to-gba-deckgl/hex-to-gba-deckgl.d.ts +1 -1
- package/dist/types/colors/rgba-deck-gl-to-hex/rgba-deck-gl-to-hex.d.ts +0 -2
- package/dist/types/features/area.d.ts +17 -0
- package/dist/types/features/types.d.ts +4 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/routes/replace-route.d.ts +23 -0
- package/dist/types/routes/types.d.ts +2 -0
- package/dist/types/strings/match-text/match-text.d.ts +0 -1
- package/dist/types/strings/normalize/normalize.d.ts +0 -1
- package/package.json +12 -6
- package/dist/types/clipboard/copy.test.d.ts +0 -1
- package/dist/types/colors/hex-to-gba-deckgl/hex-to-gba-deckgl.test.d.ts +0 -1
- package/dist/types/colors/rgba-deck-gl-to-hex/rgba-deck-gl-to-hex.test.d.ts +0 -1
- package/dist/types/formatters/date/format-date.test.d.ts +0 -1
- package/dist/types/formatters/number/format-number.test.d.ts +0 -1
- package/dist/types/strings/match-text/match-text.test.d.ts +0 -1
- package/dist/types/strings/normalize/normalize.test.d.ts +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function
|
|
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,4 +1,5 @@
|
|
|
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;
|
|
@@ -13,50 +14,50 @@ function f(t, n = 1) {
|
|
|
13
14
|
}
|
|
14
15
|
const r = t.length;
|
|
15
16
|
r < 6 && (t = t.slice(0, 6 - r).repeat(6 / r));
|
|
16
|
-
const
|
|
17
|
+
const i = parseInt(
|
|
17
18
|
t.length === 3 ? t.slice(0, 1).repeat(2) : t.slice(0, 2),
|
|
18
19
|
16
|
|
19
|
-
),
|
|
20
|
+
), e = parseInt(
|
|
20
21
|
t.length === 3 ? t.slice(1, 2).repeat(2) : t.slice(2, 4),
|
|
21
22
|
16
|
|
22
|
-
),
|
|
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,
|
|
27
|
+
return [i, e, o, n * 255];
|
|
27
28
|
}
|
|
28
|
-
function
|
|
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((
|
|
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
|
|
40
|
-
return `${n ? "#" : ""}${
|
|
40
|
+
const i = t.map((e) => e.toString(16).padStart(2, "0")).join("");
|
|
41
|
+
return `${n ? "#" : ""}${i}`;
|
|
41
42
|
}
|
|
42
|
-
function
|
|
43
|
-
const
|
|
44
|
-
return t.normalize(n).replace(
|
|
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
|
|
47
|
-
const
|
|
48
|
-
|
|
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(n) ? n.filter((
|
|
52
|
-
const a = r ? r(
|
|
53
|
-
return
|
|
54
|
-
}) :
|
|
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
|
|
57
|
+
function T(t, n, r) {
|
|
57
58
|
return Intl.DateTimeFormat(n, r).format(t);
|
|
58
59
|
}
|
|
59
|
-
const
|
|
60
|
+
const y = {
|
|
60
61
|
NUMBER: {
|
|
61
62
|
style: "decimal",
|
|
62
63
|
maximumFractionDigits: 1,
|
|
@@ -78,22 +79,88 @@ const c = {
|
|
|
78
79
|
day: "2-digit"
|
|
79
80
|
}
|
|
80
81
|
};
|
|
81
|
-
function
|
|
82
|
+
function D(t, n, r) {
|
|
82
83
|
return Intl.NumberFormat(n, r).format(t);
|
|
83
84
|
}
|
|
84
|
-
function
|
|
85
|
-
return
|
|
85
|
+
function b(t, n, r = {}) {
|
|
86
|
+
return D(t, n, { ...y.NUMBER, ...r });
|
|
87
|
+
}
|
|
88
|
+
function M(t, n, r = {}) {
|
|
89
|
+
return D(t, n, { ...y.CURRENCY, ...r });
|
|
90
|
+
}
|
|
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) => {
|
|
96
|
+
let c;
|
|
97
|
+
if (e)
|
|
98
|
+
c = n[a];
|
|
99
|
+
else {
|
|
100
|
+
const u = o.substring(1);
|
|
101
|
+
c = n[u];
|
|
102
|
+
}
|
|
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;
|
|
86
151
|
}
|
|
87
|
-
function
|
|
88
|
-
return
|
|
152
|
+
function f(t) {
|
|
153
|
+
return t * Math.PI / 180;
|
|
89
154
|
}
|
|
90
155
|
export {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
|
99
166
|
};
|
|
@@ -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;
|
|
@@ -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;
|
|
@@ -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>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -8,3 +8,7 @@ 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';
|
|
13
|
+
export { area } from './features/area';
|
|
14
|
+
export * from './features/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;
|
|
@@ -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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carto/ps-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "CARTO's Professional Service Utils library",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -17,16 +17,22 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
|
-
"
|
|
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
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"readmeFile": "./README.md"
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@turf/meta": ">= 6.x < 7.x"
|
|
30
30
|
},
|
|
31
|
-
"dependencies": {
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@turf/meta": "6.5.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@turf/helpers": "6.5.0",
|
|
36
|
+
"@types/geojson": "7946.0.11"
|
|
37
|
+
}
|
|
32
38
|
}
|
|
@@ -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 {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|