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 option for whether ambiguous width characters should be fullwidth or halfwidth #5

Closed
wants to merge 3 commits into from

Conversation

joelpinheiro
Copy link

@joelpinheiro joelpinheiro commented Jan 9, 2017

Fixes #1.

Added a set of new tests to verify the correct behaviour of this feature.

@sindresorhus
Copy link
Owner

sindresorhus commented Jan 9, 2017

It should be an option in an optional options object as the second argument, not a separate method. (stringWidth(input, [options])).

It's also missing docs.

Reference on how it should be implemented and documented.

@sindresorhus sindresorhus changed the title Add option to let users decide if ambiguous width characters should be 1 or 2 in width #1 Add option for whether ambiguous width characters should be fullwidth or halfwidth Jan 9, 2017
@@ -1,35 +1,226 @@
'use strict';
const stripAnsi = require('strip-ansi');
const isFullwidthCodePoint = require('is-fullwidth-code-point');
const codePointAt = require('code-point-at');
Copy link
Owner

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) ||
Copy link
Owner

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?

Copy link
Author

Choose a reason for hiding this comment

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

@@ -1,22 +1,24 @@
import test from 'ava';
import m from './';
import stringWidth from './';
Copy link
Owner

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.

@joelpinheiro
Copy link
Author

joelpinheiro commented Jan 9, 2017

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
Copy link
Owner

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
Copy link
Owner

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) {
Copy link
Owner

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
Copy link
Owner

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])
Copy link
Owner

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
Copy link
Owner

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>
Copy link
Owner

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)
Copy link
Owner

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.

@sindresorhus
Copy link
Owner

Ugh, sorry @joelpinheiro. I did a review 16 days ago, but forgot to click the "Submit review" button...

@sindresorhus
Copy link
Owner

Hey, @joelpinheiro, would you be willing to finish this up? :)

@joelpinheiro joelpinheiro deleted the feature branch January 4, 2018 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants