approximate
Advanced tools
Comparing version 0.0.3 to 0.0.4
63
index.js
@@ -1,28 +0,41 @@ | ||
var proxy; | ||
(function() { | ||
var proxy; | ||
module.exports = function( year ) { | ||
if (!year.toString().match(/[0-9]{4}/g).length == 1) | ||
throw new Error(year + ' is not a legit year'); | ||
var contextYear = year; | ||
// TODO: do a check here that it's a legit number at least, and year | ||
proxy = { | ||
contextYear : contextYear, | ||
parse : function( value ) { | ||
var lower = value.toLowerCase(); | ||
var matches = lower.match( | ||
/(jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec)\ +[0-9]{1,2}[^0-9]*$/g | ||
); | ||
if (matches && matches.length == 1) { | ||
// so janky | ||
var match = value.toLowerCase().match( | ||
/(jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec)\ +[0-9]{1,2}[^0-9]/g | ||
); | ||
return new Date(match + ', ' + contextYear); | ||
} else { | ||
throw new Error(value + ' can\'t be parsed.'); | ||
module.exports = function( year ) { | ||
if (!year.toString().match(/[0-9]{4}/g).length == 1) | ||
throw new Error(year + ' is not a legit year'); | ||
var contextYear = year; | ||
proxy = { | ||
contextYear : contextYear, | ||
parse : function( value ) { | ||
var lower = value.toLowerCase(); | ||
var matches = lower.match( | ||
/(jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec)\ +[0-9]{1,2}[^0-9]/g | ||
); | ||
if (matches && matches.length == 1) { | ||
return new Date(matches[0] + ', ' + contextYear); | ||
} else { | ||
return proxy.parseGeneralDate( value ) | ||
} | ||
}, | ||
parseGeneralDate : function( value ) { | ||
var lower = value.toLowerCase(); | ||
var matches = lower.match( | ||
/(1|2|3|4|5|6|7|8|9|10|11|12)\/[0-9]{1,2}[^0-9]/g | ||
); | ||
if (matches && matches.length == 1) { | ||
return new Date(matches[0] + '/' + contextYear); | ||
} else { | ||
throw new Error(value + ' can\'t be parsed.'); | ||
} | ||
} | ||
} | ||
}; | ||
return proxy; | ||
} | ||
}; | ||
return proxy; | ||
} | ||
})() | ||
{ | ||
"name": "approximate", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A library to parse \"approximate\" dates", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,1 +18,8 @@ # approximate | ||
``` | ||
...and: | ||
```javascript | ||
var approx = require('approximate')(2015); | ||
var parsedDate = approx('4/15 - April and the Showers, Flowers for April'); | ||
console.log(parsedDate); // yields ISO timestamp equivalent to January 15, 2015 00:00:00Zsomething something | ||
``` |
@@ -6,2 +6,3 @@ var expect = require('chai').expect; | ||
it('caches context year', function(){ | ||
var approx = approxModule(2015) | ||
@@ -12,2 +13,12 @@ expect(approx).to.not.be.null; | ||
it('rejects undefined', function(){ | ||
var func = function() { var approx = approxModule(undefined) }; | ||
expect(func).to.throw(Error); | ||
}); | ||
it('rejects null', function(){ | ||
var func = function() { var approx = approxModule(null) }; | ||
expect(func).to.throw(Error); | ||
}); | ||
it('rejects bogus three-digit year', function(){ | ||
@@ -42,4 +53,10 @@ var func = function() { var approx = approxModule(491) }; | ||
it('parses a simple colloquial date', function(){ | ||
it('parses a string containing multiple date MMM DD and returns the last match', function(){ | ||
var approx = approxModule(2015) | ||
var func = function() { approx.parse('JAN 15 and FEB 23 - some bands playing') }; | ||
expect(func).to.throw(Error); | ||
}); | ||
it('parses a string containing simple date MMM DD', function(){ | ||
var approx = approxModule(2015) | ||
var parsed = approx.parse('JAN 15 - Some Event - Some Bands'); | ||
@@ -49,3 +66,3 @@ expect(parsed.getTime()).to.equal(new Date('January 15, 2015').getTime()); | ||
it('parses a simple colloquial date with subsequent', function(){ | ||
it('parses a string containing date (MMM DD) with trailing text', function(){ | ||
var approx = approxModule(2015) | ||
@@ -55,4 +72,28 @@ var parsed = approx.parse('JAN 15 - Some Event - Some Bands'); | ||
}); | ||
it('parses a string containing date (MMM DD) with preceding text', function(){ | ||
var approx = approxModule(2015) | ||
var parsed = approx.parse('Wednesday JAN 16 - Some Event - Some Bands'); | ||
expect(parsed.getTime()).to.equal(new Date('January 16, 2015').getTime()); | ||
}); | ||
it('parses a string containing date (MMM DD) with preceding text', function(){ | ||
var approx = approxModule(2015) | ||
var parsed = approx.parse('Wednesday JAN 16 - Some Event - Some Bands'); | ||
expect(parsed.getTime()).to.equal(new Date('January 16, 2015').getTime()); | ||
}); | ||
it('parses a string containing date (MM/DD) with single-digit month', function(){ | ||
var approx = approxModule(2015) | ||
var parsed = approx.parse('1/16 - Some Event - Some Bands'); | ||
expect(parsed.getTime()).to.equal(new Date('January 16, 2015').getTime()); | ||
}); | ||
it('parses a string containing date (MM/DD) with double-digit month', function(){ | ||
var approx = approxModule(2015) | ||
var parsed = approx.parse('12/16 - Some Event - Some Bands'); | ||
expect(parsed.getTime()).to.equal(new Date('December 16, 2015').getTime()); | ||
}); | ||
}); | ||
6136
110
25