Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 16, 2021
1 parent 8158802 commit cec9757
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 60 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
39 changes: 15 additions & 24 deletions index.d.ts
@@ -1,29 +1,20 @@
declare const stringWidth: {
/**
Get the visual width of a string - the number of columns required to display it.
/**
Get the visual width of a string - the number of columns required to display it.
Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.
Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.
@example
```
import stringWidth = require('string-width');
@example
```
import stringWidth from 'string-width';
stringWidth('a');
//=> 1
stringWidth('a');
//=> 1
stringWidth('古');
//=> 2
stringWidth('古');
//=> 2
stringWidth('\u001B[1m古\u001B[22m');
//=> 2
```
*/
(string: string): number;

// TODO: remove this in the next major version, refactor the whole definition to:
// declare function stringWidth(string: string): number;
// export = stringWidth;
default: typeof stringWidth;
}

export = stringWidth;
stringWidth('\u001B[1m古\u001B[22m');
//=> 2
```
*/
export default function stringWidth(string: string): number;
29 changes: 12 additions & 17 deletions index.js
@@ -1,9 +1,8 @@
'use strict';
const stripAnsi = require('strip-ansi');
const isFullwidthCodePoint = require('is-fullwidth-code-point');
const emojiRegex = require('emoji-regex');
import stripAnsi from 'strip-ansi';
import isFullwidthCodePoint from 'is-fullwidth-code-point';
import emojiRegex from 'emoji-regex';

const stringWidth = string => {
export default function stringWidth(string) {
if (typeof string !== 'string' || string.length === 0) {
return 0;
}
Expand All @@ -18,30 +17,26 @@ const stringWidth = string => {

let width = 0;

for (let i = 0; i < string.length; i++) {
const code = string.codePointAt(i);
for (let index = 0; index < string.length; index++) {
const codePoint = string.codePointAt(index);

// Ignore control characters
if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {
if (codePoint <= 0x1F || (codePoint >= 0x7F && codePoint <= 0x9F)) {
continue;
}

// Ignore combining characters
if (code >= 0x300 && code <= 0x36F) {
if (codePoint >= 0x300 && codePoint <= 0x36F) {
continue;
}

// Surrogates
if (code > 0xFFFF) {
i++;
if (codePoint > 0xFFFF) {
index++;
}

width += isFullwidthCodePoint(code) ? 2 : 1;
width += isFullwidthCodePoint(codePoint) ? 2 : 1;
}

return width;
};

module.exports = stringWidth;
// TODO: remove this in the next major version
module.exports.default = stringWidth;
}
2 changes: 1 addition & 1 deletion index.test-d.ts
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import stringWidth = require('.');
import stringWidth from './index.js';

expectType<number>(stringWidth('古'));
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
19 changes: 11 additions & 8 deletions package.json
Expand Up @@ -4,13 +4,16 @@
"description": "Get the visual width of a string - the number of columns required to display it",
"license": "MIT",
"repository": "sindresorhus/string-width",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -44,13 +47,13 @@
"fixed-width"
],
"dependencies": {
"emoji-regex": "^8.0.0",
"is-fullwidth-code-point": "^3.0.0",
"strip-ansi": "^6.0.0"
"emoji-regex": "^9.2.2",
"is-fullwidth-code-point": "^4.0.0",
"strip-ansi": "^7.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
6 changes: 1 addition & 5 deletions readme.md
Expand Up @@ -6,18 +6,16 @@ Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_

Useful to be able to measure the actual width of command-line output.


## Install

```
$ npm install string-width
```


## Usage

```js
const stringWidth = require('string-width');
import stringWidth from 'string-width';

stringWidth('a');
//=> 1
Expand All @@ -29,14 +27,12 @@ stringWidth('\u001B[1m古\u001B[22m');
//=> 2
```


## Related

- [string-width-cli](https://github.com/sindresorhus/string-width-cli) - CLI for this module
- [string-length](https://github.com/sindresorhus/string-length) - Get the real length of a string
- [widest-line](https://github.com/sindresorhus/widest-line) - Get the visual width of the widest line in a string


---

<div align="center">
Expand Down
2 changes: 1 addition & 1 deletion test.js
@@ -1,5 +1,5 @@
import test from 'ava';
import stringWidth from '.';
import stringWidth from './index.js';

test('main', t => {
t.is(stringWidth('abcde'), 5);
Expand Down

0 comments on commit cec9757

Please sign in to comment.