@thi.ng/strings 3.7.2 → 3.7.4

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:04Z
3
+ - **Last updated**: 2023-12-18T13:41:19Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,13 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ### [3.7.4](https://github.com/thi-ng/umbrella/tree/@thi.ng/strings@3.7.4) (2023-12-18)
13
+
14
+ #### 🩹 Bug fixes
15
+
16
+ - improve split() regexp handling ([65fe14b](https://github.com/thi-ng/umbrella/commit/65fe14b))
17
+ - ensure given regexp has global flag enabled
18
+
12
19
  ## [3.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/strings@3.7.0) (2023-11-24)
13
20
 
14
21
  #### 🚀 Features
package/README.md CHANGED
@@ -141,7 +141,7 @@ For Node.js REPL:
141
141
  const strings = await import("@thi.ng/strings");
142
142
  ```
143
143
 
144
- Package sizes (brotli'd, pre-treeshake): ESM: 5.41 KB
144
+ Package sizes (brotli'd, pre-treeshake): ESM: 5.43 KB
145
145
 
146
146
  ## Dependencies
147
147
 
package/ansi.js CHANGED
@@ -1,38 +1,13 @@
1
1
  const RE = /\x1b\[[0-9;]+m/g;
2
- /**
3
- * Removes all ANSI control sequences from given string.
4
- *
5
- * @example
6
- * ```ts
7
- * stripAnsi("\x1B[32mhello\x1B[0m \x1B[91mworld\x1B[0m!"");
8
- * // 'hello world!'
9
- * ```
10
- *
11
- * @param x -
12
- */
13
- export const stripAnsi = (x) => x.replace(RE, "");
14
- /**
15
- * Returns true iff `x` contains ANSI control sequences.
16
- *
17
- * @param x -
18
- */
19
- export const isAnsi = (x) => /\x1b\[[0-9;]+m/i.test(x);
20
- /**
21
- * Returns true iff `x` starts w/ an ANSI control sequence.
22
- *
23
- * @param x -
24
- */
25
- export const isAnsiStart = (x) => /^\x1b\[[0-9;]+m/i.test(x);
26
- /**
27
- * Returns true iff `x` ends w/ an ANSI control sequence.
28
- *
29
- * @param x -
30
- */
31
- export const isAnsiEnd = (x) => /\x1b\[[0-9;]+m$/i.test(x);
32
- /**
33
- * Returns length of `x` excluding any ANSI control sequences (via
34
- * {@link stripAnsi}).
35
- *
36
- * @param x -
37
- */
38
- export const lengthAnsi = (x) => stripAnsi(x).length;
2
+ const stripAnsi = (x) => x.replace(RE, "");
3
+ const isAnsi = (x) => /\x1b\[[0-9;]+m/i.test(x);
4
+ const isAnsiStart = (x) => /^\x1b\[[0-9;]+m/i.test(x);
5
+ const isAnsiEnd = (x) => /\x1b\[[0-9;]+m$/i.test(x);
6
+ const lengthAnsi = (x) => stripAnsi(x).length;
7
+ export {
8
+ isAnsi,
9
+ isAnsiEnd,
10
+ isAnsiStart,
11
+ lengthAnsi,
12
+ stripAnsi
13
+ };
package/api.js CHANGED
@@ -1,5 +1,4 @@
1
- /**
2
- * UTF-8 Byte Order Mark character
3
- * https://en.wikipedia.org/wiki/Byte_order_mark
4
- */
5
- export const BOM = "\ufeff";
1
+ const BOM = "\uFEFF";
2
+ export {
3
+ BOM
4
+ };
package/case.js CHANGED
@@ -1,56 +1,24 @@
1
- /**
2
- * Uppercase string formatter.
3
- *
4
- * @param x - string to transform
5
- */
6
- export const upper = (x) => x.toUpperCase();
7
- /**
8
- * Lowercase string formatter.
9
- *
10
- * @param x - string to transform
11
- */
12
- export const lower = (x) => x.toLowerCase();
13
- /**
14
- * String formatter which capitalizes first character.
15
- *
16
- * @param x - string to transform
17
- */
18
- export const capitalize = (x) => x.length ? x[0].toUpperCase() + x.substring(1) : x;
19
- /**
20
- * Converts a CamelCase string into kebab case, with optional custom
21
- * delimiter (`-` by default).
22
- *
23
- * @example
24
- * ```ts
25
- * kebab("FooBar23Baz");
26
- * // "foo-bar23-baz"
27
- * ```
28
- *
29
- * @param x -
30
- * @param delim -
31
- */
32
- export const kebab = (x, delim = "-") => lower(x.replace(
33
- // TC39
34
- // /(?<=[a-z0-9\u00e0-\u00fd])(?=[A-Z\u00c0-\u00dd])/g,
35
- // (_, i) => (i ? delim : "")
36
- /([a-z0-9\u00e0-\u00fd])([A-Z\u00c0-\u00dd])/g, (_, a, b) => a + delim + b));
37
- /**
38
- * Short for {@link kebab} using `_` as delimiter.
39
- *
40
- * @param x -
41
- */
42
- export const snake = (x) => kebab(x, "_");
43
- /**
44
- * Uppercase version of {@link snake}.
45
- *
46
- * @param x -
47
- */
48
- export const upperSnake = (x) => snake(x).toUpperCase();
49
- /**
50
- * Converts a kebab-case or snake_case string into CamelCase. Uses `-`
51
- * as default delimiter.
52
- *
53
- * @param x -
54
- * @param delim -
55
- */
56
- export const camel = (x, delim = "-") => lower(x).replace(new RegExp(`\\${delim}+(\\w)`, "g"), (_, c) => upper(c));
1
+ const upper = (x) => x.toUpperCase();
2
+ const lower = (x) => x.toLowerCase();
3
+ const capitalize = (x) => x.length ? x[0].toUpperCase() + x.substring(1) : x;
4
+ const kebab = (x, delim = "-") => lower(
5
+ x.replace(
6
+ // TC39
7
+ // /(?<=[a-z0-9\u00e0-\u00fd])(?=[A-Z\u00c0-\u00dd])/g,
8
+ // (_, i) => (i ? delim : "")
9
+ /([a-z0-9\u00e0-\u00fd])([A-Z\u00c0-\u00dd])/g,
10
+ (_, a, b) => a + delim + b
11
+ )
12
+ );
13
+ const snake = (x) => kebab(x, "_");
14
+ const upperSnake = (x) => snake(x).toUpperCase();
15
+ const camel = (x, delim = "-") => lower(x).replace(new RegExp(`\\${delim}+(\\w)`, "g"), (_, c) => upper(c));
16
+ export {
17
+ camel,
18
+ capitalize,
19
+ kebab,
20
+ lower,
21
+ snake,
22
+ upper,
23
+ upperSnake
24
+ };
package/center.js CHANGED
@@ -1,36 +1,16 @@
1
1
  import { memoizeJ } from "@thi.ng/memoize/memoizej";
2
2
  import { repeat } from "./repeat.js";
3
3
  import { truncate } from "./truncate.js";
4
- /**
5
- * Returns stringer which pads given input with `ch` (default: space) on
6
- * both sides and returns fixed width string of given `lineWidth`.
7
- * Returns string of only pad characters for any `null` or `undefined`
8
- * values. If the string version of an input is > `lineWidth`, no
9
- * centering is performed, but the string will be truncated to
10
- * `lineWidth`.
11
- *
12
- * Note: The padding string can contain multiple characters.
13
- *
14
- * @example
15
- * ```ts
16
- * center(20, "<>")(wrap(" ")("thi.ng"))
17
- * // "<><><> thi.ng <><><>"
18
- * ```
19
- *
20
- * @param lineWidth - target length
21
- * @param pad - pad character(s)
22
- */
23
- export const center = memoizeJ((n, pad = " ") => {
24
- const buf = repeat(String(pad), n);
25
- return (x) => {
26
- if (x == null)
27
- return buf;
28
- x = x.toString();
29
- const r = (n - x.length) / 2;
30
- return x.length < n
31
- ? buf.substring(0, r) +
32
- x +
33
- buf.substring(0, r + ((n & 1) === (x.length & 1) ? 0 : 1))
34
- : truncate(n)(x);
35
- };
4
+ const center = memoizeJ((n, pad = " ") => {
5
+ const buf = repeat(String(pad), n);
6
+ return (x) => {
7
+ if (x == null)
8
+ return buf;
9
+ x = x.toString();
10
+ const r = (n - x.length) / 2;
11
+ return x.length < n ? buf.substring(0, r) + x + buf.substring(0, r + ((n & 1) === (x.length & 1) ? 0 : 1)) : truncate(n)(x);
12
+ };
36
13
  });
14
+ export {
15
+ center
16
+ };
package/currency.js CHANGED
@@ -1,10 +1,18 @@
1
1
  import { memoizeJ } from "@thi.ng/memoize/memoizej";
2
- export const currency = memoizeJ((sym, pre = true, prec = 2) => {
3
- const ff = (x) => x.toFixed(prec);
4
- return pre ? (x) => sym + ff(x) : (x) => ff(x) + sym;
2
+ const currency = memoizeJ((sym, pre = true, prec = 2) => {
3
+ const ff = (x) => x.toFixed(prec);
4
+ return pre ? (x) => sym + ff(x) : (x) => ff(x) + sym;
5
5
  });
6
- export const chf = currency("CHF ");
7
- export const eur = currency("");
8
- export const gbp = currency("£");
9
- export const usd = currency("$");
10
- export const yen = currency("¥");
6
+ const chf = currency("CHF ");
7
+ const eur = currency("\u20AC");
8
+ const gbp = currency("\xA3");
9
+ const usd = currency("$");
10
+ const yen = currency("\xA5");
11
+ export {
12
+ chf,
13
+ currency,
14
+ eur,
15
+ gbp,
16
+ usd,
17
+ yen
18
+ };
package/cursor.js CHANGED
@@ -1,38 +1,17 @@
1
- /**
2
- * Takes a string, linear index position and optional line split delimiter (or
3
- * regexp, default: "\n"). Computes and returns position of index as cursor
4
- * coords tuple: `[line, column]`
5
- *
6
- * @remarks
7
- * By default the returned coords are 1-based, but can be configured via
8
- * optional `offset` arg (also in `[line,column]` order).
9
- *
10
- * @example
11
- * ```ts
12
- * computeCursorPos("thi.ng\numbrella", 10);
13
- * // [ 2, 4 ]
14
- *
15
- * // w/ custom offset
16
- * computeCursorPos("thi.ng\numbrella", 10, "\n", [11, 1]);
17
- * // [ 12, 4 ]
18
- * ```
19
- *
20
- * @param str -
21
- * @param pos -
22
- * @param delim -
23
- * @param offset -
24
- */
25
- export const computeCursorPos = (str, pos, delim = "\n", offset = [1, 1]) => {
26
- if (!str.length)
27
- return [1, 1];
28
- pos = Math.min(Math.max(0, pos), str.length);
29
- const lines = str.split(delim);
30
- const n = lines.length;
31
- for (let i = 0; i < n; i++) {
32
- const l = lines[i];
33
- if (pos <= l.length)
34
- return [i + offset[0], pos + offset[1]];
35
- pos -= l.length + 1;
36
- }
37
- return [n + offset[0] - 1, offset[1]];
1
+ const computeCursorPos = (str, pos, delim = "\n", offset = [1, 1]) => {
2
+ if (!str.length)
3
+ return [1, 1];
4
+ pos = Math.min(Math.max(0, pos), str.length);
5
+ const lines = str.split(delim);
6
+ const n = lines.length;
7
+ for (let i = 0; i < n; i++) {
8
+ const l = lines[i];
9
+ if (pos <= l.length)
10
+ return [i + offset[0], pos + offset[1]];
11
+ pos -= l.length + 1;
12
+ }
13
+ return [n + offset[0] - 1, offset[1]];
14
+ };
15
+ export {
16
+ computeCursorPos
38
17
  };
package/entities.js CHANGED
@@ -1,151 +1,138 @@
1
- /**
2
- * @remarks
3
- * References:
4
- * - https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
5
- * - https://www.w3.org/TR/xml-entity-names/byalpha.html
6
- */
7
- export const ENTITIES = {
8
- "&": "&amp;",
9
- "<": "&lt;",
10
- ">": "&gt;",
11
- "": "&le;",
12
- "": "&ge;",
13
- '"': "&quot;",
14
- "'": "&apos;",
15
- "": "&mdash;",
16
- "": "&ndash;",
17
- "": "&hellip;",
18
- "": "&vellip;",
19
- "⋯": "&ctdot;",
20
- "¢": "&cent;",
21
- "": "&euro;",
22
- "£": "&pound;",
23
- "¥": "&yen;",
24
- "": "&#8377;",
25
- 元: "&#20803;",
26
- "§": "&sect;",
27
- "¶": "&para;",
28
- "©": "&copy;",
29
- "®": "&reg;",
30
- "": "&trade;",
31
- "": "&#8451;",
32
- "": "&#8457;",
33
- K: "&#8490;",
34
- "": "&ltrif;",
35
- "": "&rtrif;",
36
- "": "&utrif;",
37
- "": "&dtrif;",
38
- "": "&larr;",
39
- "": "&rarr;",
40
- "": "&uarr;",
41
- "": "&darr;",
42
- "": "&crarr;",
43
- "": "&larrhk;",
44
- "": "&lArr;",
45
- "": "&rArr;",
46
- "": "&olarr;",
47
- "": "&orarr;",
48
- "¹": "&sup1;",
49
- "²": "&sup2;",
50
- "³": "&sup3;",
51
- "½": "&frac12;",
52
- "": "&frac13;",
53
- "⅔": "&frac23;",
54
- "¼": "&frac14;",
55
- "¾": "&frac34;",
56
- "⅕": "&frac15;",
57
- "⅙": "&frac16;",
58
- "⅛": "&frac18;",
59
- Ä: "&Auml;",
60
- Ë: "&Euml;",
61
- Ï: "&Iuml;",
62
- Ö: "&Ouml;",
63
- Ü: "&Uuml;",
64
- ä: "&auml;",
65
- ë: "&euml;",
66
- ï: "&iuml;",
67
- ö: "&ouml;",
68
- ü: "&uuml;",
69
- ß: "&szlig;",
70
- α: "&alpha;",
71
- β: "&beta;",
72
- γ: "&gamma;",
73
- δ: "&delta;",
74
- ε: "&epsi;",
75
- θ: "&theta;",
76
- λ: "&lambda;",
77
- μ: "&mu;",
78
- π: "&pi;",
79
- σ: "&sigma;",
80
- τ: "&tau;",
81
- φ: "&phi;",
82
- ω: "&omega;",
83
- Δ: "&Delta;",
84
- Ω: "&Omega;",
85
- "": "&infin;",
86
- "±": "&plusmn;",
87
- "": "&Cross;",
88
- "°": "&deg;",
89
- "": "&sum;",
90
- "": "&prod;",
91
- "": "&int;",
92
- "": "&radic;",
93
- "": "&angle;",
94
- "": "&and;",
95
- "": "&or;",
96
- "": "&cap;",
97
- "": "&cup;",
98
- "": "&sub;",
99
- "": "&sup;",
100
- "": "&isin;",
101
- "": "&notin;",
102
- "": "&empty;",
103
- "": "&oplus;",
104
- "": "&ominus;",
105
- "": "&otimes;",
106
- "⌖": "&target;",
107
- "☆": "&star;",
108
- "★": "&starf;",
109
- "✓": "&check;",
110
- "✗": "&cross;",
111
- "\xa0": "&nbsp;",
1
+ const ENTITIES = {
2
+ "&": "&amp;",
3
+ "<": "&lt;",
4
+ ">": "&gt;",
5
+ "\u2264": "&le;",
6
+ "\u2265": "&ge;",
7
+ '"': "&quot;",
8
+ "'": "&apos;",
9
+ "\u2014": "&mdash;",
10
+ "\u2013": "&ndash;",
11
+ "\u2026": "&hellip;",
12
+ "\u22EE": "&vellip;",
13
+ "\u22EF": "&ctdot;",
14
+ "\xA2": "&cent;",
15
+ "\u20AC": "&euro;",
16
+ "\xA3": "&pound;",
17
+ "\xA5": "&yen;",
18
+ "\u20B9": "&#8377;",
19
+ \u5143: "&#20803;",
20
+ "\xA7": "&sect;",
21
+ "\xB6": "&para;",
22
+ "\xA9": "&copy;",
23
+ "\xAE": "&reg;",
24
+ "\u2122": "&trade;",
25
+ "\u2103": "&#8451;",
26
+ "\u2109": "&#8457;",
27
+ K: "&#8490;",
28
+ "\u25C2": "&ltrif;",
29
+ "\u25B8": "&rtrif;",
30
+ "\u25B4": "&utrif;",
31
+ "\u25BE": "&dtrif;",
32
+ "\u2190": "&larr;",
33
+ "\u2192": "&rarr;",
34
+ "\u2191": "&uarr;",
35
+ "\u2193": "&darr;",
36
+ "\u21B5": "&crarr;",
37
+ "\u21A9": "&larrhk;",
38
+ "\u21D0": "&lArr;",
39
+ "\u21D2": "&rArr;",
40
+ "\u21BA": "&olarr;",
41
+ "\u21BB": "&orarr;",
42
+ "\xB9": "&sup1;",
43
+ "\xB2": "&sup2;",
44
+ "\xB3": "&sup3;",
45
+ "\xBD": "&frac12;",
46
+ "\u2153": "&frac13;",
47
+ "\u2154": "&frac23;",
48
+ "\xBC": "&frac14;",
49
+ "\xBE": "&frac34;",
50
+ "\u2155": "&frac15;",
51
+ "\u2159": "&frac16;",
52
+ "\u215B": "&frac18;",
53
+ \u00C4: "&Auml;",
54
+ \u00CB: "&Euml;",
55
+ \u00CF: "&Iuml;",
56
+ \u00D6: "&Ouml;",
57
+ \u00DC: "&Uuml;",
58
+ \u00E4: "&auml;",
59
+ \u00EB: "&euml;",
60
+ \u00EF: "&iuml;",
61
+ \u00F6: "&ouml;",
62
+ \u00FC: "&uuml;",
63
+ \u00DF: "&szlig;",
64
+ \u03B1: "&alpha;",
65
+ \u03B2: "&beta;",
66
+ \u03B3: "&gamma;",
67
+ \u03B4: "&delta;",
68
+ \u03B5: "&epsi;",
69
+ \u03B8: "&theta;",
70
+ \u03BB: "&lambda;",
71
+ \u03BC: "&mu;",
72
+ \u03C0: "&pi;",
73
+ \u03C3: "&sigma;",
74
+ \u03C4: "&tau;",
75
+ \u03C6: "&phi;",
76
+ \u03C9: "&omega;",
77
+ \u0394: "&Delta;",
78
+ \u03A9: "&Omega;",
79
+ "\u221E": "&infin;",
80
+ "\xB1": "&plusmn;",
81
+ "\u2A2F": "&Cross;",
82
+ "\xB0": "&deg;",
83
+ "\u2211": "&sum;",
84
+ "\u220F": "&prod;",
85
+ "\u222B": "&int;",
86
+ "\u221A": "&radic;",
87
+ "\u2220": "&angle;",
88
+ "\u2227": "&and;",
89
+ "\u2228": "&or;",
90
+ "\u2229": "&cap;",
91
+ "\u222A": "&cup;",
92
+ "\u2282": "&sub;",
93
+ "\u2283": "&sup;",
94
+ "\u2208": "&isin;",
95
+ "\u2209": "&notin;",
96
+ "\u2205": "&empty;",
97
+ "\u2295": "&oplus;",
98
+ "\u2296": "&ominus;",
99
+ "\u2297": "&otimes;",
100
+ "\u2316": "&target;",
101
+ "\u2606": "&star;",
102
+ "\u2605": "&starf;",
103
+ "\u2713": "&check;",
104
+ "\u2717": "&cross;",
105
+ "\xA0": "&nbsp;"
112
106
  };
113
- export const RE_ENTITIES = new RegExp(`[${Object.keys(ENTITIES).join("")}]`, "gu");
114
- export const ENTITIES_REV = Object.entries(ENTITIES).reduce((acc, [k, v]) => ((acc[v] = k), acc), {});
115
- export const RE_ENTITIES_REV = new RegExp(`(${Object.keys(ENTITIES_REV).join("|")})`, "g");
116
- export const RE_ENTITIES_NUM = /&#(x?)([0-9a-f]+);/gi;
117
- /**
118
- * Replaces all occurrences of character keys in {@link ENTITIES} with their
119
- * named HTML entities.
120
- *
121
- * @remarks
122
- * Only use this function when targetting HTML output. For XML/SVG etc. use
123
- * {@link escapeEntitiesNum}.
124
- *
125
- * @param src
126
- */
127
- export const escapeEntities = (src) => src.replace(RE_ENTITIES, (x) => ENTITIES[x]);
128
- /**
129
- * Similar to {@link escapeEntities}, but only uses _named_ entities for `&`,
130
- * `<`, `>`, `'`, `"` and numeric entities for all others.
131
- *
132
- * @remarks
133
- * This function is used as default by thi.ng/hiccup `serialize()` to escape
134
- * characters and ensure compatibility with XML (which by default only supports
135
- * named entities for the above 5 characters).
136
- *
137
- * @param src
138
- */
139
- export const escapeEntitiesNum = (src) => src.replace(RE_ENTITIES, (x) => {
140
- const code = x.charCodeAt(0);
141
- return code < 128 ? ENTITIES[x] : `&#x${code.toString(16)};`;
107
+ const RE_ENTITIES = new RegExp(
108
+ `[${Object.keys(ENTITIES).join("")}]`,
109
+ "gu"
110
+ );
111
+ const ENTITIES_REV = Object.entries(ENTITIES).reduce(
112
+ (acc, [k, v]) => (acc[v] = k, acc),
113
+ {}
114
+ );
115
+ const RE_ENTITIES_REV = new RegExp(
116
+ `(${Object.keys(ENTITIES_REV).join("|")})`,
117
+ "g"
118
+ );
119
+ const RE_ENTITIES_NUM = /&#(x?)([0-9a-f]+);/gi;
120
+ const escapeEntities = (src) => src.replace(RE_ENTITIES, (x) => ENTITIES[x]);
121
+ const escapeEntitiesNum = (src) => src.replace(RE_ENTITIES, (x) => {
122
+ const code = x.charCodeAt(0);
123
+ return code < 128 ? ENTITIES[x] : `&#x${code.toString(16)};`;
142
124
  });
143
- /**
144
- * Replace all known named and numeric entities with their original characters.
145
- * Opposite op of {@link escapeEntities} and {@link escapeEntitiesNum}.
146
- *
147
- * @param src
148
- */
149
- export const unescapeEntities = (src) => src
150
- .replace(RE_ENTITIES_REV, (x) => ENTITIES_REV[x])
151
- .replace(RE_ENTITIES_NUM, (_, hex, x) => String.fromCharCode(parseInt(x, hex ? 16 : 10)));
125
+ const unescapeEntities = (src) => src.replace(RE_ENTITIES_REV, (x) => ENTITIES_REV[x]).replace(
126
+ RE_ENTITIES_NUM,
127
+ (_, hex, x) => String.fromCharCode(parseInt(x, hex ? 16 : 10))
128
+ );
129
+ export {
130
+ ENTITIES,
131
+ ENTITIES_REV,
132
+ RE_ENTITIES,
133
+ RE_ENTITIES_NUM,
134
+ RE_ENTITIES_REV,
135
+ escapeEntities,
136
+ escapeEntitiesNum,
137
+ unescapeEntities
138
+ };