string-length
Advanced tools
Comparing version 4.0.2 to 5.0.0
@@ -0,1 +1,10 @@ | ||
export interface Options { | ||
/** | ||
Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default. | ||
@default false | ||
*/ | ||
readonly countAnsiEscapeCodes?: boolean; | ||
} | ||
/** | ||
@@ -8,3 +17,3 @@ Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi). | ||
``` | ||
import stringLength = require('string-length'); | ||
import stringLength from 'string-length'; | ||
@@ -21,4 +30,2 @@ '🐴'.length; | ||
*/ | ||
declare function stringLength(string: string): number; | ||
export = stringLength; | ||
export default function stringLength(string: string, options?: Options): number; |
19
index.js
@@ -1,6 +0,5 @@ | ||
'use strict'; | ||
const stripAnsi = require('strip-ansi'); | ||
const charRegex = require('char-regex'); | ||
import stripAnsi from 'strip-ansi'; | ||
import charRegex from 'char-regex'; | ||
const stringLength = string => { | ||
export default function stringLength(string, {countAnsiEscapeCodes = false} = {}) { | ||
if (string === '') { | ||
@@ -10,11 +9,11 @@ return 0; | ||
const strippedString = stripAnsi(string); | ||
if (!countAnsiEscapeCodes) { | ||
string = stripAnsi(string); | ||
} | ||
if (strippedString === '') { | ||
if (string === '') { | ||
return 0; | ||
} | ||
return strippedString.match(charRegex()).length; | ||
}; | ||
module.exports = stringLength; | ||
return string.match(charRegex()).length; | ||
} |
{ | ||
"name": "string-length", | ||
"version": "4.0.2", | ||
"version": "5.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", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
@@ -13,4 +14,6 @@ "name": "Sindre Sorhus", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=12.20" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"scripts": { | ||
@@ -38,10 +41,10 @@ "test": "xo && ava && tsd" | ||
"dependencies": { | ||
"char-regex": "^1.0.2", | ||
"strip-ansi": "^6.0.0" | ||
"char-regex": "^2.0.0", | ||
"strip-ansi": "^7.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.1.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.25.3" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.40.2" | ||
} | ||
} |
@@ -16,3 +16,3 @@ # string-length | ||
```js | ||
const stringLength = require('string-length'); | ||
import stringLength from 'string-length'; | ||
@@ -29,2 +29,17 @@ '🐴'.length; | ||
## API | ||
### stringLength(string, options?) | ||
#### options | ||
Type: `object` | ||
##### countAnsiEscapeCodes | ||
Type: `boolean`\ | ||
Default: `false` | ||
Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default. | ||
## Related | ||
@@ -31,0 +46,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4631
36
59
Yes
+ Addedansi-regex@6.1.0(transitive)
+ Addedchar-regex@2.0.1(transitive)
+ Addedstrip-ansi@7.1.0(transitive)
- Removedansi-regex@5.0.1(transitive)
- Removedchar-regex@1.0.2(transitive)
- Removedstrip-ansi@6.0.1(transitive)
Updatedchar-regex@^2.0.0
Updatedstrip-ansi@^7.0.0