@carto/ps-utils 0.1.0-alpha.0 → 0.1.0-alpha.3

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.
@@ -23,4 +23,34 @@ function hexToRgbaDeckGL(hexadecimal, alpha = 1) {
23
23
  const b = parseInt(hexadecimal.length === 3 ? hexadecimal.slice(2, 3).repeat(2) : hexadecimal.slice(4, 6), 16);
24
24
  return [r, g, b, alpha * 255];
25
25
  }
26
- export { hexToRgbaDeckGL };
26
+ function rgbaDeckGLToHex(rgba, { withPrefix = true } = {}) {
27
+ if (!rgba || !rgba.length) {
28
+ console.warn(`rgbaDeckGLToHex: invalid array or empty values`);
29
+ return;
30
+ }
31
+ const hasInvalidValue = rgba.some((value) => value < 0 || value > 255);
32
+ if (hasInvalidValue) {
33
+ console.warn(`rgbaDeckGLToHex: [${rgba.toString()}] invalid array value format`);
34
+ return;
35
+ }
36
+ const hexadecimal = rgba.map((value) => {
37
+ const hex = value.toString(16);
38
+ return hex.padStart(2, "0");
39
+ }).join("");
40
+ return `${withPrefix ? "#" : ""}${hexadecimal}`;
41
+ }
42
+ function normalize(data, { format = "NFD", replaceUnicode = "[\u0300-\u036F]" } = {}) {
43
+ const unicode = new RegExp(replaceUnicode, "g");
44
+ return data.normalize(format).replace(unicode, "");
45
+ }
46
+ function matchText(text, data, { matchFn, normalizeOptions } = {}) {
47
+ const regex = new RegExp(normalize(text.toLowerCase(), normalizeOptions), "gi");
48
+ if (!Array.isArray(data)) {
49
+ return normalize(data).match(regex) ? data : null;
50
+ }
51
+ return data.filter((match) => {
52
+ const newData = matchFn ? matchFn(match) : match;
53
+ return normalize(newData).match(regex);
54
+ });
55
+ }
56
+ export { hexToRgbaDeckGL, matchText, normalize, rgbaDeckGLToHex };
@@ -1 +1 @@
1
- (function(n,s){typeof exports=="object"&&typeof module!="undefined"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(n=typeof globalThis!="undefined"?globalThis:n||self,s(n.PsUtils={}))})(this,function(n){"use strict";function s(t,o=1){if(!t){console.warn(`hexToRgbaDeckGL: ${t} invalid hexadecimal format`);return}if(o<0||o>1){console.warn(`hexToRgbaDeckGL: ${o} invalid alpha format`);return}if(t!=null&&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 e=t.length;e<6&&(t=t.slice(0,6-e).repeat(6/e));const r=parseInt(t.length===3?t.slice(0,1).repeat(2):t.slice(0,2),16),f=parseInt(t.length===3?t.slice(1,2).repeat(2):t.slice(2,4),16),u=parseInt(t.length===3?t.slice(2,3).repeat(2):t.slice(4,6),16);return[r,f,u,o*255]}n.hexToRgbaDeckGL=s,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
1
+ (function(t,i){typeof exports=="object"&&typeof module!="undefined"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(t=typeof globalThis!="undefined"?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
- * Function to parse hexadecimal string to deck.gl colors format.
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
+ };
@@ -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.0",
3
+ "version": "0.1.0-alpha.3",
4
4
  "description": "CARTO's Professional Service Utils library",
5
5
  "files": [
6
6
  "dist"