Skip to content

Commit 6c47986

Browse files
authored
Fix character matching for emoji (#7)
1 parent 39e7dcb commit 6c47986

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

index.d.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
declare const stringLength: {
2-
/**
3-
Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi).
1+
/**
2+
Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi).
43
5-
`String#length` errornously counts [astral symbols](https://web.archive.org/web/20150721114550/http://www.tlg.uci.edu/~opoudjis/unicode/unicode_astral.html) as two characters.
4+
`String#length` errornously counts [astral symbols](https://web.archive.org/web/20150721114550/http://www.tlg.uci.edu/~opoudjis/unicode/unicode_astral.html) as two characters.
65
7-
@example
8-
```
9-
import stringLength = require('string-length');
6+
@example
7+
```
8+
import stringLength = require('string-length');
109
11-
'🐴'.length;
12-
//=> 2
10+
'🐴'.length;
11+
//=> 2
1312
14-
stringLength('🐴');
15-
//=> 1
13+
stringLength('🐴');
14+
//=> 1
1615
17-
stringLength('\u001B[1municorn\u001B[22m');
18-
//=> 7
19-
```
20-
*/
21-
(string: string): number;
22-
23-
// TODO: Remove this for the next major release, refactor the whole definition to:
24-
// declare function stringLength(string: string): number;
25-
// export = stringLength;
26-
default: typeof stringLength;
27-
};
16+
stringLength('\u001B[1municorn\u001B[22m');
17+
//=> 7
18+
```
19+
*/
20+
declare function stringLength(string: string): number;
2821

2922
export = stringLength;

index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict';
22
const stripAnsi = require('strip-ansi');
3-
const astralRegex = require('astral-regex');
3+
const charRegex = require('char-regex');
44

5-
const stringLength = string => stripAnsi(string).replace(astralRegex(), ' ').length;
5+
const stringLength = string => stripAnsi(string).match(charRegex()).length;
66

77
module.exports = stringLength;
8-
// TODO: Remove this for the next major release
9-
module.exports.default = stringLength;

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=8"
13+
"node": ">=10"
1414
},
1515
"scripts": {
1616
"test": "xo && ava && tsd"
@@ -34,12 +34,12 @@
3434
"codes"
3535
],
3636
"dependencies": {
37-
"astral-regex": "^1.0.0",
38-
"strip-ansi": "^5.2.0"
37+
"char-regex": "^1.0.2",
38+
"strip-ansi": "^6.0.0"
3939
},
4040
"devDependencies": {
41-
"ava": "^1.4.1",
42-
"tsd": "^0.7.1",
43-
"xo": "^0.24.0"
41+
"ava": "^3.1.0",
42+
"tsd": "^0.11.0",
43+
"xo": "^0.25.3"
4444
}
4545
}

test.js

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

44
test('get the real length of a string', t => {
55
t.is(stringLength('𠀔'), 1);
@@ -9,4 +9,8 @@ test('get the real length of a string', t => {
99
t.is(stringLength('🐴'), 1);
1010
t.is(stringLength('𝌆'), 1);
1111
t.is(stringLength('\u001B[1mfoo\u001B[22m'), 3);
12+
t.is(stringLength('❤️'), 1);
13+
t.is(stringLength('👊🏽'), 1);
14+
t.is(stringLength('🏴󠁧󠁢󠁥󠁮󠁧󠁿❤️谢👪'), 4);
15+
t.is(stringLength('\u001B[1m👩‍👧‍👦°✿\u001B[22m'), 3);
1216
});

0 commit comments

Comments
 (0)