-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
50 lines (38 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
var defined = require('defined');
var supported = typeof Intl === 'object'
&& typeof Intl.NumberFormat === 'function';
module.exports = format;
function format( opts, amount ){
if (amount === undefined) {
amount = opts;
opts = {};
}
var currency = opts.currency || amount.currency;
var major = defined(amount.major, amount.amount, amount);
if (typeof major !== 'number' || isNaN(major))
throw new Error('Missing amount or amount not a number');
var minimumFractionDigits = opts.minimumFractionDigits === undefined
? 2
: opts.minimumFractionDigits;
if (!supported) {
if (currency)
return currency.toUpperCase()+' '+major.toFixed(minimumFractionDigits);
return major.toFixed(minimumFractionDigits);
}
var locales = typeof opts === 'string' || Array.isArray(opts)
? opts
: (opts.locales || opts.locale || 'en');
if (currency)
return major.toLocaleString(locales, {
style: 'currency',
currency: currency,
currencyDisplay: opts.display,
localeMatcher: opts.matcher,
useGrouping: opts.grouping,
});
return major.toLocaleString(locales, {
minimumFractionDigits: minimumFractionDigits,
useGrouping: opts.grouping,
});
}