Permalink
Browse files

behave more like native Date

  • Loading branch information...
1 parent 3b7972d commit 4100e6fc29aaef02d750e1269f41670064c962fd @zackdever committed Sep 10, 2012
Showing with 83 additions and 66 deletions.
  1. +23 −17 README.md
  2. +24 −25 test/time.test.js
  3. +36 −24 time.js
View
@@ -7,27 +7,33 @@ Built for [Promt](http://promtapp.com).
Parses strings such as "8:20" into a Date-less Time.
- Time.parse('1') // { hours: 1, minutes: 0 }
- Time.parse('1:23') // { hours: 1, minutes: 23 }
- Time.parse('1.23') // { hours: 1, minutes: 23 }
- Time.parse('123') // { hours: 1, minutes: 23 }
+ new Time('1') // 1:00
+ new Time('1:23') // 1:23
+
+If you fancy it, you can use safely drop the 'new'.
+
+ Time('1.23') // 1:23
+ Time('123') // 1:23
Converts Time into the next corresponding JavaScript Date.
// assume it's 3:15 pm Aug 10
- Time.parseToDate('3:15') // 3:15 pm Aug 10
- Time.parseToDate('415') // 4:15 pm Aug 10
- Time.parseToDate('2') // 2:00 am Aug 11
-
-Does simple validation.
-
- Time.isValid('1') // true
- Time.isValid('1.20') // true
- Time.isValid('8:00') // true
- Time.isValid('12.0') // false
- Time.isValid('12:202') // false
- Time.isValid('12:60') // false
- Time.isValid('13:23') // false
+ Time('3:15').nextDate() // 3:15 pm Aug 10
+ Time('415').nextDate() // 4:15 pm Aug 10
+ Time('2').nextDate() // 2:00 am Aug 11
+
+Does validation statically...
+
+ Time.isValid('8:00') // true
+ Time.isValid('12:60') // false
+ Time.isValid('13:23') // false
+
+... or after contruction.
+
+ Time('1').isValid() // true
+ Time('12.0').isValid() // false
+ Time('12:202').isValid() // false
+
Periods (am/pm) and military time are not supported, but probably will be.
View
@@ -5,29 +5,29 @@ var hours=[], minutes=[], periods=[];
for (var i = 1; i < 13; i++) hours.push(i.toString());
for (var i = 0; i < 60; i++) minutes.push(('0' + i).slice(-2));
-describe('time', function() {
+describe('Time', function() {
describe('#isValid', function() {
it('should pass all the hours 1-12', function() {
var hour, result;
for (var i = 0; i < hours.length; i++) {
hour = hours[i];
- time.isValid(hour).should.be.ok;
- result = time.parse(hours[i]);
+ result = time(hour);
+ result.isValid().should.be.ok;
result.hours.should.equal(parseInt(hour));
result.minutes.should.equal(0);
}
});
it('should fail made up hours e.g. 0, 13, 50', function() {
- time.isValid('0').should.not.be.ok;
- time.isValid('13').should.not.be.ok;
- time.isValid('50').should.not.be.ok;
+ time('0').isValid().should.not.be.ok;
+ time('13').isValid().should.not.be.ok;
+ time('50').isValid().should.not.be.ok;
});
it('should fail made up hours e.g. 0:20, 13:12, 50:00', function() {
- time.isValid('0:20').should.not.be.ok;
- time.isValid('13:12').should.not.be.ok;
- time.isValid('50:00').should.not.be.ok;
+ time('0:20').isValid().should.not.be.ok;
+ time('13:12').isValid().should.not.be.ok;
+ time('50:00').isValid().should.not.be.ok;
});
it('should pass all the hours with all the minutes 1:00 - 12:59', function() {
@@ -37,8 +37,8 @@ describe('time', function() {
hour = hours[i];
minute = minutes[j];
input = hour + ':' + minute;
- time.isValid(input).should.be.ok;
- result = time.parse(input);
+ result = time(input);
+ result.isValid().should.be.ok;
result.hours.should.equal(parseInt(hour));
result.minutes.should.equal(parseInt(minute));
}
@@ -51,30 +51,29 @@ describe('time', function() {
for (var j = 0; j < minutes.length; j++) {
hour = hours[i];
minute = minutes[j];
- input = hour + minute;
- time.isValid(input).should.be.ok;
- result = time.parse(input);
+ result = time(hour + minute);
+ result.isValid().should.be.ok;
result.hours.should.equal(parseInt(hour));
result.minutes.should.equal(parseInt(minute));
}
}
});
it('should fail made up minutes e.g. 1:3, 1:60, 1:122', function() {
- time.isValid('1:3').should.not.be.ok;
- time.isValid('1:60').should.not.be.ok;
- time.isValid('1:122').should.not.be.ok;
- time.isValid('12:99').should.not.be.ok;
- time.isValid('12:021').should.not.be.ok;
- time.isValid('12:218').should.not.be.ok;
+ time('1:3').isValid().should.not.be.ok;
+ time('1:60').isValid().should.not.be.ok;
+ time('1:122').isValid().should.not.be.ok;
+ time('12:99').isValid().should.not.be.ok;
+ time('12:021').isValid().should.not.be.ok;
+ time('12:218').isValid().should.not.be.ok;
});
it('should fail made up minutes without the colon e.g. 13, 160', function() {
- time.isValid('13').should.not.be.ok;
- time.isValid('160').should.not.be.ok;
- time.isValid('1299').should.not.be.ok;
- time.isValid('12021').should.not.be.ok;
- time.isValid('12218').should.not.be.ok;
+ time('14').isValid().should.not.be.ok;
+ time('160').isValid().should.not.be.ok;
+ time('1299').isValid().should.not.be.ok;
+ time('12021').isValid().should.not.be.ok;
+ time('12218').isValid().should.not.be.ok;
});
});
});
View
@@ -1,39 +1,51 @@
(function() {
var root = (typeof exports == 'undefined' ? window : exports);
- var re = /^(10|11|12|[1-9])(?::|\.)?([0-5][0-9])?$/;
-
- function Time() { }
root.Time = Time;
- Time.isValid = function(time) {
- return re.test(time);
- };
-
- Time.parse = function(time) {
- time.replace(' ', '');
- var result = re.exec(time);
- if (!result) return null;
-
- var minutes = result[2] ? parseInt(result[2]) : 0;
- return { hours: parseInt(result[1]), minutes: minutes};
- };
-
- Time.parseToDate = function(time) {
- var result = Time.parse(time);
- if (result == null) return null;
- return toDate(result.hours, result.minutes);
+ Time.re = /^(10|11|12|[1-9])(?::|\.)?([0-5][0-9])?$/;
+
+ function Time(time) {
+ if (!(this instanceof Time)) return new Time(time);
+
+ if (time) {
+ time.replace(' ', '');
+ var result = Time.re.exec(time);
+ if (result) {
+ this.hours = parseInt(result[1]);
+ this.minutes = result[2] ? parseInt(result[2]) : 0;
+ }
+ } else {
+ // set to current time
+ var d = new Date()
+ , hours = d.getHours();
+ this.hours = hours > 11 ? hours - 12 : hours;
+ this.minutes = d.getMinutes();
+ }
}
- function toDate(hours, minutes) {
- if (hours === 12) hours = 0; // this lets us uniformly handle am/pm adjustments
+ Time.prototype.nextDate = function () {
+ if (!this.isValid()) return null;
+ var hours = this.hours === 12 ? 0 : this.hours; // uniformly handle am/pm adjustments
var d = new Date();
- d.setMinutes(minutes);
+ d.setMinutes(this.minutes);
d.setHours(hours);
// if it has already passed, add 12 hours at a time until it's in the future
while (new Date() > d) d.setHours(d.getHours() + 12);
-
return d;
}
+
+ Time.isValid = function(time) {
+ return Time.re.test(time);
+ }
+
+ Time.prototype.isValid = function() {
+ return Time.isValid(this.toString());
+ }
+
+ Time.prototype.toString = function() {
+ var minutes = this.minutes < 10 ? '0' + this.minutes : this.minutes;
+ return this.hours + ':' + minutes;
+ }
})();

0 comments on commit 4100e6f

Please sign in to comment.