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)
name{string} name of the cookievalue{string|object} value of the cookie, Objects automatically serialized to JSONoptions{object} set cookie options- Return {string} a string ready to set sent view
res.setHeader('Set-Coookie')
Available Options
domain{string} set the domain of the cookie e.g.:{ domain: 'example.com' }expires{string} UTC date string, indicating when the cookie expireshttpOnly{boolean}truewill stop javascript access to the cookiemaxAge{number} number of seconds the cookie shall be validsecure{boolean}truefor HTTPS only cookiessameSite{string} acceptsStrict,Lax, orNoneas values to control cross-origin
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)
n{mixed} the number, or string containing parsable number-like dataprec{number} precision; how many places after decimal to retain- Returns: {string} the
numberreformatted as directed
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)
n{mixed} number, or string containing parsable number-like datapos{number} positions; how many places after decimal to retainpt{mixed} single character to use as the decimal-point (en-us:.); OR, a valid locale identifier string, to use Intl/ICU to determine bothptandsepsep{string} single character to use as the thousands separator (en-us:,); OR, optional/undefinedwhen passing locale aspt- Returns: {string} input
nreformatted as directed, including all separators
Reformat provided number in "human" style with commas and dots (or, dots and commas).
static Format.magnitude(val, opts)
val{mixed} number, or string containing parsable number-like dataopts{object} options, as described below- Returns: {string} input
nreformatted as directed
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
'magnitudes'{string} (Default:'KMGTPEZY') string listing the displayed magnitude prefixes in ascending scale from 1000's to yotta's'force'{mixed} (Default:false) force magnitude, disable automatic scaling; accepts single character of the availablemagnitudes'suffix'{string} (Default:'B') suffix for units label'round'{boolean} (Default:true) round to nearest whole number'space'{boolean} (Default:false) insert one space before units label'locale'(Default:undefined) any valid Intl/ICU locale string to adjust comma and dot usage;undefineduses system default locale
static Format.bytes(number, options)
number{mixed} number, or string containing parsable number-like dataoptions{object} options, as described below- Returns: {string} input
nreformatted as directed
Functionally similar to "prettyBytes", adapted here to reduce dependency.
Calls .magnitude() with unconditional upstream options:
{ space: true, round: false, magnitudes: 'kMGTPEZY' }
.bytes() Options
- bits {boolean} add upstream option
{ suffix: 'bit' } - locale {string} pass through to upstream option
locale
static Format.inetPtoN(p)
p{string} any individual standard IPv4 or IPv6 address- Returns: {string} address, packed into binary string:
- of length
4with IPv4 input - of length
16with IPv6 input
Convert IPv4 or IPv6 addresses from text to binary form, compatible with standard C inetpton()_ function call
static Format.inetNtoP(n)
n{string} IPv4 binary string of length4, or IPv6 of length16- Returns: {string} IP address, in normalized standard form
Convert IPv4 or IPv6 addresses from binary string to text form, compatible with standard C inetntop()_ function call
static Format.ip(ip, padding, web)
ip{string} any individual standard IPv4 or IPv6 addresspadding{string} character to use for padding in fixed-position modeweb{boolean} emit HTML-safe non-breaking spaces- Returns: {string} input IP, reformatted
Convert input IPv4 or IPv6 to normalized standard form, with optional padding and/or HTML-safe output
static Format.ProgressBar(fmt, options)
fmt{string} Format descriptoroptions{object|number} options or total- Returns: {ProgressBar} ProgressBar instance
Initialize a ProgressBar with the given fmt string and options or total.
Tokens in fmt
:barthe progress bar itself:currentcurrent tick number:totaltotal ticks:elapsedtime elapsed in seconds:percentcompletion percentage:etaeta in seconds:raterate of ticks per second
Available Options in options
currcurrent completed indextotaltotal number of ticks to completewidththe displayed width of the progress bar defaulting to totalstreamthe output stream defaulting to stderrheadhead character defaulting to complete charactercompletecompletion character defaulting to "="incompleteincomplete character defaulting to "-"renderThrottleminimum time between updates in milliseconds defaulting to 16callbackoptional function to call when the progress bar completesclearwill clear the progress bar upon termination
Format.ProgressBar.setTotal(total)
total{number} Total
Change the current instance internal/private total to total.
Format.ProgressBar.setFmt(fmt)
fmt{string} Format descriptor
Change the current instance internal/private format to fmt, as described in
the constructor.
Format.ProgressBar.tick(len, tokens)
len{number|object} Tick length (or tokens)tokens{object} Tokens
"tick" the progress bar with optional len and optional tokens.
Format.ProgressBar.render(tokens, force)
tokens{object} Tokensforce{boolean} Forcibly render now
Method to render the progress bar with optional tokens to place in the
progress bar's fmt field.
Format.ProgressBar.update(tokens, force)
ratio{number} The ratio (between 0 and 1 inclusive) to set the overall completion to.tokens{object} Tokens
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)
message{string} The message to write.
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)
message{mixed} number, or string containing parsable number-like datafgc{string} color of foregroundbgc{string} color of backgroundset{string} style to setrst{string} style to reset- Returns: {string} input message wrapped in ANSI terminal style and color
Allows for terminal-destined output to have basic color and styling added.
Valid Color Names
'Black','Red','Green','Yellow''Blue','Magenta','Cyan','Light Gray''Dark Gray','Light Red','Light Green''Light Yellow','Light Blue','Light Magenta''Light Cyan','White'
Valid Style Names
- (set)
'Default'* or * (reset)'All' 'Bold','Dim','Underlined''Blink','Reverse','Hidden'