# printable-characters

> A little helper for handling strings containing zero width control characters, ANSI styling, whitespaces, newlines, 💩, etc.

Latest version **1.0.42** (published 2018-07-16) · Unlicense license · 0 weekly downloads

## Install

```sh
npm install printable-characters
pnpm add printable-characters
yarn add printable-characters
bun add printable-characters
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.42 |
| Published | 2018-07-16 |
| First published | 2017-06-30 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 187.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 19 |
| Author | Vitaly Gordon |
| Maintainers | xpl |
| Keywords | string width, string length, real string width, real string length, optical string width, printed width, unicode, codepoints, code points, strlen, zero width, zero width symbols, zero width characters, visible characters, visible symbols, visible, invisible, invisible symbols, invisible characters, printable, printable length, printable symbols, printable characters, non-printable characters, nonprintable characters, characters, symbols, string length, real string length, string trimming, trimming, escapes, escape codes, codes, ansi escapes, tokenizing, ansi, whitespaces |

## Links

- npm: https://www.npmjs.com/package/printable-characters
- Repository: https://github.com/xpl/printable-characters
- Issues: https://github.com/xpl/printable-characters/issues
- npm.io page: https://npm.io/package/printable-characters

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.42 (latest) — 2018-07-16
- 1.0.39 — 2018-04-29
- 1.0.38 — 2017-10-07
- 1.0.37 — 2017-10-07
- 1.0.36 — 2017-10-07
- 1.0.35 — 2017-10-07
- 1.0.34 — 2017-10-07
- 1.0.33 — 2017-09-27
- 1.0.32 — 2017-09-27
- 1.0.31 — 2017-09-27
- 1.0.30 — 2017-09-27
- 1.0.29 — 2017-09-27
- 1.0.28 — 2017-07-23
- 1.0.27 — 2017-07-23
- 1.0.26 — 2017-07-22
- … 25 more at https://npm.io/package/printable-characters/versions

## README

# printable-characters

[![Build Status](https://travis-ci.org/xpl/printable-characters.svg?branch=master)](https://travis-ci.org/xpl/printable-characters) [![Coverage Status](https://coveralls.io/repos/github/xpl/printable-characters/badge.svg)](https://coveralls.io/github/xpl/printable-characters) [![npm](https://img.shields.io/npm/v/printable-characters.svg)](https://npmjs.com/package/printable-characters) [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/xpl/printable-characters.svg)](https://scrutinizer-ci.com/g/xpl/printable-characters/?branch=master) [![dependencies Status](https://david-dm.org/xpl/printable-characters/status.svg)](https://david-dm.org/xpl/printable-characters)

A little helper for handling strings containing zero width characters, ANSI styling, whitespaces, newlines, [weird Unicode 💩 symbols](http://blog.jonnew.com/posts/poo-dot-length-equals-two), etc.

## Determining the real (visible) length of a string

```javascript
const { strlen } = require ('printable-characters')

strlen ('foo bar') // === 7
strlen ('\u001b[106mfoo bar\u001b[49m') // === 7
```

## Detecting blank text

```javascript
const { isBlank } = require ('printable-characters')

isBlank ('foobar') // === false
isBlank ('\u001b[106m  \t  \t   \n     \u001b[49m') // === true
```

## Obtaining a blank string of the same width

```javascript
const { blank } = require ('printable-characters')

blank ('💩')          // === ' '
blank ('foo')         // === '   '
blank ('\tfoo \nfoo') // === '\t    \n   '
blank ('\u001b[22m\u001b[1mfoo \t\u001b[39m\u001b[22m')) // === '    \t'
```

## Matching invisible characters

```javascript
const { ansiEscapeCodes, zeroWidthCharacters } = require ('printable-characters')

const s = '\u001b[106m' + 'foo' + '\n' + 'bar' + '\u001b[49m'

s.replace (ansiEscapeCodes, '')     // === 'foo\nbar'
 .replace (zeroWidthCharacters, '') // === 'foobar'
```

## Getting the first N visible symbols, preserving the invisible parts

Use for safely truncating strings to maximum width without breaking ANSI codes:

```javascript
const { first } = require ('printable-characters')

const s = '\u001b[22mfoobar\u001b[22m'

first (s, 0) // === '\u001b[22m\u001b[22m'
first (s, 1) // === '\u001b[22mf\u001b[22m'
first (s, 3) // === '\u001b[22mfoo\u001b[22m'
first (s, 6) // === '\u001b[22mfoobar\u001b[22m'
```

## Extracting the invisible parts followed by the visible ones (parsing)

```javascript
const { partition } = require ('printable-characters')

partition ('')                        // [                                                     ])
partition ('foo')                     // [['',          'foo']                                 ])
partition ('\u001b[1mfoo')            // [['\u001b[1m', 'foo']                                 ])
partition ('\u001b[1mfoo\u0000bar')   // [['\u001b[1m', 'foo'],   ['\u0000', 'bar']            ])
partition ('\u001b[1mfoo\u0000bar\n') // [['\u001b[1m', 'foo'],   ['\u0000', 'bar'], ['\n', '']])
```

## Applications

- [as-table](https://github.com/xpl/as-table) — a simple function that prints objects as ASCII tables
- [string.bullet](https://github.com/xpl/string.bullet) — ASCII-mode bulleting for the list-style data
- [string.ify](https://github.com/xpl/string.ify) — a fancy pretty printer for the JavaScript entities
- [Ololog!](https://github.com/xpl/ololog) — a better `console.log` for the log-driven debugging junkies!

## TODO

Handle multi-component emojis, as in [this article](http://blog.jonnew.com/posts/poo-dot-length-equals-two):

```javascript
assert.equal (strlen ('👩‍❤️‍💋‍👩'), 1)  // FAILING, see http://blog.jonnew.com/posts/poo-dot-length-equals-two for possible solution
assert.equal (blank ('👩‍❤️‍💋‍👩'), ' ') // FAILING, see http://blog.jonnew.com/posts/poo-dot-length-equals-two for possible solution
```

---
_Source: https://npm.io/package/printable-characters · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
