Skip to content

Commit

Permalink
Require Node.js 16 and improve length calculation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb authored Jun 4, 2023
1 parent a1cdfe8 commit 51aa68d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 18
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import stripAnsi from 'strip-ansi';
import charRegex from 'char-regex';

const segmenter = new Intl.Segmenter();

export default function stringLength(string, {countAnsiEscapeCodes = false} = {}) {
if (string === '') {
Expand All @@ -14,5 +15,11 @@ export default function stringLength(string, {countAnsiEscapeCodes = false} = {}
return 0;
}

return string.match(charRegex()).length;
let length = 0;

for (const _ of segmenter.segment(string)) { // eslint-disable-line no-unused-vars
length++;
}

return length;
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=12.20"
"node": ">=16"
},
"type": "module",
"exports": "./index.js",
Expand All @@ -37,7 +37,6 @@
"codes"
],
"dependencies": {
"char-regex": "^2.0.0",
"strip-ansi": "^7.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit 51aa68d

Please sign in to comment.