@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/radix.js CHANGED
@@ -1,59 +1,37 @@
1
- import { U16 as $16, U24 as $24, U32 as $32, U64HL, U8 as $8, } from "@thi.ng/hex";
1
+ import {
2
+ U16 as $16,
3
+ U24 as $24,
4
+ U32 as $32,
5
+ U64HL,
6
+ U8 as $8
7
+ } from "@thi.ng/hex";
2
8
  import { memoizeJ } from "@thi.ng/memoize/memoizej";
3
9
  import { repeat } from "./repeat.js";
4
- /**
5
- * Returns a {@link Stringer} which formats given numbers to `radix`, `len` and
6
- * with optional prefix (not included in `len`).
7
- *
8
- * @remarks
9
- * Only bases 2 - 36 are supported, due to native `Number.toString()`
10
- * limitations.
11
- *
12
- * @param radix -
13
- * @param len -
14
- * @param prefix -
15
- */
16
- export const radix = memoizeJ((radix, n, prefix = "") => {
10
+ const radix = memoizeJ(
11
+ (radix2, n, prefix = "") => {
17
12
  const buf = repeat("0", n);
18
13
  return (x) => {
19
- x = (x >>> 0).toString(radix);
20
- return prefix + (x.length < n ? buf.substring(x.length) + x : x);
14
+ x = (x >>> 0).toString(radix2);
15
+ return prefix + (x.length < n ? buf.substring(x.length) + x : x);
21
16
  };
22
- });
23
- /**
24
- * 8bit binary conversion preset.
25
- */
26
- export const B8 = radix(2, 8);
27
- /**
28
- * 16bit binary conversion preset.
29
- */
30
- export const B16 = radix(2, 16);
31
- /**
32
- * 32bit binary conversion preset.
33
- */
34
- export const B32 = radix(2, 32);
35
- /**
36
- * 8bit hex conversion preset.
37
- * Assumes unsigned inputs.
38
- */
39
- export const U8 = $8;
40
- /**
41
- * 16bit hex conversion preset.
42
- * Assumes unsigned inputs.
43
- */
44
- export const U16 = $16;
45
- /**
46
- * 24bit hex conversion preset.
47
- * Assumes unsigned inputs.
48
- */
49
- export const U24 = $24;
50
- /**
51
- * 32bit hex conversion preset.
52
- * Assumes unsigned inputs.
53
- */
54
- export const U32 = $32;
55
- /**
56
- * 64bit hex conversion preset (2x 32bit ints)
57
- * Assumes unsigned inputs.
58
- */
59
- export const U64 = U64HL;
17
+ }
18
+ );
19
+ const B8 = radix(2, 8);
20
+ const B16 = radix(2, 16);
21
+ const B32 = radix(2, 32);
22
+ const U8 = $8;
23
+ const U16 = $16;
24
+ const U24 = $24;
25
+ const U32 = $32;
26
+ const U64 = U64HL;
27
+ export {
28
+ B16,
29
+ B32,
30
+ B8,
31
+ U16,
32
+ U24,
33
+ U32,
34
+ U64,
35
+ U8,
36
+ radix
37
+ };
package/range.js CHANGED
@@ -1,21 +1,16 @@
1
- /**
2
- * Yields iterator of characters [`from`..`to`] (inclusive). Uses
3
- * reverse ordering if `to` < `from`.
4
- *
5
- * @param from -
6
- * @param to -
7
- */
8
- export function* charRange(from, to) {
9
- let i = typeof from === "string" ? from.charCodeAt(0) : from;
10
- const end = typeof to === "string" ? to.charCodeAt(0) : to;
11
- if (i <= end) {
12
- for (; i <= end; i++) {
13
- yield String.fromCharCode(i);
14
- }
1
+ function* charRange(from, to) {
2
+ let i = typeof from === "string" ? from.charCodeAt(0) : from;
3
+ const end = typeof to === "string" ? to.charCodeAt(0) : to;
4
+ if (i <= end) {
5
+ for (; i <= end; i++) {
6
+ yield String.fromCharCode(i);
15
7
  }
16
- else {
17
- for (; i >= end; i--) {
18
- yield String.fromCharCode(i);
19
- }
8
+ } else {
9
+ for (; i >= end; i--) {
10
+ yield String.fromCharCode(i);
20
11
  }
12
+ }
21
13
  }
14
+ export {
15
+ charRange
16
+ };
package/repeat.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { memoizeJ } from "@thi.ng/memoize/memoizej";
2
- /**
3
- * @param ch - character
4
- * @param n - repeat count
5
- */
6
- export const repeat = memoizeJ((ch, n) => ch.repeat(n));
2
+ const repeat = memoizeJ(
3
+ (ch, n) => ch.repeat(n)
4
+ );
5
+ export {
6
+ repeat
7
+ };
package/ruler.js CHANGED
@@ -1,65 +1,14 @@
1
1
  import { repeat } from "./repeat.js";
2
- /**
3
- * Returns a ruler-like string of given `width`, using `a` character for major
4
- * ticks and `b` for minor ticks.
5
- *
6
- * @example
7
- * ```ts
8
- * console.log(ruler(40))
9
- * // |''''|''''|''''|''''|''''|''''|''''|''''
10
- *
11
- * console.log(ruler(40, 8, "!", "."))
12
- * // !.......!.......!.......!.......!.......
13
- * ```
14
- *
15
- * @param width -
16
- * @param major -
17
- * @param a -
18
- * @param b -
19
- */
20
- export const ruler = (width, major = 5, a = "|", b = "'") => repeat(a + repeat(b, major - 1), Math.ceil(width / major)).substring(0, width);
21
- /**
22
- * Returns a grid of given `cols` x `rows` as string, each cell of size `w` x
23
- * `h`. The optional `chars` can be used to customize the grid.
24
- *
25
- * @example
26
- * ```ts
27
- * console.log(grid(3, 3, 4, 2));
28
- * // +---+---+---+
29
- * // | | | |
30
- * // +---+---+---+
31
- * // | | | |
32
- * // +---+---+---+
33
- * // | | | |
34
- * // +---+---+---+
35
- *
36
- * console.log(grid(3, 3, 4, 2, "*_/"));
37
- * // *___*___*___*
38
- * // / / / /
39
- * // *___*___*___*
40
- * // / / / /
41
- * // *___*___*___*
42
- * // / / / /
43
- * // *___*___*___*
44
- *
45
- * console.log(grid(3, 2, 3, 3, "+ #"));
46
- * // + + +
47
- * // ## ##
48
- * // ## ##
49
- * // + + +
50
- * // ## ##
51
- * // ## ##
52
- * // + + +
53
- * ```
54
- *
55
- * @param cols -
56
- * @param rows -
57
- * @param w -
58
- * @param h -
59
- * @param chars -
60
- */
61
- export const grid = (cols, rows, w, h, [a, b, c, d] = "+-| ") => {
62
- const major = ruler(cols * w, w, a, b) + a + "\n";
63
- const minor = ruler(cols * w, w, c, d) + c + "\n";
64
- return repeat(major + repeat(minor, h - 1), rows) + major;
2
+ const ruler = (width, major = 5, a = "|", b = "'") => repeat(a + repeat(b, major - 1), Math.ceil(width / major)).substring(
3
+ 0,
4
+ width
5
+ );
6
+ const grid = (cols, rows, w, h, [a, b, c, d] = "+-| ") => {
7
+ const major = ruler(cols * w, w, a, b) + a + "\n";
8
+ const minor = ruler(cols * w, w, c, d) + c + "\n";
9
+ return repeat(major + repeat(minor, h - 1), rows) + major;
10
+ };
11
+ export {
12
+ grid,
13
+ ruler
65
14
  };
package/slugify.js CHANGED
@@ -1,52 +1,16 @@
1
- const src = "àáäâãåèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;";
1
+ const src = "\xE0\xE1\xE4\xE2\xE3\xE5\xE8\xE9\xEB\xEA\xEC\xED\xEF\xEE\xF2\xF3\xF6\xF4\xF9\xFA\xFC\xFB\xF1\xE7\xDF\xFF\u0153\xE6\u0155\u015B\u0144\u1E55\u1E83\u01F5\u01F9\u1E3F\u01D8\u1E8D\u017A\u1E27\xB7/_,:;";
2
2
  const dest = "aaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------";
3
3
  const re = new RegExp(src.split("").join("|"), "g");
4
- /**
5
- * Based on:
6
- * https://medium.com/@matthagemann/the-ultimate-way-to-slugify-a-url-string-in-javascript-b8e4a0d849e1
7
- *
8
- * @example
9
- * ```ts
10
- * slugify("Me, myself (& ëye) 😀!")
11
- * // "me-myself-and-eye"
12
- * ```
13
- *
14
- * @param str -
15
- */
16
- export const slugify = (str) => {
17
- return (str
18
- .toLowerCase()
19
- .replace(/\s+/g, "-")
20
- .replace(re, (c) => dest[src.indexOf(c)])
21
- .replace(/&+/g, "-and-")
22
- .replace(/[^\w\-]+/g, "")
23
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes
24
- .replace(/\p{Emoji_Presentation}/gu, "")
25
- .replace(/\-{2,}/g, "-")
26
- .replace(/(^-+)|(-+$)/g, ""));
4
+ const slugify = (str) => {
5
+ return str.toLowerCase().replace(/\s+/g, "-").replace(re, (c) => dest[src.indexOf(c)]).replace(/&+/g, "-and-").replace(/[^\w\-]+/g, "").replace(/\p{Emoji_Presentation}/gu, "").replace(/\-{2,}/g, "-").replace(/(^-+)|(-+$)/g, "");
27
6
  };
28
- /**
29
- * Similar to {@link slugify}, however uses GitHub's anchor naming rules
30
- * for headings in markdown files (AFAICT).
31
- *
32
- * @example
33
- * ```ts
34
- * slugifyGH("Me, myself (& ëye) 😀!")
35
- * // "me-myself--ëye-"
36
- * ```
37
- *
38
- * @param str -
39
- */
40
- export const slugifyGH = (str) => {
41
- return (str
42
- .toLowerCase()
43
- // remove all punctuations:
44
- // - ascii
45
- // - https://www.unicode.org/charts/PDF/U2000.pdf (general)
46
- // - https://www.unicode.org/charts/PDF/U2700.pdf (dingbats)
47
- // - https://www.unicode.org/charts/PDF/U2E00.pdf (supplemental)
48
- .replace(/[!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~\u0000-\u001f\u2000-\u206f\u2700-\u27bf\u2e00-\u2e7f]/g, "")
49
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes
50
- .replace(/\p{Emoji_Presentation}/gu, "")
51
- .replace(/\s/g, "-"));
7
+ const slugifyGH = (str) => {
8
+ return str.toLowerCase().replace(
9
+ /[!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~\u0000-\u001f\u2000-\u206f\u2700-\u27bf\u2e00-\u2e7f]/g,
10
+ ""
11
+ ).replace(/\p{Emoji_Presentation}/gu, "").replace(/\s/g, "-");
12
+ };
13
+ export {
14
+ slugify,
15
+ slugifyGH
52
16
  };
package/splice.js CHANGED
@@ -1,31 +1,17 @@
1
1
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
2
- /**
3
- * Forms a new strings which inserts given `insert` string into `src`
4
- * string at `from` position and appends remaining `src` chars from
5
- * original `to` position. If `from` and `to` are equal (`to` by default
6
- * is), the operation is a pure insertion. If not, then some chars from
7
- * `src` will be removed in the new string. If either position is
8
- * negative, it'll be considered relative to the end of the `src`.
9
- *
10
- * @param src -
11
- * @param insert -
12
- * @param from -
13
- * @param to -
14
- */
15
- export const splice = (src, insert, from, to = from) => {
16
- if (from < 0) {
17
- from += src.length;
18
- }
19
- if (to < 0) {
20
- to += src.length;
21
- }
22
- if (from > to) {
23
- illegalArgs("'from' index must be <= 'to'");
24
- }
25
- to = Math.max(to, 0);
26
- return from <= 0
27
- ? insert + src.substring(to)
28
- : from >= src.length
29
- ? src + insert
30
- : src.substring(0, from) + insert + src.substring(to);
2
+ const splice = (src, insert, from, to = from) => {
3
+ if (from < 0) {
4
+ from += src.length;
5
+ }
6
+ if (to < 0) {
7
+ to += src.length;
8
+ }
9
+ if (from > to) {
10
+ illegalArgs("'from' index must be <= 'to'");
11
+ }
12
+ to = Math.max(to, 0);
13
+ return from <= 0 ? insert + src.substring(to) : from >= src.length ? src + insert : src.substring(0, from) + insert + src.substring(to);
14
+ };
15
+ export {
16
+ splice
31
17
  };
package/split.d.ts CHANGED
@@ -8,6 +8,9 @@
8
8
  * This function is ~2x faster for large strings (benchmarked with 8MB & 16MB
9
9
  * inputs), with dramatically lower memory consumption.
10
10
  *
11
+ * If given an regexp as `delim`, the function ensures the regexp has its global
12
+ * flag enabled.
13
+ *
11
14
  * @param src -
12
15
  * @param delim -
13
16
  * @param includeDelim -
package/split.js CHANGED
@@ -1,30 +1,26 @@
1
- /**
2
- * Iterator version of `String.prototype.split()`. Yields iterable of substrings
3
- * of `src`, delimited by given regexp (default: unix & windows linebreaks).
4
- *
5
- * @remarks
6
- * Use this function to avoid creating an entire full copy of the original
7
- * source string, e.g. when only single-token or line-based accesses are needed.
8
- * This function is ~2x faster for large strings (benchmarked with 8MB & 16MB
9
- * inputs), with dramatically lower memory consumption.
10
- *
11
- * @param src -
12
- * @param delim -
13
- * @param includeDelim -
14
- */
15
- export function* split(src, delim = /\r?\n/g, includeDelim = false) {
16
- let i = 0;
17
- const n = src.length;
18
- const include = ~~includeDelim;
19
- const re = typeof delim === "string" ? new RegExp(delim, "g") : delim;
20
- for (; i < n;) {
21
- const m = re.exec(src);
22
- if (!m) {
23
- yield src.substring(i);
24
- return;
25
- }
26
- const len = m[0].length;
27
- yield src.substring(i, m.index + include * len);
28
- i = m.index + len;
1
+ function* split(src, delim = /\r?\n/g, includeDelim = false) {
2
+ let i = 0;
3
+ const n = src.length;
4
+ const include = ~~includeDelim;
5
+ let re;
6
+ if (typeof delim === "string") {
7
+ re = new RegExp(delim, "g");
8
+ } else if (!delim.flags.includes("g")) {
9
+ re = new RegExp(delim, delim.flags + "g");
10
+ } else {
11
+ re = delim;
12
+ }
13
+ for (; i < n; ) {
14
+ const m = re.exec(src);
15
+ if (!m) {
16
+ yield src.substring(i);
17
+ return;
29
18
  }
19
+ const len = m[0].length;
20
+ yield src.substring(i, m.index + include * len);
21
+ i = m.index + len;
22
+ }
30
23
  }
24
+ export {
25
+ split
26
+ };
package/stringify.js CHANGED
@@ -1,12 +1,4 @@
1
- /**
2
- * Higher order version of `JSON.stringify()` with the option to treat strings
3
- * and numbers differently. If `all` is `false` (default), strings and numbers
4
- * are simply converted using `String(x)`. If `indent` is given, it will be used
5
- * for `JSON.stringify(x, null, indent)`
6
- *
7
- * @param all -
8
- * @param indent -
9
- */
10
- export const stringify = (all = false, indent) => (x) => all || (typeof x !== "string" && typeof x !== "number")
11
- ? JSON.stringify(x, null, indent)
12
- : String(x);
1
+ const stringify = (all = false, indent) => (x) => all || typeof x !== "string" && typeof x !== "number" ? JSON.stringify(x, null, indent) : String(x);
2
+ export {
3
+ stringify
4
+ };
package/tabs.js CHANGED
@@ -1,87 +1,46 @@
1
1
  import { repeat } from "./repeat.js";
2
2
  const nextTab = (x, tabSize) => Math.floor((x + tabSize) / tabSize) * tabSize;
3
- /**
4
- * Multi-line version of {@link tabsToSpacesLine}.
5
- *
6
- * @example
7
- * ```ts
8
- * console.log(
9
- * tabsToSpaces("0\t1\t2", 10)
10
- * + "\n" +
11
- * tabsToSpaces("0\t45\t890\t\t6\t0")
12
- * + "\n" +
13
- * tabsToSpaces("^\t^\t^\t^\t^\t^")
14
- * );
15
- * // 0 1 2
16
- * // 0 45 890 6 0
17
- * // ^ ^ ^ ^ ^ ^
18
- * ```
19
- *
20
- * @param src -
21
- * @param tabSize -
22
- */
23
- export const tabsToSpaces = (src, tabSize = 4) => src
24
- .split(/\r?\n/g)
25
- .map((line) => tabsToSpacesLine(line, tabSize))
26
- .join("\n");
27
- /**
28
- * Takes a single line string and converts all tab characters to spaces, using
29
- * given `tabSize`.
30
- *
31
- * @param line -
32
- * @param tabSize -
33
- */
34
- export const tabsToSpacesLine = (line, tabSize = 4) => {
35
- let res = "";
36
- let words = line.split(/\t/g);
37
- let n = words.length - 1;
38
- for (let i = 0; i < n; i++) {
39
- const w = words[i];
40
- res += w;
41
- res += repeat(" ", nextTab(res.length, tabSize) - res.length);
42
- }
43
- res += words[n];
44
- return res;
3
+ const tabsToSpaces = (src, tabSize = 4) => src.split(/\r?\n/g).map((line) => tabsToSpacesLine(line, tabSize)).join("\n");
4
+ const tabsToSpacesLine = (line, tabSize = 4) => {
5
+ let res = "";
6
+ let words = line.split(/\t/g);
7
+ let n = words.length - 1;
8
+ for (let i = 0; i < n; i++) {
9
+ const w = words[i];
10
+ res += w;
11
+ res += repeat(" ", nextTab(res.length, tabSize) - res.length);
12
+ }
13
+ res += words[n];
14
+ return res;
45
15
  };
46
- /**
47
- * Multi-line version of {@link spacesToTabsLine}.
48
- *
49
- * @param src -
50
- * @param tabSize -
51
- */
52
- export const spacesToTabs = (src, tabSize = 4) => src
53
- .split(/\r?\n/g)
54
- .map((line) => spacesToTabsLine(line, tabSize))
55
- .join("\n");
56
- /**
57
- * Takes a single line string and converts all tab characters to spaces, using
58
- * given `tabSize`. Inverse op of {@link tabsToSpacesLine}.
59
- *
60
- * @param line -
61
- * @param tabSize -
62
- */
63
- export const spacesToTabsLine = (line, tabSize = 4) => {
64
- const re = /\s{2,}/g;
65
- let i = 0;
66
- let res = "";
67
- let m;
68
- while ((m = re.exec(line))) {
69
- const numSpaces = m[0].length;
70
- res += line.substring(i, m.index);
71
- i = m.index;
72
- const end = m.index + numSpaces;
73
- while (i < end) {
74
- const j = nextTab(i, tabSize);
75
- if (j <= end) {
76
- res += "\t";
77
- i = j;
78
- }
79
- else {
80
- res += repeat(" ", end - i);
81
- i = end;
82
- }
83
- }
16
+ const spacesToTabs = (src, tabSize = 4) => src.split(/\r?\n/g).map((line) => spacesToTabsLine(line, tabSize)).join("\n");
17
+ const spacesToTabsLine = (line, tabSize = 4) => {
18
+ const re = /\s{2,}/g;
19
+ let i = 0;
20
+ let res = "";
21
+ let m;
22
+ while (m = re.exec(line)) {
23
+ const numSpaces = m[0].length;
24
+ res += line.substring(i, m.index);
25
+ i = m.index;
26
+ const end = m.index + numSpaces;
27
+ while (i < end) {
28
+ const j = nextTab(i, tabSize);
29
+ if (j <= end) {
30
+ res += " ";
31
+ i = j;
32
+ } else {
33
+ res += repeat(" ", end - i);
84
34
  i = end;
35
+ }
85
36
  }
86
- return res + line.substring(i);
37
+ i = end;
38
+ }
39
+ return res + line.substring(i);
40
+ };
41
+ export {
42
+ spacesToTabs,
43
+ spacesToTabsLine,
44
+ tabsToSpaces,
45
+ tabsToSpacesLine
87
46
  };
package/trim.js CHANGED
@@ -1,24 +1,9 @@
1
1
  import { memoize1 } from "@thi.ng/memoize/memoize1";
2
- /**
3
- * Higher order trim function (both sides) with support for user defined
4
- * trimmable characters (default: whitespace only).
5
- *
6
- * @example
7
- * ```ts
8
- * trim()(" Hello ")
9
- * // "Hello"
10
- *
11
- * trim(" -+")("-+-+- Hello -+-+-")
12
- * // "Hello"
13
- * ```
14
- *
15
- * @param chars -
16
- */
17
- export const trim = memoize1((chars = " \t\n\r") => {
18
- chars = `(${chars
19
- .split("")
20
- .map((x) => `\\${x}`)
21
- .join("|")})`;
22
- const re = new RegExp(`(^${chars}+)|(${chars}+$)`, "g");
23
- return (x) => x.replace(re, "");
2
+ const trim = memoize1((chars = " \n\r") => {
3
+ chars = `(${chars.split("").map((x) => `\\${x}`).join("|")})`;
4
+ const re = new RegExp(`(^${chars}+)|(${chars}+$)`, "g");
5
+ return (x) => x.replace(re, "");
24
6
  });
7
+ export {
8
+ trim
9
+ };
package/truncate-left.js CHANGED
@@ -1,4 +1,7 @@
1
1
  import { memoizeJ } from "@thi.ng/memoize/memoizej";
2
- export const truncateLeft = memoizeJ((n, prefix = "") => (x) => x.length > n
3
- ? prefix + x.substring(x.length - n + prefix.length)
4
- : x);
2
+ const truncateLeft = memoizeJ(
3
+ (n, prefix = "") => (x) => x.length > n ? prefix + x.substring(x.length - n + prefix.length) : x
4
+ );
5
+ export {
6
+ truncateLeft
7
+ };
package/truncate.js CHANGED
@@ -1,6 +1,9 @@
1
1
  import { memoizeJ } from "@thi.ng/memoize/memoizej";
2
- export const truncate = memoizeJ((n, suffix = "") => (x) => x.length > n ? x.substring(0, n - suffix.length) + suffix : x);
3
- /**
4
- * Alias for {@link truncate}
5
- */
6
- export const truncateRight = truncate;
2
+ const truncate = memoizeJ(
3
+ (n, suffix = "") => (x) => x.length > n ? x.substring(0, n - suffix.length) + suffix : x
4
+ );
5
+ const truncateRight = truncate;
6
+ export {
7
+ truncate,
8
+ truncateRight
9
+ };