Skip to content

Commit e681f7d

Browse files
authored
Require Node.js 12.20 and add an option to count ANSI escape codes (#16)
1 parent 0656975 commit e681f7d

File tree

7 files changed

+52
-25
lines changed

7 files changed

+52
-25
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
1616
steps:
1717
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
18+
- uses: actions/setup-node@v2
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install

index.d.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
export interface Options {
2+
/**
3+
Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default.
4+
5+
@default false
6+
*/
7+
readonly countAnsiEscapeCodes?: boolean;
8+
}
9+
110
/**
211
Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi).
312
413
`String#length` erroneously counts [astral symbols](https://web.archive.org/web/20150721114550/http://www.tlg.uci.edu/~opoudjis/unicode/unicode_astral.html) as two characters.
514
615
@example
716
```
8-
import stringLength = require('string-length');
17+
import stringLength from 'string-length';
918
1019
'🐴'.length;
1120
//=> 2
@@ -17,6 +26,4 @@ stringLength('\u001B[1municorn\u001B[22m');
1726
//=> 7
1827
```
1928
*/
20-
declare function stringLength(string: string): number;
21-
22-
export = stringLength;
29+
export default function stringLength(string: string, options?: Options): number;

index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
'use strict';
2-
const stripAnsi = require('strip-ansi');
3-
const charRegex = require('char-regex');
1+
import stripAnsi from 'strip-ansi';
2+
import charRegex from 'char-regex';
43

5-
const stringLength = string => {
4+
export default function stringLength(string, {countAnsiEscapeCodes = false} = {}) {
65
if (string === '') {
76
return 0;
87
}
98

10-
const strippedString = stripAnsi(string);
9+
if (!countAnsiEscapeCodes) {
10+
string = stripAnsi(string);
11+
}
1112

12-
if (strippedString === '') {
13+
if (string === '') {
1314
return 0;
1415
}
1516

16-
return strippedString.match(charRegex()).length;
17-
};
18-
19-
module.exports = stringLength;
17+
return string.match(charRegex()).length;
18+
}

index.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {expectType} from 'tsd';
2-
import stringLength = require('.');
2+
import stringLength from './index.js';
33

44
expectType<number>(stringLength('🐴'));
55
expectType<number>(stringLength('\u001B[1municorn\u001B[22m'));
6+
expectType<number>(stringLength('\u001B[1municorn\u001B[22m', {countAnsiEscapeCodes: true}));

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"url": "https://sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=10"
13+
"node": ">=12.20"
1414
},
15+
"type": "module",
16+
"exports": "./index.js",
1517
"scripts": {
1618
"test": "xo && ava && tsd"
1719
},
@@ -34,12 +36,12 @@
3436
"codes"
3537
],
3638
"dependencies": {
37-
"char-regex": "^1.0.2",
39+
"char-regex": "^2.0.0",
3840
"strip-ansi": "^6.0.0"
3941
},
4042
"devDependencies": {
41-
"ava": "^3.1.0",
42-
"tsd": "^0.11.0",
43-
"xo": "^0.25.3"
43+
"ava": "^3.15.0",
44+
"tsd": "^0.17.0",
45+
"xo": "^0.40.2"
4446
}
4547
}

readme.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ npm install string-length
1313
## Usage
1414

1515
```js
16-
const stringLength = require('string-length');
16+
import stringLength from 'string-length';
1717

1818
'🐴'.length;
1919
//=> 2
@@ -25,6 +25,21 @@ stringLength('\u001B[1municorn\u001B[22m');
2525
//=> 7
2626
```
2727

28+
## API
29+
30+
### stringLength(string, options?)
31+
32+
#### options
33+
34+
Type: `object`
35+
36+
##### countAnsiEscapeCodes
37+
38+
Type: `boolean`\
39+
Default: `false`
40+
41+
Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default.
42+
2843
## Related
2944

3045
- [string-length-cli](https://github.com/LitoMore/string-length-cli) - CLI for this module

test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
const test = require('ava');
2-
const stringLength = require('.');
1+
import test from 'ava';
2+
import stringLength from './index.js';
33

44
test('get the real length of a string', t => {
55
t.is(stringLength(''), 0);
66
t.is(stringLength('\u001B[1m\u001B[22m'), 0);
7+
t.is(stringLength('\u001B[1m\u001B[22m', {countAnsiEscapeCodes: true}), 9);
78
t.is(stringLength('𠀔'), 1);
89
t.is(stringLength('foo𠁐bar𠀃'), 8);
910
t.is(stringLength('あ'), 1);
1011
t.is(stringLength('谢'), 1);
1112
t.is(stringLength('🐴'), 1);
1213
t.is(stringLength('𝌆'), 1);
1314
t.is(stringLength('\u001B[1mfoo\u001B[22m'), 3);
15+
t.is(stringLength('\u001B[1mfoo\u001B[22m', {countAnsiEscapeCodes: true}), 12);
1416
t.is(stringLength('❤️'), 1);
1517
t.is(stringLength('👊🏽'), 1);
1618
t.is(stringLength('🏴󠁧󠁢󠁥󠁮󠁧󠁿❤️谢👪'), 4);
1719
t.is(stringLength('\u001B[1m👩‍👧‍👦°✿\u001B[22m'), 3);
20+
t.is(stringLength('\u001B[1m👩‍👧‍👦°✿\u001B[22m', {countAnsiEscapeCodes: true}), 12);
1821
});

0 commit comments

Comments
 (0)