pax_global_header00006660000000000000000000000064151341623530014515gustar00rootroot0000000000000052 comment=92065dcd97fa5c6046a6f846e334dc151d52b016 sindresorhus-string-length-2bedc26/000077500000000000000000000000001513416235300175215ustar00rootroot00000000000000sindresorhus-string-length-2bedc26/.editorconfig000066400000000000000000000002571513416235300222020ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 sindresorhus-string-length-2bedc26/.gitattributes000066400000000000000000000000231513416235300224070ustar00rootroot00000000000000* text=auto eol=lf sindresorhus-string-length-2bedc26/.github/000077500000000000000000000000001513416235300210615ustar00rootroot00000000000000sindresorhus-string-length-2bedc26/.github/security.md000066400000000000000000000002631513416235300232530ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. sindresorhus-string-length-2bedc26/.github/workflows/000077500000000000000000000000001513416235300231165ustar00rootroot00000000000000sindresorhus-string-length-2bedc26/.github/workflows/main.yml000066400000000000000000000006451513416235300245720ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 24 - 20 steps: - uses: actions/checkout@v6 - uses: actions/setup-node@v6 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test sindresorhus-string-length-2bedc26/.gitignore000066400000000000000000000000271513416235300215100ustar00rootroot00000000000000node_modules yarn.lock sindresorhus-string-length-2bedc26/.npmrc000066400000000000000000000000231513416235300206340ustar00rootroot00000000000000package-lock=false sindresorhus-string-length-2bedc26/index.d.ts000066400000000000000000000014401513416235300214210ustar00rootroot00000000000000export type 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; }; /** Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi). `String#length` erroneously counts [astral symbols](https://web.archive.org/web/20150721114550/http://www.tlg.uci.edu/~opoudjis/unicode/unicode_astral.html) as two characters. @example ``` import stringLength from 'string-length'; '๐Ÿด'.length; //=> 2 stringLength('๐Ÿด'); //=> 1 stringLength('\u001B[1municorn\u001B[22m'); //=> 7 ``` */ export default function stringLength(string: string, options?: Options): number; sindresorhus-string-length-2bedc26/index.js000066400000000000000000000010541513416235300211660ustar00rootroot00000000000000// eslint-disable-next-line n/prefer-global/process const {stripVTControlCharacters} = globalThis.process?.getBuiltinModule?.('node:util') ?? {}; const segmenter = new Intl.Segmenter(); export default function stringLength(string, {countAnsiEscapeCodes = false} = {}) { if (string === '') { return 0; } if (!countAnsiEscapeCodes && stripVTControlCharacters) { string = stripVTControlCharacters(string); if (string === '') { return 0; } } let length = 0; for (const _ of segmenter.segment(string)) { length++; } return length; } sindresorhus-string-length-2bedc26/index.test-d.ts000066400000000000000000000004201513416235300223730ustar00rootroot00000000000000import {expectType} from 'tsd'; import stringLength from './index.js'; expectType(stringLength('๐Ÿด')); expectType(stringLength('\u001B[1municorn\u001B[22m')); expectType(stringLength('\u001B[1municorn\u001B[22m', {countAnsiEscapeCodes: true})); sindresorhus-string-length-2bedc26/license000066400000000000000000000021351513416235300210670ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. sindresorhus-string-length-2bedc26/package.json000066400000000000000000000015441513416235300220130ustar00rootroot00000000000000{ "name": "string-length", "version": "7.0.1", "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": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": { "types": "./index.d.ts", "default": "./index.js" }, "sideEffects": false, "engines": { "node": ">=20" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "unicode", "string", "length", "size", "count", "astral", "symbol", "surrogates", "codepoints", "ansi", "escape", "codes" ], "devDependencies": { "ava": "^6.4.1", "xo": "^1.2.3" } } sindresorhus-string-length-2bedc26/readme.md000066400000000000000000000017751513416235300213120ustar00rootroot00000000000000# string-length > Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi) `String#length` erroneously counts [astral symbols](https://web.archive.org/web/20150721114550/http://www.tlg.uci.edu/~opoudjis/unicode/unicode_astral.html) as two characters. ## Install ```sh npm install string-length ``` ## Usage ```js import stringLength from 'string-length'; '๐Ÿด'.length; //=> 2 stringLength('๐Ÿด'); //=> 1 stringLength('\u001B[1municorn\u001B[22m'); //=> 7 ``` ## 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 - [string-length-cli](https://github.com/LitoMore/string-length-cli) - CLI for this module - [string-width](https://github.com/sindresorhus/string-width) - Get visual width of a string sindresorhus-string-length-2bedc26/test.js000066400000000000000000000016141513416235300210400ustar00rootroot00000000000000import 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); t.is(stringLength('่ฐข'), 1); 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); });