Skip to content

Commit d474918

Browse files
committedJun 13, 2019
Meta tweaks
1 parent baf4461 commit d474918

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ declare namespace cliTruncate {
99

1010
/**
1111
Add a space between the text and the ellipsis.
12-
12+
1313
@default false
1414
1515
@example

‎index.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22
const sliceAnsi = require('slice-ansi');
33
const stringWidth = require('string-width');
44

5+
function getIndexOfNearestSpace(string, index, shouldSearchRight) {
6+
if (string.charAt(index) === ' ') {
7+
return index;
8+
}
9+
10+
for (let i = 1; i <= 3; i++) {
11+
if (shouldSearchRight) {
12+
if (string.charAt(index + i) === ' ') {
13+
return index + i;
14+
}
15+
} else if (string.charAt(index - i) === ' ') {
16+
return index - i;
17+
}
18+
}
19+
20+
return index;
21+
}
22+
523
module.exports = (text, columns, options) => {
624
options = {
725
position: 'end',
@@ -86,21 +104,3 @@ module.exports = (text, columns, options) => {
86104

87105
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
88106
};
89-
90-
function getIndexOfNearestSpace(string, index, shouldSearchRight) {
91-
if (string.charAt(index) === ' ') {
92-
return index;
93-
}
94-
95-
for (let i = 1; i <= 3; i++) {
96-
if (shouldSearchRight) {
97-
if (string.charAt(index + i) === ' ') {
98-
return index + i;
99-
}
100-
} else if (string.charAt(index - i) === ' ') {
101-
return index - i;
102-
}
103-
}
104-
105-
return index;
106-
}

‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
"term",
3131
"shell",
3232
"width",
33-
"ansi"
33+
"ansi",
34+
"string"
3435
],
3536
"dependencies": {
3637
"slice-ansi": "^2.1.0",
3738
"string-width": "^4.1.0"
3839
},
3940
"devDependencies": {
40-
"ava": "^1.4.1",
41+
"ava": "^2.1.0",
4142
"tsd": "^0.7.2",
4243
"xo": "^0.24.0"
4344
}

‎readme.md

+6-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ cliTruncate(paragraph, process.stdout.columns));
5050

5151
## API
5252

53-
### cliTruncate(text, columns, [options])
53+
### cliTruncate(text, columns, options?)
5454

5555
#### text
5656

@@ -66,13 +66,13 @@ Columns to occupy in the terminal.
6666

6767
#### options
6868

69-
Type: `Object`
69+
Type: `object`
7070

7171
##### position
7272

7373
Type: `string`<br>
74-
Default: `end`<br>
75-
Values: `start` `middle` `end`
74+
Default: `'end'`<br>
75+
Values: `'start'` `'middle'` `'end'`
7676

7777
Position to truncate the string.
7878

@@ -118,17 +118,13 @@ cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncatio
118118
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true})
119119
//=> 'unico…'
120120

121-
// preferTruncationOnSpace would have no effect if space isn't found within next 3 indexes
121+
// preferTruncationOnSpace would have no effect if space isn't found within next 3 indexes
122122
cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true})
123123
//=> 'uni…ns'
124124
```
125125

126+
126127
## Related
127128

128129
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
129130
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
130-
131-
132-
## License
133-
134-
MIT © [Sindre Sorhus](https://sindresorhus.com)

0 commit comments

Comments
 (0)
Please sign in to comment.