Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 18, 2017
1 parent 2af7be5 commit d0d3ccb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 62 deletions.
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
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ module.exports = str => {
return 0;
}

let width = 0;

str = stripAnsi(str);

let width = 0;

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

// Ignore control characters & combining characters
if (code <= 0x1F || (code >= 0x7F && code <= 0x9F) || (code >= 0x300 && code <= 0x36F)) {
// Ignore control characters
if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {
continue;
}

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

Expand All @@ -24,11 +29,7 @@ module.exports = str => {
i++;
}

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

return width;
Expand Down
106 changes: 53 additions & 53 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
{
"name": "string-width",
"version": "2.1.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": ">=4"
},
"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": {
"is-fullwidth-code-point": "^2.0.0",
"strip-ansi": "^4.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "string-width",
"version": "2.1.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": ">=4"
},
"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": {
"is-fullwidth-code-point": "^2.0.0",
"strip-ansi": "^4.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
}

0 comments on commit d0d3ccb

Please sign in to comment.