Skip to content

Commit

Permalink
Require Node.js 6
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 15, 2018
1 parent 0bc308a commit 5b6b7ca
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
12 changes: 6 additions & 6 deletions index.js
Expand Up @@ -3,19 +3,19 @@ const stripAnsi = require('strip-ansi');
const isFullwidthCodePoint = require('is-fullwidth-code-point');
const emojiRegex = require('emoji-regex')();

module.exports = str => {
str = str.replace(emojiRegex, ' ');
module.exports = input => {
input = input.replace(emojiRegex, ' ');

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

str = stripAnsi(str);
input = stripAnsi(input);

let width = 0;

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

// Ignore control characters
if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
Expand Down Expand Up @@ -47,10 +47,10 @@
"dependencies": {
"emoji-regex": "^7.0.1",
"is-fullwidth-code-point": "^2.0.0",
"strip-ansi": "^4.0.0"
"strip-ansi": "^5.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.0.1",
"xo": "^0.23.0"
}
}
40 changes: 20 additions & 20 deletions test.js
@@ -1,30 +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(m('\u{231A}'), 2, '⌚ default emoji presentation character (Emoji_Presentation)');
t.is(m('\u{2194}\u{FE0F}'), 2, '↔️ default text presentation character rendered as emoji');
t.is(m('\u{1F469}'), 2, '👩 emoji modifier base (Emoji_Modifier_Base)');
t.is(m('\u{1F469}\u{1F3FF}'), 2, '👩🏿 emoji modifier base followed by a modifier');
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(m('x\u0300'), 1);
t.is(stringWidth('x\u0300'), 1);
});

0 comments on commit 5b6b7ca

Please sign in to comment.