@cjser/string-width 8.2.1-cjser.2 → 8.3.0-cjser.2

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.
@@ -26,7 +26,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  ));
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
 
29
- // packages/@cjser/string-width/index.js
29
+ // packages/@cjser/string-width.tmp-26-1790266896938/index.js
30
30
  var index_exports = {};
31
31
  __export(index_exports, {
32
32
  default: () => stringWidth
@@ -35,8 +35,8 @@ module.exports = __toCommonJS(index_exports);
35
35
  var import_strip_ansi = __toESM(require("@cjser/strip-ansi"), 1);
36
36
  var import_get_east_asian_width = require("@cjser/get-east-asian-width");
37
37
  var segmenter = new Intl.Segmenter();
38
- var zeroWidthClusterRegex = new RegExp("^(?:\\p{Default_Ignorable_Code_Point}|\\p{Control}|\\p{Format}|\\p{Mark}|\\p{Surrogate})+$", "v");
39
- var leadingNonPrintingRegex = new RegExp("^[\\p{Default_Ignorable_Code_Point}\\p{Control}\\p{Format}\\p{Mark}\\p{Surrogate}]+", "v");
38
+ var visibleCharacterRegex = new RegExp("[^\\p{Default_Ignorable_Code_Point}\\p{Control}\\p{Format}\\p{Nonspacing_Mark}\\p{Enclosing_Mark}\\p{Surrogate}]", "v");
39
+ var spacingMarkRegex = new RegExp("\\p{Spacing_Mark}", "v");
40
40
  var rgiEmojiRegex = new RegExp("^\\p{RGI_Emoji}$", "v");
41
41
  var unqualifiedKeycapRegex = /^[\d#*]\u20E3$/;
42
42
  var extendedPictographicRegex = new RegExp("\\p{Extended_Pictographic}", "gu");
@@ -54,10 +54,8 @@ function isDoubleWidthNonRgiEmojiSequence(segment) {
54
54
  return false;
55
55
  }
56
56
  function baseVisible(segment) {
57
- return segment.replace(leadingNonPrintingRegex, "");
58
- }
59
- function isZeroWidthCluster(segment) {
60
- return zeroWidthClusterRegex.test(segment);
57
+ const index = segment.search(visibleCharacterRegex);
58
+ return index === -1 ? void 0 : segment.slice(index);
61
59
  }
62
60
  function isHangulLeadingJamo(codePoint) {
63
61
  return codePoint >= 4352 && codePoint <= 4447 || codePoint >= 43360 && codePoint <= 43388;
@@ -74,7 +72,7 @@ function isHangulJamo(codePoint) {
74
72
  function hangulClusterWidth(visibleSegment, eastAsianWidthOptions) {
75
73
  const codePoints = [];
76
74
  for (const character of visibleSegment) {
77
- if (zeroWidthClusterRegex.test(character)) {
75
+ if (!visibleCharacterRegex.test(character)) {
78
76
  continue;
79
77
  }
80
78
  codePoints.push(character.codePointAt(0));
@@ -103,7 +101,7 @@ function hangulClusterWidth(visibleSegment, eastAsianWidthOptions) {
103
101
  }
104
102
  return width;
105
103
  }
106
- function trailingHalfwidthWidth(visibleSegment, eastAsianWidthOptions) {
104
+ function trailingWidth(visibleSegment, eastAsianWidthOptions) {
107
105
  let extra = 0;
108
106
  let first = true;
109
107
  for (const character of visibleSegment) {
@@ -111,7 +109,7 @@ function trailingHalfwidthWidth(visibleSegment, eastAsianWidthOptions) {
111
109
  first = false;
112
110
  continue;
113
111
  }
114
- if (character >= "\uFF00" && character <= "\uFFEF") {
112
+ if (spacingMarkRegex.test(character) || character >= "\uFF00" && character <= "\uFFEF") {
115
113
  extra += (0, import_get_east_asian_width.eastAsianWidth)(character.codePointAt(0), eastAsianWidthOptions);
116
114
  }
117
115
  }
@@ -138,14 +136,14 @@ function stringWidth(input, options = {}) {
138
136
  let width = 0;
139
137
  const eastAsianWidthOptions = { ambiguousAsWide: !ambiguousIsNarrow };
140
138
  for (const { segment } of segmenter.segment(string)) {
141
- if (isZeroWidthCluster(segment)) {
139
+ const visibleSegment = baseVisible(segment);
140
+ if (visibleSegment === void 0) {
142
141
  continue;
143
142
  }
144
143
  if (rgiEmojiRegex.test(segment) || isDoubleWidthNonRgiEmojiSequence(segment)) {
145
144
  width += 2;
146
145
  continue;
147
146
  }
148
- const visibleSegment = baseVisible(segment);
149
147
  const hangulWidth = hangulClusterWidth(visibleSegment, eastAsianWidthOptions);
150
148
  if (hangulWidth !== void 0) {
151
149
  width += hangulWidth;
@@ -153,7 +151,7 @@ function stringWidth(input, options = {}) {
153
151
  }
154
152
  const codePoint = visibleSegment.codePointAt(0);
155
153
  width += (0, import_get_east_asian_width.eastAsianWidth)(codePoint, eastAsianWidthOptions);
156
- width += trailingHalfwidthWidth(visibleSegment, eastAsianWidthOptions);
154
+ width += trailingWidth(visibleSegment, eastAsianWidthOptions);
157
155
  }
158
156
  return width;
159
157
  }
package/index.js CHANGED
@@ -5,21 +5,20 @@ import {eastAsianWidth} from '@cjser/get-east-asian-width';
5
5
  Logic:
6
6
  - Segment graphemes to match how terminals render clusters.
7
7
  - Width rules:
8
- 1. Skip non-printing clusters (Default_Ignorable, Control, pure Mark, lone Surrogates). Tabs are ignored by design.
8
+ 1. Skip non-printing clusters (Default_Ignorable, Control, pure nonspacing/enclosing Mark, lone Surrogates). Tabs are ignored by design.
9
9
  2. RGI emoji clusters (\p{RGI_Emoji}) are double-width.
10
10
  3. Minimally-qualified/unqualified emoji clusters (ZWJ sequences with 2+ Extended_Pictographic, or keycap sequences) are double-width.
11
11
  4. Hangul jamo collapse each standard modern Hangul L+V or L+V+T syllable piece to width 2.
12
12
  Unmatched repeated leading/vowel/trailing jamo stay additive because that matches how the terminals we target render them.
13
- 5. Otherwise use East Asian Width of the cluster's first visible code point, and add widths for trailing Halfwidth/Fullwidth Forms within the same cluster (e.g., dakuten/handakuten/prolonged sound mark).
13
+ 5. Otherwise use East Asian Width of the cluster's first visible code point, and add widths for trailing spacing marks and Halfwidth/Fullwidth Forms within the same cluster (e.g., dakuten/handakuten/prolonged sound mark).
14
14
  */
15
15
 
16
16
  const segmenter = new Intl.Segmenter();
17
17
 
18
- // Whole-cluster zero-width
19
- const zeroWidthClusterRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Mark}|\p{Surrogate})+$/v;
20
-
21
- // Pick the base scalar if the cluster starts with Prepend/Format/Marks
22
- const leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}\p{Surrogate}]+/v;
18
+ // Matches a visible (printing) character. Used to find the base scalar of a cluster (skipping leading Prepend/Format/Marks) and to detect whole-cluster zero-width.
19
+ // Intentionally a single negated class without a quantifier: a repeated pattern like `^(?:…|…)+$` backtracks exponentially on overlapping properties, and even `^[…]+$` overflows V8's backtrack stack (RangeError) on clusters with millions of marks.
20
+ const visibleCharacterRegex = /[^\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Nonspacing_Mark}\p{Enclosing_Mark}\p{Surrogate}]/v;
21
+ const spacingMarkRegex = /\p{Spacing_Mark}/v;
23
22
 
24
23
  // RGI emoji sequences
25
24
  const rgiEmojiRegex = /^\p{RGI_Emoji}$/v;
@@ -47,12 +46,10 @@ function isDoubleWidthNonRgiEmojiSequence(segment) {
47
46
  return false;
48
47
  }
49
48
 
49
+ // Returns the cluster starting at its first visible character, or `undefined` if the whole cluster is zero-width.
50
50
  function baseVisible(segment) {
51
- return segment.replace(leadingNonPrintingRegex, '');
52
- }
53
-
54
- function isZeroWidthCluster(segment) {
55
- return zeroWidthClusterRegex.test(segment);
51
+ const index = segment.search(visibleCharacterRegex);
52
+ return index === -1 ? undefined : segment.slice(index);
56
53
  }
57
54
 
58
55
  function isHangulLeadingJamo(codePoint) {
@@ -80,7 +77,7 @@ function hangulClusterWidth(visibleSegment, eastAsianWidthOptions) {
80
77
  const codePoints = [];
81
78
 
82
79
  for (const character of visibleSegment) {
83
- if (zeroWidthClusterRegex.test(character)) {
80
+ if (!visibleCharacterRegex.test(character)) {
84
81
  continue;
85
82
  }
86
83
 
@@ -125,7 +122,7 @@ function hangulClusterWidth(visibleSegment, eastAsianWidthOptions) {
125
122
  return width;
126
123
  }
127
124
 
128
- function trailingHalfwidthWidth(visibleSegment, eastAsianWidthOptions) {
125
+ function trailingWidth(visibleSegment, eastAsianWidthOptions) {
129
126
  let extra = 0;
130
127
  let first = true;
131
128
 
@@ -135,7 +132,10 @@ function trailingHalfwidthWidth(visibleSegment, eastAsianWidthOptions) {
135
132
  continue;
136
133
  }
137
134
 
138
- if (character >= '\uFF00' && character <= '\uFFEF') {
135
+ if (
136
+ spacingMarkRegex.test(character)
137
+ || (character >= '\uFF00' && character <= '\uFFEF')
138
+ ) {
139
139
  extra += eastAsianWidth(character.codePointAt(0), eastAsianWidthOptions);
140
140
  }
141
141
  }
@@ -174,7 +174,8 @@ export default function stringWidth(input, options = {}) {
174
174
 
175
175
  for (const {segment} of segmenter.segment(string)) {
176
176
  // Zero-width / non-printing clusters
177
- if (isZeroWidthCluster(segment)) {
177
+ const visibleSegment = baseVisible(segment);
178
+ if (visibleSegment === undefined) {
178
179
  continue;
179
180
  }
180
181
 
@@ -184,7 +185,6 @@ export default function stringWidth(input, options = {}) {
184
185
  continue;
185
186
  }
186
187
 
187
- const visibleSegment = baseVisible(segment);
188
188
  const hangulWidth = hangulClusterWidth(visibleSegment, eastAsianWidthOptions);
189
189
  if (hangulWidth !== undefined) {
190
190
  width += hangulWidth;
@@ -195,8 +195,8 @@ export default function stringWidth(input, options = {}) {
195
195
  const codePoint = visibleSegment.codePointAt(0);
196
196
  width += eastAsianWidth(codePoint, eastAsianWidthOptions);
197
197
 
198
- // Add width for trailing Halfwidth and Fullwidth Forms (e.g., ゙, ゚, ー)
199
- width += trailingHalfwidthWidth(visibleSegment, eastAsianWidthOptions);
198
+ // Add width for trailing spacing marks and Halfwidth/Fullwidth Forms (e.g., ゙, ゚, ー)
199
+ width += trailingWidth(visibleSegment, eastAsianWidthOptions);
200
200
  }
201
201
 
202
202
  return width;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cjser/string-width",
3
- "version": "8.2.1-cjser.2",
3
+ "version": "8.3.0-cjser.2",
4
4
  "description": "Get the visual width of a string - the number of columns required to display it",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -59,7 +59,7 @@
59
59
  "east-asian-width"
60
60
  ],
61
61
  "dependencies": {
62
- "@cjser/get-east-asian-width": "1.5.0-cjser.2",
62
+ "@cjser/get-east-asian-width": "1.7.0-cjser.2",
63
63
  "@cjser/strip-ansi": "7.2.0-cjser.2"
64
64
  },
65
65
  "devDependencies": {
@@ -70,11 +70,11 @@
70
70
  "types": "./index.d.ts",
71
71
  "main": "./dist-cjser/index.cjs",
72
72
  "cjser": {
73
- "sourceVersion": "8.2.1",
73
+ "sourceVersion": "8.3.0",
74
74
  "cjserVersion": 2,
75
75
  "original": {
76
76
  "name": "string-width",
77
- "version": "8.2.1",
77
+ "version": "8.3.0",
78
78
  "exports": {
79
79
  "types": "./index.d.ts",
80
80
  "default": "./index.js"