Skip to content

Commit bd8025b

Browse files
committedJan 13, 2017
ES2015ify and require Node.js 4
1 parent b76c964 commit bd8025b

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed
 

‎.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
* text=auto
2+
*.js text eol=lf

‎.travis.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
language: node_js
22
node_js:
3-
- '5'
3+
- '6'
44
- '4'
5-
- '0.12'
6-
- '0.10'

‎index.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
'use strict';
2-
var sliceAnsi = require('slice-ansi');
3-
var stringWidth = require('string-width');
2+
const sliceAnsi = require('slice-ansi');
3+
const stringWidth = require('string-width');
44

5-
module.exports = function (input, columns, options) {
6-
options = options || {};
5+
module.exports = (input, columns, opts) => {
6+
opts = Object.assign({
7+
position: 'end'
8+
}, opts);
79

8-
var position = options.position || 'end';
9-
var ellipsis = '…';
10+
const position = opts.position;
11+
const ellipsis = '…';
1012

1113
if (typeof input !== 'string') {
12-
throw new TypeError('Expected `input` to be a string, got ' + typeof input);
14+
throw new TypeError(`Expected \`input\` to be a string, got ${typeof input}`);
1315
}
1416

1517
if (typeof columns !== 'number') {
16-
throw new TypeError('Expected `columns` to be a number, got ' + typeof columns);
18+
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
1719
}
1820

1921
if (columns < 1) {
@@ -24,7 +26,7 @@ module.exports = function (input, columns, options) {
2426
return ellipsis;
2527
}
2628

27-
var length = stringWidth(input);
29+
const length = stringWidth(input);
2830

2931
if (length <= columns) {
3032
return input;
@@ -33,11 +35,11 @@ module.exports = function (input, columns, options) {
3335
if (position === 'start') {
3436
return ellipsis + sliceAnsi(input, length - columns + 1, length);
3537
} else if (position === 'middle') {
36-
var half = Math.floor(columns / 2);
38+
const half = Math.floor(columns / 2);
3739
return sliceAnsi(input, 0, half) + ellipsis + sliceAnsi(input, length - (columns - half) + 1, length);
3840
} else if (position === 'end') {
3941
return sliceAnsi(input, 0, columns - 1) + ellipsis;
4042
}
4143

42-
throw new Error('Expected `options.position` to be either `start`, `middle` or `end`, got ' + position);
44+
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
4345
};

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=0.10.0"
13+
"node": ">=4"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"
@@ -33,7 +33,7 @@
3333
],
3434
"dependencies": {
3535
"slice-ansi": "0.0.4",
36-
"string-width": "^1.0.1"
36+
"string-width": "^2.0.0"
3737
},
3838
"devDependencies": {
3939
"ava": "*",

‎readme.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const cliTruncate = require('cli-truncate');
2020
cliTruncate('unicorn', 4);
2121
//=> 'uni…'
2222

23-
// truncate at different positions
23+
// Truncate at different positions
2424
cliTruncate('unicorn', 4, {position: 'start'});
2525
//=> '…orn'
2626

@@ -30,7 +30,7 @@ cliTruncate('unicorn', 4, {position: 'middle'});
3030
cliTruncate('\u001b[31municorn\u001b[39m', 4);
3131
//=> '\u001b[31muni\u001b[39m…'
3232

33-
// truncate the paragraph to the terminal width
33+
// Truncate the paragraph to the terminal width
3434
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
3535
cliTruncate(paragraph, process.stdout.columns));
3636
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
@@ -55,6 +55,8 @@ Columns to occupy in the terminal.
5555

5656
#### options
5757

58+
Type: `Object`
59+
5860
##### position
5961

6062
Type: `string`<br>

‎test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
22
import m from './';
33

4-
// because of https://github.com/chalk/slice-ansi/issues/6
4+
// Because of https://github.com/chalk/slice-ansi/issues/6
55
const mootEscapes = '\u001b[31m\u001b[31m\u001b[31m\u001b[31m';
66

77
test(t => {
@@ -17,8 +17,8 @@ test(t => {
1717
t.is(m('\u001b[31municorn\u001b[39m', 1), '…');
1818
t.is(m('\u001b[31municorn\u001b[39m', 4), mootEscapes + '\u001b[31muni\u001b[39m…');
1919
// TODO
20-
t.skip.is(m('a\ud83c\ude00b\ud83c\ude00c', 5), 'a\ud83c\ude00b…', 'surrogate pairs');
21-
t.skip.is(m('안녕하세요', 3), '안…', 'wide char');
20+
// t.skip.is(m('a\ud83c\ude00b\ud83c\ude00c', 5), 'a\ud83c\ude00b…', 'surrogate pairs');
21+
// t.skip.is(m('안녕하세요', 3), '안…', 'wide char');
2222
t.is(m('unicorn', 5, {position: 'start'}), '…corn');
2323
t.is(m('unicorn', 6, {position: 'start'}), '…icorn');
2424
t.is(m('unicorn', 5, {position: 'middle'}), 'un…rn');

0 commit comments

Comments
 (0)
Please sign in to comment.