Permalink
Browse files

Merge pull request #428 from adamwdraper/develop

2.0.4
  • Loading branch information...
2 parents c08f9e3 + 3223168 commit a7a2dedde724ee6c74cf3370b958c06d19c54659 @adamwdraper committed on GitHub Dec 21, 2016
Showing with 270 additions and 84 deletions.
  1. +62 −6 Gruntfile.js
  2. +6 −0 README.md
  3. +1 −1 bower.json
  4. +1 −1 component.json
  5. +1 −1 locales.js
  6. +1 −1 min/locales.min.js
  7. +2 −2 min/numeral.min.js
  8. +82 −34 numeral.js
  9. +3 −1 package.json
  10. +36 −23 src/formats/currency.js
  11. +46 −11 src/numeral.js
  12. +1 −1 templates/types.js
  13. +4 −2 tests/formats/currency.js
  14. +24 −0 tests/numeral.js
View
@@ -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,66 @@ 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.config('string-replace.numeral', {
+ files: {
+ 'src/numeral.js': 'src/numeral.js'
+ },
+ options: {
+ replacements: [
+ {
+ pattern: /version : .*/,
+ replacement: 'version : ' + version
+ },
+ {
+ pattern: /VERSION = .*/,
+ replacement: 'VERSION = \'' + version + '\','
+ }
+ ]
+ }
+ });
+
+ 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'
+ ]);
+ });
+
// Travis CI task.
grunt.registerTask('travis', [
'build',
View
@@ -56,6 +56,12 @@ See [the english unit tests](https://github.com/adamwdraper/Numeral-js/blob/mast
# Changelog
+### 2.0.4
+
+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
Bug fix: Updated module definitions
View
@@ -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",
View
@@ -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",
View
@@ -1,6 +1,6 @@
/*! @preserve
* numeral.js
- * locales: 2.0.3
+ * locales : 2.0.4
* license : MIT
* http://adamwdraper.github.com/Numeral-js/
*/
View
@@ -1,6 +1,6 @@
/*! @preserve
* numeral.js
- * locales: 2.0.3
+ * locales : 2.0.4
* license : MIT
* http://adamwdraper.github.com/Numeral-js/
*/
View
Oops, something went wrong.
View
@@ -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 = {
@@ -102,23 +102,24 @@
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,
max,
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('+') : value < 0 ? format.indexOf('-') : -1;
+ format = format.replace(/[\+|\-]/g, '');
}
// see if abbreviation is wanted
@@ -167,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(',');
@@ -201,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);
@@ -215,7 +235,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 +301,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');
@@ -728,44 +763,57 @@
},
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?/, '');
// 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;
}
});
View
@@ -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,13 +38,15 @@
"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",
"karma-firefox-launcher": "^1.0.0",
"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"
},
Oops, something went wrong.

0 comments on commit a7a2ded

Please sign in to comment.