Skip to content

Commit

Permalink
add month and year
Browse files Browse the repository at this point in the history
  • Loading branch information
E.Azer Koçulu committed Oct 27, 2013
1 parent 4577ccb commit aacf3a9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 37 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -81,6 +81,19 @@ $ npm install english-time
* week
* weeks


**Month(s)**

* mo
* month
* months

**Year(s)**

* y
* year
* years

## Translations

* [turkish-time](http://github.com/azer/turkish-time)
Expand Down
22 changes: 18 additions & 4 deletions lib/rewrite.js
Expand Up @@ -3,13 +3,27 @@ var singular = ['millisecond',
'minute',
'hour',
'day',
'week'];
'week',
'month',
'year'];

var numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'];

module.exports = rewrite;

function rewrite(input, customSingular){
var re = new RegExp('((?:[a-z]\\s)|^)(' + (customSingular || singular).join('|') + ')(?=\\s|$)');
return input.replace(re, function(_, prefix, unit){
function rewrite(input, customSingular, customNumbers){
var re = new RegExp('((?:[a-z]\\s)|^)(' + (customNumbers || numbers).join('|') + ')(?=\\s|$)', 'g');

var output = input.replace(re, function (_, prefix, n) {
var value = numbers.indexOf(n);
return prefix + (value == -1 ? n : value);
});

re = new RegExp('((?:[a-z]\\s)|^)(' + (customSingular || singular).join('|') + ')(?=\\s|$)', 'g');

output = output.replace(re, function(_, prefix, unit){
return prefix + '1 ' + unit;
});

return output;
}
79 changes: 46 additions & 33 deletions lib/units.js
Expand Up @@ -2,46 +2,59 @@ var seconds = x(milliseconds, 1000),
minutes = x(seconds, 60),
hours = x(minutes, 60),
days = x(hours, 24),
weeks = x(days, 7);
weeks = x(days, 7),
months = x(days, 31);

module.exports = {
m : milliseconds,
ms : milliseconds,
millisec : milliseconds,
millisecs : milliseconds,
millisecond : milliseconds,
milliseconds : milliseconds,

s : seconds,
sec : seconds,
secs : seconds,
seconds : seconds,
second : seconds,

m : minutes,
min : minutes,
mins : minutes,
minute : minutes,
minutes : minutes,

h : hours,
hour : hours,
hours : hours,

d : days,
day : days,
days : days,

w : weeks,
week : weeks,
weeks : weeks
m: milliseconds,
ms: milliseconds,
millisec: milliseconds,
millisecs: milliseconds,
millisecond: milliseconds,
milliseconds: milliseconds,

s: seconds,
sec: seconds,
secs: seconds,
seconds: seconds,
second: seconds,

m: minutes,
min: minutes,
mins: minutes,
minute: minutes,
minutes: minutes,

h: hours,
hour: hours,
hours: hours,

d: days,
day: days,
days: days,

w: weeks,
week: weeks,
weeks: weeks,

mo: months,
month: months,
months: months,

y: years,
year: years,
years: years
};

function milliseconds(n){
function milliseconds (n){
return n;
}

function x(fn, multiples){
function years (n) {
return n * days(365) + hours(6);
}

function x (fn, multiples){
return function(n){
return fn(n) * multiples;
};
Expand Down
5 changes: 5 additions & 0 deletions test.js
Expand Up @@ -5,6 +5,7 @@ it('converts time written in english to epoch', function(){
expect(time('375 milliseconds')).to.equal(375);

expect(time('1 second')).to.equal(1000);
expect(time('second and second')).to.equal(2000);
expect(time('5 Minutes')).to.equal(300000);
expect(time('5 minutes 15 sEConds')).to.equal(315000);
expect(time('2 Hours, 5 Minutes and 15 Seconds')).to.equal(7515000);
Expand All @@ -13,6 +14,10 @@ it('converts time written in english to epoch', function(){
expect(time('20h 5m 15s')).to.equal(72315000);

expect(time('3 weeks, 5 days, 6 hours')).to.equal(2268000000);
expect(time('a month')).to.equal(2678400000);
expect(time('5 months')).to.equal(13392000000);
expect(time('one year')).to.equal(31557600000);
expect(time('one year and five months')).to.equal(31557600000 + 13392000000);
});

it('ignores unrecognized patterns', function(){
Expand Down

0 comments on commit aacf3a9

Please sign in to comment.