-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill.number.toLocaleString.js
301 lines (301 loc) · 6.63 KB
/
polyfill.number.toLocaleString.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
(function() {
'use strict';
// Got this from MDN:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Example:_Checking_for_support_for_locales_and_options_arguments
function toLocaleStringSupportsLocales() {
var number = 0;
try {
number.toLocaleString("i");
} catch(e) {
return e.name === "RangeError";
}
return false;
}
var roundOff = function (number, precision) {
return +((+number).toFixed(precision));
};
var replaceSeparators = function(sNum, separators, options) {
sNum = '' + roundOff(sNum, 3);
var sNumParts = sNum.split('.');
if(separators && separators.thousands) {
sNumParts[0] = sNumParts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + separators.thousands);
} else if(separators && separators.hundreds) {
sNumParts[0] = sNumParts[0].replace(/(\d)(?=(\d\d)+(?!\d))/g, "$1" + separators.hundreds);
}
if(options && options.minimumFractionDigits > 0) {
if (sNumParts.length !== 2) {
sNumParts[1] = '';
}
sNumParts[1] = sNumParts[1].padEnd(options.minimumFractionDigits, '0');
}
sNum = sNumParts.join(separators.decimal);
return sNum;
};
var getLast3Digits = function(sNum) {
sNum = '' + roundOff(sNum, 3);
var sNumParts = sNum.split('.');
switch(sNumParts[0].length) {
case 0:
sNumParts[0] = '000';
break;
case 1:
sNumParts[0] = '00' + sNumParts[0];
break;
case 2:
sNumParts[0] = '0' + sNumParts[0];
break;
}
sNum = sNumParts.join('.');
return sNum;
};
var renderFormat = function(template, props) {
for(var prop in props) {
template = template.replace("{{" + prop + "}}", props[prop]);
}
return template;
};
var mapMatch = function(map, locale) {
var match = locale;
var language = locale && locale.toLowerCase().match(/^\w+/);
if(!map.hasOwnProperty(locale)) {
if(map.hasOwnProperty(language)) {
match = language;
} else {
match = "en";
}
}
return map[match];
};
var dotThousCommaDec = function(sNum, options) {
var separators = {
decimal: ',',
thousands: '.'
};
return replaceSeparators(sNum, separators, options);
};
var commaThousDotDec = function(sNum, options) {
var separators = {
decimal: '.',
thousands: ','
};
return replaceSeparators(sNum, separators, options);
};
var spaceThousCommaDec = function(sNum, options) {
var seperators = {
decimal: ',',
thousands: '\u00A0'
};
return replaceSeparators(sNum, seperators, options);
};
var spaceHundredsCommaThousCommaDec = function(sNum) {
var hundredSeperators = {
decimal: '.',
hundreds: ','
},
thoudandSeperators = {
decimal: '.',
thousands: ','
};
sNum = +sNum;
if(sNum >= 1000) {
return replaceSeparators(Math.floor(sNum / 1000) + '', hundredSeperators) + ',' + getLast3Digits((sNum % 1000) + '');
} else {
return replaceSeparators(sNum + '', thoudandSeperators);
}
};
var apostrophThousDotDec = function(sNum, options) {
var seperators = {
decimal: '.',
thousands: '\u0027'
};
return replaceSeparators(sNum, seperators, options);
};
var transformForLocale = {
en: commaThousDotDec,
it: dotThousCommaDec,
fr: spaceThousCommaDec,
de: dotThousCommaDec,
"de-DE": dotThousCommaDec,
"de-AT": dotThousCommaDec,
"de-CH": apostrophThousDotDec,
"de-LI": apostrophThousDotDec,
"de-BE": dotThousCommaDec,
"hi-IN": spaceHundredsCommaThousCommaDec,
"en-IN": spaceHundredsCommaThousCommaDec,
ro: dotThousCommaDec,
"ro-RO": dotThousCommaDec,
hu: spaceThousCommaDec,
"hu-HU": spaceThousCommaDec,
"da-DK": dotThousCommaDec,
"nb-NO": spaceThousCommaDec,
"sv-SE": spaceThousCommaDec
};
var currencyFormatMap = {
en: "pre",
it: "post",
fr: "post",
de: "post",
pl: "post",
"es-ES": "post",
"de-DE": "post",
"de-AT": "prespace",
"de-CH": "prespace",
"de-LI": "post",
"de-BE": "post",
ro: "post",
"ro-RO": "post",
hu: "post",
"hu-HU": "post",
"da-DK": "post",
"nb-NO": "post",
"sv-SE": "post"
};
var currencySymbols = {
"afn": "؋",
"ars": "ARS",
"awg": "ƒ",
"aud": "A$",
"azn": "₼",
"bsd": "$",
"bbd": "$",
"bdt": "BDT",
"byr": "p.",
"bzd": "BZ$",
"bmd": "$",
"bob": "Bs.",
"bam": "KM",
"bwp": "P",
"bgn": "лв",
"brl": "R$",
"bnd": "$",
"khr": "៛",
"cad": "CA$",
"kyd": "$",
"clp": "CLP",
"cny": "¥",
"cop": "$",
"crc": "₡",
"hrk": "kn",
"cup": "₱",
"czk": "Kč",
"dkk": "kr.",
"dop": "RD$",
"xcd": "$",
"egp": "£",
"svc": "$",
"eek": "kr",
"eur": "€",
"fkp": "£",
"fjd": "$",
"ghc": "¢",
"gip": "£",
"gtq": "Q",
"ggp": "£",
"gyd": "$",
"hnl": "L",
"hkd": "HK$",
"huf": "Ft",
"isk": "kr",
"inr": "₹",
"idr": "Rp",
"irr": "﷼",
"imp": "£",
"ils": "₪",
"jmd": "J$",
"jpy": "¥",
"jep": "£",
"kes": "KSh",
"kzt": "лв",
"kpw": "₩",
"krw": "₩",
"kgs": "лв",
"lak": "₭",
"lvl": "Ls",
"lbp": "£",
"lrd": "$",
"ltl": "Lt",
"mkd": "ден",
"myr": "RM",
"mur": "₨",
"mxn": "$",
"mnt": "₮",
"mzn": "MT",
"nad": "$",
"npr": "₨",
"ang": "ƒ",
"nzd": "NZ$",
"nio": "C$",
"ngn": "₦",
"nok": "kr",
"omr": "﷼",
"pkr": "₨",
"pab": "B/.",
"pyg": "Gs",
"pen": "S/.",
"php": "₱",
"pln": "zł",
"qar": "﷼",
"ron": "lei",
"rub": "₽",
"shp": "£",
"sar": "﷼",
"rsd": "Дин.",
"scr": "₨",
"sgd": "S$",
"sbd": "$",
"sos": "S",
"zar": "R",
"lkr": "₨",
"sek": "kr",
"chf": "CHF",
"srd": "$",
"syp": "£",
"tzs": "TSh",
"twd": "NT$",
"thb": "฿",
"ttd": "TT$",
"try": "",
"trl": "₤",
"tvd": "$",
"ugx": "USh",
"uah": "₴",
"gbp": "£",
"usd": "$",
"uyu": "$U",
"uzs": "лв",
"vef": "Bs",
"vnd": "₫",
"yer": "﷼",
"zwd": "Z$"
};
var currencyFormats = {
pre: "{{code}}{{num}}",
post: "{{num}} {{code}}",
prespace: "{{code}} {{num}}"
};
Number.prototype.toLocaleString = function(locale, options) {
if(locale && locale.length < 2) throw new RangeError("Invalid language tag: " + locale);
var sNum;
if(options && options.minimumFractionDigits !== undefined) {
sNum = this.toFixed(options.minimumFractionDigits);
} else {
sNum = this.toString();
}
sNum = mapMatch(transformForLocale, locale)(sNum, options);
if(options && options.currency && options.style === "currency") {
var format = currencyFormats[mapMatch(currencyFormatMap, locale)];
if(options.currencyDisplay === "code") {
sNum = renderFormat(format, {
num: sNum,
code: options.currency.toUpperCase()
});
} else {
sNum = renderFormat(format, {
num: sNum,
code: currencySymbols[options.currency.toLowerCase()]
});
}
}
return '' + sNum;
};
}());