Skip to content

Commit 38ea95c

Browse files
committedAug 10, 2021
Require Node.js 12.20 and move to ESM
1 parent 0c2e152 commit 38ea95c

File tree

8 files changed

+84
-83
lines changed

8 files changed

+84
-83
lines changed
 

‎.github/workflows/main.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 16
1714
steps:
1815
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
2017
with:
2118
node-version: ${{ matrix.node-version }}
2219
- run: npm install

‎index.d.ts

+44-44
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
1-
declare namespace cliTruncate {
2-
interface Options {
3-
/**
4-
Position to truncate the string.
1+
export interface Options {
2+
/**
3+
The position to truncate the string.
54
6-
@default 'end'
7-
*/
8-
readonly position?: 'start' | 'middle' | 'end';
5+
@default 'end'
6+
*/
7+
readonly position?: 'start' | 'middle' | 'end';
98

10-
/**
11-
Add a space between the text and the ellipsis.
9+
/**
10+
Add a space between the text and the ellipsis.
1211
13-
@default false
12+
@default false
1413
15-
@example
16-
```
17-
cliTruncate('unicorns', 5, {position: 'end', space: true});
18-
//=> 'uni …'
14+
@example
15+
```
16+
import cliTruncate from 'cli-truncate';
1917
20-
cliTruncate('unicorns', 5, {position: 'end', space: false});
21-
//=> 'unic…'
18+
cliTruncate('unicorns', 5, {position: 'end', space: true});
19+
//=> 'uni …'
2220
23-
cliTruncate('unicorns', 6, {position: 'start', space: true});
24-
//=> '… orns'
21+
cliTruncate('unicorns', 5, {position: 'end', space: false});
22+
//=> 'unic…'
2523
26-
cliTruncate('unicorns', 7, {position: 'middle', space: true});
27-
//=> 'uni … s'
28-
```
29-
*/
30-
readonly space?: boolean;
24+
cliTruncate('unicorns', 6, {position: 'start', space: true});
25+
//=> '… orns'
3126
32-
/**
33-
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
27+
cliTruncate('unicorns', 7, {position: 'middle', space: true});
28+
//=> 'uni … s'
29+
```
30+
*/
31+
readonly space?: boolean;
3432

35-
@default false
33+
/**
34+
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
3635
37-
@example
38-
```
39-
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
40-
//=> '…rainbow dragons'
36+
@default false
4137
42-
cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
43-
//=> 'unicorns…dragons'
38+
@example
39+
```
40+
import cliTruncate from 'cli-truncate';
4441
45-
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
46-
//=> 'unico…'
47-
````
48-
*/
49-
readonly preferTruncationOnSpace?: boolean;
50-
}
42+
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
43+
//=> '…rainbow dragons'
44+
45+
cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
46+
//=> 'unicorns…dragons'
47+
48+
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
49+
//=> 'unico…'
50+
````
51+
*/
52+
readonly preferTruncationOnSpace?: boolean;
5153
}
5254

5355
/**
5456
Truncate a string to a specific width in the terminal.
5557
5658
@param text - Text to truncate.
57-
@param columns - Columns to occupy in the terminal.
59+
@param columns - The number of columns to occupy in the terminal.
5860
5961
@example
6062
```
61-
import cliTruncate = require('cli-truncate');
63+
import cliTruncate from 'cli-truncate';
6264
6365
cliTruncate('unicorn', 4);
6466
//=> 'uni…'
@@ -87,10 +89,8 @@ cliTruncate(paragraph, process.stdout.columns));
8789
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
8890
```
8991
*/
90-
declare function cliTruncate(
92+
export default function cliTruncate(
9193
text: string,
9294
columns: number,
93-
options?: cliTruncate.Options
95+
options?: Options
9496
): string;
95-
96-
export = cliTruncate;

‎index.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
'use strict';
2-
const sliceAnsi = require('slice-ansi');
3-
const stringWidth = require('string-width');
1+
import sliceAnsi from 'slice-ansi';
2+
import stringWidth from 'string-width';
43

5-
function getIndexOfNearestSpace(string, index, shouldSearchRight) {
6-
if (string.charAt(index) === ' ') {
7-
return index;
4+
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
5+
if (string.charAt(wantedIndex) === ' ') {
6+
return wantedIndex;
87
}
98

10-
for (let i = 1; i <= 3; i++) {
9+
for (let index = 1; index <= 3; index++) {
1110
if (shouldSearchRight) {
12-
if (string.charAt(index + i) === ' ') {
13-
return index + i;
11+
if (string.charAt(wantedIndex + index) === ' ') {
12+
return wantedIndex + index;
1413
}
15-
} else if (string.charAt(index - i) === ' ') {
16-
return index - i;
14+
} else if (string.charAt(wantedIndex - index) === ' ') {
15+
return wantedIndex - index;
1716
}
1817
}
1918

20-
return index;
19+
return wantedIndex;
2120
}
2221

23-
module.exports = (text, columns, options) => {
22+
export default function cliTruncate(text, columns, options) {
2423
options = {
2524
position: 'end',
2625
preferTruncationOnSpace: false,
27-
...options
26+
...options,
2827
};
2928

3029
const {position, space, preferTruncationOnSpace} = options;
@@ -69,7 +68,7 @@ module.exports = (text, columns, options) => {
6968

7069
if (position === 'middle') {
7170
if (space === true) {
72-
ellipsis = ' ' + ellipsis + ' ';
71+
ellipsis = ` ${ellipsis} `;
7372
ellipsisWidth = 3;
7473
}
7574

@@ -82,9 +81,9 @@ module.exports = (text, columns, options) => {
8281
}
8382

8483
return (
85-
sliceAnsi(text, 0, half) +
86-
ellipsis +
87-
sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
84+
sliceAnsi(text, 0, half)
85+
+ ellipsis
86+
+ sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
8887
);
8988
}
9089

@@ -95,12 +94,12 @@ module.exports = (text, columns, options) => {
9594
}
9695

9796
if (space === true) {
98-
ellipsis = ' ' + ellipsis;
97+
ellipsis = ` ${ellipsis}`;
9998
ellipsisWidth = 2;
10099
}
101100

102101
return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis;
103102
}
104103

105104
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
106-
};
105+
}

‎index.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType} from 'tsd';
2-
import cliTruncate = require('.');
2+
import cliTruncate from './index.js';
33

44
expectType<string>(cliTruncate('unicorn', 4));
55
expectType<string>(cliTruncate('unicorn', 4, {position: 'start'}));

‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

‎package.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"author": {
99
"name": "Sindre Sorhus",
1010
"email": "sindresorhus@gmail.com",
11-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=8"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd"
@@ -35,12 +37,12 @@
3537
"string"
3638
],
3739
"dependencies": {
38-
"slice-ansi": "^3.0.0",
39-
"string-width": "^4.2.0"
40+
"slice-ansi": "^5.0.0",
41+
"string-width": "^5.0.0"
4042
},
4143
"devDependencies": {
42-
"ava": "^2.1.0",
43-
"tsd": "^0.11.0",
44-
"xo": "^0.25.3"
44+
"ava": "^3.15.0",
45+
"tsd": "^0.17.0",
46+
"xo": "^0.44.0"
4547
}
4648
}

‎readme.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ npm install cli-truncate
1313
## Usage
1414

1515
```js
16-
const cliTruncate = require('cli-truncate');
16+
import cliTruncate from 'cli-truncate';
1717

1818
cliTruncate('unicorn', 4);
1919
//=> 'uni…'
@@ -59,7 +59,7 @@ Text to truncate.
5959

6060
Type: `number`
6161

62-
Columns to occupy in the terminal.
62+
The number of columns to occupy in the terminal.
6363

6464
#### options
6565

@@ -71,7 +71,7 @@ Type: `string`\
7171
Default: `'end'`\
7272
Values: `'start'` `'middle'` `'end'`
7373

74-
Position to truncate the string.
74+
The position to truncate the string.
7575

7676
##### space
7777

@@ -81,6 +81,8 @@ Default: `false`
8181
Add a space between the text and the ellipsis.
8282

8383
```js
84+
import cliTruncate from 'cli-truncate';
85+
8486
cliTruncate('unicorns', 5, {space: false});
8587
//=> 'unic…'
8688

@@ -102,6 +104,8 @@ Default: `false`
102104
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
103105

104106
```js
107+
import cliTruncate from 'cli-truncate';
108+
105109
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true})
106110
//=> '…rainbow dragons'
107111

@@ -125,7 +129,6 @@ cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncation
125129
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
126130
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
127131

128-
129132
---
130133

131134
<div align="center">

‎test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import cliTruncate from '.';
2+
import cliTruncate from './index.js';
33

44
test('main', t => {
55
t.is(cliTruncate('unicorn', 4), 'uni…');

0 commit comments

Comments
 (0)
Please sign in to comment.