Skip to content

Commit

Permalink
Fix incorrect default for ambiguousIsNarrow option
Browse files Browse the repository at this point in the history
Fixes #38
  • Loading branch information
sindresorhus committed Feb 27, 2022
1 parent 56a47ea commit 483baa1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -2,7 +2,7 @@ export interface Options {
/**
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).
@default false
@default true
*/
readonly ambiguousIsNarrow: boolean;
}
Expand Down
9 changes: 7 additions & 2 deletions index.js
Expand Up @@ -7,6 +7,11 @@ export default function stringWidth(string, options = {}) {
return 0;
}

options = {
ambiguousIsNarrow: true,
...options
};

string = stripAnsi(string);

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

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

const ambiguousCharWidth = options.ambiguousIsNarrow ? 1 : 2;
const ambiguousCharacterWidth = options.ambiguousIsNarrow ? 1 : 2;
let width = 0;

for (let index = 0; index < string.length; index++) {
Expand All @@ -38,7 +43,7 @@ export default function stringWidth(string, options = {}) {
width += 2;
break;
case 'A':
width += ambiguousCharWidth;
width += ambiguousCharacterWidth;
break;
default:
width += 1;
Expand Down
5 changes: 3 additions & 2 deletions test.js
Expand Up @@ -5,8 +5,9 @@ test('main', t => {
t.is(stringWidth('abcde'), 5);
t.is(stringWidth('古池や'), 6);
t.is(stringWidth('あいうabc'), 9);
t.is(stringWidth('あいう★'), 8);
t.is(stringWidth('あいう★', {ambiguousIsNarrow: true}), 7);
t.is(stringWidth('あいう★'), 7);
t.is(stringWidth('あいう★', {ambiguousIsNarrow: false}), 8);
t.is(stringWidth('±'), 1);
t.is(stringWidth('ノード.js'), 9);
t.is(stringWidth('你好'), 4);
t.is(stringWidth('안녕하세요'), 10);
Expand Down

0 comments on commit 483baa1

Please sign in to comment.