Appends a bullet before string (to mimick lists in ASCII)

Downloads in past


501.0.126 years ago8 years agoMinified + gzip package size for string.bullet in KB


Build Status Coverage Status npm dependencies Status Scrutinizer Code Quality
npm install string.bullet
const bullet = require ('string.bullet')

Formatting a list item with as a bullet:
bullet ('• ', 'foo\nbar\nbaz') // returns bulleted string
bullet ('• ', ['foo', 'bar', 'baz'])  // ...or arrays (returning an array of resulting lines)
• foo

Humanized rendering of a JSON-like structure:
const left  = '{' + '\t' + 'this_is_a_list: '
    , right = '[foo,\n bar,\n baz]' + '\t' + '}'

bullet (left, right)
{    this_is_a_list: [foo,
                      baz]    }

The problem

Although it seems so simple that one may think it does not deserve a separate GitHub page — it is not so. Imagine we had tabulation symbols or ANSI escape codes (or other control characters) in the bullet string:
const x = '\t\u001b[101m• \u001b[49m'

bullet (x, ['foo', 'bar', 'baz'])

To make proper indentation, one must be able to recognize these special sequences, replacing ordinary letters with whitespaces but keeping these special sequences intact, because we don't know how much screen space they will occupy upon rendering:
\t\u001b[101m• \u001b[49mfoo\n
\t  bar\n
\t  baz

The solution

Under the hood it depends on the tiny printable-characters module that solves that specific problem:
const { ansiEscapeCodes, printableCharacters } = require ('printable-characters')
const indent = bullet.replace (ansiEscapeCodes, '')
                     .replace (printableCharacters, ' ')


  • string.ify — a fancy pretty printer for JavaScript entities
  • ololog — a better console.log for the log-driven debugging junkies!