console-toolkit 1.2.0 → 1.2.1
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/README.md +5 -4
- package/package.json +1 -1
- package/src/strings/split.js +28 -15
package/README.md
CHANGED
|
@@ -89,7 +89,8 @@ BSD 3-Clause License
|
|
|
89
89
|
|
|
90
90
|
## Release history
|
|
91
91
|
|
|
92
|
-
- 1.2.
|
|
93
|
-
- 1.
|
|
94
|
-
- 1.1.
|
|
95
|
-
- 1.
|
|
92
|
+
- 1.2.1 *Added support for `Bun.stringWidth()`.*
|
|
93
|
+
- 1.2.0 *Refactored `strings`.*
|
|
94
|
+
- 1.1.1 *Minor bugfixes in `Table`, some improvements, updated deps.*
|
|
95
|
+
- 1.1.0 *Minor improvements, enhanced `Writer` and `Updater`.*
|
|
96
|
+
- 1.0.0 *Initial release.*
|
package/package.json
CHANGED
package/src/strings/split.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
// Loosely adapted
|
|
1
|
+
// Loosely adapted from https://www.npmjs.com/package/string-width by
|
|
2
2
|
// [Sindre Sorhus](https://www.npmjs.com/~sindresorhus) under the MIT license.
|
|
3
3
|
|
|
4
|
-
let emojiRegex = null
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
try {
|
|
13
|
-
|
|
14
|
-
} catch (error) {
|
|
15
|
-
|
|
4
|
+
let emojiRegex = null,
|
|
5
|
+
eastAsianWidth = null;
|
|
6
|
+
if (!globalThis.Bun) {
|
|
7
|
+
try {
|
|
8
|
+
emojiRegex = (await import('emoji-regex')).default();
|
|
9
|
+
} catch (error) {
|
|
10
|
+
// squelch
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
eastAsianWidth = (await import('get-east-asian-width')).eastAsianWidth;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
// squelch
|
|
16
|
+
}
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
const segmenter = new Intl.Segmenter();
|
|
@@ -22,7 +23,8 @@ export const split = (s, options = {}) => {
|
|
|
22
23
|
if (!s) return {graphemes: [], width: 0};
|
|
23
24
|
|
|
24
25
|
const {ignoreControlSymbols = false, ambiguousAsWide = false} = options,
|
|
25
|
-
eastAsianWidthOptions = {ambiguousAsWide}
|
|
26
|
+
eastAsianWidthOptions = {ambiguousAsWide},
|
|
27
|
+
bunStringWidthOptions = {ambiguousAsNarrow: !ambiguousAsWide};
|
|
26
28
|
|
|
27
29
|
const graphemes = [];
|
|
28
30
|
let width = 0;
|
|
@@ -41,6 +43,12 @@ export const split = (s, options = {}) => {
|
|
|
41
43
|
if (graphemes.length) graphemes[graphemes.length - 1].symbol += segment;
|
|
42
44
|
continue;
|
|
43
45
|
}
|
|
46
|
+
if (globalThis.Bun) {
|
|
47
|
+
const w = Bun.stringWidth(segment, bunStringWidthOptions);
|
|
48
|
+
graphemes.push({symbol: segment, width: w});
|
|
49
|
+
width += w;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
44
52
|
if (emojiRegex && ((emojiRegex.lastIndex = 0), emojiRegex.test(segment))) {
|
|
45
53
|
graphemes.push({symbol: segment, width: 2});
|
|
46
54
|
width += 2;
|
|
@@ -63,7 +71,8 @@ export const size = (s, options = {}) => {
|
|
|
63
71
|
if (!s) return 0;
|
|
64
72
|
|
|
65
73
|
const {ignoreControlSymbols = false, ambiguousAsWide = false} = options,
|
|
66
|
-
eastAsianWidthOptions = {ambiguousAsWide}
|
|
74
|
+
eastAsianWidthOptions = {ambiguousAsWide},
|
|
75
|
+
bunStringWidthOptions = {ambiguousAsNarrow: !ambiguousAsWide};
|
|
67
76
|
|
|
68
77
|
let width = 0;
|
|
69
78
|
for (const {segment} of segmenter.segment(s)) {
|
|
@@ -80,6 +89,10 @@ export const size = (s, options = {}) => {
|
|
|
80
89
|
) {
|
|
81
90
|
continue;
|
|
82
91
|
}
|
|
92
|
+
if (globalThis.Bun) {
|
|
93
|
+
width += Bun.stringWidth(segment, bunStringWidthOptions);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
83
96
|
if (emojiRegex && ((emojiRegex.lastIndex = 0), emojiRegex.test(segment))) {
|
|
84
97
|
width += 2;
|
|
85
98
|
continue;
|