Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 18, 2019
1 parent bd7822c commit 855b046
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '10'
- '8'
- '6'
16 changes: 15 additions & 1 deletion index.d.ts
Expand Up @@ -2,5 +2,19 @@
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](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.
@example
```
import stringWidth from 'string-width';
stringWidth('a');
//=> 1
stringWidth('古');
//=> 2
stringWidth('\u001B[1m古\u001B[22m');
//=> 2
```
*/
export default function stringWidth(text: string): number;
export default function stringWidth(string: string): number;
14 changes: 7 additions & 7 deletions index.js
@@ -1,21 +1,21 @@
'use strict';
const stripAnsi = require('strip-ansi');
const isFullwidthCodePoint = require('is-fullwidth-code-point');
const emojiRegex = require('emoji-regex')();
const emojiRegex = require('emoji-regex');

const stringWidth = input => {
input = input.replace(emojiRegex, ' ');
const stringWidth = string => {
string = string.replace(emojiRegex(), ' ');

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

input = stripAnsi(input);
string = stripAnsi(string);

let width = 0;

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

// Ignore control characters
if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {
Expand Down
6 changes: 2 additions & 4 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd-check"
Expand All @@ -21,9 +21,7 @@
],
"keywords": [
"string",
"str",
"character",
"char",
"unicode",
"width",
"visual",
Expand All @@ -47,7 +45,7 @@
],
"dependencies": {
"emoji-regex": "^8.0.0",
"is-fullwidth-code-point": "^2.0.0",
"is-fullwidth-code-point": "^3.0.0",
"strip-ansi": "^5.1.0"
},
"devDependencies": {
Expand Down
11 changes: 4 additions & 7 deletions readme.md
Expand Up @@ -19,17 +19,14 @@ $ npm install string-width
```js
const stringWidth = require('string-width');

stringWidth('a');
//=> 1

stringWidth('');
//=> 2

stringWidth('\u001b[1m古\u001b[22m');
stringWidth('\u001B[1m古\u001B[22m');
//=> 2

stringWidth('a');
//=> 1

stringWidth('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');
// => 5
```


Expand Down

0 comments on commit 855b046

Please sign in to comment.