From 44d94d6ba403bc9ac8d3fafeb9e7eef81ab7d284 Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Sun, 18 Dec 2016 22:21:48 -0700 Subject: [PATCH 1/9] fix number formatting for sign --- numeral.js | 34 ++++++++++++++++++++++++++-------- src/numeral.js | 32 ++++++++++++++++++++++++-------- tests/numeral.js | 17 +++++++++++++++++ 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/numeral.js b/numeral.js index 26692cbf..8f076d9a 100644 --- a/numeral.js +++ b/numeral.js @@ -102,13 +102,14 @@ numberToFormat: function(value, format, roundingFunction) { var locale = locales[numeral.options.currentLocale], negP = false, - signed = false, optDec = false, abbr = '', trillion = 1000000000000, billion = 1000000000, million = 1000000, thousand = 1000, + decimal = '', + neg = false, abbrForce, // force abbreviation abs, min, @@ -116,23 +117,25 @@ power, int, precision, + signed, thousands, - decimal = '', - neg = false; + output; // make sure we never format a null value value = value || 0; abs = Math.abs(value); + console.log(format, numeral._.includes(format, '(')); + // see if we should use parentheses for negative number or if we should prefix with a sign // if both are present we default to parentheses if (numeral._.includes(format, '(')) { negP = true; - format = format.slice(1, -1); - } else if (numeral._.includes(format, '+')) { - signed = true; - format = format.replace(/\+/g, ''); + format = format.replace(/[\(|\)]/g, ''); + } else if (numeral._.includes(format, '+') || numeral._.includes(format, '-')) { + signed = numeral._.includes(format, '+') ? format.indexOf('+') : format.indexOf('-'); + format = format.replace(/[\+|\-]/g, ''); } // see if abbreviation is wanted @@ -215,7 +218,19 @@ int = ''; } - return (negP && neg ? '(' : '') + (!negP && neg ? '-' : '') + (!neg && signed ? '+' : '') + int + decimal + (abbr ? abbr : '') + (negP && neg ? ')' : ''); + output = int + decimal + (abbr ? abbr : ''); + + if (negP) { + output = (negP && neg ? '(' : '') + output + (negP && neg ? ')' : ''); + } else { + if (signed >= 0) { + output = signed === 0 ? (neg ? '-' : '+') + output : output + (neg ? '-' : '+'); + } else if (neg) { + output = '-' + output; + } + } + + return output; }, // unformats numbers separators, decimals places, signs, abbreviations stringToNumber: function(string) { @@ -269,6 +284,9 @@ includes: function(string, search) { return string.indexOf(search) !== -1; }, + insert: function(string, subString, start) { + return string.slice(0, start) + subString + string.slice(start); + }, reduce: function(array, callback /*, initialValue*/) { if (this === null) { throw new TypeError('Array.prototype.reduce called on null or undefined'); diff --git a/src/numeral.js b/src/numeral.js index fc9a79a4..a84e4aa4 100644 --- a/src/numeral.js +++ b/src/numeral.js @@ -102,13 +102,14 @@ numberToFormat: function(value, format, roundingFunction) { var locale = locales[numeral.options.currentLocale], negP = false, - signed = false, optDec = false, abbr = '', trillion = 1000000000000, billion = 1000000000, million = 1000000, thousand = 1000, + decimal = '', + neg = false, abbrForce, // force abbreviation abs, min, @@ -116,9 +117,9 @@ power, int, precision, + signed, thousands, - decimal = '', - neg = false; + output; // make sure we never format a null value value = value || 0; @@ -129,10 +130,10 @@ // if both are present we default to parentheses if (numeral._.includes(format, '(')) { negP = true; - format = format.slice(1, -1); - } else if (numeral._.includes(format, '+')) { - signed = true; - format = format.replace(/\+/g, ''); + format = format.replace(/[\(|\)]/g, ''); + } else if (numeral._.includes(format, '+') || numeral._.includes(format, '-')) { + signed = numeral._.includes(format, '+') ? format.indexOf('+') : format.indexOf('-'); + format = format.replace(/[\+|\-]/g, ''); } // see if abbreviation is wanted @@ -215,7 +216,19 @@ int = ''; } - return (negP && neg ? '(' : '') + (!negP && neg ? '-' : '') + (!neg && signed ? '+' : '') + int + decimal + (abbr ? abbr : '') + (negP && neg ? ')' : ''); + output = int + decimal + (abbr ? abbr : ''); + + if (negP) { + output = (negP && neg ? '(' : '') + output + (negP && neg ? ')' : ''); + } else { + if (signed >= 0) { + output = signed === 0 ? (neg ? '-' : '+') + output : output + (neg ? '-' : '+'); + } else if (neg) { + output = '-' + output; + } + } + + return output; }, // unformats numbers separators, decimals places, signs, abbreviations stringToNumber: function(string) { @@ -269,6 +282,9 @@ includes: function(string, search) { return string.indexOf(search) !== -1; }, + insert: function(string, subString, start) { + return string.slice(0, start) + subString + string.slice(start); + }, reduce: function(array, callback /*, initialValue*/) { if (this === null) { throw new TypeError('Array.prototype.reduce called on null or undefined'); diff --git a/tests/numeral.js b/tests/numeral.js index b0b9b699..4ba8f09c 100644 --- a/tests/numeral.js +++ b/tests/numeral.js @@ -152,6 +152,7 @@ describe('Numeral', function() { [-10000,'(0,0.0000)','(10,000.0000)'], [-12300,'+0,0.0000','-12,300.0000'], [1230,'+0,0','+1,230'], + [-1230.4,'0,0.0+','1,230.4-'], [100.78, '0', '101'], [100.28, '0', '100'], [1.932,'0.0','1.9'], @@ -438,4 +439,20 @@ describe('Numeral', function() { }); }); }); + + describe('Utilities', function() { + describe('Insert', function() { + it('should insert into string', function() { + var tests = [ + ['1000', '+', 0, '+1000'], + ['1000', '-', 4, '1000-'] + ], + i; + + for (i = 0; i < tests.length; i++) { + expect(numeral._.insert(tests[i][0], tests[i][1], tests[i][2])).to.equal(tests[i][3]); + } + }); + }); + }); }); From eae7ed48ce4899c13602efeef92d249bac068c2b Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Mon, 19 Dec 2016 23:55:03 -0700 Subject: [PATCH 2/9] fix currency formatting --- numeral.js | 63 +++++++++++++++++++++++---------------- src/formats/currency.js | 59 ++++++++++++++++++++++-------------- src/numeral.js | 2 +- tests/formats/currency.js | 6 ++-- tests/numeral.js | 5 ++++ 5 files changed, 83 insertions(+), 52 deletions(-) diff --git a/numeral.js b/numeral.js index 8f076d9a..f2e1e1e6 100644 --- a/numeral.js +++ b/numeral.js @@ -126,15 +126,13 @@ abs = Math.abs(value); - console.log(format, numeral._.includes(format, '(')); - // see if we should use parentheses for negative number or if we should prefix with a sign // if both are present we default to parentheses if (numeral._.includes(format, '(')) { negP = true; format = format.replace(/[\(|\)]/g, ''); } else if (numeral._.includes(format, '+') || numeral._.includes(format, '-')) { - signed = numeral._.includes(format, '+') ? format.indexOf('+') : format.indexOf('-'); + signed = numeral._.includes(format, '+') ? format.indexOf('+') : value < 0 ? format.indexOf('-') : -1; format = format.replace(/[\+|\-]/g, ''); } @@ -746,12 +744,13 @@ }, format: function(value, format, roundingFunction) { var locale = numeral.locales[numeral.options.currentLocale], - symbolIndex = format.indexOf('$'), - openParenIndex = format.indexOf('('), - minusSignIndex = format.indexOf('-'), - space = numeral._.includes(format, ' $') || numeral._.includes(format, '$ ') ? ' ' : '', - spliceIndex, - output; + symbols = { + before: format.match(/^([\+|\-|\(|\s|\$]*)/)[0], + after: format.match(/([\+|\-|\)|\s|\$]*)$/)[0] + }, + output, + symbol, + i; // strip format of spaces and $ format = format.replace(/\s?\$\s?/, ''); @@ -759,31 +758,43 @@ // format the number output = numeral._.numberToFormat(value, format, roundingFunction); - // position the symbol - if (symbolIndex <= 1) { - if (numeral._.includes(output, '(') || numeral._.includes(output, '-')) { - output = output.split(''); - - spliceIndex = symbolIndex < openParenIndex || symbolIndex < minusSignIndex ? 0 : 1; + // update the before and after based on value + if (value >= 0) { + symbols.before = symbols.before.replace(/[\-\(]/, ''); + symbols.after = symbols.after.replace(/[\-\)]/, ''); + } else if (value < 0 && (!numeral._.includes(symbols.before, '-') && !numeral._.includes(symbols.before, '('))) { + symbols.before = '-' + symbols.before; + } - output.splice(spliceIndex, 0, locale.currency.symbol + space); + // loop through each before symbol + for (i = 0; i < symbols.before.length; i++) { + symbol = symbols.before[i]; - output = output.join(''); - } else { - output = locale.currency.symbol + space + output; + switch (symbol) { + case '$': + output = numeral._.insert(output, locale.currency.symbol, i); + break; + case ' ': + output = numeral._.insert(output, ' ', i); + break; } - } else { - if (numeral._.includes(output, ')')) { - output = output.split(''); + } - output.splice(-1, 0, space + locale.currency.symbol); + // loop through each after symbol + for (i = symbols.after.length - 1; i >= 0; i--) { + symbol = symbols.after[i]; - output = output.join(''); - } else { - output = output + space + locale.currency.symbol; + switch (symbol) { + case '$': + output = i === symbols.after.length - 1 ? output + locale.currency.symbol : numeral._.insert(output, locale.currency.symbol, -(symbols.after.length - (1 + i))); + break; + case ' ': + output = i === symbols.after.length - 1 ? output + ' ' : numeral._.insert(output, ' ', -(symbols.after.length - (1 + i))); + break; } } + return output; } }); diff --git a/src/formats/currency.js b/src/formats/currency.js index 938466e4..a4eb8c8a 100644 --- a/src/formats/currency.js +++ b/src/formats/currency.js @@ -17,12 +17,13 @@ }, format: function(value, format, roundingFunction) { var locale = numeral.locales[numeral.options.currentLocale], - symbolIndex = format.indexOf('$'), - openParenIndex = format.indexOf('('), - minusSignIndex = format.indexOf('-'), - space = numeral._.includes(format, ' $') || numeral._.includes(format, '$ ') ? ' ' : '', - spliceIndex, - output; + symbols = { + before: format.match(/^([\+|\-|\(|\s|\$]*)/)[0], + after: format.match(/([\+|\-|\)|\s|\$]*)$/)[0] + }, + output, + symbol, + i; // strip format of spaces and $ format = format.replace(/\s?\$\s?/, ''); @@ -30,31 +31,43 @@ // format the number output = numeral._.numberToFormat(value, format, roundingFunction); - // position the symbol - if (symbolIndex <= 1) { - if (numeral._.includes(output, '(') || numeral._.includes(output, '-')) { - output = output.split(''); - - spliceIndex = symbolIndex < openParenIndex || symbolIndex < minusSignIndex ? 0 : 1; + // update the before and after based on value + if (value >= 0) { + symbols.before = symbols.before.replace(/[\-\(]/, ''); + symbols.after = symbols.after.replace(/[\-\)]/, ''); + } else if (value < 0 && (!numeral._.includes(symbols.before, '-') && !numeral._.includes(symbols.before, '('))) { + symbols.before = '-' + symbols.before; + } - output.splice(spliceIndex, 0, locale.currency.symbol + space); + // loop through each before symbol + for (i = 0; i < symbols.before.length; i++) { + symbol = symbols.before[i]; - output = output.join(''); - } else { - output = locale.currency.symbol + space + output; + switch (symbol) { + case '$': + output = numeral._.insert(output, locale.currency.symbol, i); + break; + case ' ': + output = numeral._.insert(output, ' ', i); + break; } - } else { - if (numeral._.includes(output, ')')) { - output = output.split(''); + } - output.splice(-1, 0, space + locale.currency.symbol); + // loop through each after symbol + for (i = symbols.after.length - 1; i >= 0; i--) { + symbol = symbols.after[i]; - output = output.join(''); - } else { - output = output + space + locale.currency.symbol; + switch (symbol) { + case '$': + output = i === symbols.after.length - 1 ? output + locale.currency.symbol : numeral._.insert(output, locale.currency.symbol, -(symbols.after.length - (1 + i))); + break; + case ' ': + output = i === symbols.after.length - 1 ? output + ' ' : numeral._.insert(output, ' ', -(symbols.after.length - (1 + i))); + break; } } + return output; } }); diff --git a/src/numeral.js b/src/numeral.js index a84e4aa4..6500b464 100644 --- a/src/numeral.js +++ b/src/numeral.js @@ -132,7 +132,7 @@ negP = true; format = format.replace(/[\(|\)]/g, ''); } else if (numeral._.includes(format, '+') || numeral._.includes(format, '-')) { - signed = numeral._.includes(format, '+') ? format.indexOf('+') : format.indexOf('-'); + signed = numeral._.includes(format, '+') ? format.indexOf('+') : value < 0 ? format.indexOf('-') : -1; format = format.replace(/[\+|\-]/g, ''); } diff --git a/tests/formats/currency.js b/tests/formats/currency.js index 0156d421..24bbef87 100644 --- a/tests/formats/currency.js +++ b/tests/formats/currency.js @@ -18,11 +18,13 @@ describe('Currency', function() { [1000.234,'$0,0.00','$1,000.23'], [1001,'$ 0,0.[00]','$ 1,001'], [1000.234,'0,0.00 $','1,000.23 $'], + [-1000.234,'0,0.00 $','-1,000.23 $'], [-1000.234,'($0,0)','($1,000)'], [-1000.234,'(0,0$)','(1,000$)'], + [-1000.234,'(0,0 $)','(1,000 $)'], [-1000.234,'$0.00','-$1000.23'], + [-1000.234,'$ 0.00','-$ 1000.23'], [1230974,'($0.00 a)','$1.23 m'], - // test symbol position before negative sign / open parens [-1000.234,'$ (0,0)','$ (1,000)'], [-1000.234,'$(0,0)','$(1,000)'], [-1000.234,'$ (0,0.00)','$ (1,000.23)'], @@ -35,7 +37,7 @@ describe('Currency', function() { [1000.234,'$ (0,0.00)','$ 1,000.23'], [1000.234,'$(0,0.00)','$1,000.23'], [1000.238,'$(0,0.00)','$1,000.24'], - [1000.234,'$-0,0)','$1,000'], + [1000.234,'$-0,0','$1,000'], [1000.234,'$ -0,0','$ 1,000'] ], i; diff --git a/tests/numeral.js b/tests/numeral.js index 4ba8f09c..3e2a566f 100644 --- a/tests/numeral.js +++ b/tests/numeral.js @@ -149,10 +149,15 @@ describe('Numeral', function() { [10000.001,'0[.]00','10000'], [10000.45,'0[.]00[0]','10000.45'], [10000.456,'0[.]00[0]','10000.456'], + [10000,'(0,0.0000)','10,000.0000'], [-10000,'(0,0.0000)','(10,000.0000)'], [-12300,'+0,0.0000','-12,300.0000'], [1230,'+0,0','+1,230'], + [1230,'-0,0','1,230'], + [-1230,'-0,0','-1,230'], [-1230.4,'0,0.0+','1,230.4-'], + [-1230.4,'0,0.0-','1,230.4-'], + [1230.4,'0,0.0-','1,230.4'], [100.78, '0', '101'], [100.28, '0', '100'], [1.932,'0.0','1.9'], From 807927c11e40db2b936702ff150ff105ef427c87 Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Mon, 19 Dec 2016 23:56:12 -0700 Subject: [PATCH 3/9] update readme with change --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 02089c17..eac56b78 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,10 @@ See [the english unit tests](https://github.com/adamwdraper/Numeral-js/blob/mast # Changelog +### 2.1.0 + +Bug fix: Signed currency is inconsistent + ### 2.0.2 Bug fix: Updated module definitions From 14d0cd392d796a1fde8487617fe3455dad85b17e Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Tue, 20 Dec 2016 22:33:27 -0700 Subject: [PATCH 4/9] fix rounding --- min/numeral.min.js | 2 +- numeral.js | 21 ++++++++++++++++++++- src/numeral.js | 21 ++++++++++++++++++++- tests/numeral.js | 2 ++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/min/numeral.min.js b/min/numeral.min.js index 67f2f9c7..efcfbb45 100644 --- a/min/numeral.min.js +++ b/min/numeral.min.js @@ -5,4 +5,4 @@ * license : MIT * http://adamwdraper.github.com/Numeral-js/ */ -!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof module&&module.exports?module.exports=b():a.numeral=b()}(this,function(){function a(a,b){this._input=a,this._value=b}var b,c,d="2.0.3",e={},f={},g={currentLocale:"en",zeroFormat:null,nullFormat:null,defaultFormat:"0,0"},h={currentLocale:g.currentLocale,zeroFormat:g.zeroFormat,nullFormat:g.nullFormat,defaultFormat:g.defaultFormat};return b=function(d){var f,g,i,j;if(b.isNumeral(d))f=d.value();else if(0===d||"undefined"==typeof d)f=0;else if(null===d||c.isNaN(d))f=null;else if("string"==typeof d)if(h.zeroFormat&&d===h.zeroFormat)f=0;else if(h.nullFormat&&d===h.nullFormat||!d.replace(/[^0-9]+/g,"").length)f=null;else{for(g in e)if(j="function"==typeof e[g].regexps.unformat?e[g].regexps.unformat():e[g].regexps.unformat,j&&d.match(j)){i=e[g].unformat;break}i=i||b._.stringToNumber,f=i(d)}else f=Number(d)||null;return new a(d,f)},b.version=d,b.isNumeral=function(b){return b instanceof a},b._=c={numberToFormat:function(a,c,d){var e,g,h,i,j,k=f[b.options.currentLocale],l=!1,m=!1,n=!1,o="",p=1e12,q=1e9,r=1e6,s=1e3,t="",u=!1;return a=a||0,g=Math.abs(a),b._.includes(c,"(")?(l=!0,c=c.slice(1,-1)):b._.includes(c,"+")&&(m=!0,c=c.replace(/\+/g,"")),b._.includes(c,"a")&&(e=c.match(/a(k|m|b|t)?/),e=e?e[1]:!1,b._.includes(c," a")&&(o=" "),c=c.replace(new RegExp(o+"a[kmbt]?"),""),g>=p&&!e||"t"===e?(o+=k.abbreviations.trillion,a/=p):p>g&&g>=q&&!e||"b"===e?(o+=k.abbreviations.billion,a/=q):q>g&&g>=r&&!e||"m"===e?(o+=k.abbreviations.million,a/=r):(r>g&&g>=s&&!e||"k"===e)&&(o+=k.abbreviations.thousand,a/=s)),b._.includes(c,"[.]")&&(n=!0,c=c.replace("[.]",".")),h=a.toString().split(".")[0],i=c.split(".")[1],j=c.indexOf(","),i?(b._.includes(i,"[")?(i=i.replace("]",""),i=i.split("["),t=b._.toFixed(a,i[0].length+i[1].length,d,i[1].length)):t=b._.toFixed(a,i.length,d),h=t.split(".")[0],t=b._.includes(t,".")?k.delimiters.decimal+t.split(".")[1]:"",n&&0===Number(t.slice(1))&&(t="")):h=b._.toFixed(a,null,d),b._.includes(h,"-")&&(h=h.slice(1),u=!0),j>-1&&(h=h.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+k.delimiters.thousands)),0===c.indexOf(".")&&(h=""),(l&&u?"(":"")+(!l&&u?"-":"")+(!u&&m?"+":"")+h+t+(o?o:"")+(l&&u?")":"")},stringToNumber:function(a){var b,c,d,e=f[h.currentLocale],g=a,i={thousand:3,million:6,billion:9,trillion:12};if(h.zeroFormat&&a===h.zeroFormat)c=0;else if(h.nullFormat&&a===h.nullFormat||!a.replace(/[^0-9]+/g,"").length)c=null;else{c=1,"."!==e.delimiters.decimal&&(a=a.replace(/\./g,"").replace(e.delimiters.decimal,"."));for(b in i)if(d=new RegExp("[^a-zA-Z]"+e.abbreviations[b]+"(?:\\)|(\\"+e.currency.symbol+")?(?:\\))?)?$"),g.match(d)){c*=Math.pow(10,i[b]);break}c*=(a.split("-").length+Math.min(a.split("(").length-1,a.split(")").length-1))%2?1:-1,a=a.replace(/[^0-9\.]+/g,""),c*=Number(a)}return c},isNaN:function(a){return"number"==typeof a&&isNaN(a)},includes:function(a,b){return-1!==a.indexOf(b)},reduce:function(a,b){if(null===this)throw new TypeError("Array.prototype.reduce called on null or undefined");if("function"!=typeof b)throw new TypeError(b+" is not a function");var c,d=Object(a),e=d.length>>>0,f=0;if(3===arguments.length)c=arguments[2];else{for(;e>f&&!(f in d);)f++;if(f>=e)throw new TypeError("Reduce of empty array with no initial value");c=d[f++]}for(;e>f;f++)f in d&&(c=b(c,d[f],f,d));return c},multiplier:function(a){var b=a.toString().split(".");return b.length<2?1:Math.pow(10,b[1].length)},correctionFactor:function(){var a=Array.prototype.slice.call(arguments);return a.reduce(function(a,b){var d=c.multiplier(b);return a>d?a:d},1)},toFixed:function(a,b,c,d){var e,f,g,h,i=a.toString().split("."),j=b-(d||0);return e=2===i.length?Math.min(Math.max(i[1].length,j),b):j,g=Math.pow(10,e),h=(c(a*g)/g).toFixed(e),d>b-e&&(f=new RegExp("\\.?0{1,"+(d-(b-e))+"}$"),h=h.replace(f,"")),h}},b.options=h,b.formats=e,b.locales=f,b.locale=function(a){return a&&(h.currentLocale=a.toLowerCase()),h.currentLocale},b.localeData=function(a){if(!a)return f[h.currentLocale];if(a=a.toLowerCase(),!f[a])throw new Error("Unknown locale : "+a);return f[a]},b.reset=function(){for(var a in g)h[a]=g[a]},b.zeroFormat=function(a){h.zeroFormat="string"==typeof a?a:null},b.nullFormat=function(a){h.nullFormat="string"==typeof a?a:null},b.defaultFormat=function(a){h.defaultFormat="string"==typeof a?a:"0.0"},b.register=function(a,b,c){if(b=b.toLowerCase(),this[a+"s"][b])throw new TypeError(b+" "+a+" already registered.");return this[a+"s"][b]=c,c},b.validate=function(a,c){var d,e,f,g,h,i,j,k;if("string"!=typeof a&&(a+="",console.warn&&console.warn("Numeral.js: Value is not string. It has been co-erced to: ",a)),a=a.trim(),a.match(/^\d+$/))return!0;if(""===a)return!1;try{j=b.localeData(c)}catch(l){j=b.localeData(b.locale())}return f=j.currency.symbol,h=j.abbreviations,d=j.delimiters.decimal,e="."===j.delimiters.thousands?"\\.":j.delimiters.thousands,k=a.match(/^[^\d]+/),null!==k&&(a=a.substr(1),k[0]!==f)?!1:(k=a.match(/[^\d]+$/),null!==k&&(a=a.slice(0,-1),k[0]!==h.thousand&&k[0]!==h.million&&k[0]!==h.billion&&k[0]!==h.trillion)?!1:(i=new RegExp(e+"{2}"),a.match(/[^\d.,]/g)?!1:(g=a.split(d),g.length>2?!1:g.length<2?!!g[0].match(/^\d+.*\d$/)&&!g[0].match(i):1===g[0].length?!!g[0].match(/^\d+$/)&&!g[0].match(i)&&!!g[1].match(/^\d+$/):!!g[0].match(/^\d+.*\d$/)&&!g[0].match(i)&&!!g[1].match(/^\d+$/))))},b.fn=a.prototype={clone:function(){return b(this)},format:function(a,c){var d,f,g,i=this._value,j=a||h.defaultFormat;if(c=c||Math.round,0===i&&null!==h.zeroFormat)f=h.zeroFormat;else if(null===i&&null!==h.nullFormat)f=h.nullFormat;else{for(d in e)if(j.match(e[d].regexps.format)){g=e[d].format;break}g=g||b._.numberToFormat,f=g(i,j,c)}return f},value:function(){return this._value},input:function(){return this._input},set:function(a){return this._value=Number(a),this},add:function(a){function b(a,b,c,e){return a+Math.round(d*b)}var d=c.correctionFactor.call(null,this._value,a);return this._value=c.reduce([this._value,a],b,0)/d,this},subtract:function(a){function b(a,b,c,e){return a-Math.round(d*b)}var d=c.correctionFactor.call(null,this._value,a);return this._value=c.reduce([a],b,Math.round(this._value*d))/d,this},multiply:function(a){function b(a,b,d,e){var f=c.correctionFactor(a,b);return Math.round(a*f)*Math.round(b*f)/Math.round(f*f)}return this._value=c.reduce([this._value,a],b,1),this},divide:function(a){function b(a,b,d,e){var f=c.correctionFactor(a,b);return Math.round(a*f)/Math.round(b*f)}return this._value=c.reduce([this._value,a],b),this},difference:function(a){return Math.abs(b(this._value).subtract(a).value())}},b.register("locale","en",{delimiters:{thousands:",",decimal:"."},abbreviations:{thousand:"k",million:"m",billion:"b",trillion:"t"},ordinal:function(a){var b=a%10;return 1===~~(a%100/10)?"th":1===b?"st":2===b?"nd":3===b?"rd":"th"},currency:{symbol:"$"}}),function(){var a={base:1e3,suffixes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]},c={base:1024,suffixes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]};b.register("format","bytes",{regexps:{format:/([0\s]i?b)/,unformat:new RegExp("("+a.suffixes.concat(c.suffixes).join("|")+")")},format:function(d,e,f){var g,h,i,j,k=b._.includes(e,"ib")?c:a,l=b._.includes(e," b")||b._.includes(e," ib")?" ":"";for(e=e.replace(/\s?i?b/,""),h=0;h<=k.suffixes.length;h++)if(i=Math.pow(k.base,h),j=Math.pow(k.base,h+1),null===d||0===d||d>=i&&j>d){l+=k.suffixes[h],i>0&&(d/=i);break}return g=b._.numberToFormat(d,e,f),g+l},unformat:function(d){var e,f,g=b._.stringToNumber(d);if(g){for(e=a.suffixes.length-1;e>=0;e--){if(b._.includes(d,a.suffixes[e])){f=Math.pow(a.base,e);break}if(b._.includes(d,c.suffixes[e])){f=Math.pow(c.base,e);break}}g*=f||1}return g}})}(),function(){b.register("format","currency",{regexps:{format:/(\$)/},format:function(a,c,d){var e,f,g=b.locales[b.options.currentLocale],h=c.indexOf("$"),i=c.indexOf("("),j=c.indexOf("-"),k=b._.includes(c," $")||b._.includes(c,"$ ")?" ":"";return c=c.replace(/\s?\$\s?/,""),f=b._.numberToFormat(a,c,d),1>=h?b._.includes(f,"(")||b._.includes(f,"-")?(f=f.split(""),e=i>h||j>h?0:1,f.splice(e,0,g.currency.symbol+k),f=f.join("")):f=g.currency.symbol+k+f:b._.includes(f,")")?(f=f.split(""),f.splice(-1,0,k+g.currency.symbol),f=f.join("")):f=f+k+g.currency.symbol,f}})}(),function(){b.register("format","exponential",{regexps:{format:/(e\+|e-)/,unformat:/(e\+|e-)/},format:function(a,c,d){var e,f="number"!=typeof a||b._.isNaN(a)?"0e+0":a.toExponential(),g=f.split("e");return c=c.replace(/e[\+|\-]{1}0/,""),e=b._.numberToFormat(Number(g[0]),c,d),e+"e"+g[1]},unformat:function(a){function c(a,c,d,e){var f=b._.correctionFactor(a,c),g=a*f*(c*f)/(f*f);return g}var d=b._.includes(a,"e+")?a.split("e+"):a.split("e-"),e=Number(d[0]),f=Number(d[1]);return f=b._.includes(a,"e-")?f*=-1:f,b._.reduce([e,Math.pow(10,f)],c,1)}})}(),function(){b.register("format","ordinal",{regexps:{format:/(o)/},format:function(a,c,d){var e,f=b.locales[b.options.currentLocale],g=b._.includes(c," o")?" ":"";return c=c.replace(/\s?o/,""),g+=f.ordinal(a),e=b._.numberToFormat(a,c,d),e+g}})}(),function(){b.register("format","percentage",{regexps:{format:/(%)/,unformat:/(%)/},format:function(a,c,d){var e,f=b._.includes(c," %")?" ":"";return a=100*a,c=c.replace(/\s?\%/,""),e=b._.numberToFormat(a,c,d),b._.includes(e,")")?(e=e.split(""),e.splice(-1,0,f+"%"),e=e.join("")):e=e+f+"%",e},unformat:function(a){return.01*b._.stringToNumber(a)}})}(),function(){b.register("format","time",{regexps:{format:/(:)/,unformat:/(:)/},format:function(a,b,c){var d=Math.floor(a/60/60),e=Math.floor((a-60*d*60)/60),f=Math.round(a-60*d*60-60*e);return d+":"+(10>e?"0"+e:e)+":"+(10>f?"0"+f:f)},unformat:function(a){var b=a.split(":"),c=0;return 3===b.length?(c+=60*Number(b[0])*60,c+=60*Number(b[1]),c+=Number(b[2])):2===b.length&&(c+=60*Number(b[0]),c+=Number(b[1])),Number(c)}})}(),b}); \ No newline at end of file +!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof module&&module.exports?module.exports=b():a.numeral=b()}(this,function(){function a(a,b){this._input=a,this._value=b}var b,c,d="2.0.3",e={},f={},g={currentLocale:"en",zeroFormat:null,nullFormat:null,defaultFormat:"0,0"},h={currentLocale:g.currentLocale,zeroFormat:g.zeroFormat,nullFormat:g.nullFormat,defaultFormat:g.defaultFormat};return b=function(d){var f,g,i,j;if(b.isNumeral(d))f=d.value();else if(0===d||"undefined"==typeof d)f=0;else if(null===d||c.isNaN(d))f=null;else if("string"==typeof d)if(h.zeroFormat&&d===h.zeroFormat)f=0;else if(h.nullFormat&&d===h.nullFormat||!d.replace(/[^0-9]+/g,"").length)f=null;else{for(g in e)if(j="function"==typeof e[g].regexps.unformat?e[g].regexps.unformat():e[g].regexps.unformat,j&&d.match(j)){i=e[g].unformat;break}i=i||b._.stringToNumber,f=i(d)}else f=Number(d)||null;return new a(d,f)},b.version=d,b.isNumeral=function(b){return b instanceof a},b._=c={numberToFormat:function(a,c,d){var e,g,h,i,j,k,l,m=f[b.options.currentLocale],n=!1,o=!1,p="",q=1e12,r=1e9,s=1e6,t=1e3,u="",v=!1;if(a=a||0,g=Math.abs(a),b._.includes(c,"(")?(n=!0,c=c.replace(/[\(|\)]/g,"")):(b._.includes(c,"+")||b._.includes(c,"-"))&&(j=b._.includes(c,"+")?c.indexOf("+"):0>a?c.indexOf("-"):-1,c=c.replace(/[\+|\-]/g,"")),b._.includes(c,"a")&&(e=c.match(/a(k|m|b|t)?/),e=e?e[1]:!1,b._.includes(c," a")&&(p=" "),c=c.replace(new RegExp(p+"a[kmbt]?"),""),g>=q&&!e||"t"===e?(p+=m.abbreviations.trillion,a/=q):q>g&&g>=r&&!e||"b"===e?(p+=m.abbreviations.billion,a/=r):r>g&&g>=s&&!e||"m"===e?(p+=m.abbreviations.million,a/=s):(s>g&&g>=t&&!e||"k"===e)&&(p+=m.abbreviations.thousand,a/=t)),b._.includes(c,"[.]")&&(o=!0,c=c.replace("[.]",".")),h=a.toString().split(".")[0],i=c.split(".")[1],k=c.indexOf(","),i?(b._.includes(i,"[")?(i=i.replace("]",""),i=i.split("["),u=b._.toFixed(a,i[0].length+i[1].length,d,i[1].length)):u=b._.toFixed(a,i.length,d),h=u.split(".")[0],u=b._.includes(u,".")?m.delimiters.decimal+u.split(".")[1]:"",o&&0===Number(u.slice(1))&&(u="")):h=b._.toFixed(a,null,d),p&&!e&&Number(h)>=1e3&&p!==m.abbreviations.trillion)switch(h=String(Number(h)/1e3),p){case m.abbreviations.thousand:p=m.abbreviations.million;break;case m.abbreviations.million:p=m.abbreviations.billion;break;case m.abbreviations.billion:p=m.abbreviations.trillion}return b._.includes(h,"-")&&(h=h.slice(1),v=!0),k>-1&&(h=h.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+m.delimiters.thousands)),0===c.indexOf(".")&&(h=""),l=h+u+(p?p:""),n?l=(n&&v?"(":"")+l+(n&&v?")":""):j>=0?l=0===j?(v?"-":"+")+l:l+(v?"-":"+"):v&&(l="-"+l),l},stringToNumber:function(a){var b,c,d,e=f[h.currentLocale],g=a,i={thousand:3,million:6,billion:9,trillion:12};if(h.zeroFormat&&a===h.zeroFormat)c=0;else if(h.nullFormat&&a===h.nullFormat||!a.replace(/[^0-9]+/g,"").length)c=null;else{c=1,"."!==e.delimiters.decimal&&(a=a.replace(/\./g,"").replace(e.delimiters.decimal,"."));for(b in i)if(d=new RegExp("[^a-zA-Z]"+e.abbreviations[b]+"(?:\\)|(\\"+e.currency.symbol+")?(?:\\))?)?$"),g.match(d)){c*=Math.pow(10,i[b]);break}c*=(a.split("-").length+Math.min(a.split("(").length-1,a.split(")").length-1))%2?1:-1,a=a.replace(/[^0-9\.]+/g,""),c*=Number(a)}return c},isNaN:function(a){return"number"==typeof a&&isNaN(a)},includes:function(a,b){return-1!==a.indexOf(b)},insert:function(a,b,c){return a.slice(0,c)+b+a.slice(c)},reduce:function(a,b){if(null===this)throw new TypeError("Array.prototype.reduce called on null or undefined");if("function"!=typeof b)throw new TypeError(b+" is not a function");var c,d=Object(a),e=d.length>>>0,f=0;if(3===arguments.length)c=arguments[2];else{for(;e>f&&!(f in d);)f++;if(f>=e)throw new TypeError("Reduce of empty array with no initial value");c=d[f++]}for(;e>f;f++)f in d&&(c=b(c,d[f],f,d));return c},multiplier:function(a){var b=a.toString().split(".");return b.length<2?1:Math.pow(10,b[1].length)},correctionFactor:function(){var a=Array.prototype.slice.call(arguments);return a.reduce(function(a,b){var d=c.multiplier(b);return a>d?a:d},1)},toFixed:function(a,b,c,d){var e,f,g,h,i=a.toString().split("."),j=b-(d||0);return e=2===i.length?Math.min(Math.max(i[1].length,j),b):j,g=Math.pow(10,e),h=(c(a*g)/g).toFixed(e),d>b-e&&(f=new RegExp("\\.?0{1,"+(d-(b-e))+"}$"),h=h.replace(f,"")),h}},b.options=h,b.formats=e,b.locales=f,b.locale=function(a){return a&&(h.currentLocale=a.toLowerCase()),h.currentLocale},b.localeData=function(a){if(!a)return f[h.currentLocale];if(a=a.toLowerCase(),!f[a])throw new Error("Unknown locale : "+a);return f[a]},b.reset=function(){for(var a in g)h[a]=g[a]},b.zeroFormat=function(a){h.zeroFormat="string"==typeof a?a:null},b.nullFormat=function(a){h.nullFormat="string"==typeof a?a:null},b.defaultFormat=function(a){h.defaultFormat="string"==typeof a?a:"0.0"},b.register=function(a,b,c){if(b=b.toLowerCase(),this[a+"s"][b])throw new TypeError(b+" "+a+" already registered.");return this[a+"s"][b]=c,c},b.validate=function(a,c){var d,e,f,g,h,i,j,k;if("string"!=typeof a&&(a+="",console.warn&&console.warn("Numeral.js: Value is not string. It has been co-erced to: ",a)),a=a.trim(),a.match(/^\d+$/))return!0;if(""===a)return!1;try{j=b.localeData(c)}catch(l){j=b.localeData(b.locale())}return f=j.currency.symbol,h=j.abbreviations,d=j.delimiters.decimal,e="."===j.delimiters.thousands?"\\.":j.delimiters.thousands,k=a.match(/^[^\d]+/),null!==k&&(a=a.substr(1),k[0]!==f)?!1:(k=a.match(/[^\d]+$/),null!==k&&(a=a.slice(0,-1),k[0]!==h.thousand&&k[0]!==h.million&&k[0]!==h.billion&&k[0]!==h.trillion)?!1:(i=new RegExp(e+"{2}"),a.match(/[^\d.,]/g)?!1:(g=a.split(d),g.length>2?!1:g.length<2?!!g[0].match(/^\d+.*\d$/)&&!g[0].match(i):1===g[0].length?!!g[0].match(/^\d+$/)&&!g[0].match(i)&&!!g[1].match(/^\d+$/):!!g[0].match(/^\d+.*\d$/)&&!g[0].match(i)&&!!g[1].match(/^\d+$/))))},b.fn=a.prototype={clone:function(){return b(this)},format:function(a,c){var d,f,g,i=this._value,j=a||h.defaultFormat;if(c=c||Math.round,0===i&&null!==h.zeroFormat)f=h.zeroFormat;else if(null===i&&null!==h.nullFormat)f=h.nullFormat;else{for(d in e)if(j.match(e[d].regexps.format)){g=e[d].format;break}g=g||b._.numberToFormat,f=g(i,j,c)}return f},value:function(){return this._value},input:function(){return this._input},set:function(a){return this._value=Number(a),this},add:function(a){function b(a,b,c,e){return a+Math.round(d*b)}var d=c.correctionFactor.call(null,this._value,a);return this._value=c.reduce([this._value,a],b,0)/d,this},subtract:function(a){function b(a,b,c,e){return a-Math.round(d*b)}var d=c.correctionFactor.call(null,this._value,a);return this._value=c.reduce([a],b,Math.round(this._value*d))/d,this},multiply:function(a){function b(a,b,d,e){var f=c.correctionFactor(a,b);return Math.round(a*f)*Math.round(b*f)/Math.round(f*f)}return this._value=c.reduce([this._value,a],b,1),this},divide:function(a){function b(a,b,d,e){var f=c.correctionFactor(a,b);return Math.round(a*f)/Math.round(b*f)}return this._value=c.reduce([this._value,a],b),this},difference:function(a){return Math.abs(b(this._value).subtract(a).value())}},b.register("locale","en",{delimiters:{thousands:",",decimal:"."},abbreviations:{thousand:"k",million:"m",billion:"b",trillion:"t"},ordinal:function(a){var b=a%10;return 1===~~(a%100/10)?"th":1===b?"st":2===b?"nd":3===b?"rd":"th"},currency:{symbol:"$"}}),function(){var a={base:1e3,suffixes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]},c={base:1024,suffixes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]};b.register("format","bytes",{regexps:{format:/([0\s]i?b)/,unformat:new RegExp("("+a.suffixes.concat(c.suffixes).join("|")+")")},format:function(d,e,f){var g,h,i,j,k=b._.includes(e,"ib")?c:a,l=b._.includes(e," b")||b._.includes(e," ib")?" ":"";for(e=e.replace(/\s?i?b/,""),h=0;h<=k.suffixes.length;h++)if(i=Math.pow(k.base,h),j=Math.pow(k.base,h+1),null===d||0===d||d>=i&&j>d){l+=k.suffixes[h],i>0&&(d/=i);break}return g=b._.numberToFormat(d,e,f),g+l},unformat:function(d){var e,f,g=b._.stringToNumber(d);if(g){for(e=a.suffixes.length-1;e>=0;e--){if(b._.includes(d,a.suffixes[e])){f=Math.pow(a.base,e);break}if(b._.includes(d,c.suffixes[e])){f=Math.pow(c.base,e);break}}g*=f||1}return g}})}(),function(){b.register("format","currency",{regexps:{format:/(\$)/},format:function(a,c,d){var e,f,g,h=b.locales[b.options.currentLocale],i={before:c.match(/^([\+|\-|\(|\s|\$]*)/)[0],after:c.match(/([\+|\-|\)|\s|\$]*)$/)[0]};for(c=c.replace(/\s?\$\s?/,""),e=b._.numberToFormat(a,c,d),a>=0?(i.before=i.before.replace(/[\-\(]/,""),i.after=i.after.replace(/[\-\)]/,"")):0>a&&!b._.includes(i.before,"-")&&!b._.includes(i.before,"(")&&(i.before="-"+i.before),g=0;g=0;g--)switch(f=i.after[g]){case"$":e=g===i.after.length-1?e+h.currency.symbol:b._.insert(e,h.currency.symbol,-(i.after.length-(1+g)));break;case" ":e=g===i.after.length-1?e+" ":b._.insert(e," ",-(i.after.length-(1+g)))}return e}})}(),function(){b.register("format","exponential",{regexps:{format:/(e\+|e-)/,unformat:/(e\+|e-)/},format:function(a,c,d){var e,f="number"!=typeof a||b._.isNaN(a)?"0e+0":a.toExponential(),g=f.split("e");return c=c.replace(/e[\+|\-]{1}0/,""),e=b._.numberToFormat(Number(g[0]),c,d),e+"e"+g[1]},unformat:function(a){function c(a,c,d,e){var f=b._.correctionFactor(a,c),g=a*f*(c*f)/(f*f);return g}var d=b._.includes(a,"e+")?a.split("e+"):a.split("e-"),e=Number(d[0]),f=Number(d[1]);return f=b._.includes(a,"e-")?f*=-1:f,b._.reduce([e,Math.pow(10,f)],c,1)}})}(),function(){b.register("format","ordinal",{regexps:{format:/(o)/},format:function(a,c,d){var e,f=b.locales[b.options.currentLocale],g=b._.includes(c," o")?" ":"";return c=c.replace(/\s?o/,""),g+=f.ordinal(a),e=b._.numberToFormat(a,c,d),e+g}})}(),function(){b.register("format","percentage",{regexps:{format:/(%)/,unformat:/(%)/},format:function(a,c,d){var e,f=b._.includes(c," %")?" ":"";return a=100*a,c=c.replace(/\s?\%/,""),e=b._.numberToFormat(a,c,d),b._.includes(e,")")?(e=e.split(""),e.splice(-1,0,f+"%"),e=e.join("")):e=e+f+"%",e},unformat:function(a){return.01*b._.stringToNumber(a)}})}(),function(){b.register("format","time",{regexps:{format:/(:)/,unformat:/(:)/},format:function(a,b,c){var d=Math.floor(a/60/60),e=Math.floor((a-60*d*60)/60),f=Math.round(a-60*d*60-60*e);return d+":"+(10>e?"0"+e:e)+":"+(10>f?"0"+f:f)},unformat:function(a){var b=a.split(":"),c=0;return 3===b.length?(c+=60*Number(b[0])*60,c+=60*Number(b[1]),c+=Number(b[2])):2===b.length&&(c+=60*Number(b[0]),c+=Number(b[1])),Number(c)}})}(),b}); \ No newline at end of file diff --git a/numeral.js b/numeral.js index f2e1e1e6..7199afc3 100644 --- a/numeral.js +++ b/numeral.js @@ -168,12 +168,13 @@ } } - + // check for optional decimals if (numeral._.includes(format, '[.]')) { optDec = true; format = format.replace('[.]', '.'); } + // break number and format int = value.toString().split('.')[0]; precision = format.split('.')[1]; thousands = format.indexOf(','); @@ -202,6 +203,24 @@ int = numeral._.toFixed(value, null, roundingFunction); } + // check abbreviation again after rounding + if (abbr && !abbrForce && Number(int) >= 1000 && abbr !== locale.abbreviations.trillion) { + int = String(Number(int) / 1000); + + switch (abbr) { + case locale.abbreviations.thousand: + abbr = locale.abbreviations.million; + break; + case locale.abbreviations.million: + abbr = locale.abbreviations.billion; + break; + case locale.abbreviations.billion: + abbr = locale.abbreviations.trillion; + break; + } + } + + // format number if (numeral._.includes(int, '-')) { int = int.slice(1); diff --git a/src/numeral.js b/src/numeral.js index 6500b464..b6a50007 100644 --- a/src/numeral.js +++ b/src/numeral.js @@ -168,12 +168,13 @@ } } - + // check for optional decimals if (numeral._.includes(format, '[.]')) { optDec = true; format = format.replace('[.]', '.'); } + // break number and format int = value.toString().split('.')[0]; precision = format.split('.')[1]; thousands = format.indexOf(','); @@ -202,6 +203,24 @@ int = numeral._.toFixed(value, null, roundingFunction); } + // check abbreviation again after rounding + if (abbr && !abbrForce && Number(int) >= 1000 && abbr !== locale.abbreviations.trillion) { + int = String(Number(int) / 1000); + + switch (abbr) { + case locale.abbreviations.thousand: + abbr = locale.abbreviations.million; + break; + case locale.abbreviations.million: + abbr = locale.abbreviations.billion; + break; + case locale.abbreviations.billion: + abbr = locale.abbreviations.trillion; + break; + } + } + + // format number if (numeral._.includes(int, '-')) { int = int.slice(1); diff --git a/tests/numeral.js b/tests/numeral.js index 3e2a566f..8436e163 100644 --- a/tests/numeral.js +++ b/tests/numeral.js @@ -175,6 +175,8 @@ describe('Numeral', function() { [1230974,'0.0a','1.2m'], [1460,'0a','1k'], [-104000,'0 a','-104 k'], + [999950,'0.0a','1.0m'], + [999999999,'0a','1b'], // forced abbreviations [-5444333222111, '0,0 ak', '-5,444,333,222 k'], [5444333222111, '0,0 am', '5,444,333 m'], From 2a69b1886b599133997f6ec328bed0c5098285e6 Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Wed, 21 Dec 2016 10:57:05 -0700 Subject: [PATCH 5/9] task for versioning of son files --- Gruntfile.js | 34 ++++++++++++++++++++++++++++------ bower.json | 2 +- component.json | 2 +- package.json | 4 +++- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 71133c20..9ebcff97 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -44,6 +44,8 @@ module.exports = function(grunt) { grunt.file.write('numeral.js', numeral); }; + require('load-grunt-tasks')(grunt); + grunt.initConfig({ mochaTest : { all: [ @@ -152,12 +154,6 @@ module.exports = function(grunt) { } }); - grunt.loadNpmTasks('grunt-contrib-copy'); - grunt.loadNpmTasks('grunt-contrib-uglify'); - grunt.loadNpmTasks('grunt-contrib-jshint'); - grunt.loadNpmTasks('grunt-mocha-test'); - grunt.loadNpmTasks('grunt-karma'); - grunt.registerTask('default', [ 'test' ]); @@ -194,6 +190,32 @@ module.exports = function(grunt) { 'uglify' ]); + grunt.registerTask('version', function (version) { + if (!version || version.split('.').length !== 3) { + grunt.fail.fatal('malformed version. Use\n\n grunt version:1.2.3'); + } + + grunt.config('string-replace.json', { + files: { + 'package.json': 'package.json', + 'component.json': 'component.json', + 'bower.json': 'bower.json' + }, + options: { + replacements: [ + { + pattern: /"version": .*/, + replacement: '"version": "' + version + '",' + } + ] + } + }); + + grunt.task.run([ + 'string-replace:json' + ]); + }); + // Travis CI task. grunt.registerTask('travis', [ 'build', diff --git a/bower.json b/bower.json index 1c452e26..a1c69051 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "numeral", "repo": "adamwdraper/Numeral-js", - "version": "2.0.3", + "version": "2.0.4", "description": "Format and manipulate numbers.", "keywords": [ "numeral", diff --git a/component.json b/component.json index 1c452e26..a1c69051 100644 --- a/component.json +++ b/component.json @@ -1,7 +1,7 @@ { "name": "numeral", "repo": "adamwdraper/Numeral-js", - "version": "2.0.3", + "version": "2.0.4", "description": "Format and manipulate numbers.", "keywords": [ "numeral", diff --git a/package.json b/package.json index 760c6524..804cd7b6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "numeral", - "version": "2.0.3", + "version": "2.0.4", "description": "Format and manipulate numbers.", "homepage": "http://numeraljs.com", "author": { @@ -38,6 +38,7 @@ "grunt-karma": "^2.0.0", "grunt-mocha-test": "^0.13.2", "grunt-saucelabs": "*", + "grunt-string-replace": "^1.3.1", "karma": "^1.3.0", "karma-chai": "^0.1.0", "karma-chrome-launcher": "^2.0.0", @@ -45,6 +46,7 @@ "karma-mocha": "^1.3.0", "karma-mocha-reporter": "^2.2.1", "karma-sauce-launcher": "^1.1.0", + "load-grunt-tasks": "^3.5.2", "mocha": "^3.1.2", "uglify-js": "latest" }, From 4ca9ac16134a8d18c70ebad295514f8fef29ac23 Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Wed, 21 Dec 2016 11:01:59 -0700 Subject: [PATCH 6/9] update numeral version --- Gruntfile.js | 21 ++++++++++++++++++++- src/numeral.js | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 9ebcff97..dae287cf 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -211,8 +211,27 @@ module.exports = function(grunt) { } }); + grunt.config('string-replace.numeral', { + files: { + 'src/numeral.js': 'src/numeral.js' + }, + options: { + replacements: [ + { + pattern: /version : .*/, + replacement: 'version : ' + version + }, + { + pattern: /VERSION = .*/, + replacement: 'VERSION = \'' + version + '\',' + } + ] + } + }); + grunt.task.run([ - 'string-replace:json' + 'string-replace:json', + 'string-replace:numeral' ]); }); diff --git a/src/numeral.js b/src/numeral.js index b6a50007..4b024c28 100644 --- a/src/numeral.js +++ b/src/numeral.js @@ -1,6 +1,6 @@ /*! @preserve * numeral.js - * version : 2.0.3 + * version : 2.0.4 * author : Adam Draper * license : MIT * http://adamwdraper.github.com/Numeral-js/ @@ -21,7 +21,7 @@ var numeral, _, - VERSION = '2.0.3', + VERSION = '2.0.4', formats = {}, locales = {}, defaults = { From be2205a3e1a41fbf05ab2b50b28a3477eb90434f Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Wed, 21 Dec 2016 11:19:47 -0700 Subject: [PATCH 7/9] update template --- Gruntfile.js | 15 +++++++++++++++ templates/types.js | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index dae287cf..a1da9e49 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -229,8 +229,23 @@ module.exports = function(grunt) { } }); + grunt.config('string-replace.templates', { + files: { + 'templates/types.js': 'templates/types.js' + }, + options: { + replacements: [ + { + pattern: /: .*/, + replacement: ': ' + version + } + ] + } + }); + grunt.task.run([ 'string-replace:json', + 'string-replace:templates', 'string-replace:numeral' ]); }); diff --git a/templates/types.js b/templates/types.js index 6ba9005e..a09d14d5 100644 --- a/templates/types.js +++ b/templates/types.js @@ -1,6 +1,6 @@ /*! @preserve * numeral.js - * <%= type %>: 2.0.3 + * <%= type %> : 2.0.4 * license : MIT * http://adamwdraper.github.com/Numeral-js/ */ From 41d586ccbddfa4a06b6bf2df6499ed3059cd5cf2 Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Wed, 21 Dec 2016 11:20:30 -0700 Subject: [PATCH 8/9] build files --- locales.js | 2 +- min/locales.min.js | 2 +- min/numeral.min.js | 4 ++-- numeral.js | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/locales.js b/locales.js index 6f18d729..e4df734f 100644 --- a/locales.js +++ b/locales.js @@ -1,6 +1,6 @@ /*! @preserve * numeral.js - * locales: 2.0.3 + * locales : 2.0.4 * license : MIT * http://adamwdraper.github.com/Numeral-js/ */ diff --git a/min/locales.min.js b/min/locales.min.js index 628fe679..a301ea6c 100644 --- a/min/locales.min.js +++ b/min/locales.min.js @@ -1,6 +1,6 @@ /*! @preserve * numeral.js - * locales: 2.0.3 + * locales : 2.0.4 * license : MIT * http://adamwdraper.github.com/Numeral-js/ */ diff --git a/min/numeral.min.js b/min/numeral.min.js index efcfbb45..4e3e7c8b 100644 --- a/min/numeral.min.js +++ b/min/numeral.min.js @@ -1,8 +1,8 @@ /*! @preserve * numeral.js - * version : 2.0.3 + * version : 2.0.4 * author : Adam Draper * license : MIT * http://adamwdraper.github.com/Numeral-js/ */ -!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof module&&module.exports?module.exports=b():a.numeral=b()}(this,function(){function a(a,b){this._input=a,this._value=b}var b,c,d="2.0.3",e={},f={},g={currentLocale:"en",zeroFormat:null,nullFormat:null,defaultFormat:"0,0"},h={currentLocale:g.currentLocale,zeroFormat:g.zeroFormat,nullFormat:g.nullFormat,defaultFormat:g.defaultFormat};return b=function(d){var f,g,i,j;if(b.isNumeral(d))f=d.value();else if(0===d||"undefined"==typeof d)f=0;else if(null===d||c.isNaN(d))f=null;else if("string"==typeof d)if(h.zeroFormat&&d===h.zeroFormat)f=0;else if(h.nullFormat&&d===h.nullFormat||!d.replace(/[^0-9]+/g,"").length)f=null;else{for(g in e)if(j="function"==typeof e[g].regexps.unformat?e[g].regexps.unformat():e[g].regexps.unformat,j&&d.match(j)){i=e[g].unformat;break}i=i||b._.stringToNumber,f=i(d)}else f=Number(d)||null;return new a(d,f)},b.version=d,b.isNumeral=function(b){return b instanceof a},b._=c={numberToFormat:function(a,c,d){var e,g,h,i,j,k,l,m=f[b.options.currentLocale],n=!1,o=!1,p="",q=1e12,r=1e9,s=1e6,t=1e3,u="",v=!1;if(a=a||0,g=Math.abs(a),b._.includes(c,"(")?(n=!0,c=c.replace(/[\(|\)]/g,"")):(b._.includes(c,"+")||b._.includes(c,"-"))&&(j=b._.includes(c,"+")?c.indexOf("+"):0>a?c.indexOf("-"):-1,c=c.replace(/[\+|\-]/g,"")),b._.includes(c,"a")&&(e=c.match(/a(k|m|b|t)?/),e=e?e[1]:!1,b._.includes(c," a")&&(p=" "),c=c.replace(new RegExp(p+"a[kmbt]?"),""),g>=q&&!e||"t"===e?(p+=m.abbreviations.trillion,a/=q):q>g&&g>=r&&!e||"b"===e?(p+=m.abbreviations.billion,a/=r):r>g&&g>=s&&!e||"m"===e?(p+=m.abbreviations.million,a/=s):(s>g&&g>=t&&!e||"k"===e)&&(p+=m.abbreviations.thousand,a/=t)),b._.includes(c,"[.]")&&(o=!0,c=c.replace("[.]",".")),h=a.toString().split(".")[0],i=c.split(".")[1],k=c.indexOf(","),i?(b._.includes(i,"[")?(i=i.replace("]",""),i=i.split("["),u=b._.toFixed(a,i[0].length+i[1].length,d,i[1].length)):u=b._.toFixed(a,i.length,d),h=u.split(".")[0],u=b._.includes(u,".")?m.delimiters.decimal+u.split(".")[1]:"",o&&0===Number(u.slice(1))&&(u="")):h=b._.toFixed(a,null,d),p&&!e&&Number(h)>=1e3&&p!==m.abbreviations.trillion)switch(h=String(Number(h)/1e3),p){case m.abbreviations.thousand:p=m.abbreviations.million;break;case m.abbreviations.million:p=m.abbreviations.billion;break;case m.abbreviations.billion:p=m.abbreviations.trillion}return b._.includes(h,"-")&&(h=h.slice(1),v=!0),k>-1&&(h=h.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+m.delimiters.thousands)),0===c.indexOf(".")&&(h=""),l=h+u+(p?p:""),n?l=(n&&v?"(":"")+l+(n&&v?")":""):j>=0?l=0===j?(v?"-":"+")+l:l+(v?"-":"+"):v&&(l="-"+l),l},stringToNumber:function(a){var b,c,d,e=f[h.currentLocale],g=a,i={thousand:3,million:6,billion:9,trillion:12};if(h.zeroFormat&&a===h.zeroFormat)c=0;else if(h.nullFormat&&a===h.nullFormat||!a.replace(/[^0-9]+/g,"").length)c=null;else{c=1,"."!==e.delimiters.decimal&&(a=a.replace(/\./g,"").replace(e.delimiters.decimal,"."));for(b in i)if(d=new RegExp("[^a-zA-Z]"+e.abbreviations[b]+"(?:\\)|(\\"+e.currency.symbol+")?(?:\\))?)?$"),g.match(d)){c*=Math.pow(10,i[b]);break}c*=(a.split("-").length+Math.min(a.split("(").length-1,a.split(")").length-1))%2?1:-1,a=a.replace(/[^0-9\.]+/g,""),c*=Number(a)}return c},isNaN:function(a){return"number"==typeof a&&isNaN(a)},includes:function(a,b){return-1!==a.indexOf(b)},insert:function(a,b,c){return a.slice(0,c)+b+a.slice(c)},reduce:function(a,b){if(null===this)throw new TypeError("Array.prototype.reduce called on null or undefined");if("function"!=typeof b)throw new TypeError(b+" is not a function");var c,d=Object(a),e=d.length>>>0,f=0;if(3===arguments.length)c=arguments[2];else{for(;e>f&&!(f in d);)f++;if(f>=e)throw new TypeError("Reduce of empty array with no initial value");c=d[f++]}for(;e>f;f++)f in d&&(c=b(c,d[f],f,d));return c},multiplier:function(a){var b=a.toString().split(".");return b.length<2?1:Math.pow(10,b[1].length)},correctionFactor:function(){var a=Array.prototype.slice.call(arguments);return a.reduce(function(a,b){var d=c.multiplier(b);return a>d?a:d},1)},toFixed:function(a,b,c,d){var e,f,g,h,i=a.toString().split("."),j=b-(d||0);return e=2===i.length?Math.min(Math.max(i[1].length,j),b):j,g=Math.pow(10,e),h=(c(a*g)/g).toFixed(e),d>b-e&&(f=new RegExp("\\.?0{1,"+(d-(b-e))+"}$"),h=h.replace(f,"")),h}},b.options=h,b.formats=e,b.locales=f,b.locale=function(a){return a&&(h.currentLocale=a.toLowerCase()),h.currentLocale},b.localeData=function(a){if(!a)return f[h.currentLocale];if(a=a.toLowerCase(),!f[a])throw new Error("Unknown locale : "+a);return f[a]},b.reset=function(){for(var a in g)h[a]=g[a]},b.zeroFormat=function(a){h.zeroFormat="string"==typeof a?a:null},b.nullFormat=function(a){h.nullFormat="string"==typeof a?a:null},b.defaultFormat=function(a){h.defaultFormat="string"==typeof a?a:"0.0"},b.register=function(a,b,c){if(b=b.toLowerCase(),this[a+"s"][b])throw new TypeError(b+" "+a+" already registered.");return this[a+"s"][b]=c,c},b.validate=function(a,c){var d,e,f,g,h,i,j,k;if("string"!=typeof a&&(a+="",console.warn&&console.warn("Numeral.js: Value is not string. It has been co-erced to: ",a)),a=a.trim(),a.match(/^\d+$/))return!0;if(""===a)return!1;try{j=b.localeData(c)}catch(l){j=b.localeData(b.locale())}return f=j.currency.symbol,h=j.abbreviations,d=j.delimiters.decimal,e="."===j.delimiters.thousands?"\\.":j.delimiters.thousands,k=a.match(/^[^\d]+/),null!==k&&(a=a.substr(1),k[0]!==f)?!1:(k=a.match(/[^\d]+$/),null!==k&&(a=a.slice(0,-1),k[0]!==h.thousand&&k[0]!==h.million&&k[0]!==h.billion&&k[0]!==h.trillion)?!1:(i=new RegExp(e+"{2}"),a.match(/[^\d.,]/g)?!1:(g=a.split(d),g.length>2?!1:g.length<2?!!g[0].match(/^\d+.*\d$/)&&!g[0].match(i):1===g[0].length?!!g[0].match(/^\d+$/)&&!g[0].match(i)&&!!g[1].match(/^\d+$/):!!g[0].match(/^\d+.*\d$/)&&!g[0].match(i)&&!!g[1].match(/^\d+$/))))},b.fn=a.prototype={clone:function(){return b(this)},format:function(a,c){var d,f,g,i=this._value,j=a||h.defaultFormat;if(c=c||Math.round,0===i&&null!==h.zeroFormat)f=h.zeroFormat;else if(null===i&&null!==h.nullFormat)f=h.nullFormat;else{for(d in e)if(j.match(e[d].regexps.format)){g=e[d].format;break}g=g||b._.numberToFormat,f=g(i,j,c)}return f},value:function(){return this._value},input:function(){return this._input},set:function(a){return this._value=Number(a),this},add:function(a){function b(a,b,c,e){return a+Math.round(d*b)}var d=c.correctionFactor.call(null,this._value,a);return this._value=c.reduce([this._value,a],b,0)/d,this},subtract:function(a){function b(a,b,c,e){return a-Math.round(d*b)}var d=c.correctionFactor.call(null,this._value,a);return this._value=c.reduce([a],b,Math.round(this._value*d))/d,this},multiply:function(a){function b(a,b,d,e){var f=c.correctionFactor(a,b);return Math.round(a*f)*Math.round(b*f)/Math.round(f*f)}return this._value=c.reduce([this._value,a],b,1),this},divide:function(a){function b(a,b,d,e){var f=c.correctionFactor(a,b);return Math.round(a*f)/Math.round(b*f)}return this._value=c.reduce([this._value,a],b),this},difference:function(a){return Math.abs(b(this._value).subtract(a).value())}},b.register("locale","en",{delimiters:{thousands:",",decimal:"."},abbreviations:{thousand:"k",million:"m",billion:"b",trillion:"t"},ordinal:function(a){var b=a%10;return 1===~~(a%100/10)?"th":1===b?"st":2===b?"nd":3===b?"rd":"th"},currency:{symbol:"$"}}),function(){var a={base:1e3,suffixes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]},c={base:1024,suffixes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]};b.register("format","bytes",{regexps:{format:/([0\s]i?b)/,unformat:new RegExp("("+a.suffixes.concat(c.suffixes).join("|")+")")},format:function(d,e,f){var g,h,i,j,k=b._.includes(e,"ib")?c:a,l=b._.includes(e," b")||b._.includes(e," ib")?" ":"";for(e=e.replace(/\s?i?b/,""),h=0;h<=k.suffixes.length;h++)if(i=Math.pow(k.base,h),j=Math.pow(k.base,h+1),null===d||0===d||d>=i&&j>d){l+=k.suffixes[h],i>0&&(d/=i);break}return g=b._.numberToFormat(d,e,f),g+l},unformat:function(d){var e,f,g=b._.stringToNumber(d);if(g){for(e=a.suffixes.length-1;e>=0;e--){if(b._.includes(d,a.suffixes[e])){f=Math.pow(a.base,e);break}if(b._.includes(d,c.suffixes[e])){f=Math.pow(c.base,e);break}}g*=f||1}return g}})}(),function(){b.register("format","currency",{regexps:{format:/(\$)/},format:function(a,c,d){var e,f,g,h=b.locales[b.options.currentLocale],i={before:c.match(/^([\+|\-|\(|\s|\$]*)/)[0],after:c.match(/([\+|\-|\)|\s|\$]*)$/)[0]};for(c=c.replace(/\s?\$\s?/,""),e=b._.numberToFormat(a,c,d),a>=0?(i.before=i.before.replace(/[\-\(]/,""),i.after=i.after.replace(/[\-\)]/,"")):0>a&&!b._.includes(i.before,"-")&&!b._.includes(i.before,"(")&&(i.before="-"+i.before),g=0;g=0;g--)switch(f=i.after[g]){case"$":e=g===i.after.length-1?e+h.currency.symbol:b._.insert(e,h.currency.symbol,-(i.after.length-(1+g)));break;case" ":e=g===i.after.length-1?e+" ":b._.insert(e," ",-(i.after.length-(1+g)))}return e}})}(),function(){b.register("format","exponential",{regexps:{format:/(e\+|e-)/,unformat:/(e\+|e-)/},format:function(a,c,d){var e,f="number"!=typeof a||b._.isNaN(a)?"0e+0":a.toExponential(),g=f.split("e");return c=c.replace(/e[\+|\-]{1}0/,""),e=b._.numberToFormat(Number(g[0]),c,d),e+"e"+g[1]},unformat:function(a){function c(a,c,d,e){var f=b._.correctionFactor(a,c),g=a*f*(c*f)/(f*f);return g}var d=b._.includes(a,"e+")?a.split("e+"):a.split("e-"),e=Number(d[0]),f=Number(d[1]);return f=b._.includes(a,"e-")?f*=-1:f,b._.reduce([e,Math.pow(10,f)],c,1)}})}(),function(){b.register("format","ordinal",{regexps:{format:/(o)/},format:function(a,c,d){var e,f=b.locales[b.options.currentLocale],g=b._.includes(c," o")?" ":"";return c=c.replace(/\s?o/,""),g+=f.ordinal(a),e=b._.numberToFormat(a,c,d),e+g}})}(),function(){b.register("format","percentage",{regexps:{format:/(%)/,unformat:/(%)/},format:function(a,c,d){var e,f=b._.includes(c," %")?" ":"";return a=100*a,c=c.replace(/\s?\%/,""),e=b._.numberToFormat(a,c,d),b._.includes(e,")")?(e=e.split(""),e.splice(-1,0,f+"%"),e=e.join("")):e=e+f+"%",e},unformat:function(a){return.01*b._.stringToNumber(a)}})}(),function(){b.register("format","time",{regexps:{format:/(:)/,unformat:/(:)/},format:function(a,b,c){var d=Math.floor(a/60/60),e=Math.floor((a-60*d*60)/60),f=Math.round(a-60*d*60-60*e);return d+":"+(10>e?"0"+e:e)+":"+(10>f?"0"+f:f)},unformat:function(a){var b=a.split(":"),c=0;return 3===b.length?(c+=60*Number(b[0])*60,c+=60*Number(b[1]),c+=Number(b[2])):2===b.length&&(c+=60*Number(b[0]),c+=Number(b[1])),Number(c)}})}(),b}); \ No newline at end of file +!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof module&&module.exports?module.exports=b():a.numeral=b()}(this,function(){function a(a,b){this._input=a,this._value=b}var b,c,d="2.0.4",e={},f={},g={currentLocale:"en",zeroFormat:null,nullFormat:null,defaultFormat:"0,0"},h={currentLocale:g.currentLocale,zeroFormat:g.zeroFormat,nullFormat:g.nullFormat,defaultFormat:g.defaultFormat};return b=function(d){var f,g,i,j;if(b.isNumeral(d))f=d.value();else if(0===d||"undefined"==typeof d)f=0;else if(null===d||c.isNaN(d))f=null;else if("string"==typeof d)if(h.zeroFormat&&d===h.zeroFormat)f=0;else if(h.nullFormat&&d===h.nullFormat||!d.replace(/[^0-9]+/g,"").length)f=null;else{for(g in e)if(j="function"==typeof e[g].regexps.unformat?e[g].regexps.unformat():e[g].regexps.unformat,j&&d.match(j)){i=e[g].unformat;break}i=i||b._.stringToNumber,f=i(d)}else f=Number(d)||null;return new a(d,f)},b.version=d,b.isNumeral=function(b){return b instanceof a},b._=c={numberToFormat:function(a,c,d){var e,g,h,i,j,k,l,m=f[b.options.currentLocale],n=!1,o=!1,p="",q=1e12,r=1e9,s=1e6,t=1e3,u="",v=!1;if(a=a||0,g=Math.abs(a),b._.includes(c,"(")?(n=!0,c=c.replace(/[\(|\)]/g,"")):(b._.includes(c,"+")||b._.includes(c,"-"))&&(j=b._.includes(c,"+")?c.indexOf("+"):0>a?c.indexOf("-"):-1,c=c.replace(/[\+|\-]/g,"")),b._.includes(c,"a")&&(e=c.match(/a(k|m|b|t)?/),e=e?e[1]:!1,b._.includes(c," a")&&(p=" "),c=c.replace(new RegExp(p+"a[kmbt]?"),""),g>=q&&!e||"t"===e?(p+=m.abbreviations.trillion,a/=q):q>g&&g>=r&&!e||"b"===e?(p+=m.abbreviations.billion,a/=r):r>g&&g>=s&&!e||"m"===e?(p+=m.abbreviations.million,a/=s):(s>g&&g>=t&&!e||"k"===e)&&(p+=m.abbreviations.thousand,a/=t)),b._.includes(c,"[.]")&&(o=!0,c=c.replace("[.]",".")),h=a.toString().split(".")[0],i=c.split(".")[1],k=c.indexOf(","),i?(b._.includes(i,"[")?(i=i.replace("]",""),i=i.split("["),u=b._.toFixed(a,i[0].length+i[1].length,d,i[1].length)):u=b._.toFixed(a,i.length,d),h=u.split(".")[0],u=b._.includes(u,".")?m.delimiters.decimal+u.split(".")[1]:"",o&&0===Number(u.slice(1))&&(u="")):h=b._.toFixed(a,null,d),p&&!e&&Number(h)>=1e3&&p!==m.abbreviations.trillion)switch(h=String(Number(h)/1e3),p){case m.abbreviations.thousand:p=m.abbreviations.million;break;case m.abbreviations.million:p=m.abbreviations.billion;break;case m.abbreviations.billion:p=m.abbreviations.trillion}return b._.includes(h,"-")&&(h=h.slice(1),v=!0),k>-1&&(h=h.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+m.delimiters.thousands)),0===c.indexOf(".")&&(h=""),l=h+u+(p?p:""),n?l=(n&&v?"(":"")+l+(n&&v?")":""):j>=0?l=0===j?(v?"-":"+")+l:l+(v?"-":"+"):v&&(l="-"+l),l},stringToNumber:function(a){var b,c,d,e=f[h.currentLocale],g=a,i={thousand:3,million:6,billion:9,trillion:12};if(h.zeroFormat&&a===h.zeroFormat)c=0;else if(h.nullFormat&&a===h.nullFormat||!a.replace(/[^0-9]+/g,"").length)c=null;else{c=1,"."!==e.delimiters.decimal&&(a=a.replace(/\./g,"").replace(e.delimiters.decimal,"."));for(b in i)if(d=new RegExp("[^a-zA-Z]"+e.abbreviations[b]+"(?:\\)|(\\"+e.currency.symbol+")?(?:\\))?)?$"),g.match(d)){c*=Math.pow(10,i[b]);break}c*=(a.split("-").length+Math.min(a.split("(").length-1,a.split(")").length-1))%2?1:-1,a=a.replace(/[^0-9\.]+/g,""),c*=Number(a)}return c},isNaN:function(a){return"number"==typeof a&&isNaN(a)},includes:function(a,b){return-1!==a.indexOf(b)},insert:function(a,b,c){return a.slice(0,c)+b+a.slice(c)},reduce:function(a,b){if(null===this)throw new TypeError("Array.prototype.reduce called on null or undefined");if("function"!=typeof b)throw new TypeError(b+" is not a function");var c,d=Object(a),e=d.length>>>0,f=0;if(3===arguments.length)c=arguments[2];else{for(;e>f&&!(f in d);)f++;if(f>=e)throw new TypeError("Reduce of empty array with no initial value");c=d[f++]}for(;e>f;f++)f in d&&(c=b(c,d[f],f,d));return c},multiplier:function(a){var b=a.toString().split(".");return b.length<2?1:Math.pow(10,b[1].length)},correctionFactor:function(){var a=Array.prototype.slice.call(arguments);return a.reduce(function(a,b){var d=c.multiplier(b);return a>d?a:d},1)},toFixed:function(a,b,c,d){var e,f,g,h,i=a.toString().split("."),j=b-(d||0);return e=2===i.length?Math.min(Math.max(i[1].length,j),b):j,g=Math.pow(10,e),h=(c(a*g)/g).toFixed(e),d>b-e&&(f=new RegExp("\\.?0{1,"+(d-(b-e))+"}$"),h=h.replace(f,"")),h}},b.options=h,b.formats=e,b.locales=f,b.locale=function(a){return a&&(h.currentLocale=a.toLowerCase()),h.currentLocale},b.localeData=function(a){if(!a)return f[h.currentLocale];if(a=a.toLowerCase(),!f[a])throw new Error("Unknown locale : "+a);return f[a]},b.reset=function(){for(var a in g)h[a]=g[a]},b.zeroFormat=function(a){h.zeroFormat="string"==typeof a?a:null},b.nullFormat=function(a){h.nullFormat="string"==typeof a?a:null},b.defaultFormat=function(a){h.defaultFormat="string"==typeof a?a:"0.0"},b.register=function(a,b,c){if(b=b.toLowerCase(),this[a+"s"][b])throw new TypeError(b+" "+a+" already registered.");return this[a+"s"][b]=c,c},b.validate=function(a,c){var d,e,f,g,h,i,j,k;if("string"!=typeof a&&(a+="",console.warn&&console.warn("Numeral.js: Value is not string. It has been co-erced to: ",a)),a=a.trim(),a.match(/^\d+$/))return!0;if(""===a)return!1;try{j=b.localeData(c)}catch(l){j=b.localeData(b.locale())}return f=j.currency.symbol,h=j.abbreviations,d=j.delimiters.decimal,e="."===j.delimiters.thousands?"\\.":j.delimiters.thousands,k=a.match(/^[^\d]+/),null!==k&&(a=a.substr(1),k[0]!==f)?!1:(k=a.match(/[^\d]+$/),null!==k&&(a=a.slice(0,-1),k[0]!==h.thousand&&k[0]!==h.million&&k[0]!==h.billion&&k[0]!==h.trillion)?!1:(i=new RegExp(e+"{2}"),a.match(/[^\d.,]/g)?!1:(g=a.split(d),g.length>2?!1:g.length<2?!!g[0].match(/^\d+.*\d$/)&&!g[0].match(i):1===g[0].length?!!g[0].match(/^\d+$/)&&!g[0].match(i)&&!!g[1].match(/^\d+$/):!!g[0].match(/^\d+.*\d$/)&&!g[0].match(i)&&!!g[1].match(/^\d+$/))))},b.fn=a.prototype={clone:function(){return b(this)},format:function(a,c){var d,f,g,i=this._value,j=a||h.defaultFormat;if(c=c||Math.round,0===i&&null!==h.zeroFormat)f=h.zeroFormat;else if(null===i&&null!==h.nullFormat)f=h.nullFormat;else{for(d in e)if(j.match(e[d].regexps.format)){g=e[d].format;break}g=g||b._.numberToFormat,f=g(i,j,c)}return f},value:function(){return this._value},input:function(){return this._input},set:function(a){return this._value=Number(a),this},add:function(a){function b(a,b,c,e){return a+Math.round(d*b)}var d=c.correctionFactor.call(null,this._value,a);return this._value=c.reduce([this._value,a],b,0)/d,this},subtract:function(a){function b(a,b,c,e){return a-Math.round(d*b)}var d=c.correctionFactor.call(null,this._value,a);return this._value=c.reduce([a],b,Math.round(this._value*d))/d,this},multiply:function(a){function b(a,b,d,e){var f=c.correctionFactor(a,b);return Math.round(a*f)*Math.round(b*f)/Math.round(f*f)}return this._value=c.reduce([this._value,a],b,1),this},divide:function(a){function b(a,b,d,e){var f=c.correctionFactor(a,b);return Math.round(a*f)/Math.round(b*f)}return this._value=c.reduce([this._value,a],b),this},difference:function(a){return Math.abs(b(this._value).subtract(a).value())}},b.register("locale","en",{delimiters:{thousands:",",decimal:"."},abbreviations:{thousand:"k",million:"m",billion:"b",trillion:"t"},ordinal:function(a){var b=a%10;return 1===~~(a%100/10)?"th":1===b?"st":2===b?"nd":3===b?"rd":"th"},currency:{symbol:"$"}}),function(){var a={base:1e3,suffixes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]},c={base:1024,suffixes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]};b.register("format","bytes",{regexps:{format:/([0\s]i?b)/,unformat:new RegExp("("+a.suffixes.concat(c.suffixes).join("|")+")")},format:function(d,e,f){var g,h,i,j,k=b._.includes(e,"ib")?c:a,l=b._.includes(e," b")||b._.includes(e," ib")?" ":"";for(e=e.replace(/\s?i?b/,""),h=0;h<=k.suffixes.length;h++)if(i=Math.pow(k.base,h),j=Math.pow(k.base,h+1),null===d||0===d||d>=i&&j>d){l+=k.suffixes[h],i>0&&(d/=i);break}return g=b._.numberToFormat(d,e,f),g+l},unformat:function(d){var e,f,g=b._.stringToNumber(d);if(g){for(e=a.suffixes.length-1;e>=0;e--){if(b._.includes(d,a.suffixes[e])){f=Math.pow(a.base,e);break}if(b._.includes(d,c.suffixes[e])){f=Math.pow(c.base,e);break}}g*=f||1}return g}})}(),function(){b.register("format","currency",{regexps:{format:/(\$)/},format:function(a,c,d){var e,f,g,h=b.locales[b.options.currentLocale],i={before:c.match(/^([\+|\-|\(|\s|\$]*)/)[0],after:c.match(/([\+|\-|\)|\s|\$]*)$/)[0]};for(c=c.replace(/\s?\$\s?/,""),e=b._.numberToFormat(a,c,d),a>=0?(i.before=i.before.replace(/[\-\(]/,""),i.after=i.after.replace(/[\-\)]/,"")):0>a&&!b._.includes(i.before,"-")&&!b._.includes(i.before,"(")&&(i.before="-"+i.before),g=0;g=0;g--)switch(f=i.after[g]){case"$":e=g===i.after.length-1?e+h.currency.symbol:b._.insert(e,h.currency.symbol,-(i.after.length-(1+g)));break;case" ":e=g===i.after.length-1?e+" ":b._.insert(e," ",-(i.after.length-(1+g)))}return e}})}(),function(){b.register("format","exponential",{regexps:{format:/(e\+|e-)/,unformat:/(e\+|e-)/},format:function(a,c,d){var e,f="number"!=typeof a||b._.isNaN(a)?"0e+0":a.toExponential(),g=f.split("e");return c=c.replace(/e[\+|\-]{1}0/,""),e=b._.numberToFormat(Number(g[0]),c,d),e+"e"+g[1]},unformat:function(a){function c(a,c,d,e){var f=b._.correctionFactor(a,c),g=a*f*(c*f)/(f*f);return g}var d=b._.includes(a,"e+")?a.split("e+"):a.split("e-"),e=Number(d[0]),f=Number(d[1]);return f=b._.includes(a,"e-")?f*=-1:f,b._.reduce([e,Math.pow(10,f)],c,1)}})}(),function(){b.register("format","ordinal",{regexps:{format:/(o)/},format:function(a,c,d){var e,f=b.locales[b.options.currentLocale],g=b._.includes(c," o")?" ":"";return c=c.replace(/\s?o/,""),g+=f.ordinal(a),e=b._.numberToFormat(a,c,d),e+g}})}(),function(){b.register("format","percentage",{regexps:{format:/(%)/,unformat:/(%)/},format:function(a,c,d){var e,f=b._.includes(c," %")?" ":"";return a=100*a,c=c.replace(/\s?\%/,""),e=b._.numberToFormat(a,c,d),b._.includes(e,")")?(e=e.split(""),e.splice(-1,0,f+"%"),e=e.join("")):e=e+f+"%",e},unformat:function(a){return.01*b._.stringToNumber(a)}})}(),function(){b.register("format","time",{regexps:{format:/(:)/,unformat:/(:)/},format:function(a,b,c){var d=Math.floor(a/60/60),e=Math.floor((a-60*d*60)/60),f=Math.round(a-60*d*60-60*e);return d+":"+(10>e?"0"+e:e)+":"+(10>f?"0"+f:f)},unformat:function(a){var b=a.split(":"),c=0;return 3===b.length?(c+=60*Number(b[0])*60,c+=60*Number(b[1]),c+=Number(b[2])):2===b.length&&(c+=60*Number(b[0]),c+=Number(b[1])),Number(c)}})}(),b}); \ No newline at end of file diff --git a/numeral.js b/numeral.js index 7199afc3..83ff379a 100644 --- a/numeral.js +++ b/numeral.js @@ -1,6 +1,6 @@ /*! @preserve * numeral.js - * version : 2.0.3 + * version : 2.0.4 * author : Adam Draper * license : MIT * http://adamwdraper.github.com/Numeral-js/ @@ -21,7 +21,7 @@ var numeral, _, - VERSION = '2.0.3', + VERSION = '2.0.4', formats = {}, locales = {}, defaults = { From f4041277d127df04b1078a60b29d541eba82088f Mon Sep 17 00:00:00 2001 From: Adam Draper Date: Wed, 21 Dec 2016 11:37:51 -0700 Subject: [PATCH 9/9] update change log --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eac56b78..4c20f55d 100644 --- a/README.md +++ b/README.md @@ -56,9 +56,11 @@ See [the english unit tests](https://github.com/adamwdraper/Numeral-js/blob/mast # Changelog -### 2.1.0 +### 2.0.4 -Bug fix: Signed currency is inconsistent +Bug fix: Incorrect abbreviations for values rounded up [#187](https://github.com/adamwdraper/Numeral-js/issues/187) + +Bug fix: Signed currency is inconsistent [#89](https://github.com/adamwdraper/Numeral-js/issues/89) ### 2.0.2