Format

Introduced in 4.0.0

Stability: 2 - Stable

const Format = require('kado/lib/Format')

The Format library implements a common set of output formatting methods.

Class: Format

Format is a completely static class of loosely related methods

static Format.cookie(name, value, options)

Available Options

Example

const Format = require('kado/lib/Format')
const Module = require('kado/lib/Module')
class MyModule extends Module {
  someRoute (req, res) {
    const cookie = Format.cookie('myCookie', { id: 1 }, { httpOnly: true })
    res.setHeader('Set-Cookie', cookie)
    // or use the built in cookie helper like so
    res.cookie('myCookie', { id: 1 }, { httpOnly: true })
  }
}

static Format.toFixedFix(n, prec)

This method performs similar to built-in toFixed() method with repairs to some edge cases that return strange results when the built-in is used directly.

static Format.number(n, pos, pt, sep)

Reformat provided number in "human" style with commas and dots (or, dots and commas).

static Format.magnitude(val, opts)

Converts input number to the best possible human-readable magnitude, at optional fixed width with K/M/G/T/P/E/Z/Y and suffix which defaults to B

.magnitude() Options

static Format.bytes(number, options)

Functionally similar to "prettyBytes", adapted here to reduce dependency. Calls .magnitude() with unconditional upstream options:

.bytes() Options

static Format.inetPtoN(p)

Convert IPv4 or IPv6 addresses from text to binary form, compatible with standard C inetpton()_ function call

static Format.inetNtoP(n)

Convert IPv4 or IPv6 addresses from binary string to text form, compatible with standard C inetntop()_ function call

static Format.ip(ip, padding, web)

Convert input IPv4 or IPv6 to normalized standard form, with optional padding and/or HTML-safe output

static Format.ProgressBar(fmt, options)

Initialize a ProgressBar with the given fmt string and options or total.

Tokens in fmt

Available Options in options

Format.ProgressBar.setTotal(total)

Change the current instance internal/private total to total.

Format.ProgressBar.setFmt(fmt)

Change the current instance internal/private format to fmt, as described in the constructor.

Format.ProgressBar.tick(len, tokens)

"tick" the progress bar with optional len and optional tokens.

Format.ProgressBar.render(tokens, force)

Method to render the progress bar with optional tokens to place in the progress bar's fmt field.

Format.ProgressBar.update(tokens, force)

Method to "update" the progress bar to represent an exact percentage. The ratio (between 0 and 1) specified will be multiplied by total and floored, representing the closest available "tick." For example, if a progress bar has a length of 3 and update(0.5) is called, the progress will be set to 1.

A ratio of 0.5 will attempt to set the progress to halfway.

Format.ProgressBar.interrupt(message)

Method to "interrupt" the progress bar and write a message above it.

Format.ProgressBar.terminate()

Terminates a progress bar.

static Format.color(message, fgc, bgc, set, rst)

Allows for terminal-destined output to have basic color and styling added.

Valid Color Names
Valid Style Names