From 792eddb9fb9ffef9bf03de4bcbef4e2b7e98be86 Mon Sep 17 00:00:00 2001 From: Mike Barrett Date: Tue, 27 May 2014 20:45:04 +0100 Subject: [PATCH 01/17] misc tweaks --- README.md | 18 +++++++++--------- package.json | 4 ++-- timestring.js | 14 +++++++------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 6c1e5f8..2c5bc77 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -#timestring +#Timestring Attempts to parse a human readable time string into a time based value. @@ -11,7 +11,7 @@ var time = str.parseTime(); console.log(time); // will log 4500 ``` -In the example above `str` is just a plain old `String` object. A new method to the `String` objects prototype named `parseTime`. This method parses the string and returns a time based value. +In the example above `str` is just a plain old `String` object. A new method is added to the `String` objects prototype named `parseTime`. This method parses the string and returns a time based value. **By default the returned time value will be in seconds.** @@ -44,7 +44,7 @@ console.log(time); // will log 4500 ##Keywords -timestring will parse the following keywords into time values: +Timestring will parse the following keywords into time values: 1. `s, sec, secs, second, seconds` - will parse to seconds 2. `m, min, mins, minute, minutes` - will parse to minutes @@ -124,7 +124,7 @@ var time = (new Timestring(settings)).parse(str, 'h'); console.log(time); // will log 1 ``` -In the example of above `hoursPerDay` is being set to `1`. When the time string is being parsed, the return value is being specified as hours. Normally `1d` would parse to `24` hours (as by deafult there are 24 hours in a day) but because `hoursPerDay` has been set to `1`, `1d` will now only parse to `1` hour. +In the example above `hoursPerDay` is being set to `1`. When the time string is being parsed, the return value is being specified as hours. Normally `1d` would parse to `24` hours (as by deafult there are 24 hours in a day) but because `hoursPerDay` has been set to `1`, `1d` will now only parse to `1` hour. This would be useful for specific application needs. @@ -156,16 +156,16 @@ console.log(daysThisWeek); // will log 5 ##Installation -### Browser +###Browser All you need to do to get timestring working in the browser is download / clone this repo and make sure you include the `timestring.js` script on your page: ```html ``` -### Node.js +###Node -Timestring is also node compatible. To install for a project, navigate to the projects root folder and in your terminal and type the following: +To install for a node application, navigate to the projects root folder and in your terminal type the following: ``` npm install timestring @@ -173,10 +173,10 @@ npm install timestring You may need to use `sudo` if you get errors. -In your node application you need to require the timestirng module: +In your node application you need to require the timestring module: ```js var Timestring = require('timestring'); ``` -Once you have done this, you will beable to use timestring in node, the same way you do in the browser! +Once you have done this, you will be able to use timestring in your node application, the same way you do in the browser! diff --git a/package.json b/package.json index 2bc0410..d34ebf9 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,11 @@ "functional", "time" ], - "author" : "Mike Barrett ", + "author" : "Michael David Barrett ", "repository" : { "type": "git", "url": "git://github.com/mike182uk/timestring.git" }, "main" : "timestring.js", - "version" : "1.0.1" + "version" : "1.0.2" } diff --git a/timestring.js b/timestring.js index 06f18d4..fe53b7b 100644 --- a/timestring.js +++ b/timestring.js @@ -1,5 +1,4 @@ (function(){ - "use strict"; var Timestring = function(settings) { @@ -83,10 +82,11 @@ .match(/[-+]?[0-9]+[a-z]+/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups) if (groups !== null) { - for( var i = 0; i < groups.length; i++ ) { - var g = groups[i], - value = g.match(/[0-9]+/g)[0], - unit = g.match(/[a-z]+/g)[0]; + for(var i = 0; i < groups.length; i++) { + var g = groups[i]; + var value = g.match(/[0-9]+/g)[0]; + var unit = g.match(/[a-z]+/g)[0]; + totalSeconds += getSeconds(value, unit); } } @@ -95,12 +95,12 @@ return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds; } - // add convenience method to string proto + // add convenience method to string prototype String.prototype.parseTime = function (unit, settings) { return (new Timestring(settings)).parse(this, unit); } - // export Timestring object for either the browser or node.js + // export Timestring object for either the browser or node if (typeof module !== 'undefined' && module.exports) { module.exports = Timestring; } From 052d6ee5b3c9735652e3484d17fe5e206424c0e4 Mon Sep 17 00:00:00 2001 From: Mike Barrett Date: Thu, 17 Jul 2014 21:35:55 +0100 Subject: [PATCH 02/17] add editorconfig --- .editorconfig | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..c6c8b36 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true From 59172d82e2bef2e89ee9d40e851194595973552f Mon Sep 17 00:00:00 2001 From: Mike Barrett Date: Thu, 17 Jul 2014 21:37:26 +0100 Subject: [PATCH 03/17] format code --- package.json | 25 +++---- timestring.js | 204 ++++++++++++++++++++++++++------------------------ 2 files changed, 116 insertions(+), 113 deletions(-) diff --git a/package.json b/package.json index d34ebf9..c7675ed 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,12 @@ { - "name" : "timestring", - "description" : "Parse a human readable time string into a time based value", - "homepage" : "https://github.com/mike182uk/timestring", - "keywords" : [ - "util", - "functional", - "time" - ], - "author" : "Michael David Barrett ", - "repository" : { - "type": "git", - "url": "git://github.com/mike182uk/timestring.git" - }, - "main" : "timestring.js", - "version" : "1.0.2" + "name" : "timestring", + "description" : "Parse a human readable time string into a time based value", + "homepage" : "https://github.com/mike182uk/timestring", + "author" : "Michael David Barrett ", + "repository" : { + "type": "git", + "url": "git://github.com/mike182uk/timestring.git" + }, + "main" : "timestring.js", + "version" : "1.0.2" } diff --git a/timestring.js b/timestring.js index fe53b7b..d3698b4 100644 --- a/timestring.js +++ b/timestring.js @@ -1,111 +1,119 @@ (function(){ - "use strict"; + "use strict"; - var Timestring = function(settings) { - // default settings - var defaults = { - hoursPerDay: 24, - daysPerWeek: 7, - weeksPerMonth: 4, - monthsPerYear: 12 - }; - - // merge default settings with user settings - var settings = settings || {}; - this.settings = {}; - for (var property in defaults) { this.settings[property] = defaults[property]; } - for (var property in settings) { this.settings[property] = settings[property]; } - - // time units - this.units = { - s: ['s', 'sec', 'secs', 'second', 'seconds'], - m: ['m', 'min', 'mins', 'minute', 'minutes'], - h: ['h', 'hr', 'hrs', 'hour', 'hours'], - d: ['d', 'day', 'days'], - w: ['w', 'week', 'weeks'], - mth: ['mth', 'mths','month', 'months'], - y: ['y', 'yr', 'yrs', 'year', 'years'] - }; - - // time unit seconds mappings - this.unitValues = { - s: 1, - m: 60, - h: 3600 - }; - - // dynamic time unit seconds mappings - // these are dynamic based on the settings - this.unitValues.d = this.settings.hoursPerDay * this.unitValues.h; - this.unitValues.w = this.settings.daysPerWeek * this.unitValues.d; - this.unitValues.mth = this.settings.weeksPerMonth * this.unitValues.w; - this.unitValues.y = this.settings.monthsPerYear * this.unitValues.w; + var Timestring = function(settings) { + // default settings + var defaults = { + hoursPerDay: 24, + daysPerWeek: 7, + weeksPerMonth: 4, + monthsPerYear: 12 }; - Timestring.prototype.parse = function(string, returnUnit) { - // reference to this - var self = this; + // merge default settings with user settings + var settings = settings || {}; + this.settings = {}; - // get unit key helper - function getUnitKey(unit) { - for (var key in self.units) { - for (var u in self.units[key]) { - if (unit === self.units[key][u]) { - return key; - } - } - } - - // throw exception if invalid unit is passed - throw 'The unit [' + unit + '] is not supported by timestring'; - } - - // convert a value to a specific unit - function convert(value, unit) { - var baseValue = self.unitValues[getUnitKey(unit)]; - return value / baseValue; - } - - // get a value in seconds based on a specific unit - function getSeconds(value, unit) { - var baseValue = self.unitValues[getUnitKey(unit)]; - return value * baseValue; - } - - // seconds counter - var totalSeconds = 0; - - // split string into groups and get total seconds for each group - var groups = string - .toLowerCase() // convert words to lower case - .replace(/[^\.\w+-]+/g, '') // remove white space - .match(/[-+]?[0-9]+[a-z]+/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups) - - if (groups !== null) { - for(var i = 0; i < groups.length; i++) { - var g = groups[i]; - var value = g.match(/[0-9]+/g)[0]; - var unit = g.match(/[a-z]+/g)[0]; - - totalSeconds += getSeconds(value, unit); - } - } - - // return total, convert if needed - return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds; + for (var property in defaults) { + this.settings[property] = defaults[property]; } - // add convenience method to string prototype - String.prototype.parseTime = function (unit, settings) { - return (new Timestring(settings)).parse(this, unit); + for (var property in settings) { + this.settings[property] = settings[property]; } - // export Timestring object for either the browser or node - if (typeof module !== 'undefined' && module.exports) { - module.exports = Timestring; + // time units + this.units = { + s: ['s', 'sec', 'secs', 'second', 'seconds'], + m: ['m', 'min', 'mins', 'minute', 'minutes'], + h: ['h', 'hr', 'hrs', 'hour', 'hours'], + d: ['d', 'day', 'days'], + w: ['w', 'week', 'weeks'], + mth: ['mth', 'mths','month', 'months'], + y: ['y', 'yr', 'yrs', 'year', 'years'] + }; + + // time unit seconds mappings + this.unitValues = { + s: 1, + m: 60, + h: 3600 + }; + + // dynamic time unit seconds mappings + // these are dynamic based on the settings + this.unitValues.d = this.settings.hoursPerDay * this.unitValues.h; + this.unitValues.w = this.settings.daysPerWeek * this.unitValues.d; + this.unitValues.mth = this.settings.weeksPerMonth * this.unitValues.w; + this.unitValues.y = this.settings.monthsPerYear * this.unitValues.w; + }; + + Timestring.prototype.parse = function(string, returnUnit) { + // reference to this + var self = this; + + // get unit key helper + function getUnitKey(unit) { + for (var key in self.units) { + for (var u in self.units[key]) { + if (unit === self.units[key][u]) { + return key; + } + } + } + + // throw exception if invalid unit is passed + throw 'The unit [' + unit + '] is not supported by timestring'; } - else { - this.Timestring = Timestring; + + // convert a value to a specific unit + function convert(value, unit) { + var baseValue = self.unitValues[getUnitKey(unit)]; + + return value / baseValue; } + // get a value in seconds based on a specific unit + function getSeconds(value, unit) { + var baseValue = self.unitValues[getUnitKey(unit)]; + + return value * baseValue; + } + + // seconds counter + var totalSeconds = 0; + + // split string into groups and get total seconds for each group + var groups = string + .toLowerCase() // convert words to lower case + .replace(/[^\.\w+-]+/g, '') // remove white space + .match(/[-+]?[0-9]+[a-z]+/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups) + + if (groups !== null) { + for(var i = 0; i < groups.length; i++) { + var g = groups[i]; + var value = g.match(/[0-9]+/g)[0]; + var unit = g.match(/[a-z]+/g)[0]; + + totalSeconds += getSeconds(value, unit); + } + } + + // return total, convert if needed + return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds; + } + + // add convenience method to string prototype + String.prototype.parseTime = function (unit, settings) { + return (new Timestring(settings)).parse(this, unit); + } + + // export Timestring object for either the browser or node + if (typeof module !== 'undefined' && module.exports) { + module.exports = Timestring; + } + else { + this.Timestring = Timestring; + } + }).call(this); From 587bcbcbc857c872e7ff4b7fba098c757625f3c3 Mon Sep 17 00:00:00 2001 From: Mike Barrett Date: Thu, 17 Jul 2014 21:57:10 +0100 Subject: [PATCH 04/17] setup grunt --- .gitignore | 1 + .jshintrc | 3 +++ .npmignore | 1 + Gruntfile.js | 31 ++++++++++++++++++++++++++++++ package.json | 21 +++++++++++++------- timestring.js => src/timestring.js | 0 6 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 .jshintrc create mode 100644 Gruntfile.js rename timestring.js => src/timestring.js (100%) diff --git a/.gitignore b/.gitignore index 9daeafb..d3e222c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ test +node_modules diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..0db3279 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,3 @@ +{ + +} diff --git a/.npmignore b/.npmignore index 9daeafb..d3e222c 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1,2 @@ test +node_modules diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..42afc92 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,31 @@ +module.exports = function(grunt) { + // measure the time each task takes + require('time-grunt')(grunt); + + // autoload Grunt tasks + require('load-grunt-tasks')(grunt); + + // main project config + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + uglify: { + dist: { + files: { + 'dist/<%= pkg.name %>.min.js': 'src/<%= pkg.name %>.js' + } + } + }, + jshint: { + options: { + jshintrc: true + }, + files: [ + 'Gruntfile.js', + 'src/**/*.js', + ] + } + }); + + // user defined tasks + grunt.registerTask('default', ['jshint', 'uglify']); +}; diff --git a/package.json b/package.json index c7675ed..81d6d64 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,19 @@ { - "name" : "timestring", - "description" : "Parse a human readable time string into a time based value", - "homepage" : "https://github.com/mike182uk/timestring", - "author" : "Michael David Barrett ", - "repository" : { + "name": "timestring", + "description": "Parse a human readable time string into a time based value", + "homepage": "https://github.com/mike182uk/timestring", + "author": "Michael David Barrett ", + "repository": { "type": "git", "url": "git://github.com/mike182uk/timestring.git" }, - "main" : "timestring.js", - "version" : "1.0.2" + "main": "timestring.js", + "version": "1.0.2", + "devDependencies": { + "grunt": "^0.4.5", + "grunt-contrib-jshint": "^0.10.0", + "grunt-contrib-uglify": "^0.5.0", + "load-grunt-tasks": "^0.6.0", + "time-grunt": "^0.4.0" + } } diff --git a/timestring.js b/src/timestring.js similarity index 100% rename from timestring.js rename to src/timestring.js From 4740173d3e33d8a5645823bcada4f892f16bf52a Mon Sep 17 00:00:00 2001 From: Mike Barrett Date: Thu, 17 Jul 2014 22:10:24 +0100 Subject: [PATCH 05/17] fix jhint errors --- src/timestring.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/timestring.js b/src/timestring.js index d3698b4..c84ad2c 100644 --- a/src/timestring.js +++ b/src/timestring.js @@ -11,16 +11,10 @@ }; // merge default settings with user settings - var settings = settings || {}; + settings = settings || {}; this.settings = {}; - - for (var property in defaults) { - this.settings[property] = defaults[property]; - } - - for (var property in settings) { - this.settings[property] = settings[property]; - } + for (var d in defaults) { this.settings[d] = defaults[d]; } + for (var s in settings) { this.settings[s] = settings[s]; } // time units this.units = { @@ -101,12 +95,12 @@ // return total, convert if needed return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds; - } + }; // add convenience method to string prototype String.prototype.parseTime = function (unit, settings) { return (new Timestring(settings)).parse(this, unit); - } + }; // export Timestring object for either the browser or node if (typeof module !== 'undefined' && module.exports) { From 34c8e04352e296c0f69bb2748ebdb16c0d466f20 Mon Sep 17 00:00:00 2001 From: Mike Barrett Date: Thu, 17 Jul 2014 22:16:35 +0100 Subject: [PATCH 06/17] misc tweaks --- src/timestring.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/timestring.js b/src/timestring.js index c84ad2c..281809f 100644 --- a/src/timestring.js +++ b/src/timestring.js @@ -44,14 +44,14 @@ Timestring.prototype.parse = function(string, returnUnit) { // reference to this - var self = this; + var that = this; // get unit key helper function getUnitKey(unit) { - for (var key in self.units) { - for (var u in self.units[key]) { - if (unit === self.units[key][u]) { - return key; + for (var k in that.units) { + for (var u in that.units[k]) { + if (unit === that.units[k][u]) { + return k; } } } @@ -62,14 +62,14 @@ // convert a value to a specific unit function convert(value, unit) { - var baseValue = self.unitValues[getUnitKey(unit)]; + var baseValue = that.unitValues[getUnitKey(unit)]; return value / baseValue; } // get a value in seconds based on a specific unit function getSeconds(value, unit) { - var baseValue = self.unitValues[getUnitKey(unit)]; + var baseValue = that.unitValues[getUnitKey(unit)]; return value * baseValue; } @@ -102,7 +102,7 @@ return (new Timestring(settings)).parse(this, unit); }; - // export Timestring object for either the browser or node + // export Timestring object if (typeof module !== 'undefined' && module.exports) { module.exports = Timestring; } From 09089fa812cc06a34551d2891d9ad8922b1abe9d Mon Sep 17 00:00:00 2001 From: Mike Barrett Date: Thu, 17 Jul 2014 22:49:43 +0100 Subject: [PATCH 07/17] add mocha + chai --- Gruntfile.js | 12 +++++++++++- package.json | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index 42afc92..2fa8098 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -22,10 +22,20 @@ module.exports = function(grunt) { files: [ 'Gruntfile.js', 'src/**/*.js', + 'test/**/*.js' ] + }, + mochaTest: { + test: { + options: { + reporter: 'spec' + }, + src: ['test/**/*.js'] + } } }); // user defined tasks - grunt.registerTask('default', ['jshint', 'uglify']); + grunt.registerTask('test', ['mochaTest']); + grunt.registerTask('default', ['jshint', 'mochaTest', 'uglify']); }; diff --git a/package.json b/package.json index 81d6d64..dee5c49 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,13 @@ "main": "timestring.js", "version": "1.0.2", "devDependencies": { + "chai": "^1.9.1", "grunt": "^0.4.5", "grunt-contrib-jshint": "^0.10.0", "grunt-contrib-uglify": "^0.5.0", + "grunt-mocha-test": "^0.11.0", "load-grunt-tasks": "^0.6.0", + "mocha": "^1.20.1", "time-grunt": "^0.4.0" } } From 60b303f7cb2cff62dec4009e8a3b3b11d5abca2c Mon Sep 17 00:00:00 2001 From: Mike Barrett Date: Thu, 17 Jul 2014 23:13:01 +0100 Subject: [PATCH 08/17] add first mocha test --- .gitignore | 1 - .npmignore | 6 +++++- Gruntfile.js | 4 ++-- test/timestring.js | 21 +++++++++++++++++++++ src/timestring.js => timestring.js | 0 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 test/timestring.js rename src/timestring.js => timestring.js (100%) diff --git a/.gitignore b/.gitignore index d3e222c..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -test node_modules diff --git a/.npmignore b/.npmignore index d3e222c..d481e52 100644 --- a/.npmignore +++ b/.npmignore @@ -1,2 +1,6 @@ test -node_modules +.editorconfig +.jshintrc +Gruntfile.js +.DS_Store +dist diff --git a/Gruntfile.js b/Gruntfile.js index 2fa8098..44c694a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -11,7 +11,7 @@ module.exports = function(grunt) { uglify: { dist: { files: { - 'dist/<%= pkg.name %>.min.js': 'src/<%= pkg.name %>.js' + 'dist/<%= pkg.name %>.min.js': '<%= pkg.name %>.js' } } }, @@ -21,7 +21,7 @@ module.exports = function(grunt) { }, files: [ 'Gruntfile.js', - 'src/**/*.js', + '<%= pkg.name %>.js', 'test/**/*.js' ] }, diff --git a/test/timestring.js b/test/timestring.js new file mode 100644 index 0000000..8a54209 --- /dev/null +++ b/test/timestring.js @@ -0,0 +1,21 @@ +var chai = require('chai'); +var expect = chai.expect; +var timestring = require('../timestring'); + +describe('timestring', function() { + it('should expose a method on String.prototype that will attempt to parse the string as a timestring', function(done){ + var str = '1min'; + + // no arguments passed + expect(str.parseTime()).to.equal(60); + + // units argument passed + expect(str.parseTime('m')).to.equal(1); + + // units + settings argument passed + str = '5h'; + expect(str.parseTime('d', { hoursPerDay: 5 })).to.equal(1); + + done(); + }); +}); diff --git a/src/timestring.js b/timestring.js similarity index 100% rename from src/timestring.js rename to timestring.js From 9099f90693af19754158a64039db4475c560e317 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 09:25:12 +0100 Subject: [PATCH 09/17] add test for error when invalid unit used --- test/timestring.js | 10 +++++++++- timestring.js | 7 +++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/test/timestring.js b/test/timestring.js index 8a54209..39301ee 100644 --- a/test/timestring.js +++ b/test/timestring.js @@ -3,7 +3,15 @@ var expect = chai.expect; var timestring = require('../timestring'); describe('timestring', function() { - it('should expose a method on String.prototype that will attempt to parse the string as a timestring', function(done){ + it('throws an error when an invalid unit is used in the timestring', function(done) { + var ts = new timestring(); + + expect(ts.parse.bind(ts, '1g')).to.throw(Error); + + done(); + }); + + it('should expose a method on String.prototype that will parse the string as a timestring', function(done){ var str = '1min'; // no arguments passed diff --git a/timestring.js b/timestring.js index 281809f..bab6110 100644 --- a/timestring.js +++ b/timestring.js @@ -12,8 +12,7 @@ // merge default settings with user settings settings = settings || {}; - this.settings = {}; - for (var d in defaults) { this.settings[d] = defaults[d]; } + this.settings = defaults; for (var s in settings) { this.settings[s] = settings[s]; } // time units @@ -56,8 +55,8 @@ } } - // throw exception if invalid unit is passed - throw 'The unit [' + unit + '] is not supported by timestring'; + // throw error if invalid unit was passed + throw new Error('The unit [' + unit + '] is not supported by timestring'); } // convert a value to a specific unit From f4b5099f6a36c5be6d37fc4195a267288f964d0f Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 20:27:58 +0100 Subject: [PATCH 10/17] add more tests + fix year conversion bug --- test/timestring.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ timestring.js | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/test/timestring.js b/test/timestring.js index 39301ee..d27ec9d 100644 --- a/test/timestring.js +++ b/test/timestring.js @@ -3,6 +3,46 @@ var expect = chai.expect; var timestring = require('../timestring'); describe('timestring', function() { + it('can parse a timestring', function(done) { + var ts = new timestring(); + + expect(ts.parse('1s')).to.equal(1); + expect(ts.parse('1m')).to.equal(60); + expect(ts.parse('1h')).to.equal(3600); + expect(ts.parse('1d')).to.equal(86400); + expect(ts.parse('1w')).to.equal(604800); + expect(ts.parse('1mth')).to.equal(2419200); + expect(ts.parse('1y')).to.equal(29030400); + + done(); + }); + + it('can return a value in a specified unit', function(done) { + expect((new timestring()).parse('5m', 's')).to.equal(300); + expect((new timestring()).parse('5m', 'm')).to.equal(5); + + done(); + }); + + it('uses the passed settings instead of the defaults', function(done) { + var settings = { + hoursPerDay: 1, + daysPerWeek: 2, + weeksPerMonth: 3, + monthsPerYear: 4 + }; + + var ts = new timestring(settings); + + expect(ts.parse('1d', 'h')).to.equal(1); + expect(ts.parse('1w', 'd')).to.equal(2); + expect(ts.parse('1mth', 'w')).to.equal(3); + expect(ts.parse('1y', 'mth')).to.equal(4); + + + done(); + }); + it('throws an error when an invalid unit is used in the timestring', function(done) { var ts = new timestring(); @@ -11,6 +51,12 @@ describe('timestring', function() { done(); }); + it('can parse a messy time string', function(done) { + expect((new timestring()).parse('5 D a YS 4 h 2 0 mI nS')).to.equal(447600); + + done(); + }); + it('should expose a method on String.prototype that will parse the string as a timestring', function(done){ var str = '1min'; diff --git a/timestring.js b/timestring.js index bab6110..b0249b2 100644 --- a/timestring.js +++ b/timestring.js @@ -38,7 +38,7 @@ this.unitValues.d = this.settings.hoursPerDay * this.unitValues.h; this.unitValues.w = this.settings.daysPerWeek * this.unitValues.d; this.unitValues.mth = this.settings.weeksPerMonth * this.unitValues.w; - this.unitValues.y = this.settings.monthsPerYear * this.unitValues.w; + this.unitValues.y = this.settings.monthsPerYear * this.unitValues.mth; }; Timestring.prototype.parse = function(string, returnUnit) { From c2e13afd7a7e23dad83897da2644dd769cc1719f Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 20:28:23 +0100 Subject: [PATCH 11/17] add license --- LICENSE | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..669e532 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) Michael David Barrett + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. From 5b20e4151d7da2cec83b064dc4cde1f0790a96bc Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 20:40:09 +0100 Subject: [PATCH 12/17] add bower.json --- bower.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 bower.json diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..9cf4dee --- /dev/null +++ b/bower.json @@ -0,0 +1,16 @@ +{ + "name": "timestring", + "main": "timestring.js", + "version": "1.1.0", + "description": "Parse a human readable time string into a time based value", + "homepage": "https://github.com/mike182uk/timestring", + "authors": [ + "Michael David Barrett " + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "test" + ] +} From 4703c4a81ec4ce28fae8cfcc60457f7e86a6a663 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 20:40:36 +0100 Subject: [PATCH 13/17] add changelog --- CHANGELOG.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..79e3dc6 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +#Changelog + +##1.1.0 + +- Added MIT license +- Added Changelog +- Added Grunt config +- Added editorconfig +- Added Tests +- Fixed JSHint errors +- Formatted code as per editorconfig +- Fixed bug with conversion from year(s) to another unit +- Make sure `Error` (type) is thrown when invalid unit is encountered +- Added minified distributable version +- Updated package.json +- Added bower config From d922519f13d2579bc49ba9bda23c136f43112347 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 20:41:10 +0100 Subject: [PATCH 14/17] update package.json & npmignore --- .npmignore | 1 - package.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.npmignore b/.npmignore index d481e52..44aaf22 100644 --- a/.npmignore +++ b/.npmignore @@ -3,4 +3,3 @@ test .jshintrc Gruntfile.js .DS_Store -dist diff --git a/package.json b/package.json index dee5c49..ab0ad73 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "url": "git://github.com/mike182uk/timestring.git" }, "main": "timestring.js", - "version": "1.0.2", + "version": "1.1.0", "devDependencies": { "chai": "^1.9.1", "grunt": "^0.4.5", From 38abe015f5bb2b8a8028690acee9de3f249f8574 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 20:53:04 +0100 Subject: [PATCH 15/17] add travis config --- .jshintrc | 1 - .travis.yml | 7 +++++++ CHANGELOG.md | 1 + Gruntfile.js | 2 ++ 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .travis.yml diff --git a/.jshintrc b/.jshintrc index 0db3279..2c63c08 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,3 +1,2 @@ { - } diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f94f4ed --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: + - "0.10" + - "0.11" +before_script: + - npm install -g grunt-cli +script: grunt test diff --git a/CHANGELOG.md b/CHANGELOG.md index 79e3dc6..82ae255 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,3 +14,4 @@ - Added minified distributable version - Updated package.json - Added bower config +- Added travis config diff --git a/Gruntfile.js b/Gruntfile.js index 44c694a..30835ef 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -37,5 +37,7 @@ module.exports = function(grunt) { // user defined tasks grunt.registerTask('test', ['mochaTest']); + grunt.registerTask('lint', ['jshint']); + grunt.registerTask('build', ['uglify']); grunt.registerTask('default', ['jshint', 'mochaTest', 'uglify']); }; From 996eca77eb3bb5e43c93b707f7f452f82269ff33 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 21:14:09 +0100 Subject: [PATCH 16/17] update readme --- README.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2c5bc77..042da42 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ #Timestring +[![Build Status](https://travis-ci.org/mike182uk/timestring.svg?branch=master)](https://travis-ci.org/mike182uk/timestring) + +[![NPM](https://nodei.co/npm/timestring.png?downloads=true&stars=true)](https://nodei.co/npm/timestring/) + Attempts to parse a human readable time string into a time based value. ##Overview @@ -158,11 +162,18 @@ console.log(daysThisWeek); // will log 5 ###Browser -All you need to do to get timestring working in the browser is download / clone this repo and make sure you include the `timestring.js` script on your page: +All you need to do to get timestring working in the browser is download / clone this repo and make sure you include the `dist/timestring.min.js` script on your page: ```html - + ``` + +Alternatively you can you use bower to manage this dependency for you: + +``` +bower install timestring --save +``` + ###Node To install for a node application, navigate to the projects root folder and in your terminal type the following: From 336c35854c9322319b8106b6eed2d9f9b5331454 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 21:22:05 +0100 Subject: [PATCH 17/17] add minified version --- dist/timestring.min.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 dist/timestring.min.js diff --git a/dist/timestring.min.js b/dist/timestring.min.js new file mode 100644 index 0000000..470b3d1 --- /dev/null +++ b/dist/timestring.min.js @@ -0,0 +1 @@ +(function(){"use strict";var a=function(a){var b={hoursPerDay:24,daysPerWeek:7,weeksPerMonth:4,monthsPerYear:12};a=a||{},this.settings=b;for(var c in a)this.settings[c]=a[c];this.units={s:["s","sec","secs","second","seconds"],m:["m","min","mins","minute","minutes"],h:["h","hr","hrs","hour","hours"],d:["d","day","days"],w:["w","week","weeks"],mth:["mth","mths","month","months"],y:["y","yr","yrs","year","years"]},this.unitValues={s:1,m:60,h:3600},this.unitValues.d=this.settings.hoursPerDay*this.unitValues.h,this.unitValues.w=this.settings.daysPerWeek*this.unitValues.d,this.unitValues.mth=this.settings.weeksPerMonth*this.unitValues.w,this.unitValues.y=this.settings.monthsPerYear*this.unitValues.mth};a.prototype.parse=function(a,b){function c(a){for(var b in f.units)for(var c in f.units[b])if(a===f.units[b][c])return b;throw new Error("The unit ["+a+"] is not supported by timestring")}function d(a,b){var d=f.unitValues[c(b)];return a/d}function e(a,b){var d=f.unitValues[c(b)];return a*d}var f=this,g=0,h=a.toLowerCase().replace(/[^\.\w+-]+/g,"").match(/[-+]?[0-9]+[a-z]+/g);if(null!==h)for(var i=0;i