Formatr
Simple utility for formatting string messages. If a single object is passed it is converted to string using node's util.inspect. When string format tokens are present e.g. %s, %d, %j and so on the string is formatted using node's util.format. For more advanced formatting you can use templating which allows piping values in order through custom transform handlers. Kind of similar to Angular's early pipes.
Install
$ npm install formatr
Usage
Import the module.
;// OR;
Format an object.
;// util.inspect converts object to string.result = "{ category: 'Movies', title: 'Office Space' }"
Format using string format tokens.
;// util.format maps args to tokens.result = 'The movie Office Space was released in 1999'
Format using templating.
;;// template formatter maps object values.result = 'My name is Milton Waddams and I want my Swingline stapler.'
Format using templating and transforms.
// Add custom transform for quotes.formatr.setOption'transforms.quote',`""`;;;// values are mapped from object name is titlecased and wrapped in quotes.result = 'My name is "Milton Waddams" and I want my Swingline stapler.'
Options
Please refer to docs for more details but here are the basics.
Default
When using templating ONLY this is the default value when undefined is returned.
Name | default |
Type | string |
Default | '' |
Colorize
This property is passed to util.inspect when formatting objects.
Name | colorize |
Type | boolean |
Default | false |
Hidden
This property is passed to util.inspect when formatting objects.
Name | hidden |
Type | boolean |
Default | null |
Depth
This property is passed to util.inspect when formatting objects.
Name | depth |
Type | number |
Default | null |
Exp
When using template formatting this is the RegExp for parsing templates.
Name | exp |
Type | RegExp |
Default | /{{([\s\S]+?)}}/g |
Strip
When using template formatting this is the RegExp for stripping parsed templates.
Name | strip |
Type | RegExp |
Default | /[{}]+/g |
Split
When using template formatting this character used to split transforms.
Name | split |
Type | string |
Default | | |
Transform
When using template formatting this is a handler run on each value after getting from format object.
Name | transform |
Type | function |
Signature | (val?: any, key?: string, obj?: object) |
Default | undefined |
Transforms
When using template formatting this an object of built in transforms.
Name | transforms |
Type | object |
Default | capitalize, lowercase, uppercase, camelcase, titlecase |
Docs
See https://blujedis.github.io/formatr/
Change
See CHANGE.md
License
See LICENSE.md