Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/string-width
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 282cf3d53918a92cc3ee0778dcf938039bcbc47b
Choose a base ref
...
head repository: sindresorhus/string-width
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 58147d249083e44c15c00f9de032d1f25e2039fe
Choose a head ref
  • 13 commits
  • 10 files changed
  • 4 contributors

Commits on Sep 22, 2016

  1. Verified

    This commit was signed with the committer’s verified signature.
    hyoban Stephen Zhou
    Copy the full SHA
    14c663d View commit details
  2. 2.0.0

    sindresorhus committed Sep 22, 2016
    Copy the full SHA
    523d7ba View commit details

Commits on May 9, 2017

  1. Meta tweaks

    sindresorhus committed May 9, 2017

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    7dd3956 View commit details
  2. Add failing test for #6

    sindresorhus committed May 9, 2017
    Copy the full SHA
    130d0ad View commit details

Commits on Jun 26, 2017

  1. Update strip-ansi from ^3.0.0 to ^4.0.0 (#10)

    Includes a breaking change chalk/strip-ansi@0978944 but doesn't affect strip-ansi, which has already dropped support for Node.js < 4.
    shinnn authored and sindresorhus committed Jun 26, 2017
    Copy the full SHA
    73ba3b5 View commit details
  2. Meta tweaks

    sindresorhus committed Jun 26, 2017
    Copy the full SHA
    79c4e30 View commit details
  3. 2.1.0

    sindresorhus committed Jun 26, 2017
    Copy the full SHA
    175b26f View commit details

Commits on Jul 18, 2017

  1. Copy the full SHA
    2af7be5 View commit details
  2. Meta tweaks

    sindresorhus committed Jul 18, 2017
    Copy the full SHA
    d0d3ccb View commit details
  3. 2.1.1

    sindresorhus committed Jul 18, 2017
    Copy the full SHA
    74d8d55 View commit details

Commits on Dec 15, 2018

  1. Support emoji (#17)

    tonytonyjan authored and sindresorhus committed Dec 15, 2018
    Copy the full SHA
    0bc308a View commit details
  2. Require Node.js 6

    sindresorhus committed Dec 15, 2018
    Copy the full SHA
    5b6b7ca View commit details
  3. 3.0.0

    sindresorhus committed Dec 15, 2018
    Copy the full SHA
    58147d2 View commit details
Showing with 110 additions and 111 deletions.
  1. +1 −1 .editorconfig
  2. +1 −1 .gitattributes
  3. +1 −0 .gitignore
  4. +1 −0 .npmrc
  5. +2 −3 .travis.yml
  6. +21 −19 index.js
  7. +4 −16 license
  8. +54 −54 package.json
  9. +2 −2 readme.md
  10. +23 −15 test.js
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
- '0.12'
- '0.10'
40 changes: 21 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
'use strict';
var stripAnsi = require('strip-ansi');
var codePointAt = require('code-point-at');
var isFullwidthCodePoint = require('is-fullwidth-code-point');
const stripAnsi = require('strip-ansi');
const isFullwidthCodePoint = require('is-fullwidth-code-point');
const emojiRegex = require('emoji-regex')();

// https://github.com/nodejs/io.js/blob/cff7300a578be1b10001f2d967aaedc88aee6402/lib/readline.js#L1345
module.exports = function (str) {
if (typeof str !== 'string' || str.length === 0) {
module.exports = input => {
input = input.replace(emojiRegex, ' ');

if (typeof input !== 'string' || input.length === 0) {
return 0;
}

var width = 0;
input = stripAnsi(input);

str = stripAnsi(str);
let width = 0;

for (var i = 0; i < str.length; i++) {
var code = codePointAt(str, i);
for (let i = 0; i < input.length; i++) {
const code = input.codePointAt(i);

// ignore control characters
if (code <= 0x1f || (code >= 0x7f && code <= 0x9f)) {
// Ignore control characters
if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {
continue;
}

// surrogates
if (code >= 0x10000) {
i++;
// Ignore combining characters
if (code >= 0x300 && code <= 0x36F) {
continue;
}

if (isFullwidthCodePoint(code)) {
width += 2;
} else {
width++;
// Surrogates
if (code > 0xFFFF) {
i++;
}

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

return width;
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
108 changes: 54 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
{
"name": "string-width",
"version": "1.0.2",
"description": "Get the visual width of a string - the number of columns required to display it",
"license": "MIT",
"repository": "sindresorhus/string-width",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"string",
"str",
"character",
"char",
"unicode",
"width",
"visual",
"column",
"columns",
"fullwidth",
"full-width",
"full",
"ansi",
"escape",
"codes",
"cli",
"command-line",
"terminal",
"console",
"cjk",
"chinese",
"japanese",
"korean",
"fixed-width"
],
"dependencies": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
"strip-ansi": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "string-width",
"version": "3.0.0",
"description": "Get the visual width of a string - the number of columns required to display it",
"license": "MIT",
"repository": "sindresorhus/string-width",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"string",
"str",
"character",
"char",
"unicode",
"width",
"visual",
"column",
"columns",
"fullwidth",
"full-width",
"full",
"ansi",
"escape",
"codes",
"cli",
"command-line",
"terminal",
"console",
"cjk",
"chinese",
"japanese",
"korean",
"fixed-width"
],
"dependencies": {
"emoji-regex": "^7.0.1",
"is-fullwidth-code-point": "^2.0.0",
"strip-ansi": "^5.0.0"
},
"devDependencies": {
"ava": "^1.0.1",
"xo": "^0.23.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -2,15 +2,15 @@

> Get the visual width of a string - the number of columns required to display it
Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.
Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.

Useful to be able to measure the actual width of command-line output.


## Install

```
$ npm install --save string-width
$ npm install string-width
```


38 changes: 23 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import test from 'ava';
import m from './';
import stringWidth from '.';

test('main', t => {
t.is(m('abcde'), 5);
t.is(m('古池や'), 6);
t.is(m('あいうabc'), 9);
t.is(m('ノード.js'), 9);
t.is(m('你好'), 4);
t.is(m('안녕하세요'), 10);
t.is(m('A\ud83c\ude00BC'), 5, 'surrogate');
t.is(m('\u001b[31m\u001b[39m'), 0);
t.is(stringWidth('abcde'), 5);
t.is(stringWidth('古池や'), 6);
t.is(stringWidth('あいうabc'), 9);
t.is(stringWidth('ノード.js'), 9);
t.is(stringWidth('你好'), 4);
t.is(stringWidth('안녕하세요'), 10);
t.is(stringWidth('A\uD83C\uDE00BC'), 5, 'surrogate');
t.is(stringWidth('\u001B[31m\u001B[39m'), 0);
t.is(stringWidth('\u{231A}'), 2, '⌚ default emoji presentation character (Emoji_Presentation)');
t.is(stringWidth('\u{2194}\u{FE0F}'), 2, '↔️ default text presentation character rendered as emoji');
t.is(stringWidth('\u{1F469}'), 2, '👩 emoji modifier base (Emoji_Modifier_Base)');
t.is(stringWidth('\u{1F469}\u{1F3FF}'), 2, '👩🏿 emoji modifier base followed by a modifier');
});

test('ignores control characters', t => {
t.is(m(String.fromCharCode(0)), 0);
t.is(m(String.fromCharCode(31)), 0);
t.is(m(String.fromCharCode(127)), 0);
t.is(m(String.fromCharCode(134)), 0);
t.is(m(String.fromCharCode(159)), 0);
t.is(m('\u001b'), 0);
t.is(stringWidth(String.fromCharCode(0)), 0);
t.is(stringWidth(String.fromCharCode(31)), 0);
t.is(stringWidth(String.fromCharCode(127)), 0);
t.is(stringWidth(String.fromCharCode(134)), 0);
t.is(stringWidth(String.fromCharCode(159)), 0);
t.is(stringWidth('\u001B'), 0);
});

test('handles combining characters', t => {
t.is(stringWidth('x\u0300'), 1);
});