Skip to content

Commit 483baa1

Browse files
committed
Fix incorrect default for ambiguousIsNarrow option
Fixes #38
1 parent 56a47ea commit 483baa1

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface Options {
22
/**
33
Count [ambiguous width characters](https://www.unicode.org/reports/tr11/#Ambiguous) as having narrow width (count of 1) instead of wide width (count of 2).
44
5-
@default false
5+
@default true
66
*/
77
readonly ambiguousIsNarrow: boolean;
88
}

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export default function stringWidth(string, options = {}) {
77
return 0;
88
}
99

10+
options = {
11+
ambiguousIsNarrow: true,
12+
...options
13+
};
14+
1015
string = stripAnsi(string);
1116

1217
if (string.length === 0) {
@@ -15,7 +20,7 @@ export default function stringWidth(string, options = {}) {
1520

1621
string = string.replace(emojiRegex(), ' ');
1722

18-
const ambiguousCharWidth = options.ambiguousIsNarrow ? 1 : 2;
23+
const ambiguousCharacterWidth = options.ambiguousIsNarrow ? 1 : 2;
1924
let width = 0;
2025

2126
for (let index = 0; index < string.length; index++) {
@@ -38,7 +43,7 @@ export default function stringWidth(string, options = {}) {
3843
width += 2;
3944
break;
4045
case 'A':
41-
width += ambiguousCharWidth;
46+
width += ambiguousCharacterWidth;
4247
break;
4348
default:
4449
width += 1;

test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ test('main', t => {
55
t.is(stringWidth('abcde'), 5);
66
t.is(stringWidth('古池や'), 6);
77
t.is(stringWidth('あいうabc'), 9);
8-
t.is(stringWidth('あいう★'), 8);
9-
t.is(stringWidth('あいう★', {ambiguousIsNarrow: true}), 7);
8+
t.is(stringWidth('あいう★'), 7);
9+
t.is(stringWidth('あいう★', {ambiguousIsNarrow: false}), 8);
10+
t.is(stringWidth('±'), 1);
1011
t.is(stringWidth('ノード.js'), 9);
1112
t.is(stringWidth('你好'), 4);
1213
t.is(stringWidth('안녕하세요'), 10);

0 commit comments

Comments
 (0)