diff --git a/index.d.ts b/index.d.ts index d8da3ce..aed9fdf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; } diff --git a/index.js b/index.js index 35882b0..23f3f9a 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,11 @@ export default function stringWidth(string, options = {}) { return 0; } + options = { + ambiguousIsNarrow: true, + ...options + }; + string = stripAnsi(string); if (string.length === 0) { @@ -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++) { @@ -38,7 +43,7 @@ export default function stringWidth(string, options = {}) { width += 2; break; case 'A': - width += ambiguousCharWidth; + width += ambiguousCharacterWidth; break; default: width += 1; diff --git a/test.js b/test.js index c12fa8e..41b7181 100644 --- a/test.js +++ b/test.js @@ -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);