Formatter Library (NodeJS and Browser)
Features:
- Date Formatter
- Number Formatter
- String Formatter (format, align, pluralize)
- Static Methods
- Languages and Cultures
- MaskJS util support
Usage
NodeJS
var Formatter = ;
Browser
windowFormatter
Date Formatter
Placeholder | Description |
---|---|
yyyy |
Full Year Number |
yy |
Short Year Number |
MM |
Month Number in 2 digits, e.g. '03' |
#M |
Month Number in one or 2 digits |
Mm |
Short Month Name, e.g. 'Jan', 'Feb' |
MMM |
Full Month Name, e.g. 'January' |
dd |
Date Number in 2 digits |
#d |
Date Number in one or 2 digits |
Dd |
Short Day Name, e.g. 'Mo', 'Tu' |
DD |
Full Day Name, e.g. 'Monday' |
HH |
Hours Number in 2 digits, e.g. '03' |
#H |
Hours Number in one or 2 digits |
hh |
alias for 'HH' |
#h |
alias for '#H' |
mm |
Minutes in 2 digits |
#m |
Minutes in on or 2 digits |
ss |
Seconds in 2 digits |
#s |
Seconds in on or 2 digits |
Standalone NodeJS example:
var format = ;var str = ;//> 1 January, 2014 (09:55)
Mask example: mask
div > 'Today - ~[format: today, "#d MMM, yyyy (hh:mm)"]'
javascript model
today:
Output
Today - 1 January, 2014 (09:55)
Javascript example:
var str = mask_;//> 1 January, 2014 (09:55)
Number Formatter
Pattern: e.g. ,0.0
Placeholder | Description |
---|---|
, |
(optional) First char setts the integral part delimiter. Comma is used for default, which is defined by the current culture info. |
0 |
Then goes the integral part of the number. More Zeros can be specified for the minimal digit output |
. |
(optional) Floating point. If omitted then the number is rounded to integer. When no zeros follow the point, faction will be printed as is. |
0 |
(optional) Fraction. When defined, the fraction part of the number is rounded to the specified zeros count. When zeros in pattern are taken in parenthese, then trailing zeros will be removed from result. |
Samples:
Value | Formatter | Result |
---|---|---|
1234.123 |
,00000.0 |
01,234.1 |
1234.123 |
0 |
1234 |
1.5 |
00.00 |
01.50 |
3.145 |
00. |
03.145 |
3.4 |
0.(000) |
3.4 |
3.4566 |
0.(000) |
3.457 |
Standalone NodeJS example:
var format = ;var str = ;//> 4,500.39
Mask example
Javascript model
sum: 45003851
Output
Sum - 4,500.39
Javascript example
var str = mask_;//> 4,500.39
String Formatter
{ accessor[,alignment][:pattern][;switch] }
-
accessor
: index or dot-notated property/* index *///> 'Hello World, my World'/* property */;//> 'Hello Bar - 20' -
alignment
: minimum chars count with right/left alignment;//> 'x Qx';//> 'xQ x' -
pattern
:Date
Number
format patterns. Refer to Date Formatter and Number Formatter;//> Year: 2015 -
switch/pluralization
:pattern:string;otherPattern:string; ...
Choose interpolated string according to arguments value. Each string can contain nested interpolations. i18n benefits of this feature- Number patterns:
- Strict and Ending patterns:
12
,*12
- Ranges:
12-16
,*12-16
- Groups:
0,1,2
- Any:
*
- Strict and Ending patterns:
//> 'Baz Qux'// i18n, e.g. russian has 3 plural forms. 'N day(s)' sample//> '21 день'// It is also possible to move pluralization pattern to the cultureInfo.// then it would be//> '21 день' - Number patterns:
Standalone NodeJS example:
var format = ;var str = ;//> Name: John; Born: 01 January 1975; Salary: $17,000.00
Mask example
;;",
Javascript model
user: name: 'John' birth: 1975 0 1 salary: 17000
Output
Name: John; Born: 01 January 1975; Salary: $17,000.00
Javascript example
var str = mask_;//> 005
Static methods
The format
method takes the formatter by the input value. You can use formatters directly. Formatters will also try to convert the input value to the desired type. For instance, when a string
is passed to formatDate
then we try to convert it to the Date
instance.
var Formatter = ;Formatter
Internationalization
There are already EN
and DE
support.
Add culture support sample:
var formatter = ;formatterLang;formatterLang;
Build, Test
-
Build
$ npm install atma -g$ atma -
Test
$ atma test
(c) MIT - Atma.js Project