@thi.ng/strings 3.7.2 → 3.7.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.
package/units.js CHANGED
@@ -1,81 +1,114 @@
1
1
  import { memoizeJ } from "@thi.ng/memoize/memoizej";
2
- export const units = memoizeJ((exp, base, prec = 2) => {
3
- const groups = exp
4
- .map((x) => [
5
- x[0],
6
- x[2] != null ? x[2] : prec,
7
- x[1],
8
- ])
9
- .sort((a, b) => a[0] - b[0]);
10
- return (x) => {
11
- if (x === 0) {
12
- return `0${base}`;
13
- }
14
- const absX = Math.abs(x);
15
- for (let i = groups.length; i-- > 0;) {
16
- const g = groups[i];
17
- if (absX >= g[0] || i === 0) {
18
- return (x / g[0]).toFixed(g[1]) + g[2];
19
- }
20
- }
21
- return "";
22
- };
2
+ const units = memoizeJ((exp, base, prec = 2) => {
3
+ const groups = exp.map(
4
+ (x) => [
5
+ x[0],
6
+ x[2] != null ? x[2] : prec,
7
+ x[1]
8
+ ]
9
+ ).sort((a, b) => a[0] - b[0]);
10
+ return (x) => {
11
+ if (x === 0) {
12
+ return `0${base}`;
13
+ }
14
+ const absX = Math.abs(x);
15
+ for (let i = groups.length; i-- > 0; ) {
16
+ const g = groups[i];
17
+ if (absX >= g[0] || i === 0) {
18
+ return (x / g[0]).toFixed(g[1]) + g[2];
19
+ }
20
+ }
21
+ return "";
22
+ };
23
23
  });
24
24
  const KB = 1024;
25
- export const bits = units([
25
+ const bits = units(
26
+ [
26
27
  [1, " bits", 0],
27
28
  [KB, " Kb"],
28
29
  [KB ** 2, " Mb"],
29
- [KB ** 3, " Gb"],
30
- ], " bits", 2);
31
- export const bytes = units([
30
+ [KB ** 3, " Gb"]
31
+ ],
32
+ " bits",
33
+ 2
34
+ );
35
+ const bytes = units(
36
+ [
32
37
  [1, " bytes", 0],
33
38
  [KB, " KB"],
34
39
  [KB ** 2, " MB"],
35
40
  [KB ** 3, " GB"],
36
41
  [KB ** 4, " TB"],
37
- [KB ** 5, " PB"],
38
- ], " bytes", 2);
39
- export const seconds = units([
42
+ [KB ** 5, " PB"]
43
+ ],
44
+ " bytes",
45
+ 2
46
+ );
47
+ const seconds = units(
48
+ [
40
49
  [1e-12, " ps"],
41
50
  [1e-9, " ns"],
42
- [1e-6, " µs"],
51
+ [1e-6, " \xB5s"],
43
52
  [1e-3, " ms"],
44
53
  [1, " secs"],
45
54
  [60, " mins"],
46
55
  [60 * 60, " hours"],
47
- [24 * 60 * 60, " days"],
48
- ], " secs", 3);
49
- export const meters = units([
56
+ [24 * 60 * 60, " days"]
57
+ ],
58
+ " secs",
59
+ 3
60
+ );
61
+ const meters = units(
62
+ [
50
63
  [1e-12, " pm"],
51
64
  [1e-9, " nm"],
52
- [1e-6, " µm"],
65
+ [1e-6, " \xB5m"],
53
66
  [1e-3, " mm"],
54
- [1e-2, " cm"],
67
+ [0.01, " cm"],
55
68
  [1, " m"],
56
- [1e3, " km"],
57
- ], " m", 2);
58
- export const grams = units([
69
+ [1e3, " km"]
70
+ ],
71
+ " m",
72
+ 2
73
+ );
74
+ const grams = units(
75
+ [
59
76
  [1e-12, " pg"],
60
77
  [1e-9, " ng"],
61
- [1e-6, " µg"],
78
+ [1e-6, " \xB5g"],
62
79
  [1e-3, " mg"],
63
80
  [1, " g"],
64
81
  [1e3, " kg"],
65
82
  [1e6, " t"],
66
83
  [1e9, " kt"],
67
- [1e12, " Mt"],
68
- ], " g", 2);
69
- export const unitless = units([
84
+ [1e12, " Mt"]
85
+ ],
86
+ " g",
87
+ 2
88
+ );
89
+ const unitless = units(
90
+ [
70
91
  [1e-15, "f", 1],
71
92
  [1e-12, "p", 1],
72
93
  [1e-9, "n", 1],
73
- [1e-6, "µ", 1],
94
+ [1e-6, "\xB5", 1],
74
95
  [1e-3, "m", 1],
75
96
  [1, ""],
76
97
  [1e3, "k", 1],
77
98
  [1e6, "M", 1],
78
99
  [1e9, "G", 1],
79
100
  [1e12, "T", 1],
80
- [1e15, "P", 1],
81
- ], "", 0);
101
+ [1e15, "P", 1]
102
+ ],
103
+ "",
104
+ 0
105
+ );
106
+ export {
107
+ bits,
108
+ bytes,
109
+ grams,
110
+ meters,
111
+ seconds,
112
+ unitless,
113
+ units
114
+ };
package/utf8.js CHANGED
@@ -1,140 +1,82 @@
1
1
  import { defError } from "@thi.ng/errors/deferror";
2
- /**
3
- * Returns the number of bytes required to encode the given string as UTF-8.
4
- *
5
- * @param str
6
- */
7
- export const utf8Length = (str) => {
8
- const n = str.length;
9
- let len = 0;
10
- for (let i = 0; i < n; ++i) {
11
- let u = str.charCodeAt(i);
12
- if (u >= 0xd800 && u < 0xe0000) {
13
- u = (0x10000 + ((u & 0x3ff) << 10)) | (str.charCodeAt(++i) & 0x3ff);
14
- }
15
- len +=
16
- u < 0x80
17
- ? 1
18
- : u < 0x800
19
- ? 2
20
- : u < 0x10000
21
- ? 3
22
- : u < 0x200000
23
- ? 4
24
- : u < 0x4000000
25
- ? 5
26
- : 6;
2
+ const utf8Length = (str) => {
3
+ const n = str.length;
4
+ let len = 0;
5
+ for (let i = 0; i < n; ++i) {
6
+ let u = str.charCodeAt(i);
7
+ if (u >= 55296 && u < 917504) {
8
+ u = 65536 + ((u & 1023) << 10) | str.charCodeAt(++i) & 1023;
27
9
  }
28
- return len;
10
+ len += u < 128 ? 1 : u < 2048 ? 2 : u < 65536 ? 3 : u < 2097152 ? 4 : u < 67108864 ? 5 : 6;
11
+ }
12
+ return len;
29
13
  };
30
- /**
31
- * Non-transducer version of
32
- * [`utf8Decode()`](https://docs.thi.ng/umbrella/transducers-binary/functions/utf8Decode.html).
33
- * Decodes `num` bytes from `start` index in given byte buffer. In Firefox this
34
- * is much faster than using the `TextDecoder` API.
35
- *
36
- * @param buf
37
- * @param start
38
- * @param num
39
- */
40
- export const utf8Decode = (buf, start, num) => {
41
- const end = start + num;
42
- let i = start;
43
- let result = "";
44
- let c;
45
- while (i < end) {
46
- c = buf[i++];
47
- if (c < 0x80) {
48
- result += String.fromCharCode(c);
49
- }
50
- else {
51
- if (c >= 0xc0 && c < 0xe0) {
52
- c = ((c & 0x1f) << 6) | (buf[i++] & 0x3f);
53
- }
54
- else if (c >= 0xe0 && c < 0xf0) {
55
- c =
56
- ((c & 0x0f) << 12) |
57
- ((buf[i++] & 0x3f) << 6) |
58
- (buf[i++] & 0x3f);
59
- }
60
- else if (c >= 0xf0 && c < 0xf8) {
61
- c =
62
- ((c & 7) << 18) |
63
- ((buf[i++] & 0x3f) << 12) |
64
- ((buf[i++] & 0x3f) << 6) |
65
- (buf[i++] & 0x3f);
66
- }
67
- else
68
- utf8Error();
69
- result += fromUtf8CodePoint(c);
70
- }
14
+ const utf8Decode = (buf, start, num) => {
15
+ const end = start + num;
16
+ let i = start;
17
+ let result = "";
18
+ let c;
19
+ while (i < end) {
20
+ c = buf[i++];
21
+ if (c < 128) {
22
+ result += String.fromCharCode(c);
23
+ } else {
24
+ if (c >= 192 && c < 224) {
25
+ c = (c & 31) << 6 | buf[i++] & 63;
26
+ } else if (c >= 224 && c < 240) {
27
+ c = (c & 15) << 12 | (buf[i++] & 63) << 6 | buf[i++] & 63;
28
+ } else if (c >= 240 && c < 248) {
29
+ c = (c & 7) << 18 | (buf[i++] & 63) << 12 | (buf[i++] & 63) << 6 | buf[i++] & 63;
30
+ } else
31
+ utf8Error();
32
+ result += fromUtf8CodePoint(c);
71
33
  }
72
- return result;
34
+ }
35
+ return result;
73
36
  };
74
- /**
75
- * Non-transducer version of
76
- * [`utf8Encode()`](https://docs.thi.ng/umbrella/transducers-binary/functions/utf8Encode.html).
77
- *
78
- * @remarks
79
- * If `capacity` is given, initializes the byte array to that size (and assumes
80
- * that it is sufficient to store the entire string, e.g. by using
81
- * {@link utf8Length} to pre-determine the number of bytes required for a given
82
- * string). If `capacity` is _not_ provided, the buffer will be initialized to
83
- * `4 * src.length`.
84
- *
85
- * Based on:
86
- * - https://github.com/thi-ng/umbrella/blob/main/packages/transducers-binary/src/utf8.ts
87
- * - https://gist.github.com/pascaldekloe/62546103a1576803dade9269ccf76330
88
- *
89
- * @param buf
90
- */
91
- export const utf8Encode = (src, capacity) => {
92
- const n = src.length;
93
- const buf = new Uint8Array(capacity || n << 2);
94
- let pos = 0;
95
- let c;
96
- for (let i = 0; i < n; i++) {
97
- c = src.charCodeAt(i);
98
- if (c < 0x80) {
99
- buf[pos++] = c;
100
- }
101
- else {
102
- if (c < 0x800) {
103
- buf[pos++] = 0xc0 | (c >> 6);
104
- }
105
- else {
106
- if (c >= 0xd800 && c < 0xdc00) {
107
- c =
108
- 0x10000 +
109
- ((c & 0x03ff) << 10) +
110
- (src.charCodeAt(++i) & 0x3ff);
111
- buf[pos++] = 0xf0 | (c >> 18);
112
- buf[pos++] = 0x80 | ((c >> 12) & 0x3f);
113
- }
114
- else
115
- buf[pos++] = 0xe0 | (c >> 12);
116
- buf[pos++] = 0x80 | ((c >> 6) & 0x3f);
117
- }
118
- buf[pos++] = 0x80 | (c & 0x3f);
119
- }
37
+ const utf8Encode = (src, capacity) => {
38
+ const n = src.length;
39
+ const buf = new Uint8Array(capacity || n << 2);
40
+ let pos = 0;
41
+ let c;
42
+ for (let i = 0; i < n; i++) {
43
+ c = src.charCodeAt(i);
44
+ if (c < 128) {
45
+ buf[pos++] = c;
46
+ } else {
47
+ if (c < 2048) {
48
+ buf[pos++] = 192 | c >> 6;
49
+ } else {
50
+ if (c >= 55296 && c < 56320) {
51
+ c = 65536 + ((c & 1023) << 10) + (src.charCodeAt(++i) & 1023);
52
+ buf[pos++] = 240 | c >> 18;
53
+ buf[pos++] = 128 | c >> 12 & 63;
54
+ } else
55
+ buf[pos++] = 224 | c >> 12;
56
+ buf[pos++] = 128 | c >> 6 & 63;
57
+ }
58
+ buf[pos++] = 128 | c & 63;
120
59
  }
121
- return buf.subarray(0, pos);
60
+ }
61
+ return buf.subarray(0, pos);
122
62
  };
123
- /**
124
- * Returns character string for given UTF-8 codepoint.
125
- *
126
- * @param x
127
- */
128
- export const fromUtf8CodePoint = (x) => {
129
- if (x < 0x10000)
130
- return String.fromCharCode(x);
131
- if (x < 0x110000) {
132
- x -= 0x10000;
133
- return String.fromCharCode(0xd800 | (x >>> 10), 0xdc00 | (x & 0x3ff));
134
- }
135
- return utf8Error(`invalid codepoint 0x${x.toString(16)}`);
63
+ const fromUtf8CodePoint = (x) => {
64
+ if (x < 65536)
65
+ return String.fromCharCode(x);
66
+ if (x < 1114112) {
67
+ x -= 65536;
68
+ return String.fromCharCode(55296 | x >>> 10, 56320 | x & 1023);
69
+ }
70
+ return utf8Error(`invalid codepoint 0x${x.toString(16)}`);
136
71
  };
137
- export const UTF8Error = defError(() => "UTF-8 error");
72
+ const UTF8Error = defError(() => "UTF-8 error");
138
73
  const utf8Error = (msg) => {
139
- throw new UTF8Error(msg);
74
+ throw new UTF8Error(msg);
75
+ };
76
+ export {
77
+ UTF8Error,
78
+ fromUtf8CodePoint,
79
+ utf8Decode,
80
+ utf8Encode,
81
+ utf8Length
140
82
  };
package/uuid.js CHANGED
@@ -1,15 +1,5 @@
1
1
  import { uuid as $uuid } from "@thi.ng/hex";
2
- /**
3
- * Same as [`uuid()`](https://docs.thi.ng/umbrella/hex/functions/uuid.html).
4
- * Returns UUID formatted string of given byte array from optional start index
5
- * `i` (default: 0). Array must have min. length
6
- * 16.
7
- *
8
- * @remarks
9
- * Use [`uuid()`](https://docs.thi.ng/umbrella/random/functions/uuid.html) to
10
- * also generate an UUID from scratch (rather than just format one).
11
- *
12
- * @param id -
13
- * @param i -
14
- */
15
- export const uuid = $uuid;
2
+ const uuid = $uuid;
3
+ export {
4
+ uuid
5
+ };
package/vector.js CHANGED
@@ -1,33 +1,30 @@
1
1
  import { memoizeJ } from "@thi.ng/memoize/memoizej";
2
2
  import { float } from "./float.js";
3
- /**
4
- * Higher order formatter for n-D vectors, with each element formatted using
5
- * `prec` and using optional delimiter and pre/postfixes.
6
- *
7
- * @size - vector size (optimized for size 1-4)
8
- * @prec - precision (see {@link float}) or existing number formatter
9
- * @delim - delimiter (default: `,`)
10
- * @pre - prefix (default: `[`)
11
- * @post - prefix (default: `]`)
12
- */
13
- export const vector = memoizeJ((size, prec = 3, d = ",", pre = "[", post = "]") => {
3
+ const vector = memoizeJ(
4
+ (size, prec = 3, d = ",", pre = "[", post = "]") => {
14
5
  const f = typeof prec === "number" ? float(prec) : prec;
15
6
  switch (size) {
16
- case 1:
17
- return (v) => `${pre}${f(v[0])}${post}`;
18
- case 2:
19
- return (v) => `${pre}${f(v[0])}${d}${f(v[1])}${post}`;
20
- case 3:
21
- return (v) => `${pre}${f(v[0])}${d}${f(v[1])}${d}${f(v[2])}${post}`;
22
- case 4:
23
- return (v) => `${pre}${f(v[0])}${d}${f(v[1])}${d}${f(v[2])}${d}${f(v[3])}${post}`;
24
- default:
25
- return (v) => {
26
- const res = [];
27
- for (let i = 0; i < v.length; i++) {
28
- res.push(f(v[i]));
29
- }
30
- return `${pre}${res.join(d)}${post}`;
31
- };
7
+ case 1:
8
+ return (v) => `${pre}${f(v[0])}${post}`;
9
+ case 2:
10
+ return (v) => `${pre}${f(v[0])}${d}${f(v[1])}${post}`;
11
+ case 3:
12
+ return (v) => `${pre}${f(v[0])}${d}${f(v[1])}${d}${f(v[2])}${post}`;
13
+ case 4:
14
+ return (v) => `${pre}${f(v[0])}${d}${f(v[1])}${d}${f(v[2])}${d}${f(
15
+ v[3]
16
+ )}${post}`;
17
+ default:
18
+ return (v) => {
19
+ const res = [];
20
+ for (let i = 0; i < v.length; i++) {
21
+ res.push(f(v[i]));
22
+ }
23
+ return `${pre}${res.join(d)}${post}`;
24
+ };
32
25
  }
33
- });
26
+ }
27
+ );
28
+ export {
29
+ vector
30
+ };