Skip to content

Commit

Permalink
Merge pull request #428 from adamwdraper/develop
Browse files Browse the repository at this point in the history
2.0.4
  • Loading branch information
adamwdraper committed Dec 21, 2016
2 parents c08f9e3 + 3223168 commit a7a2ded
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 84 deletions.
68 changes: 62 additions & 6 deletions Gruntfile.js
Expand Up @@ -44,6 +44,8 @@ module.exports = function(grunt) {
grunt.file.write('numeral.js', numeral);
};

require('load-grunt-tasks')(grunt);

grunt.initConfig({
mochaTest : {
all: [
Expand Down Expand Up @@ -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'
]);
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
2 changes: 1 addition & 1 deletion 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/
*/
Expand Down
2 changes: 1 addition & 1 deletion min/locales.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions min/numeral.min.js

Large diffs are not rendered by default.

116 changes: 82 additions & 34 deletions 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/
Expand All @@ -21,7 +21,7 @@

var numeral,
_,
VERSION = '2.0.3',
VERSION = '2.0.4',
formats = {},
locales = {},
defaults = {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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(',');
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
});
Expand Down
4 changes: 3 additions & 1 deletion 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": {
Expand Down Expand Up @@ -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"
},
Expand Down

0 comments on commit a7a2ded

Please sign in to comment.