vendor/assets/javascripts/currencyFormatter.js in currencyFormatter-rails-0.1.0 vs vendor/assets/javascripts/currencyFormatter.js in currencyFormatter-rails-0.2.0
- old
+ new
@@ -1,5 +1,7 @@
+'use strict';
+
// CurrencyFormatter.js
// ---------------------------------------------------------------------
// Version: 2.0
// Release Date: 9 Jan 2018
// Created by the Bx team at OSREC Technologies (https://osrec.co.uk)
@@ -7,14 +9,12 @@
// Check out Bx @ https://usebx.com for free invoicing, expenses & project management
// If you use this library in a commercial project, we appreciate a link back to https://osrec.co.uk :)
var OSREC = OSREC || {};
-OSREC.CurrencyFormatter =
-{
- symbols:
- {
+OSREC.CurrencyFormatter = {
+ symbols: {
AED: 'د.إ.',
AFN: '؋',
ALL: 'Lekë',
AMD: '֏',
ANG: 'Naf',
@@ -34,10 +34,11 @@
BOB: 'Bs',
BRL: 'R$',
BSD: '$',
BTN: 'Nu.',
BWP: 'P',
+ BYN: 'p.',
BYR: 'р.',
BZD: '$',
CAD: '$',
CDF: 'FC',
CHF: 'CHF',
@@ -102,10 +103,11 @@
MKD: 'den',
MMK: 'K',
MNT: '₮',
MOP: 'MOP$',
MRO: 'UM',
+ MRU: 'UM',
MUR: 'Rs',
MWK: 'MK',
MXN: '$',
MYR: 'RM',
MZN: 'MTn',
@@ -137,10 +139,11 @@
SLL: 'Le',
SOS: 'S',
SRD: '$',
SSP: '£',
STD: 'Db',
+ STN: 'Db',
SYP: '£',
SZL: 'E',
THB: '฿',
TMT: 'ТМТ',
TND: 'DT',
@@ -165,12 +168,11 @@
YER: 'ر.ي.',
ZAR: 'R',
ZMW: 'K'
},
- defaultLocales:
- {
+ defaultLocales: {
AED: 'ar_AE',
AFN: 'fa_AF',
ALL: 'sq',
AMD: 'hy',
ANG: 'nl',
@@ -190,10 +192,11 @@
BOB: 'es_BO',
BRL: 'pt_BR',
BSD: 'en_BS',
BTN: 'dz',
BWP: 'en_BW',
+ BYN: 'be',
BYR: 'be',
BZD: 'en_BZ',
CAD: 'en_CA',
CDF: 'fr_CD',
CHF: 'de_CH',
@@ -257,10 +260,11 @@
MKD: 'mk',
MMK: 'my',
MNT: 'mn',
MOP: 'zh',
MRO: 'ar_MR',
+ MRU: 'ar_MR',
MUR: 'en_MU',
MVR: 'en',
MWK: 'en',
MXN: 'es_MX',
MYR: 'ms_MY',
@@ -294,10 +298,11 @@
SLL: 'en_SL',
SOS: 'so_SO',
SRD: 'nl_SR',
SSP: 'en',
STD: 'pt_ST',
+ STN: 'pt_ST',
SYP: 'ar_SY',
SZL: 'en_SZ',
THB: 'th',
TJS: 'en',
TMT: 'tk',
@@ -321,16 +326,15 @@
XOF: 'fr',
XPF: 'fr_PF',
YER: 'ar_YE',
ZAR: 'zu',
ZMW: 'en_ZM',
- ZWL: 'en_ZW',
+ ZWL: 'en_ZW'
},
- locales:
- {
+ locales: {
af: { p: '!#,##0.00', g: ' ', d: ',' },
af_NA: { h: 'af' },
af_ZA: { h: 'af' },
agq: { p: '#,##0.00!', g: ' ', d: ',' },
agq_CM: { h: 'agq' },
@@ -339,35 +343,35 @@
am: { p: '!#,##0.00', g: ',', d: '.' },
am_ET: { h: 'am' },
ar: { p: '! #,##0.00', g: ',', d: '.' },
ar_001: { h: 'ar' },
ar_AE: { h: 'ar' },
- ar_BH: { h: 'ar' },
+ ar_BH: { p: '! #,##0.000', g: ',', d: '.' },
ar_DJ: { h: 'ar' },
ar_DZ: { p: '! #,##0.00', g: '.', d: ',' },
ar_EG: { h: 'ar' },
ar_EH: { h: 'ar' },
ar_ER: { h: 'ar' },
ar_IL: { h: 'ar' },
- ar_IQ: { h: 'ar' },
- ar_JO: { h: 'ar' },
+ ar_IQ: { p: '! #,##0.000', g: ',', d: '.' },
+ ar_JO: { p: '! #,##0.000', g: ',', d: '.' },
ar_KM: { h: 'ar' },
- ar_KW: { h: 'ar' },
+ ar_KW: { p: '! #,##0.000', g: ',', d: '.' },
ar_LB: { p: '! #,##0.00', g: '.', d: ',' },
- ar_LY: { p: '! #,##0.00', g: '.', d: ',' },
+ ar_LY: { p: '! #,##0.000', g: '.', d: ',' },
ar_MA: { p: '! #,##0.00', g: '.', d: ',' },
ar_MR: { p: '! #,##0.00', g: '.', d: ',' },
- ar_OM: { h: 'ar' },
+ ar_OM: { p: '! #,##0.000', g: ',', d: '.' },
ar_PS: { h: 'ar' },
ar_QA: { h: 'ar' },
ar_SA: { h: 'ar' },
ar_SD: { h: 'ar' },
ar_SO: { h: 'ar' },
ar_SS: { h: 'ar' },
ar_SY: { h: 'ar' },
ar_TD: { h: 'ar' },
- ar_TN: { p: '! #,##0.00', g: '.', d: ',' },
+ ar_TN: { p: '! #,##0.000', g: '.', d: ',' },
ar_YE: { h: 'ar' },
as: { p: '! #,##,##0.00', g: ',', d: '.' },
as_IN: { h: 'as' },
asa: { p: '#,##0.00 !', g: ',', d: '.' },
asa_TZ: { h: 'asa' },
@@ -567,11 +571,11 @@
es: { p: '#,##0.00 !', g: '.', d: ',' },
es_419: { p: '!#,##0.00', g: ',', d: '.' },
es_AR: { p: '! #,##0.00', g: '.', d: ',' },
es_BO: { h: 'es' },
es_BR: { h: 'es' },
- es_CL: { p: '!#,##0.00;!-#,##0.00', g: '.', d: ',' },
+ es_CL: { p: '!#,##0;!-#,##0', g: '.', d: ',' },
es_CO: { p: '! #,##0.00', g: '.', d: ',' },
es_CR: { p: '#,##0.00 !', g: ' ', d: ',' },
es_CU: { h: 'es' },
es_DO: { h: 'es' },
es_EA: { h: 'es' },
@@ -585,11 +589,11 @@
es_NI: { h: 'es' },
es_PA: { h: 'es' },
es_PE: { h: 'es' },
es_PH: { h: 'es' },
es_PR: { h: 'es' },
- es_PY: { p: '! #,##0.00;! -#,##0.00', g: '.', d: ',' },
+ es_PY: { p: '! #,##0;! -#,##0', g: '.', d: ',' },
es_SV: { h: 'es' },
es_US: { h: 'es' },
es_UY: { p: '! #,##0.00', g: '.', d: ',' },
es_VE: { p: '!#,##0.00;!-#,##0.00', g: '.', d: ',' },
et: { p: '#,##0.00 !', g: ' ', d: ',' },
@@ -629,11 +633,11 @@
fr_DJ: { h: 'fr' },
fr_DZ: { h: 'fr' },
fr_FR: { h: 'fr' },
fr_GA: { h: 'fr' },
fr_GF: { h: 'fr' },
- fr_GN: { h: 'fr' },
+ fr_GN: { p: '#,##0 !', g: ' ', d: ',' },
fr_GP: { h: 'fr' },
fr_GQ: { h: 'fr' },
fr_HT: { h: 'fr' },
fr_KM: { h: 'fr' },
fr_LU: { p: '#,##0.00 !', g: '.', d: ',' },
@@ -703,17 +707,17 @@
id_ID: { h: 'id' },
ig: { p: '!#,##0.00', g: ',', d: '.' },
ig_NG: { h: 'ig' },
ii: { p: '!#,##0.00', g: ',', d: '.' },
ii_CN: { h: 'ii' },
- is: { p: '#,##0.00 !', g: '.', d: ',' },
+ is: { p: '#,##0 !', g: '.', d: ',' },
is_IS: { h: 'is' },
it: { p: '#,##0.00 !', g: '.', d: ',' },
it_CH: { p: '! #,##0.00;!-#,##0.00', g: '\'', d: '.' },
it_IT: { h: 'it' },
it_SM: { h: 'it' },
- ja: { p: '!#,##0.00', g: ',', d: '.' },
+ ja: { p: '!#,##0', g: ',', d: '.' },
ja_JP: { h: 'ja' },
jgo: { p: '! #,##0.00', g: '.', d: ',' },
jgo_CM: { h: 'jgo' },
jmc: { p: '!#,##0.00', g: ',', d: '.' },
jmc_TZ: { h: 'jmc' },
@@ -741,11 +745,11 @@
kln_KE: { h: 'kln' },
km: { p: '!#,##0.00', g: '.', d: ',' },
km_KH: { h: 'km' },
kn: { p: '!#,##0.00', g: ',', d: '.' },
kn_IN: { h: 'kn' },
- ko: { p: '!#,##0.00', g: ',', d: '.' },
+ ko: { p: '!#,##0', g: ',', d: '.' },
ko_KP: { h: 'ko' },
ko_KR: { h: 'ko' },
kok: { p: '! #,##,##0.00', g: ',', d: '.' },
kok_IN: { h: 'kok' },
ks: { p: '! #,##,##0.00', g: ',', d: '.' },
@@ -1009,11 +1013,11 @@
vai: { p: '!#,##0.00', g: ',', d: '.' },
vai_Latn: { h: 'vai' },
vai_Latn_LR: { h: 'vai' },
vai_Vaii: { h: 'vai' },
vai_Vaii_LR: { h: 'vai' },
- vi: { p: '! #,##0.00', g: '.', d: ',' },
+ vi: { p: '#,##0 !', g: '.', d: ',' },
vi_VN: { h: 'vi' },
vo: { p: '!#,##0.00', g: ',', d: '.' },
vo_001: { h: 'vo' },
vun: { p: '!#,##0.00', g: ',', d: '.' },
vun_TZ: { h: 'vun' },
@@ -1041,94 +1045,102 @@
zh_Hant: { h: 'zh' },
zh_Hant_HK: { h: 'zh' },
zh_Hant_MO: { h: 'zh' },
zh_Hant_TW: { h: 'zh' },
zu: { p: '!#,##0.00', g: ',', d: '.' },
- zu_ZA: { h: 'zu' },
+ zu_ZA: { h: 'zu' }
},
- getFormatDetails: function(p)
- {
- var locales = OSREC.CurrencyFormatter.locales;
- var defaultLocales = OSREC.CurrencyFormatter.defaultLocales;
- var symbols = OSREC.CurrencyFormatter.symbols;
+ getFormatDetails: function getFormatDetails(p) {
+ var locales = OSREC.CurrencyFormatter.locales;
+ var defaultLocales = OSREC.CurrencyFormatter.defaultLocales;
+ var symbols = OSREC.CurrencyFormatter.symbols;
- var locale, currency, symbol, pattern, decimal, group;
+ var locale, currency, symbol, pattern, decimal, group, valueOnError;
// Perform checks on inputs and set up defaults as needed (defaults to en, USD)
p = p || {};
- currency = (p.currency || 'USD').toUpperCase();
- locale = locales[p.locale || defaultLocales[currency]];
+ currency = (p.currency || 'USD').toUpperCase();
+ locale = locales[p.locale || defaultLocales[currency]];
- if(typeof locale.h !== 'undefined') { locale = locales[locale.h]; } // Locale inheritance
+ if (typeof locale.h !== 'undefined') {
+ locale = locales[locale.h];
+ } // Locale inheritance
- symbol = (p.symbol || symbols[currency]) || currency;
- pattern = p.pattern || locale.p;
- decimal = p.decimal || locale.d;
- group = p.group || locale.g;
-
- return { pattern: pattern, decimal: decimal, group: group, symbol: symbol };
+ symbol = p.symbol || symbols[currency] || currency;
+ pattern = p.pattern || locale.p;
+ decimal = p.decimal || locale.d;
+ group = p.group || locale.g;
+ valueOnError = typeof p.valueOnError === 'undefined' ? 0 : p.valueOnError;
+ var formatDetails = {
+ pattern: pattern,
+ decimal: decimal,
+ group: group,
+ symbol: symbol,
+ valueOnError: valueOnError,
+ postFormatFunction: p.postFormatFunction
+ };
+
+ return formatDetails;
},
- toFixed: function( n, precision )
- {
- return ( Math.round( Number(n) * Math.pow(10, precision) )/ Math.pow(10, precision) ).toFixed(precision);
+ toFixed: function toFixed(n, precision) {
+ return (Math.round(Number(n) * Math.pow(10, precision)) / Math.pow(10, precision)).toFixed(precision);
},
- getFormatter: function(p)
- {
+ getFormatter: function getFormatter(p) {
var formatDetails = OSREC.CurrencyFormatter.getFormatDetails(p);
-
- var pattern = formatDetails.pattern;
- var decimal = formatDetails.decimal;
- var group = formatDetails.group;
- var symbol = formatDetails.symbol;
-
+
+ var pattern = formatDetails.pattern;
+ var decimal = formatDetails.decimal;
+ var group = formatDetails.group;
+ var symbol = formatDetails.symbol;
+ var valueOnError = formatDetails.valueOnError;
+ var postFormatFunction = formatDetails.postFormatFunction;
+
// encodePattern Function - returns a few simple characteristics of the pattern provided
-
- var encodePattern = function(pattern)
- {
+
+ var encodePattern = function encodePattern(pattern) {
var numberFormatPattern = pattern.trim().match(/[#0,\.]+/)[0];
-
+
var split = numberFormatPattern.split('.');
var c = split[0]; // Decimal chars
var m = split[1]; // Decimal mantissa
-
- var groups = c.split(',');
- var groupLengths = groups.map(function(g) { return g.length; });
- var zeroLength = (groups[groups.length - 1].match(/0/g) || []).length;
- var decimalPlaces = typeof m === 'undefined' ? 0 : m.length;
- var paddingSplit = pattern.split(numberFormatPattern);
-
- var encodedPattern =
- {
+
+ var groups = c.split(',');
+ var groupLengths = groups.map(function (g) {
+ return g.length;
+ });
+ var zeroLength = (groups[groups.length - 1].match(/0/g) || []).length;
+ var decimalPlaces = typeof m === 'undefined' ? 0 : m.length;
+ var paddingSplit = pattern.split(numberFormatPattern);
+
+ var encodedPattern = {
pattern: pattern,
decimalPlaces: decimalPlaces,
frontPadding: paddingSplit[0],
backPadding: paddingSplit[1],
groupLengths: groupLengths,
zeroLength: zeroLength
};
return encodedPattern;
- }
+ };
// Zero Padding helper function
- var pad = function(n, width)
- {
+ var pad = function pad(n, width) {
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join('0') + n;
- }
+ };
// Format function
- var format = function(n, f)
- {
+ var format = function format(n, f) {
var formattedNumber = OSREC.CurrencyFormatter.toFixed(Math.abs(n), f.decimalPlaces);
var splitNumber = formattedNumber.split(".");
var segment = "";
@@ -1137,40 +1149,39 @@
var maxGroupIndex = f.groupLengths.length - 1;
var groupIndex = maxGroupIndex;
- if(maxGroupIndex > 0)
- {
- while(cursor > 0)
- {
- if(groupIndex < 1) { groupIndex = 1; } // Always reset to the last group length (useful for big numbers)
+ if (maxGroupIndex > 0) {
+ while (cursor > 0) {
+ if (groupIndex < 1) {
+ groupIndex = 1;
+ } // Always reset to the last group length (useful for big numbers)
var currentGroupLength = f.groupLengths[groupIndex];
- var start = cursor-currentGroupLength;
+ var start = cursor - currentGroupLength;
segment = splitNumber[0].substring(start, cursor) + f.group + segment;
cursor -= currentGroupLength;
--groupIndex;
}
-
- segment = segment.substring(0, segment.length-1);
- }
- else
- {
+
+ segment = segment.substring(0, segment.length - 1);
+ } else {
segment = splitNumber[0];
}
- if(segment.length < f.zeroLength) { segment = pad(segment, f.zeroLength); }
+ if (segment.length < f.zeroLength) {
+ segment = pad(segment, f.zeroLength);
+ }
- var formattedNumber = f.frontPadding + segment + ( typeof splitNumber[1] === 'undefined' ? '' : (f.decimal + splitNumber[1]) ) + f.backPadding;
+ var formattedNumber = f.frontPadding + segment + (typeof splitNumber[1] === 'undefined' ? '' : f.decimal + splitNumber[1]) + f.backPadding;
return formattedNumber.replace(/\!/g, symbol);
-
};
// Use encode function to work out pattern
var patternArray = pattern.split(";");
@@ -1187,94 +1198,85 @@
negativeFormat.decimal = decimal;
negativeFormat.group = group;
var zero = typeof patternArray[2] === 'undefined' ? format(0, positiveFormat) : patternArray[2];
- return function(n)
- {
+ return function (n) {
+ if (isNaN(n)) {
+ return valueOnError;
+ }
var formattedNumber;
n = Number(n);
- if(n > 0) { formattedNumber = format(n, positiveFormat); }
- else if(n == 0) { formattedNumber = zero.replace('!', symbol); }
- else { formattedNumber = format(n, negativeFormat); }
- return formattedNumber;
+ if (n > 0) {
+ formattedNumber = format(n, positiveFormat);
+ } else if (n == 0) {
+ formattedNumber = zero.replace('!', symbol);
+ } else {
+ formattedNumber = format(n, negativeFormat);
+ }
+ return typeof postFormatFunction === 'function' ? postFormatFunction(n, formattedNumber) : formattedNumber;
};
-
},
- formatAll: function(p)
- {
+ formatAll: function formatAll(p) {
var formatter = OSREC.CurrencyFormatter.getFormatter(p);
var matches = document.querySelectorAll(p.selector);
- for (var i = 0; i < matches.length; ++i)
- {
+ for (var i = 0; i < matches.length; ++i) {
matches[i].innerHTML = formatter(matches[i].textContent);
}
},
- formatEach: function(selector)
- {
- var formatters = {}
+ formatEach: function formatEach(selector) {
+ var formatters = {};
var matches = document.querySelectorAll(selector);
- for (var i = 0; i < matches.length; ++i)
- {
- try
- {
+ for (var i = 0; i < matches.length; ++i) {
+ try {
var ccy = matches[i].getAttribute("data-ccy");
- if (typeof formatters[ccy] === 'undefined')
- {
- formatters[ccy] = OSREC.CurrencyFormatter.getFormatter({currency: ccy});
+ if (typeof formatters[ccy] === 'undefined') {
+ formatters[ccy] = OSREC.CurrencyFormatter.getFormatter({ currency: ccy });
}
var formatter = formatters[ccy];
matches[i].innerHTML = formatter(matches[i].textContent);
- }
- catch (e)
- {
+ } catch (e) {
console.log(e);
}
}
},
- format: function(n, p)
- {
+ format: function format(n, p) {
var formatterFunction = OSREC.CurrencyFormatter.getFormatter(p);
return formatterFunction(n);
},
-
- parse: function(str, p)
- {
+
+ parse: function parse(str, p) {
var decimal = OSREC.CurrencyFormatter.getFormatDetails(p).decimal;
var mult = str.indexOf('-') >= 0 ? -1 : 1;
- return Math.abs(Number(str.replace(new RegExp(`[^0-9${decimal}]`, 'g'), '').replace(decimal, '.'))) * mult;
+ return Math.abs(Number(str.replace(new RegExp('[^0-9' + decimal + ']', 'g'), '').replace(decimal, '.'))) * mult;
}
};
-
var hasDefine = typeof define === 'function';
var hasExports = typeof module !== 'undefined' && module.exports;
-var root = (typeof window === 'undefined') ? global : window;
+var root = typeof window === 'undefined' ? global : window;
-if (hasDefine)
-{
+if (hasDefine) {
// AMD Module
- define([], function() { return OSREC.CurrencyFormatter; });
-}
-else if (hasExports)
-{
+ define([], function () {
+ return OSREC.CurrencyFormatter;
+ });
+} else if (hasExports) {
// Node.js Module
- module.exports = OSREC.CurrencyFormatter;
-}
-else
-{
+ module.exports = OSREC.CurrencyFormatter;
+} else {
// Assign to the global object
// This makes sure that the object really is assigned to the global scope
- root.OSREC = OSREC;
-}
+ root.OSREC = OSREC;
+}
\ No newline at end of file