Publican.lib formatting functions
453 words, 3-minute read
The format functions display locale-specific values such as dates and numbers. The libInit() function adds all functions:
publican.config.js excerpt
// Publican configuration
import { Publican, tacs } from 'publican';
import { libInit } from 'publican.lib';
// ...set Publican defaults...
// initialize publican.lib
libInit(publican, tacs);
// build
await publican.build();
Or you can add functions individually:
publican.config.js excerpt
// Publican configuration
import { Publican, tacs } from 'publican';
import { number, currency } from 'publican.lib/format';
// ...set Publican defaults...
// add global formatting functions
tacs.lib = tacs.lib || {};
tacs.lib.format = { number, currency };
// build
await publican.build();
setLocale(locale) #
Defines the default locale when none is explicitly set in other formatting functions.
publican.config.js excerpt
tacs.lib.format.setLocale( 'es-ES' );
number(num [, locale]) #
Returns a formatted number with appropriate thousand and fraction symbols.
template
<p>US: ${ tacs.lib.format.number( 12345.678 ) }</p>
<p>ES: ${ tacs.lib.format.number( 12345.678, 'es-ES' ) }</p>
US: 12,345.678
ES: 12.345,678
currency(num, currency [, locale]) #
Returns a formatted currency with appropriate thousand and fraction symbols.
template
<p>US: ${ tacs.lib.format.currency( 12345.678, 'USD' ) }</p>
<p>ES: ${ tacs.lib.format.currency( 12345.678, 'USD', 'es-ES' ) }</p>
US: $12,345.68
ES: 12.345,68 $
numberRound(num [, locale]) #
Rounds a number up depending on its size:
- < 1,000: to nearest 1
- > 1,000 and < 10,000: to nearest 10
- > 10,000 and < 100,000: to nearest 100
- etc.
template
<p>${ tacs.lib.format.numberRound( 12345 ) }</p>
<p>${ tacs.lib.format.numberRound( 123456 ) }</p>
12,400
124,000
dateHuman(date [, locale]) #
Returns a date in human-readable format:
template
<p>US: ${ tacs.lib.format.dateHuman('2026-09-05', 'en-US') }</p>
<p>UK: ${ tacs.lib.format.dateHuman('2026-09-05', 'en-GB') }</p>
<p>ES: ${ tacs.lib.format.dateHuman('2026-09-05', 'es-ES') }</p>
US: September 5, 2026
UK: 5 September 2026
ES: 5 de septiembre de 2026
dateUTC(date) #
Returns a date in UTC format.
template
<pre>${ tacs.lib.format.dateUTC('2026-09-05T01:23:45') }</pre>
Sat, 05 Sep 2026 01:23:45 GMT
dateISO(date) #
Returns a date in ISO format.
template
<pre>tacs.lib.format.dateISO('2026-09-05T01:23:45')</pre>
2026-09-05
dateISOfull(date) #
Returns a full datetime in ISO format.
template
<pre>tacs.lib.format.dateISOfull('2026-09-05T01:23:45')</pre>
2026-09-05T01:23:45.000Z
dateYear(date) #
Returns a year.
template
<pre>tacs.lib.format.dateYear('2026-09-05T01:23:45')</pre>
2026