Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix whitespace and add the ability to create an instance without
the `new` keyword.
- Loading branch information
Dom Harrington
committed
Feb 7, 2014
1 parent
f5b79fc
commit 5a9520b
Showing
2 changed files
with
76 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
(function(root){ | ||
'use strict'; | ||
|
||
var NumberAbbreviate = function(abbrev) { | ||
this.abbrev = abbrev == null ? ['k', 'm', 'b', 't'] : abbrev; | ||
}; | ||
|
||
NumberAbbreviate.prototype.abbreviate = function(number, decPlaces) { | ||
decPlaces = Math.pow(10, decPlaces); | ||
|
||
for (var i = this.abbrev.length - 1; i>=0; i--) { | ||
|
||
var size = Math.pow(10, (i + 1) * 3); | ||
|
||
if(size <= number) { | ||
number = Math.round(number * decPlaces / size) / decPlaces; | ||
|
||
if((number === 1000) && (i < this.abbrev.length - 1)) { | ||
number = 1; | ||
i++; | ||
} | ||
|
||
number += this.abbrev[i]; | ||
|
||
break; | ||
} | ||
} | ||
|
||
return number | ||
}; | ||
|
||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = NumberAbbreviate; | ||
} else { | ||
root.NumberAbbreviate = NumberAbbreviate; | ||
'use strict'; | ||
|
||
var NumberAbbreviate = function(abbrev) { | ||
if (!(this instanceof NumberAbbreviate)) return new NumberAbbreviate(abbrev) | ||
this.abbrev = abbrev == null ? ['k', 'm', 'b', 't'] : abbrev | ||
} | ||
|
||
NumberAbbreviate.prototype.abbreviate = function(number, decPlaces) { | ||
decPlaces = Math.pow(10, decPlaces) | ||
|
||
for (var i = this.abbrev.length - 1; i>=0; i--) { | ||
|
||
var size = Math.pow(10, (i + 1) * 3) | ||
|
||
if(size <= number) { | ||
number = Math.round(number * decPlaces / size) / decPlaces | ||
|
||
if((number === 1000) && (i < this.abbrev.length - 1)) { | ||
number = 1 | ||
i++ | ||
} | ||
|
||
number += this.abbrev[i] | ||
|
||
break | ||
} | ||
} | ||
})(this); | ||
|
||
return number | ||
} | ||
|
||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = NumberAbbreviate | ||
} else { | ||
root.NumberAbbreviate = NumberAbbreviate | ||
} | ||
|
||
})(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,43 @@ | ||
var NumAbbr = require('./') | ||
, assert = require('assert'); | ||
, numAbbrFn = require('./') | ||
, assert = require('assert') | ||
|
||
describe('number-abbreviate', function () { | ||
|
||
it('should abbreviate numbers', function () { | ||
var numAbbr = new NumAbbr(); | ||
assert.equal(numAbbr.abbreviate(12, 1), '12'); | ||
assert.equal(numAbbr.abbreviate(0, 2), '0'); | ||
assert.equal(numAbbr.abbreviate(1234, 0), '1k'); | ||
assert.equal(numAbbr.abbreviate(34567, 2), '34.57k'); | ||
assert.equal(numAbbr.abbreviate(918395, 1), '918.4k'); | ||
assert.equal(numAbbr.abbreviate(2134124, 2), '2.13m'); | ||
assert.equal(numAbbr.abbreviate(47475782130, 2), '47.48b'); | ||
assert.equal(numAbbr.abbreviate(47475782130000, 2), '47.48t'); | ||
}); | ||
it('should abbreviate to digital units', function() { | ||
var numAbbr = new NumAbbr(['KB', 'MB', 'GB', 'T']); | ||
assert.equal(numAbbr.abbreviate(13, 1), '13'); | ||
assert.equal(numAbbr.abbreviate(0.0, 4), '0'); | ||
assert.equal(numAbbr.abbreviate(4398, 0), '4KB'); | ||
assert.equal(numAbbr.abbreviate(8192, 2), '8.19KB'); | ||
assert.equal(numAbbr.abbreviate(833124111, 3), '833.124MB'); | ||
assert.equal(numAbbr.abbreviate(833124111, 3), '833.124MB'); | ||
assert.equal(numAbbr.abbreviate(16000000000, 0), '16GB'); | ||
assert.equal(numAbbr.abbreviate(2120000000001, 2), '2.12T'); | ||
}); | ||
}); | ||
it('should abbreviate numbers', function () { | ||
|
||
var numAbbr = new NumAbbr() | ||
assert.equal(numAbbr.abbreviate(12, 1), '12') | ||
assert.equal(numAbbr.abbreviate(0, 2), '0') | ||
assert.equal(numAbbr.abbreviate(1234, 0), '1k') | ||
assert.equal(numAbbr.abbreviate(34567, 2), '34.57k') | ||
assert.equal(numAbbr.abbreviate(918395, 1), '918.4k') | ||
assert.equal(numAbbr.abbreviate(2134124, 2), '2.13m') | ||
assert.equal(numAbbr.abbreviate(47475782130, 2), '47.48b') | ||
assert.equal(numAbbr.abbreviate(47475782130000, 2), '47.48t') | ||
|
||
}) | ||
|
||
it('should abbreviate to digital units', function() { | ||
|
||
var numAbbr = new NumAbbr(['KB', 'MB', 'GB', 'T']) | ||
assert.equal(numAbbr.abbreviate(13, 1), '13') | ||
assert.equal(numAbbr.abbreviate(0.0, 4), '0') | ||
assert.equal(numAbbr.abbreviate(4398, 0), '4KB') | ||
assert.equal(numAbbr.abbreviate(8192, 2), '8.19KB') | ||
assert.equal(numAbbr.abbreviate(833124111, 3), '833.124MB') | ||
assert.equal(numAbbr.abbreviate(833124111, 3), '833.124MB') | ||
assert.equal(numAbbr.abbreviate(16000000000, 0), '16GB') | ||
assert.equal(numAbbr.abbreviate(2120000000001, 2), '2.12T') | ||
|
||
}) | ||
|
||
it('should allow it to be called without `new`', function () { | ||
|
||
var numAbbr = numAbbrFn(['KB', 'MB', 'GB', 'T']) | ||
assert.equal(numAbbr.abbreviate(13, 1), '13') | ||
assert.equal(numAbbr.abbreviate(0.0, 4), '0') | ||
|
||
}) | ||
|
||
}) |