pax_global_header00006660000000000000000000000064141231332130014503gustar00rootroot0000000000000052 comment=e7a2755c834246c59a4cc5de9471bef2d531a6b1 string-width-4.2.3/000077500000000000000000000000001412313321300141345ustar00rootroot00000000000000string-width-4.2.3/.editorconfig000066400000000000000000000002571412313321300166150ustar00rootroot00000000000000root = 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 string-width-4.2.3/.gitattributes000066400000000000000000000000231412313321300170220ustar00rootroot00000000000000* text=auto eol=lf string-width-4.2.3/.github/000077500000000000000000000000001412313321300154745ustar00rootroot00000000000000string-width-4.2.3/.github/funding.yml000066400000000000000000000001661412313321300176540ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/string-width custom: https://sindresorhus.com/donate string-width-4.2.3/.github/security.md000066400000000000000000000002631412313321300176660ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. string-width-4.2.3/.github/workflows/000077500000000000000000000000001412313321300175315ustar00rootroot00000000000000string-width-4.2.3/.github/workflows/main.yml000066400000000000000000000007021412313321300211770ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 - 10 - 8 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test string-width-4.2.3/.gitignore000066400000000000000000000000271412313321300161230ustar00rootroot00000000000000node_modules yarn.lock string-width-4.2.3/.npmrc000066400000000000000000000000231412313321300152470ustar00rootroot00000000000000package-lock=false string-width-4.2.3/index.d.ts000066400000000000000000000014301412313321300160330ustar00rootroot00000000000000declare const stringWidth: { /** 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. @example ``` import stringWidth = require('string-width'); stringWidth('a'); //=> 1 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; string-width-4.2.3/index.js000066400000000000000000000016331412313321300156040ustar00rootroot00000000000000'use strict'; const stripAnsi = require('strip-ansi'); const isFullwidthCodePoint = require('is-fullwidth-code-point'); const emojiRegex = require('emoji-regex'); const stringWidth = string => { if (typeof string !== 'string' || string.length === 0) { return 0; } string = stripAnsi(string); if (string.length === 0) { return 0; } string = string.replace(emojiRegex(), ' '); let width = 0; for (let i = 0; i < string.length; i++) { const code = string.codePointAt(i); // Ignore control characters if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) { continue; } // Ignore combining characters if (code >= 0x300 && code <= 0x36F) { continue; } // Surrogates if (code > 0xFFFF) { i++; } width += isFullwidthCodePoint(code) ? 2 : 1; } return width; }; module.exports = stringWidth; // TODO: remove this in the next major version module.exports.default = stringWidth; string-width-4.2.3/index.test-d.ts000066400000000000000000000001541412313321300170120ustar00rootroot00000000000000import {expectType} from 'tsd'; import stringWidth = require('.'); expectType(stringWidth('古')); string-width-4.2.3/license000066400000000000000000000021251412313321300155010ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (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. string-width-4.2.3/package.json000066400000000000000000000016551412313321300164310ustar00rootroot00000000000000{ "name": "string-width", "version": "4.2.3", "description": "Get the visual width of a string - the number of columns required to display it", "license": "MIT", "repository": "sindresorhus/string-width", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "string", "character", "unicode", "width", "visual", "column", "columns", "fullwidth", "full-width", "full", "ansi", "escape", "codes", "cli", "command-line", "terminal", "console", "cjk", "chinese", "japanese", "korean", "fixed-width" ], "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" }, "devDependencies": { "ava": "^1.4.1", "tsd": "^0.7.1", "xo": "^0.24.0" } } string-width-4.2.3/readme.md000066400000000000000000000025641412313321300157220ustar00rootroot00000000000000# string-width > 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. 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'); stringWidth('a'); //=> 1 stringWidth('古'); //=> 2 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 ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
string-width-4.2.3/test.js000066400000000000000000000024021412313321300154470ustar00rootroot00000000000000import test from 'ava'; import stringWidth from '.'; test('main', t => { t.is(stringWidth('abcde'), 5); t.is(stringWidth('古池や'), 6); t.is(stringWidth('あいうabc'), 9); t.is(stringWidth('ノード.js'), 9); t.is(stringWidth('你好'), 4); t.is(stringWidth('안녕하세요'), 10); t.is(stringWidth('A\uD83C\uDE00BC'), 5, 'surrogate'); t.is(stringWidth('\u001B[31m\u001B[39m'), 0); t.is(stringWidth('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007'), 5); t.is(stringWidth('\u{231A}'), 2, '⌚ default emoji presentation character (Emoji_Presentation)'); t.is(stringWidth('\u{2194}\u{FE0F}'), 2, '↔️ default text presentation character rendered as emoji'); t.is(stringWidth('\u{1F469}'), 2, '👩 emoji modifier base (Emoji_Modifier_Base)'); t.is(stringWidth('\u{1F469}\u{1F3FF}'), 2, '👩🏿 emoji modifier base followed by a modifier'); }); test('ignores control characters', t => { t.is(stringWidth(String.fromCharCode(0)), 0); t.is(stringWidth(String.fromCharCode(31)), 0); t.is(stringWidth(String.fromCharCode(127)), 0); t.is(stringWidth(String.fromCharCode(134)), 0); t.is(stringWidth(String.fromCharCode(159)), 0); t.is(stringWidth('\u001B'), 0); }); test('handles combining characters', t => { t.is(stringWidth('x\u0300'), 1); });