-
-
Notifications
You must be signed in to change notification settings - Fork 31
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 option for whether ambiguous width characters should be fullwidth or halfwidth #5
Conversation
It should be an option in an optional options object as the second argument, not a separate method. ( It's also missing docs. Reference on how it should be implemented and documented. |
@@ -1,35 +1,226 @@ | |||
'use strict'; | |||
const stripAnsi = require('strip-ansi'); | |||
const isFullwidthCodePoint = require('is-fullwidth-code-point'); | |||
const codePointAt = require('code-point-at'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use String#codePointAt()
.
}; | ||
|
||
function ambiguousChar(code) { | ||
if ((code === 0x00A1) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the source of all these code point ranges?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
University of Cambridge:
https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
@@ -1,22 +1,24 @@ | |||
import test from 'ava'; | |||
import m from './'; | |||
import stringWidth from './'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do unrelated changes.
Thanks for the code review @sindresorhus. I refactored the code and I assume a default ambiguous char width of 2 columns. Please let me know if you agree. I added a new section to the documentation - API. |
module.exports = str => { | ||
module.exports = (str, opts) => { | ||
opts = Object.assign({ | ||
ambiguousCharWidth: 2 // default |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default
comment is moot.
@@ -24,6 +30,12 @@ module.exports = str => { | |||
i++; | |||
} | |||
|
|||
// could be halfwidth or fullwidth |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could => Could
@@ -33,3 +45,72 @@ module.exports = str => { | |||
|
|||
return width; | |||
}; | |||
|
|||
function ambiguousChar(code) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ambiguousChar => isAmbiguousChar
|
||
Type: `string` | ||
|
||
Text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really useful. Better to just drop it.
@@ -29,6 +29,24 @@ stringWidth('a'); | |||
//=> 1 | |||
``` | |||
|
|||
## API | |||
|
|||
### stringWidth(str, [options]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
str => input
|
||
### stringWidth(str, [options]) | ||
|
||
#### str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
str => input
|
||
##### ambiguousCharWidth | ||
|
||
Type: `int`<br> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int
is not a type in JS. Should be number
.
##### ambiguousCharWidth | ||
|
||
Type: `int`<br> | ||
Values: 1 (halfwidth) or 2 (fullwidth) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, since there are only two possible scenarios, I think we should just make it a boolean and call it: ambiguousCharsAreFullwidth
or something.
Ugh, sorry @joelpinheiro. I did a review 16 days ago, but forgot to click the "Submit review" button... |
Hey, @joelpinheiro, would you be willing to finish this up? :) |
Fixes #1.
Added a set of new tests to verify the correct behaviour of this feature.