Skip to content

Commit 8b906fc

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#6)
1 parent c6c9629 commit 8b906fc

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

index.d.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
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).
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).
34
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.
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.
56
6-
@example
7-
```
8-
import stringLength from 'string-length';
7+
@example
8+
```
9+
import stringLength = require('string-length');
910
10-
'🐴'.length;
11-
//=> 2
11+
'🐴'.length;
12+
//=> 2
1213
13-
stringLength('🐴');
14-
//=> 1
14+
stringLength('🐴');
15+
//=> 1
1516
16-
stringLength('\u001B[1municorn\u001B[22m');
17-
//=> 7
18-
```
19-
*/
20-
export default function stringLength(string: string): number;
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+
};
28+
29+
export = stringLength;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ const astralRegex = require('astral-regex');
55
const stringLength = string => stripAnsi(string).replace(astralRegex(), ' ').length;
66

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

index.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {expectType} from 'tsd-check';
2-
import stringLength from '.';
1+
import {expectType} from 'tsd';
2+
import stringLength = require('.');
33

44
expectType<number>(stringLength('🐴'));
55
expectType<number>(stringLength('\u001B[1municorn\u001B[22m'));

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=8"
1414
},
1515
"scripts": {
16-
"test": "xo && ava && tsd-check"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
1919
"index.js",
@@ -35,11 +35,11 @@
3535
],
3636
"dependencies": {
3737
"astral-regex": "^1.0.0",
38-
"strip-ansi": "^5.1.0"
38+
"strip-ansi": "^5.2.0"
3939
},
4040
"devDependencies": {
41-
"ava": "^1.3.1",
42-
"tsd-check": "^0.5.0",
41+
"ava": "^1.4.1",
42+
"tsd": "^0.7.1",
4343
"xo": "^0.24.0"
4444
}
4545
}

0 commit comments

Comments
 (0)