Skip to content

Commit a7a2ded

Browse files
authored
Merge pull request #428 from adamwdraper/develop
2.0.4
2 parents c08f9e3 + 3223168 commit a7a2ded

File tree

14 files changed

+270
-84
lines changed

14 files changed

+270
-84
lines changed

Gruntfile.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ module.exports = function(grunt) {
4444
grunt.file.write('numeral.js', numeral);
4545
};
4646

47+
require('load-grunt-tasks')(grunt);
48+
4749
grunt.initConfig({
4850
mochaTest : {
4951
all: [
@@ -152,12 +154,6 @@ module.exports = function(grunt) {
152154
}
153155
});
154156

155-
grunt.loadNpmTasks('grunt-contrib-copy');
156-
grunt.loadNpmTasks('grunt-contrib-uglify');
157-
grunt.loadNpmTasks('grunt-contrib-jshint');
158-
grunt.loadNpmTasks('grunt-mocha-test');
159-
grunt.loadNpmTasks('grunt-karma');
160-
161157
grunt.registerTask('default', [
162158
'test'
163159
]);
@@ -194,6 +190,66 @@ module.exports = function(grunt) {
194190
'uglify'
195191
]);
196192

193+
grunt.registerTask('version', function (version) {
194+
if (!version || version.split('.').length !== 3) {
195+
grunt.fail.fatal('malformed version. Use\n\n grunt version:1.2.3');
196+
}
197+
198+
grunt.config('string-replace.json', {
199+
files: {
200+
'package.json': 'package.json',
201+
'component.json': 'component.json',
202+
'bower.json': 'bower.json'
203+
},
204+
options: {
205+
replacements: [
206+
{
207+
pattern: /"version": .*/,
208+
replacement: '"version": "' + version + '",'
209+
}
210+
]
211+
}
212+
});
213+
214+
grunt.config('string-replace.numeral', {
215+
files: {
216+
'src/numeral.js': 'src/numeral.js'
217+
},
218+
options: {
219+
replacements: [
220+
{
221+
pattern: /version : .*/,
222+
replacement: 'version : ' + version
223+
},
224+
{
225+
pattern: /VERSION = .*/,
226+
replacement: 'VERSION = \'' + version + '\','
227+
}
228+
]
229+
}
230+
});
231+
232+
grunt.config('string-replace.templates', {
233+
files: {
234+
'templates/types.js': 'templates/types.js'
235+
},
236+
options: {
237+
replacements: [
238+
{
239+
pattern: /: .*/,
240+
replacement: ': ' + version
241+
}
242+
]
243+
}
244+
});
245+
246+
grunt.task.run([
247+
'string-replace:json',
248+
'string-replace:templates',
249+
'string-replace:numeral'
250+
]);
251+
});
252+
197253
// Travis CI task.
198254
grunt.registerTask('travis', [
199255
'build',

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ See [the english unit tests](https://github.com/adamwdraper/Numeral-js/blob/mast
5656

5757
# Changelog
5858

59+
### 2.0.4
60+
61+
Bug fix: Incorrect abbreviations for values rounded up [#187](https://github.com/adamwdraper/Numeral-js/issues/187)
62+
63+
Bug fix: Signed currency is inconsistent [#89](https://github.com/adamwdraper/Numeral-js/issues/89)
64+
5965
### 2.0.2
6066

6167
Bug fix: Updated module definitions

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "numeral",
33
"repo": "adamwdraper/Numeral-js",
4-
"version": "2.0.3",
4+
"version": "2.0.4",
55
"description": "Format and manipulate numbers.",
66
"keywords": [
77
"numeral",

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "numeral",
33
"repo": "adamwdraper/Numeral-js",
4-
"version": "2.0.3",
4+
"version": "2.0.4",
55
"description": "Format and manipulate numbers.",
66
"keywords": [
77
"numeral",

locales.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*! @preserve
22
* numeral.js
3-
* locales: 2.0.3
3+
* locales : 2.0.4
44
* license : MIT
55
* http://adamwdraper.github.com/Numeral-js/
66
*/

min/locales.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

min/numeral.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

numeral.js

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*! @preserve
22
* numeral.js
3-
* version : 2.0.3
3+
* version : 2.0.4
44
* author : Adam Draper
55
* license : MIT
66
* http://adamwdraper.github.com/Numeral-js/
@@ -21,7 +21,7 @@
2121

2222
var numeral,
2323
_,
24-
VERSION = '2.0.3',
24+
VERSION = '2.0.4',
2525
formats = {},
2626
locales = {},
2727
defaults = {
@@ -102,23 +102,24 @@
102102
numberToFormat: function(value, format, roundingFunction) {
103103
var locale = locales[numeral.options.currentLocale],
104104
negP = false,
105-
signed = false,
106105
optDec = false,
107106
abbr = '',
108107
trillion = 1000000000000,
109108
billion = 1000000000,
110109
million = 1000000,
111110
thousand = 1000,
111+
decimal = '',
112+
neg = false,
112113
abbrForce, // force abbreviation
113114
abs,
114115
min,
115116
max,
116117
power,
117118
int,
118119
precision,
120+
signed,
119121
thousands,
120-
decimal = '',
121-
neg = false;
122+
output;
122123

123124
// make sure we never format a null value
124125
value = value || 0;
@@ -129,10 +130,10 @@
129130
// if both are present we default to parentheses
130131
if (numeral._.includes(format, '(')) {
131132
negP = true;
132-
format = format.slice(1, -1);
133-
} else if (numeral._.includes(format, '+')) {
134-
signed = true;
135-
format = format.replace(/\+/g, '');
133+
format = format.replace(/[\(|\)]/g, '');
134+
} else if (numeral._.includes(format, '+') || numeral._.includes(format, '-')) {
135+
signed = numeral._.includes(format, '+') ? format.indexOf('+') : value < 0 ? format.indexOf('-') : -1;
136+
format = format.replace(/[\+|\-]/g, '');
136137
}
137138

138139
// see if abbreviation is wanted
@@ -167,12 +168,13 @@
167168
}
168169
}
169170

170-
171+
// check for optional decimals
171172
if (numeral._.includes(format, '[.]')) {
172173
optDec = true;
173174
format = format.replace('[.]', '.');
174175
}
175176

177+
// break number and format
176178
int = value.toString().split('.')[0];
177179
precision = format.split('.')[1];
178180
thousands = format.indexOf(',');
@@ -201,6 +203,24 @@
201203
int = numeral._.toFixed(value, null, roundingFunction);
202204
}
203205

206+
// check abbreviation again after rounding
207+
if (abbr && !abbrForce && Number(int) >= 1000 && abbr !== locale.abbreviations.trillion) {
208+
int = String(Number(int) / 1000);
209+
210+
switch (abbr) {
211+
case locale.abbreviations.thousand:
212+
abbr = locale.abbreviations.million;
213+
break;
214+
case locale.abbreviations.million:
215+
abbr = locale.abbreviations.billion;
216+
break;
217+
case locale.abbreviations.billion:
218+
abbr = locale.abbreviations.trillion;
219+
break;
220+
}
221+
}
222+
223+
204224
// format number
205225
if (numeral._.includes(int, '-')) {
206226
int = int.slice(1);
@@ -215,7 +235,19 @@
215235
int = '';
216236
}
217237

218-
return (negP && neg ? '(' : '') + (!negP && neg ? '-' : '') + (!neg && signed ? '+' : '') + int + decimal + (abbr ? abbr : '') + (negP && neg ? ')' : '');
238+
output = int + decimal + (abbr ? abbr : '');
239+
240+
if (negP) {
241+
output = (negP && neg ? '(' : '') + output + (negP && neg ? ')' : '');
242+
} else {
243+
if (signed >= 0) {
244+
output = signed === 0 ? (neg ? '-' : '+') + output : output + (neg ? '-' : '+');
245+
} else if (neg) {
246+
output = '-' + output;
247+
}
248+
}
249+
250+
return output;
219251
},
220252
// unformats numbers separators, decimals places, signs, abbreviations
221253
stringToNumber: function(string) {
@@ -269,6 +301,9 @@
269301
includes: function(string, search) {
270302
return string.indexOf(search) !== -1;
271303
},
304+
insert: function(string, subString, start) {
305+
return string.slice(0, start) + subString + string.slice(start);
306+
},
272307
reduce: function(array, callback /*, initialValue*/) {
273308
if (this === null) {
274309
throw new TypeError('Array.prototype.reduce called on null or undefined');
@@ -728,44 +763,57 @@
728763
},
729764
format: function(value, format, roundingFunction) {
730765
var locale = numeral.locales[numeral.options.currentLocale],
731-
symbolIndex = format.indexOf('$'),
732-
openParenIndex = format.indexOf('('),
733-
minusSignIndex = format.indexOf('-'),
734-
space = numeral._.includes(format, ' $') || numeral._.includes(format, '$ ') ? ' ' : '',
735-
spliceIndex,
736-
output;
766+
symbols = {
767+
before: format.match(/^([\+|\-|\(|\s|\$]*)/)[0],
768+
after: format.match(/([\+|\-|\)|\s|\$]*)$/)[0]
769+
},
770+
output,
771+
symbol,
772+
i;
737773

738774
// strip format of spaces and $
739775
format = format.replace(/\s?\$\s?/, '');
740776

741777
// format the number
742778
output = numeral._.numberToFormat(value, format, roundingFunction);
743779

744-
// position the symbol
745-
if (symbolIndex <= 1) {
746-
if (numeral._.includes(output, '(') || numeral._.includes(output, '-')) {
747-
output = output.split('');
748-
749-
spliceIndex = symbolIndex < openParenIndex || symbolIndex < minusSignIndex ? 0 : 1;
780+
// update the before and after based on value
781+
if (value >= 0) {
782+
symbols.before = symbols.before.replace(/[\-\(]/, '');
783+
symbols.after = symbols.after.replace(/[\-\)]/, '');
784+
} else if (value < 0 && (!numeral._.includes(symbols.before, '-') && !numeral._.includes(symbols.before, '('))) {
785+
symbols.before = '-' + symbols.before;
786+
}
750787

751-
output.splice(spliceIndex, 0, locale.currency.symbol + space);
788+
// loop through each before symbol
789+
for (i = 0; i < symbols.before.length; i++) {
790+
symbol = symbols.before[i];
752791

753-
output = output.join('');
754-
} else {
755-
output = locale.currency.symbol + space + output;
792+
switch (symbol) {
793+
case '$':
794+
output = numeral._.insert(output, locale.currency.symbol, i);
795+
break;
796+
case ' ':
797+
output = numeral._.insert(output, ' ', i);
798+
break;
756799
}
757-
} else {
758-
if (numeral._.includes(output, ')')) {
759-
output = output.split('');
800+
}
760801

761-
output.splice(-1, 0, space + locale.currency.symbol);
802+
// loop through each after symbol
803+
for (i = symbols.after.length - 1; i >= 0; i--) {
804+
symbol = symbols.after[i];
762805

763-
output = output.join('');
764-
} else {
765-
output = output + space + locale.currency.symbol;
806+
switch (symbol) {
807+
case '$':
808+
output = i === symbols.after.length - 1 ? output + locale.currency.symbol : numeral._.insert(output, locale.currency.symbol, -(symbols.after.length - (1 + i)));
809+
break;
810+
case ' ':
811+
output = i === symbols.after.length - 1 ? output + ' ' : numeral._.insert(output, ' ', -(symbols.after.length - (1 + i)));
812+
break;
766813
}
767814
}
768815

816+
769817
return output;
770818
}
771819
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "numeral",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"description": "Format and manipulate numbers.",
55
"homepage": "http://numeraljs.com",
66
"author": {
@@ -38,13 +38,15 @@
3838
"grunt-karma": "^2.0.0",
3939
"grunt-mocha-test": "^0.13.2",
4040
"grunt-saucelabs": "*",
41+
"grunt-string-replace": "^1.3.1",
4142
"karma": "^1.3.0",
4243
"karma-chai": "^0.1.0",
4344
"karma-chrome-launcher": "^2.0.0",
4445
"karma-firefox-launcher": "^1.0.0",
4546
"karma-mocha": "^1.3.0",
4647
"karma-mocha-reporter": "^2.2.1",
4748
"karma-sauce-launcher": "^1.1.0",
49+
"load-grunt-tasks": "^3.5.2",
4850
"mocha": "^3.1.2",
4951
"uglify-js": "latest"
5052
},

0 commit comments

Comments
 (0)