Browse files

Initial commit

  • Loading branch information...
0 parents commit adbfdac156a6ba360854e92b762f3707aedbf7c5 @tj tj committed Sep 30, 2012
Showing with 90 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +16 −0 Makefile
  3. +5 −0 Readme.md
  4. +12 −0 component.json
  5. +9 −0 index.js
  6. +14 −0 package.json
  7. +31 −0 test/index.js
3 .gitignore
@@ -0,0 +1,3 @@
+components
+build
+node_modules
16 Makefile
@@ -0,0 +1,16 @@
+
+build: components index.js
+ @component build --dev
+
+components:
+ @component install --dev
+
+clean:
+ rm -fr build components template.js
+
+test:
+ @./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec
+
+.PHONY: clean test
5 Readme.md
@@ -0,0 +1,5 @@
+
+# humanize-number
+
+ Humanize a number (1000000 -> 1,000,000)
+
12 component.json
@@ -0,0 +1,12 @@
+{
+ "name": "humanize-number",
+ "repo": "component/humanize-number",
+ "description": "Humanize a number (1000000 -> 1,000,000)",
+ "version": "0.0.1",
+ "keywords": [],
+ "dependencies": {},
+ "development": {},
+ "scripts": [
+ "index.js"
+ ]
+}
9 index.js
@@ -0,0 +1,9 @@
+
+module.exports = function(n, options){
+ options = options || {};
+ var d = options.delimiter || ',';
+ var s = options.separator || '.';
+ n = n.toString().split('.');
+ n[0] = n[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + d);
+ return n.join(s);
+};
14 package.json
@@ -0,0 +1,14 @@
+{
+ "name": "humanize-number",
+ "description": "Humanize a number (1000000 -> 1,000,000)",
+ "version": "0.0.1",
+ "keywords": [],
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*"
+ },
+ "scripts": [
+ "index.js"
+ ]
+}
31 test/index.js
@@ -0,0 +1,31 @@
+
+/**
+ * Module dependencies.
+ */
+
+var humanize = require('..');
+
+describe('humanize(n, options)', function(){
+ it('should delimit thousandths', function(){
+ humanize(1000).should.equal('1,000');
+ humanize(1000000).should.equal('1,000,000');
+ humanize(10500).should.equal('10,500');
+ })
+
+ it('should retain fractions', function(){
+ humanize(15.99).should.equal('15.99');
+ humanize(1500.99).should.equal('1,500.99');
+ })
+
+ describe('"delimiter" option', function(){
+ it('should change the delimiter', function(){
+ humanize(1500, { delimiter: '.' }).should.equal('1.500');
+ })
+ })
+
+ describe('"separator" option', function(){
+ it('should change the separator', function(){
+ humanize(15.99, { separator: ',' }).should.equal('15,99');
+ })
+ })
+})

0 comments on commit adbfdac

Please sign in to comment.