-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
55 lines (40 loc) · 1.11 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
51
52
53
54
55
'use strict';
var currencies = require('./currencies.json');
module.exports = currencies;
currencies.byCode = byCode;
currencies.byNumeric = byNumeric;
currencies.toMinor = toMinor;
currencies.toMajor = toMajor;
var codeMap;
var numericMap;
function byCode( code ){
if (codeMap === undefined)
codeMap = createMap(currencies.map(function( currency ){
return [ currency.code, currency ];
}));
if (code !== undefined)
return codeMap.get(code);
return codeMap;
}
function byNumeric( numeric ){
if (numericMap === undefined)
numericMap = createMap(currencies.map(function( currency ){
return [ currency.numeric, currency ];
}));
if (numeric !== undefined)
return numericMap.get(numeric);
return numericMap;
}
function toMinor( currency, major ){
return Math.round(major * Math.pow(10, byCode(currency).exponent));
}
function toMajor( currency, minor ){
return minor / Math.pow(10, byCode(currency).exponent);
}
// IE 11 does not support new Map(iterable)
function createMap( entries ){
var map = new Map();
for (var i = 0;i < entries.length;i++)
map.set(entries[i][0], entries[i][1]);
return map;
}