Skip to content

Refactor TypeScript definition to CommonJS compatible export #6

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 1 commit into from
Apr 2, 2019
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
39 changes: 24 additions & 15 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
/**
Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi).
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).

`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 from 'string-length';
@example
```
import stringLength = require('string-length');

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

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

stringLength('\u001B[1municorn\u001B[22m');
//=> 7
```
*/
export default function stringLength(string: string): number;
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;
};

export = stringLength;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ const astralRegex = require('astral-regex');
const stringLength = string => stripAnsi(string).replace(astralRegex(), ' ').length;

module.exports = stringLength;
// TODO: Remove this for the next major release
module.exports.default = stringLength;
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd-check';
import stringLength from '.';
import {expectType} from 'tsd';
import stringLength = require('.');

expectType<number>(stringLength('🐴'));
expectType<number>(stringLength('\u001B[1municorn\u001B[22m'));
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -35,11 +35,11 @@
],
"dependencies": {
"astral-regex": "^1.0.0",
"strip-ansi": "^5.1.0"
"strip-ansi": "^5.2.0"
},
"devDependencies": {
"ava": "^1.3.1",
"tsd-check": "^0.5.0",
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
}
}