printable-characters
A little helper for handling strings containing zero width control characters, ANSI styling, whitespaces, newlines, 💩, etc.
Last updated 8 years ago by xpl .
Unlicense · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install printable-characters 
SYNC missed versions from official npm registry.

printable-characters

Build Status Coverage Status npm Scrutinizer Code Quality dependencies Status

A little helper for handling strings containing zero width characters, ANSI styling, whitespaces, newlines, weird Unicode ???? symbols, etc.

Determining the real (visible) length of a string

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

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

Detecting blank text

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

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

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:

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)

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 — a simple function that prints objects as ASCII tables
  • string.bullet — ASCII-mode bulleting for the list-style data
  • string.ify — a fancy pretty printer for the JavaScript entities
  • Ololog! — a better console.log for the log-driven debugging junkies!

TODO

Handle multi-component emojis, as in this article:

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

Current Tags

  • 1.0.42                                ...           latest (8 years ago)

40 Versions

  • 1.0.42                                ...           8 years ago
  • 1.0.39                                ...           8 years ago
  • 1.0.38                                ...           9 years ago
  • 1.0.37                                ...           9 years ago
  • 1.0.36                                ...           9 years ago
  • 1.0.35                                ...           9 years ago
  • 1.0.34                                ...           9 years ago
  • 1.0.33                                ...           9 years ago
  • 1.0.32                                ...           9 years ago
  • 1.0.31                                ...           9 years ago
  • 1.0.30                                ...           9 years ago
  • 1.0.29                                ...           9 years ago
  • 1.0.28                                ...           9 years ago
  • 1.0.27                                ...           9 years ago
  • 1.0.26                                ...           9 years ago
  • 1.0.25                                ...           9 years ago
  • 1.0.24                                ...           9 years ago
  • 1.0.23                                ...           9 years ago
  • 1.0.22                                ...           9 years ago
  • 1.0.21                                ...           9 years ago
  • 1.0.20                                ...           9 years ago
  • 1.0.18                                ...           9 years ago
  • 1.0.17                                ...           9 years ago
  • 1.0.16                                ...           9 years ago
  • 1.0.15                                ...           9 years ago
  • 1.0.14                                ...           9 years ago
  • 1.0.13                                ...           9 years ago
  • 1.0.12                                ...           9 years ago
  • 1.0.11                                ...           9 years ago
  • 1.0.10                                ...           9 years ago
  • 1.0.9                                ...           9 years ago
  • 1.0.8                                ...           9 years ago
  • 1.0.7                                ...           9 years ago
  • 1.0.6                                ...           9 years ago
  • 1.0.5                                ...           9 years ago
  • 1.0.4                                ...           9 years ago
  • 1.0.3                                ...           9 years ago
  • 1.0.2                                ...           9 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 30
Dependencies (0)
None
Dev Dependencies (8)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |