Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ambiguousIsNarrow option #34

Merged
merged 7 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
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
*/
readonly ambiguousIsNarrow: boolean;
}

/**
Get the visual width of a string - the number of columns required to display it.

Expand All @@ -17,4 +26,4 @@ stringWidth('\u001B[1m古\u001B[22m');
//=> 2
```
*/
export default function stringWidth(string: string): number;
export default function stringWidth(string: string, options?: Options): number;
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import stripAnsi from 'strip-ansi';
import isFullwidthCodePoint from 'is-fullwidth-code-point';
import eastAsianWidth from 'eastasianwidth';
import emojiRegex from 'emoji-regex';

export default function stringWidth(string) {
export default function stringWidth(string, options = {}) {
if (typeof string !== 'string' || string.length === 0) {
return 0;
}
Expand All @@ -15,6 +15,7 @@ export default function stringWidth(string) {

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

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

for (let index = 0; index < string.length; index++) {
Expand All @@ -30,12 +31,18 @@ export default function stringWidth(string) {
continue;
}

// Surrogates
if (codePoint > 0xFFFF) {
index++;
Comment on lines -33 to -35
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surrogate pairs have already been solved here

const code = eastAsianWidth.eastAsianWidth(string.charAt(index));
switch (code) {
case 'F':
case 'W':
width += 2;
break;
case 'A':
width += ambiguousCharWidth;
break;
default:
width += 1;
}

width += isFullwidthCodePoint(codePoint) ? 2 : 1;
}

return width;
Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import {expectType} from 'tsd';
import stringWidth from './index.js';

expectType<number>(stringWidth('古'));

expectType<number>(stringWidth('★', {ambiguousIsNarrow: true}));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"fixed-width"
],
"dependencies": {
"eastasianwidth": "^0.2.0",
"emoji-regex": "^9.2.2",
"is-fullwidth-code-point": "^4.0.0",
"strip-ansi": "^7.0.1"
},
"devDependencies": {
Expand Down
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ stringWidth('\u001B[1m古\u001B[22m');
//=> 2
```

## API

### stringWidth(string, options?)

#### string

Type: `string`

The string to be counted.

#### options

Type: `object`

##### ambiguousIsNarrow

Type: `boolean`\
Default: `false`

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).

## Related

- [string-width-cli](https://github.com/sindresorhus/string-width-cli) - CLI for this module
Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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('ノード.js'), 9);
t.is(stringWidth('你好'), 4);
t.is(stringWidth('안녕하세요'), 10);
Expand Down