★ wanayoo — archive 1999 https://github.com/xpl/string.bulletNouvelle recherche | Portail wanayoo
Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.MD

string.bullet

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
  bar
  baz

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,
                      bar,
                      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, ' ')

Applications

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

Releases

No releases published

Packages

No packages published
You can’t perform that action at this time.