@carto/ps-utils 0.1.0-alpha.1 → 0.1.0-alpha.10
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/ps-utils.es.js +38 -17
- package/dist/ps-utils.umd.js +1 -1
- package/dist/types/colors/hexToRgbaDeckGL.d.ts +3 -1
- package/dist/types/colors/rgbaDeckGLToHex/rgbaDeckGLToHex.d.ts +30 -0
- package/dist/types/colors/rgbaDeckGLToHex/types.d.ts +9 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/strings/matchText/matchText.d.ts +13 -0
- package/dist/types/strings/matchText/types.d.ts +14 -0
- package/dist/types/strings/normalize/normalize.d.ts +19 -0
- package/dist/types/strings/normalize/types.d.ts +15 -0
- package/package.json +4 -3
package/dist/ps-utils.es.js
CHANGED
|
@@ -1,26 +1,47 @@
|
|
|
1
|
-
function
|
|
2
|
-
if (!
|
|
3
|
-
console.warn(`hexToRgbaDeckGL: ${
|
|
1
|
+
function f(n, r = 1) {
|
|
2
|
+
if (!n) {
|
|
3
|
+
console.warn(`hexToRgbaDeckGL: ${n} invalid hexadecimal format`);
|
|
4
4
|
return;
|
|
5
5
|
}
|
|
6
|
-
if (
|
|
7
|
-
console.warn(`hexToRgbaDeckGL: ${
|
|
6
|
+
if (r < 0 || r > 1) {
|
|
7
|
+
console.warn(`hexToRgbaDeckGL: ${r} invalid alpha format`);
|
|
8
8
|
return;
|
|
9
9
|
}
|
|
10
|
-
if (
|
|
11
|
-
|
|
10
|
+
if (n != null && n.includes("#") && (n = n.slice(1)), !/[0-9A-Fa-f]{6}/g.test(n) && !/[0-9A-Fa-f]{3}/g.test(n) && !/[0-9A-Fa-f]{2}/g.test(n)) {
|
|
11
|
+
console.warn(`hexToRgbaDeckGL: ${n} invalid hexadecimal format`);
|
|
12
|
+
return;
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
const t = n.length;
|
|
15
|
+
t < 6 && (n = n.slice(0, 6 - t).repeat(6 / t));
|
|
16
|
+
const e = parseInt(n.length === 3 ? n.slice(0, 1).repeat(2) : n.slice(0, 2), 16), o = parseInt(n.length === 3 ? n.slice(1, 2).repeat(2) : n.slice(2, 4), 16), s = parseInt(n.length === 3 ? n.slice(2, 3).repeat(2) : n.slice(4, 6), 16);
|
|
17
|
+
return [e, o, s, r * 255];
|
|
18
|
+
}
|
|
19
|
+
function g(n, { withPrefix: r = !0 } = {}) {
|
|
20
|
+
if (!n || !n.length) {
|
|
21
|
+
console.warn("rgbaDeckGLToHex: invalid array or empty values");
|
|
15
22
|
return;
|
|
16
23
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
24
|
+
if (n.some((o) => o < 0 || o > 255)) {
|
|
25
|
+
console.warn(`rgbaDeckGLToHex: [${n.toString()}] invalid array value format`);
|
|
26
|
+
return;
|
|
20
27
|
}
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
const e = n.map((o) => o.toString(16).padStart(2, "0")).join("");
|
|
29
|
+
return `${r ? "#" : ""}${e}`;
|
|
30
|
+
}
|
|
31
|
+
function u(n, { format: r = "NFD", replaceUnicode: t = "[\u0300-\u036F]" } = {}) {
|
|
32
|
+
const e = new RegExp(t, "g");
|
|
33
|
+
return n.normalize(r).replace(e, "");
|
|
34
|
+
}
|
|
35
|
+
function c(n, r, { matchFn: t, normalizeOptions: e } = {}) {
|
|
36
|
+
const o = new RegExp(u(n.toLowerCase(), e), "gi");
|
|
37
|
+
return Array.isArray(r) ? r.filter((s) => {
|
|
38
|
+
const i = t ? t(s) : s;
|
|
39
|
+
return u(i).match(o);
|
|
40
|
+
}) : u(r).match(o) ? r : null;
|
|
25
41
|
}
|
|
26
|
-
export {
|
|
42
|
+
export {
|
|
43
|
+
f as hexToRgbaDeckGL,
|
|
44
|
+
c as matchText,
|
|
45
|
+
u as normalize,
|
|
46
|
+
g as rgbaDeckGLToHex
|
|
47
|
+
};
|
package/dist/ps-utils.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(t,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(t=typeof globalThis<"u"?globalThis:t||self,i(t.PsUtils={}))})(this,function(t){"use strict";function i(n,e=1){if(!n){console.warn(`hexToRgbaDeckGL: ${n} invalid hexadecimal format`);return}if(e<0||e>1){console.warn(`hexToRgbaDeckGL: ${e} invalid alpha format`);return}if(n!=null&&n.includes("#")&&(n=n.slice(1)),!/[0-9A-Fa-f]{6}/g.test(n)&&!/[0-9A-Fa-f]{3}/g.test(n)&&!/[0-9A-Fa-f]{2}/g.test(n)){console.warn(`hexToRgbaDeckGL: ${n} invalid hexadecimal format`);return}const r=n.length;r<6&&(n=n.slice(0,6-r).repeat(6/r));const s=parseInt(n.length===3?n.slice(0,1).repeat(2):n.slice(0,2),16),o=parseInt(n.length===3?n.slice(1,2).repeat(2):n.slice(2,4),16),u=parseInt(n.length===3?n.slice(2,3).repeat(2):n.slice(4,6),16);return[s,o,u,e*255]}function c(n,{withPrefix:e=!0}={}){if(!n||!n.length){console.warn("rgbaDeckGLToHex: invalid array or empty values");return}if(n.some(o=>o<0||o>255)){console.warn(`rgbaDeckGLToHex: [${n.toString()}] invalid array value format`);return}const s=n.map(o=>o.toString(16).padStart(2,"0")).join("");return`${e?"#":""}${s}`}function f(n,{format:e="NFD",replaceUnicode:r="[\u0300-\u036F]"}={}){const s=new RegExp(r,"g");return n.normalize(e).replace(s,"")}function g(n,e,{matchFn:r,normalizeOptions:s}={}){const o=new RegExp(f(n.toLowerCase(),s),"gi");return Array.isArray(e)?e.filter(u=>{const l=r?r(u):u;return f(l).match(o)}):f(e).match(o)?e:null}t.hexToRgbaDeckGL=i,t.matchText=g,t.normalize=f,t.rgbaDeckGLToHex=c,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* It converts a hexadecimal color to an array of numbers that can be used by Deck.gl
|
|
3
3
|
*
|
|
4
4
|
* @param {string} hexadecimal - hexadecimal string, including # or not
|
|
5
5
|
* @param {number} alpha - opacity of final color, value must be between 0-1
|
|
6
6
|
*
|
|
7
|
+
* @returns An array of numbers.
|
|
8
|
+
*
|
|
7
9
|
* @example
|
|
8
10
|
*
|
|
9
11
|
* 6 digits hexadecimal:
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { RgbaDeckGLToHexOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* It converts an array of RGBA values to a hexadecimal string.
|
|
4
|
+
*
|
|
5
|
+
* @param {number[]} rgba - rgba array, including alpha or not
|
|
6
|
+
* @param {RgbaDeckGLToHexOptions} options - options to parse
|
|
7
|
+
*
|
|
8
|
+
* @returns A string
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* Without alpha value:
|
|
13
|
+
* ```ts
|
|
14
|
+
* rgbaDeckGLToHex([0, 0, 0]) // => '#000000'
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* With alpha value:
|
|
18
|
+
* ```ts
|
|
19
|
+
* rgbaDeckGLToHex([0, 0, 0, 1]) // => '#000000FF'
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Without prefix:
|
|
23
|
+
* ```ts
|
|
24
|
+
* rgbaDeckGLToHex([0, 0, 0], { withPrefix: false }) // => '000000'
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
*
|
|
28
|
+
* @alpha
|
|
29
|
+
*/
|
|
30
|
+
export declare function rgbaDeckGLToHex(rgba: number[], { withPrefix }?: RgbaDeckGLToHexOptions): string | undefined;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `RgbaDeckGLToHexOptions` is an object with an optional property `withPrefix` of type
|
|
3
|
+
* `boolean`.
|
|
4
|
+
* @property {boolean} withPrefix - boolean - Whether to include the '#' prefix in the hex
|
|
5
|
+
* string.
|
|
6
|
+
*/
|
|
7
|
+
export declare type RgbaDeckGLToHexOptions = {
|
|
8
|
+
withPrefix?: boolean;
|
|
9
|
+
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
1
|
export { hexToRgbaDeckGL } from './colors/hexToRgbaDeckGL';
|
|
2
|
+
export { rgbaDeckGLToHex } from './colors/rgbaDeckGLToHex/rgbaDeckGLToHex';
|
|
3
|
+
export * from './colors/rgbaDeckGLToHex/types';
|
|
4
|
+
export { matchText } from './strings/matchText/matchText';
|
|
5
|
+
export * from './strings/matchText/types';
|
|
6
|
+
export { normalize } from './strings/normalize/normalize';
|
|
7
|
+
export * from './strings/normalize/types';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MatchTextOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* It takes a string, an array of strings, and an optional object of options, and returns an
|
|
4
|
+
* array of strings that match the input string
|
|
5
|
+
* @param {string} text - The text to match against.
|
|
6
|
+
* @param {T[] | string} data - The data to match against.
|
|
7
|
+
* @param {MatchTextOptions} options - The options to use.
|
|
8
|
+
*
|
|
9
|
+
* @returns {T[] | string | null} - The matched data or null.
|
|
10
|
+
*
|
|
11
|
+
* @alpha
|
|
12
|
+
*/
|
|
13
|
+
export declare function matchText<T>(text: string, data: T[] | string, { matchFn, normalizeOptions }?: MatchTextOptions<T>): T[] | string | null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { NormalizeOptions } from '../normalize/types';
|
|
2
|
+
/**
|
|
3
|
+
* `MatchTextOptions` is an object with an optional `matchFn` property that is a function
|
|
4
|
+
* that takes a generic type `T` and returns a string, and an optional `normalizeOptions`
|
|
5
|
+
* property that is an object with the normalize options.
|
|
6
|
+
* @property matchFn - A function that takes in the data and returns a string to match
|
|
7
|
+
* against.
|
|
8
|
+
* @property {NormalizeOptions} normalizeOptions - This is an object that contains the
|
|
9
|
+
* options for the normalize function.
|
|
10
|
+
*/
|
|
11
|
+
export declare type MatchTextOptions<T> = {
|
|
12
|
+
matchFn?: (data: T) => string;
|
|
13
|
+
normalizeOptions?: NormalizeOptions;
|
|
14
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NormalizeOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* It takes a string and returns a string normalized.
|
|
4
|
+
* @param {string} data - The data to be normalized.
|
|
5
|
+
* @param {NormalizeOptions} options - data - The string to be normalized.
|
|
6
|
+
* @returns A string
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* The normalization by default is NFD
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* normalize('foo bar')
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @alpha
|
|
18
|
+
*/
|
|
19
|
+
export declare function normalize(data: string, { format, replaceUnicode }?: NormalizeOptions): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `NormalizeOptions` is an object with optional properties `format` and `replaceUnicode`.
|
|
3
|
+
*
|
|
4
|
+
* The `format` property is a string that can be one of four values: `'NFD'`, `'NFKD'`,
|
|
5
|
+
* `'NFC'`, or `'NFKC'`.
|
|
6
|
+
*
|
|
7
|
+
* The `replaceUnicode` property is a string.
|
|
8
|
+
* @property {'NFD' | 'NFKD' | 'NFC' | 'NFKC'} format - The normalization form to use.
|
|
9
|
+
* @property {string} replaceUnicode - The unicode character to replace the unicode
|
|
10
|
+
* characters with. By default is `[\u0300-\u036f]`.
|
|
11
|
+
*/
|
|
12
|
+
export declare type NormalizeOptions = {
|
|
13
|
+
format?: 'NFD' | 'NFKD' | 'NFC' | 'NFKC';
|
|
14
|
+
replaceUnicode?: string;
|
|
15
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carto/ps-utils",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.10",
|
|
4
4
|
"description": "CARTO's Professional Service Utils library",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -16,12 +16,13 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
|
-
"build": "vite build
|
|
19
|
+
"build": "vite build",
|
|
20
20
|
"lint": "eslint './**/*.{ts,tsx}' --ignore-path ../../.gitignore",
|
|
21
21
|
"format": "prettier --write . --ignore-path ../../.gitignore",
|
|
22
22
|
"docs:generate": "typedoc --options ../../typedoc.json src/index.ts",
|
|
23
23
|
"test:watch": "vitest",
|
|
24
|
-
"test:
|
|
24
|
+
"test:related": "vitest --run --passWithNoTests",
|
|
25
|
+
"test:coverage": "vitest run --coverage"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {}
|
|
27
28
|
}
|