@thi.ng/strings 3.7.1 → 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/CHANGELOG.md +1 -1
- package/ansi.js +12 -37
- package/api.js +4 -5
- package/case.js +24 -56
- package/center.js +12 -32
- package/currency.js +16 -8
- package/cursor.js +16 -37
- package/entities.js +136 -149
- package/escape.js +39 -62
- package/float.js +26 -45
- package/format.js +19 -27
- package/groups.js +42 -53
- package/hollerith.js +4 -16
- package/initials.js +6 -27
- package/int.js +8 -2
- package/interpolate.js +9 -36
- package/join.js +6 -21
- package/package.json +10 -8
- package/pad-left.js +18 -25
- package/pad-right.js +12 -13
- package/parse.js +10 -6
- package/percent.js +4 -7
- package/radix.js +32 -54
- package/range.js +13 -18
- package/repeat.js +6 -5
- package/ruler.js +12 -63
- package/slugify.js +12 -48
- package/splice.js +15 -29
- package/split.js +17 -28
- package/stringify.js +4 -12
- package/tabs.js +40 -81
- package/trim.js +7 -22
- package/truncate-left.js +6 -3
- package/truncate.js +8 -5
- package/units.js +77 -44
- package/utf8.js +72 -130
- package/uuid.js +4 -14
- package/vector.js +25 -28
- package/word-wrap.js +78 -141
- package/wrap.js +6 -5
package/units.js
CHANGED
|
@@ -1,81 +1,114 @@
|
|
|
1
1
|
import { memoizeJ } from "@thi.ng/memoize/memoizej";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
]
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
25
|
+
const bits = units(
|
|
26
|
+
[
|
|
26
27
|
[1, " bits", 0],
|
|
27
28
|
[KB, " Kb"],
|
|
28
29
|
[KB ** 2, " Mb"],
|
|
29
|
-
[KB ** 3, " Gb"]
|
|
30
|
-
],
|
|
31
|
-
|
|
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
|
-
],
|
|
39
|
-
|
|
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, "
|
|
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
|
-
],
|
|
49
|
-
|
|
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, "
|
|
65
|
+
[1e-6, " \xB5m"],
|
|
53
66
|
[1e-3, " mm"],
|
|
54
|
-
[
|
|
67
|
+
[0.01, " cm"],
|
|
55
68
|
[1, " m"],
|
|
56
|
-
[1e3, " km"]
|
|
57
|
-
],
|
|
58
|
-
|
|
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, "
|
|
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
|
-
],
|
|
69
|
-
|
|
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, "
|
|
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
|
-
],
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
73
36
|
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
60
|
+
}
|
|
61
|
+
return buf.subarray(0, pos);
|
|
122
62
|
};
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
72
|
+
const UTF8Error = defError(() => "UTF-8 error");
|
|
138
73
|
const utf8Error = (msg) => {
|
|
139
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
+
};
|