Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix whitespace and add the ability to create an instance without

the `new` keyword.
  • Loading branch information...
commit 5a9520b2eb3288221a9e1f1731d8be34d7f71ad8 1 parent f5b79fc
@domharrington authored
Showing with 76 additions and 59 deletions.
  1. +37 −35 index.js
  2. +39 −24 test.js
View
72 index.js
@@ -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)
View
63 test.js
@@ -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')
+
+ })
+
+})
Please sign in to comment.
Something went wrong with that request. Please try again.