Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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;
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "string-length",
"version": "3.1.0",
"version": "4.0.0",
"description": "Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes",
"license": "MIT",
"repository": "sindresorhus/string-length",
Expand All @@ -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"
}
}
6 changes: 4 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,6 @@ 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);
});