diff --git a/index.d.ts b/index.d.ts index 2c89246..0136832 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; diff --git a/index.js b/index.js index 6a455a9..ee41f6d 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/package.json b/package.json index 43fb709..020bb53 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=8" + "node": ">=10" }, "scripts": { "test": "xo && ava && tsd" @@ -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" } } diff --git a/test.js b/test.js index 6b5a209..e1691f2 100644 --- a/test.js +++ b/test.js @@ -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); @@ -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); });