Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
sindresorhus committed Apr 2, 2019
1 parent 94f2697 commit 27ed208
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
19 changes: 12 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
const sliceAnsi = require('slice-ansi');
const stringWidth = require('string-width');

module.exports = (input, columns, opts) => {
opts = Object.assign({
position: 'end'
}, opts);
module.exports = (input, columns, options) => {
options = {
position: 'end',
...options
};

const position = opts.position;
const {position} = options;
const ellipsis = '…';

if (typeof input !== 'string') {
Expand All @@ -34,10 +35,14 @@ module.exports = (input, columns, opts) => {

if (position === 'start') {
return ellipsis + sliceAnsi(input, length - columns + 1, length);
} else if (position === 'middle') {
}

if (position === 'middle') {
const half = Math.floor(columns / 2);
return sliceAnsi(input, 0, half) + ellipsis + sliceAnsi(input, length - (columns - half) + 1, length);
} else if (position === 'end') {
}

if (position === 'end') {
return sliceAnsi(input, 0, columns - 1) + ellipsis;
}

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -32,11 +32,11 @@
"ansi"
],
"dependencies": {
"slice-ansi": "^1.0.0",
"string-width": "^2.0.0"
"slice-ansi": "^2.1.0",
"string-width": "^4.1.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"xo": "^0.24.0"
}
}
38 changes: 19 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import test from 'ava';
import m from '.';
import cliTruncate from '.';

test(t => {
t.is(m('unicorn', 4), 'uni…');
t.is(m('unicorn', 4, {position: 'end'}), 'uni…');
t.is(m('unicorn', 1), '…');
t.is(m('unicorn', 0), '');
t.is(m('unicorn', -4), '');
t.is(m('unicorn', 20), 'unicorn');
t.is(m('unicorn', 7), 'unicorn');
t.is(m('unicorn', 6), 'unico…');
t.is(m('\u001B[31municorn\u001B[39m', 7), '\u001B[31municorn\u001B[39m');
t.is(m('\u001B[31municorn\u001B[39m', 1), '…');
t.is(m('\u001B[31municorn\u001B[39m', 4), '\u001B[31muni\u001B[39m…');
t.is(m('a\uD83C\uDE00b\uD83C\uDE00c', 5), 'a\uD83C\uDE00b\uD83C\uDE00…', 'surrogate pairs');
t.is(m('안녕하세요', 3), '안…', 'wide char');
t.is(m('unicorn', 5, {position: 'start'}), '…corn');
t.is(m('unicorn', 6, {position: 'start'}), '…icorn');
t.is(m('unicorn', 5, {position: 'middle'}), 'un…rn');
t.is(m('unicorns', 6, {position: 'middle'}), 'uni…ns');
test('main', t => {
t.is(cliTruncate('unicorn', 4), 'uni…');
t.is(cliTruncate('unicorn', 4, {position: 'end'}), 'uni…');
t.is(cliTruncate('unicorn', 1), '…');
t.is(cliTruncate('unicorn', 0), '');
t.is(cliTruncate('unicorn', -4), '');
t.is(cliTruncate('unicorn', 20), 'unicorn');
t.is(cliTruncate('unicorn', 7), 'unicorn');
t.is(cliTruncate('unicorn', 6), 'unico…');
t.is(cliTruncate('\u001B[31municorn\u001B[39m', 7), '\u001B[31municorn\u001B[39m');
t.is(cliTruncate('\u001B[31municorn\u001B[39m', 1), '…');
t.is(cliTruncate('\u001B[31municorn\u001B[39m', 4), '\u001B[31muni\u001B[39m…');
t.is(cliTruncate('a\uD83C\uDE00b\uD83C\uDE00c', 5), 'a\uD83C\uDE00b\uD83C\uDE00…', 'surrogate pairs');
t.is(cliTruncate('안녕하세요', 3), '안…', 'wide char');
t.is(cliTruncate('unicorn', 5, {position: 'start'}), '…corn');
t.is(cliTruncate('unicorn', 6, {position: 'start'}), '…icorn');
t.is(cliTruncate('unicorn', 5, {position: 'middle'}), 'un…rn');
t.is(cliTruncate('unicorns', 6, {position: 'middle'}), 'uni…ns');
});

0 comments on commit 27ed208

Please sign in to comment.