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


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.color(message, fgc, bgc, set, rst)

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

Valid Color Names
Valid Style Names