Skip to content

Fix character matching for emoji #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
declare const stringLength: {
/**
Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi).
/**
Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi).

`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.
`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.

@example
```
import stringLength = require('string-length');
@example
```
import stringLength = require('string-length');

'🐴'.length;
//=> 2
'🐴'.length;
//=> 2

stringLength('🐴');
//=> 1
stringLength('🐴');
//=> 1

stringLength('\u001B[1municorn\u001B[22m');
//=> 7
```
*/
(string: string): number;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function stringLength(string: string): number;
// export = stringLength;
default: typeof stringLength;
};
stringLength('\u001B[1municorn\u001B[22m');
//=> 7
```
*/
declare function stringLength(string: string): number;

export = stringLength;
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
const stripAnsi = require('strip-ansi');
const astralRegex = require('astral-regex');
const charRegex = require('char-regex');

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

module.exports = stringLength;
// TODO: Remove this for the next major release
module.exports.default = stringLength;
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -34,12 +34,12 @@
"codes"
],
"dependencies": {
"astral-regex": "^1.0.0",
"strip-ansi": "^5.2.0"
"char-regex": "^1.0.2",
"strip-ansi": "^6.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.1.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
}
}
8 changes: 6 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import stringLength from '.';
const test = require('ava');
const stringLength = require('.');

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