Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 2d558db686d148d3cdee0f49922603ba9d42d087 @vesln committed Jan 3, 2012
Showing with 362 additions and 0 deletions.
  1. +22 −0 .gitignore
  2. +12 −0 Makefile
  3. +45 −0 Readme.md
  4. +137 −0 lib/mill.js
  5. +24 −0 package.json
  6. +122 −0 test/mill.test.js
@@ -0,0 +1,22 @@
+tmp
+node_modules
+*._
+*.tmp
+.monitor
+*.diff
+*.err
+*.orig
+*.log
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+.DS_Store
+Thumbs.db
+.cache
+.project
+.settings
+.tmproj
+*.esproj
+nbproject
@@ -0,0 +1,12 @@
+TESTS = test/*.test.js
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec \
+ $(TESTS)
+
+clean:
+ rm -f examples/tmp/*
+
+.PHONY: test clean
@@ -0,0 +1,45 @@
+# mill(iseconds) - Readable time for you and other humans.
+
+http://github.com/vesln/mill
+
+## Description
+
+mill is simple library for Node.js that makes working with milliseconds more fun.
+
+## Synopsis
+
+ var mill = require('mill');
+
+ console.log( (10).seconds ); // 10000
+ console.log( (1).minute ); // 60000
+ console.log( (2).hours ); // 7200000
+ console.log( (3).weeks ); // 1814400000
+ console.log( (4).months ); // 10518975325.439999
+ console.log( (1).year ); // 31556925993.600002
+
+ console.log( (1).day + (30).seconds ); // 86430000
@stagas

stagas Jan 3, 2012

Just a note, you can also use two dots instead of the parentheses: 1..day + 30..seconds etc.

@vesln

vesln Jan 3, 2012

Owner

That's really cool. Thank you @stagas. I will update the Readme.

+
+## Requirements
+
+- NPM (http://npmjs.org/)
+- Node.js 0.6 (http://nodejs.org/)
+
+## Install
+
+ $ npm install mill
+
+## Assumptions
+
+- 1 month = 30.4368499 days
+- 1 year = 365.242199 days
+
+## Tests
+
+ $ make test
+
+## Inspiration
+
+- should.js
+- colors
+
+## License
@@ -0,0 +1,137 @@
+/**
+ * mill - Readable milliseconds for you and other humans.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Assumptions:
+ *
+ * 1 month = 30.4368499 days
+ * 1 year = 365.242199 days
+ */
+
+/**
+ * Dependencies.
+ */
+var package = require('package')(module);
+
+/**
+ * Time constructor.
+ *
+ * @param {Number} number.
+ */
+function Time(number) {
+ this.number = number;
+};
+
+/**
+ * Converts years to milliseconds.
+ *
+ * @returns {Number} milliseconds
+ */
+Time.prototype.years = function() {
+ return this.number * 1000 * 60 * 60 * 24 * 365.242199;
+};
+
+/**
+ * Converts months to milliseconds.
+ *
+ * @returns {Number} milliseconds
+ */
+Time.prototype.months = function() {
+ return this.number * 1000 * 60 * 60 * 24 * 30.4368499;
+};
+
+/**
+ * Converts weeks to milliseconds.
+ *
+ * @returns {Number} milliseconds
+ */
+Time.prototype.weeks = function() {
+ return this.number * 1000 * 60 * 60 * 24 * 7;
+};
+
+/**
+ * Converts days to milliseconds.
+ *
+ * @returns {Number} milliseconds
+ */
+Time.prototype.days = function() {
+ return this.number * 1000 * 60 * 60 * 24;
+};
+
+/**
+ * Converts hours to milliseconds.
+ *
+ * @returns {Number} milliseconds
+ */
+Time.prototype.hours = function() {
+ return this.number * 1000 * 60 * 60;
+};
+
+/**
+ * Converts minutes to milliseconds.
+ *
+ * @returns {Number} milliseconds
+ */
+Time.prototype.minutes = function() {
+ return this.number * 1000 * 60;
+};
+
+/**
+ * Converts seconds to milliseconds.
+ *
+ * @returns {Number} milliseconds
+ */
+Time.prototype.seconds = function() {
+ return this.number * 1000;
+};
+
+/**
+ * Exported methods map.
+ */
+var methods = {
+ year: 'years',
+ years: 'years',
+ month: 'months',
+ months: 'months',
+ week: 'weeks',
+ weeks: 'weeks',
+ day: 'days',
+ days: 'days',
+ hour: 'hours',
+ hours: 'hours',
+ minute: 'minutes',
+ minutes: 'minutes',
+ second: 'seconds',
+ seconds: 'seconds',
+};
+
+// Syntactic sugar.
+Object.keys(methods).forEach(function(method) {
+ Object.defineProperty(Number.prototype, method, {
+ get: function() {
+ var call = methods[method];
+ return new Time(this)[call]();
+ },
+ set: function() {},
+ configurable: true
+ });
+});
+
+/**
+ * Exporting the lib.
+ */
+module.exports = Time;
+
+/**
+ * Exporting the methods map.
+ */
+module.exports.methods = methods;
+
+/**
+ * Exporting the lib version.
+ */
+module.exports.version = package.version;
@@ -0,0 +1,24 @@
+{
+ "name": "mill"
+ , "version": "0.0.1"
+ , "description": "Readable milliseconds for you and other humans."
+ , "keywords": ["time", "milliseconds", "millisecond"]
+ , "author": "Veselin Todorov <hi@vesln.com>"
+ , "dependencies": {
+ "package": "1.0.0"
+ }
+ , "devDependencies": {
+ "mocha": "0.3.3"
+ , "should": "0.3.2"
+ }
+ , "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/vesln/mill.git"
+ }
+ , "homepage": "http://github.com/vesln/mill"
+ , "scripts": {
+ "test": "make test"
+ }
+ , "main": "./lib/mill"
+ , "engines": { "node": ">= 0.6.0 < 0.7.0" }
+}
@@ -0,0 +1,122 @@
+/**
+ * mill - Readable milliseconds for you and other humans.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var mill = require('../');
+var Time = mill;
+
+describe('mill', function() {
+ it('should export methods', function() {
+ var expected = {
+ year: 'years',
+ years: 'years',
+ month: 'months',
+ months: 'months',
+ week: 'weeks',
+ weeks: 'weeks',
+ day: 'days',
+ days: 'days',
+ hour: 'hours',
+ hours: 'hours',
+ minute: 'minutes',
+ minutes: 'minutes',
+ second: 'seconds',
+ seconds: 'seconds',
+ };
+
+ mill.methods.should.eql(expected);
+ });
+
+ it('should have version', function() {
+ mill.version.should.be.ok;
+ });
+
+ it('should export Time', function() {
+ (typeof mill).should.eql('function');
+ });
+
+ it('should define properties to Number', function() {
+ (1).second.should.eql(1000);
+ (1).seconds.should.eql(1000);
+
+ (1).minute.should.eql(1000 * 60);
+ (1).minutes.should.eql(1000 * 60);
+
+ (1).hour.should.eql(1000 * 60 * 60);
+ (1).hours.should.eql(1000 * 60 * 60);
+
+ (1).day.should.eql(1000 * 60 * 60 * 24);
+ (1).days.should.eql(1000 * 60 * 60 * 24);
+
+ (1).week.should.eql(1000 * 60 * 60 * 24 * 7);
+ (1).weeks.should.eql(1000 * 60 * 60 * 24 * 7);
+
+ (1).month.should.eql(1000 * 60 * 60 * 24 * 30.4368499);
+ (1).months.should.eql(1000 * 60 * 60 * 24 * 30.4368499);
+
+ (1).year.should.eql(1000 * 60 * 60 * 24 * 365.242199);
+ (1).years.should.eql(1000 * 60 * 60 * 24 * 365.242199);
+ });
+
+ describe('Time', function() {
+ it('should cache the passed time', function() {
+ var time = new Time(5);
+ time.number.should.eql(5);
+ });
+
+ describe('seconds', function() {
+ it('should convert seconds to milliseconds', function() {
+ var time = new Time(2);
+ time.seconds().should.eql(2 * 1000);
+ });
+ });
+
+ describe('minutes', function() {
+ it('should convert minutes to milliseconds', function() {
+ var time = new Time(2);
+ time.minutes().should.eql(2 * 1000 * 60);
+ });
+ });
+
+ describe('hours', function() {
+ it('should convert hours to milliseconds', function() {
+ var time = new Time(2);
+ time.hours().should.eql(2 * 1000 * 60 * 60);
+ });
+ });
+
+ describe('days', function() {
+ it('should convert days to milliseconds', function() {
+ var time = new Time(2);
+ time.days().should.eql(2 * 1000 * 60 * 60 * 24);
+ });
+ });
+
+ describe('weeks', function() {
+ it('should convert weeks to milliseconds', function() {
+ var time = new Time(2);
+ time.weeks().should.eql(2 * 1000 * 60 * 60 * 24 * 7);
+ });
+ });
+
+ describe('months', function() {
+ it('should convert months to milliseconds', function() {
+ var time = new Time(2);
+ time.months().should.eql(2 * 1000 * 60 * 60 * 24 * 30.4368499);
+ });
+ });
+
+ describe('years', function() {
+ it('should convert years to milliseconds', function() {
+ var time = new Time(2);
+ time.years().should.eql(2 * 1000 * 60 * 60 * 24 * 365.242199);
+ });
+ });
+ });
+});

0 comments on commit 2d558db

Please sign in to comment.