Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes #527 - Number.format modified the options object

- fixed by using Object.clone
- Optimized the negative options a bit
- Specs!
  • Loading branch information...
commit 74131ba8085056155792d2d4074c33a3faecd569 1 parent a23473c
@arian arian authored
Showing with 20 additions and 8 deletions.
  1. +5 −5 Source/Types/Number.Format.js
  2. +15 −3 Specs/1.3/Types/Number.Format.js
View
10 Source/Types/Number.Format.js
@@ -12,7 +12,7 @@ authors:
requires:
- Core/Number
- - /Locale.en-US.Number
+ - Locale.en-US.Number
provides: [Number.Extras]
@@ -25,7 +25,7 @@ Number.implement({
format: function(options){
// Thanks dojo and YUI for some inspiration
var value = this;
- if (!options) options = {};
+ options = options ? Object.clone(options) : {};
var getOption = function(key){
if (options[key] != null) return options[key];
return Locale.get('Number.' + key);
@@ -38,10 +38,10 @@ Number.implement({
decimals = getOption('decimals');
if (negative){
- var negativeLocale = Locale.get('Number.negative') || {};
+ var negativeLocale = getOption('negative') || {};
if (negativeLocale.prefix == null && negativeLocale.suffix == null) negativeLocale.prefix = '-';
- Object.each(negativeLocale, function(value, key){
- options[key] = (key == 'prefix' || key == 'suffix') ? (getOption(key) + value) : value;
+ ['prefix', 'suffix'].each(function(key){
+ if (negativeLocale[key]) options[key] = getOption(key) + negativeLocale[key];
});
value = -value;
View
18 Specs/1.3/Types/Number.Format.js
@@ -1,12 +1,12 @@
/*
-Script: Number.Extras.js
- Specs for Number.Extras.js
+Script: Number.Format.js
+ Specs for Number.Format.js
License:
MIT-style license.
*/
-describe('Number.Extras', function(){
+describe('Number.Format', function(){
describe('Number.format', function(){
@@ -26,6 +26,12 @@ describe('Number.Extras', function(){
expect((-20000).format()).toEqual('-20,000');
});
+ it('should format a negative number with a special minus sign', function(){
+ expect((-20000).format({negative: {prefix:'_'}})).toEqual('_20,000');
+ expect((-20000).format({negative: {suffix:'_'}})).toEqual('20,000_');
+ expect((-20000).format({negative: {prefix:'_', suffix: '^'}})).toEqual('_20,000^');
+ });
+
it('should format with the right decimals', function(){
expect((123.456).format({decimals: 0})).toEqual('123')
expect((123.456).format({decimals: 1, decimal: '.'})).toEqual('123.5')
@@ -71,6 +77,12 @@ describe('Number.Extras', function(){
expect((50).formatPercentage()).toEqual('50.00%');
});
+ it('should not change the options object', function(){
+ var options = {prefix: 'foo'};
+ (-3).format(options);
+ expect(options.prefix).toEqual('foo');
+ });
+
});
Please sign in to comment.
Something went wrong with that request. Please try again.