@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/radix.js
CHANGED
|
@@ -1,59 +1,37 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
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
|
-
|
|
20
|
-
|
|
14
|
+
x = (x >>> 0).toString(radix2);
|
|
15
|
+
return prefix + (x.length < n ? buf.substring(x.length) + x : x);
|
|
21
16
|
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export
|
|
16
|
-
|
|
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.js
CHANGED
|
@@ -1,30 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
+
const re = typeof delim === "string" ? new RegExp(delim, "g") : delim;
|
|
6
|
+
for (; i < n; ) {
|
|
7
|
+
const m = re.exec(src);
|
|
8
|
+
if (!m) {
|
|
9
|
+
yield src.substring(i);
|
|
10
|
+
return;
|
|
29
11
|
}
|
|
12
|
+
const len = m[0].length;
|
|
13
|
+
yield src.substring(i, m.index + include * len);
|
|
14
|
+
i = m.index + len;
|
|
15
|
+
}
|
|
30
16
|
}
|
|
17
|
+
export {
|
|
18
|
+
split
|
|
19
|
+
};
|
package/stringify.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
.
|
|
54
|
-
|
|
55
|
-
.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
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
|
-
|
|
4
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
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
|
+
};
|