From 903fa50ee9c8fa2e5e5d3e3d42c31861a011bb44 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Sat, 5 Jun 2021 00:26:22 +1200 Subject: [PATCH 1/2] Require Node.js 12.20 and add an option to count ansi escape codes Signed-off-by: Richie Bendall --- .github/workflows/main.yml | 4 ++-- index.d.ts | 15 +++++++++++---- index.js | 19 +++++++++---------- index.test-d.ts | 3 ++- package.json | 12 +++++++----- readme.md | 17 ++++++++++++++++- test.js | 7 +++++-- 7 files changed, 52 insertions(+), 25 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c1870cf..3b8aa86 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,12 +10,12 @@ jobs: fail-fast: false matrix: node-version: + - 16 - 14 - 12 - - 10 steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 9d3cc78..95ef0aa 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,12 @@ +export interface Options { + /** + Whether ansi escape codes should be counted. By default they are ignored. + + @default false + */ + countAnsiEscapeCodes?: boolean +} + /** Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi). @@ -5,7 +14,7 @@ Get the real length of a string - by correctly counting astral symbols and ignor @example ``` -import stringLength = require('string-length'); +import stringLength from 'string-length'; '๐Ÿด'.length; //=> 2 @@ -17,6 +26,4 @@ stringLength('\u001B[1municorn\u001B[22m'); //=> 7 ``` */ -declare function stringLength(string: string): number; - -export = stringLength; +export default function stringLength(string: string, options?: Options): number; diff --git a/index.js b/index.js index c2589a2..eea90e8 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,18 @@ -'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 === '') { 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; +} \ No newline at end of file diff --git a/index.test-d.ts b/index.test-d.ts index 09d16b9..86cdce0 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,6 @@ import {expectType} from 'tsd'; -import stringLength = require('.'); +import stringLength from './index.js'; expectType(stringLength('๐Ÿด')); expectType(stringLength('\u001B[1municorn\u001B[22m')); +expectType(stringLength('\u001B[1municorn\u001B[22m', {countAnsiEscapeCodes: true})); diff --git a/package.json b/package.json index 5acf08f..f34a87b 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "url": "https://sindresorhus.com" }, "engines": { - "node": ">=10" + "node": ">=12.20" }, + "type": "module", + "exports": "./index.js", "scripts": { "test": "xo && ava && tsd" }, @@ -34,12 +36,12 @@ "codes" ], "dependencies": { - "char-regex": "^1.0.2", + "char-regex": "^2.0.0", "strip-ansi": "^6.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" } } diff --git a/readme.md b/readme.md index 6156940..65d79d3 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ $ npm install string-length ## Usage ```js -const stringLength = require('string-length'); +import stringLength from 'string-length'; '๐Ÿด'.length; //=> 2 @@ -25,6 +25,21 @@ stringLength('\u001B[1municorn\u001B[22m'); //=> 7 ``` +## API + +### stringLength(string, options?) + +#### options + +Type: `object` + +##### countAnsiEscapeCodes + +Type: `boolean`\ +Default: `true` + +Whether ansi escape codes should be counted. By default they are ignored. + ## Related - [string-length-cli](https://github.com/LitoMore/string-length-cli) - CLI for this module diff --git a/test.js b/test.js index dcbf97d..d2574f5 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,10 @@ -const test = require('ava'); -const stringLength = require('.'); +import test from 'ava' +import stringLength from './index.js' test('get the real length of a string', t => { t.is(stringLength(''), 0); t.is(stringLength('\u001B[1m\u001B[22m'), 0); + t.is(stringLength('\u001B[1m\u001B[22m', {countAnsiEscapeCodes: true}), 9); t.is(stringLength('๐ €”'), 1); t.is(stringLength('foo๐ bar๐ €ƒ'), 8); t.is(stringLength('ใ‚'), 1); @@ -11,8 +12,10 @@ test('get the real length of a string', t => { t.is(stringLength('๐Ÿด'), 1); t.is(stringLength('๐Œ†'), 1); t.is(stringLength('\u001B[1mfoo\u001B[22m'), 3); + t.is(stringLength('\u001B[1mfoo\u001B[22m', {countAnsiEscapeCodes: true}), 12); t.is(stringLength('โค๏ธ'), 1); t.is(stringLength('๐Ÿ‘Š๐Ÿฝ'), 1); t.is(stringLength('๐Ÿด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟโค๏ธ่ฐข๐Ÿ‘ช'), 4); t.is(stringLength('\u001B[1m๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆยฐโœฟ\u001B[22m'), 3); + t.is(stringLength('\u001B[1m๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆยฐโœฟ\u001B[22m', {countAnsiEscapeCodes: true}), 12); }); From df7eb9f6dcb4a5fdfaabc986d3b9e6205a1ab941 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Sun, 6 Jun 2021 01:57:54 +1200 Subject: [PATCH 2/2] Fix Signed-off-by: Richie Bendall --- index.d.ts | 4 ++-- index.js | 2 +- readme.md | 4 ++-- test.js | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 95ef0aa..a3af616 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,10 +1,10 @@ export interface Options { /** - Whether ansi escape codes should be counted. By default they are ignored. + Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default. @default false */ - countAnsiEscapeCodes?: boolean + readonly countAnsiEscapeCodes?: boolean; } /** diff --git a/index.js b/index.js index eea90e8..2ec6c62 100644 --- a/index.js +++ b/index.js @@ -15,4 +15,4 @@ export default function stringLength(string, {countAnsiEscapeCodes = false} = {} } return string.match(charRegex()).length; -} \ No newline at end of file +} diff --git a/readme.md b/readme.md index 65d79d3..cf0e88b 100644 --- a/readme.md +++ b/readme.md @@ -36,9 +36,9 @@ Type: `object` ##### countAnsiEscapeCodes Type: `boolean`\ -Default: `true` +Default: `false` -Whether ansi escape codes should be counted. By default they are ignored. +Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default. ## Related diff --git a/test.js b/test.js index d2574f5..7bedaa1 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ -import test from 'ava' -import stringLength from './index.js' +import test from 'ava'; +import stringLength from './index.js'; test('get the real length of a string', t => { t.is(stringLength(''), 0);