ambit
Advanced tools
Comparing version 0.1.3 to 1.0.0
39
ambit.js
@@ -25,4 +25,4 @@ var moment = require('moment'); | ||
function getSeason(now, season, year) { | ||
var start, end, guessYear, endSeason, endYear, cal, hms; | ||
function getSeason(now, season, year, guessYear) { | ||
var start, end, endSeason, endYear, cal, hms; | ||
endSeason = season + 1; | ||
@@ -34,15 +34,11 @@ endYear = year; | ||
} | ||
if (!year) { | ||
year = new Date().getFullYear(); | ||
guessYear = true; | ||
} | ||
cal = moonbeams.jdToCalendar(moonbeams.season(endSeason, endYear)); | ||
hms = moonbeams.dayToHms(cal.day); | ||
end = moment(new Date(cal.year, cal.month - 1, cal.day, hms.hour, hms.minute, hms.second)); | ||
//Eventually need to pass now properly | ||
//if (guessYear && end <= now) { | ||
//end = moment(seasonDate(endSeason, endYear + 1)); | ||
//year = year + 1; | ||
//} | ||
if (guessYear && end <= now) { | ||
cal = moonbeams.jdToCalendar(moonbeams.season(endSeason, endYear + 1)); | ||
hms = moonbeams.dayToHms(cal.day); | ||
end = moment(new Date(cal.year, cal.month - 1, cal.day, hms.hour, hms.minute, hms.second)); | ||
year = year + 1; | ||
} | ||
cal = moonbeams.jdToCalendar(moonbeams.season(season, year)); | ||
@@ -91,3 +87,3 @@ hms = moonbeams.dayToHms(cal.day); | ||
function parseMonth(tokens, now) { | ||
var parsed, start, end, month, year; | ||
var parsed, start, end, month, year, guessYear; | ||
//Needs to end in a month or a month/year | ||
@@ -103,2 +99,3 @@ year = tokens.slice(-1)[0]; | ||
year = new Date().getFullYear(); | ||
guessYear = true; | ||
} | ||
@@ -111,3 +108,3 @@ month = MONTHS.indexOf(month.slice(0, 3)); | ||
end = moment(start).add('months', 1).subtract('seconds', 1); | ||
if (end <= now) { | ||
if (guessYear && end <= now) { | ||
start.add('years', 1); | ||
@@ -124,3 +121,3 @@ end.add('years', 1); | ||
function parseSeason(tokens, now) { | ||
var parsed, season, year; | ||
var parsed, season, year, guessYear; | ||
//Needs to end in a season or a season/year | ||
@@ -136,2 +133,3 @@ year = tokens.slice(-1)[0]; | ||
year = new Date().getFullYear(); | ||
guessYear = true; | ||
} | ||
@@ -142,3 +140,3 @@ season = SEASONS[season]; | ||
} | ||
parsed = getSeason(now, season, year); | ||
parsed = getSeason(now, season, year, guessYear); | ||
return parsed; | ||
@@ -164,6 +162,5 @@ } | ||
//TODO "Oct to Sep" for example needs to know sep is the year after | ||
moment.ambit = function ambit(str, format) { | ||
var result, startRange, endRange; | ||
//var now = new Date(); | ||
var now = new Date(); | ||
var tokens = str.trim().toLowerCase().split(/[,\s]+/); | ||
@@ -177,3 +174,3 @@ var direction = 'right'; | ||
current.push(tokens.shift()); | ||
currentAttempt = tryAll(current); | ||
currentAttempt = tryAll(current, now); | ||
if (currentAttempt) { | ||
@@ -212,2 +209,6 @@ lastAttempt = currentAttempt; | ||
} | ||
//This should currently only happen w/ two un-yeared months | ||
if (result.start > result.end) { | ||
result.end = result.end.add('years', 1); | ||
} | ||
return result; | ||
@@ -214,0 +215,0 @@ } |
{ | ||
"name": "ambit", | ||
"description": "Date parser that returns a range instead of a single value", | ||
"version": "0.1.3", | ||
"version": "1.0.0", | ||
"author": "Michael Garvin <gar@comrade.us>", | ||
"bugs": { | ||
"url": "https://github.com/wraithgar/date-range-parser/issues" | ||
"url": "https://github.com/wraithgar/ambit/issues" | ||
}, | ||
@@ -23,4 +23,3 @@ "dependencies": { | ||
"range", | ||
"node", | ||
"commonjs" | ||
"moment" | ||
], | ||
@@ -31,3 +30,3 @@ "license": "MIT", | ||
"type": "git", | ||
"url": "git://github.com/wraithgar/date-range-parser.git" | ||
"url": "git://github.com/wraithgar/ambit.git" | ||
}, | ||
@@ -34,0 +33,0 @@ "scripts": { |
#ambit | ||
So it turns out I couldn't find anything out there that parsed a date | ||
string in nominally plain English and returned a date range. This is a | ||
WIP attempt at doing so. Requires moment. | ||
So it turns out I couldn't find anything out there that parsed a date string and returned a date range. Typically if you ask for "March" you get "March 1" instead of "March 1 through March 31" | ||
This is an attempt at doing the latter. | ||
Requires [moment](http://momentjs.com/). | ||
Season calculations done via [moonbeams](https://github.com/wraithgar/moonbeams) | ||
Right now it only does day-level granularity. | ||
If a year is not given, ambit will make every effort to assume the dates should be in the future | ||
#use | ||
```javascript | ||
var ambit = require('ambit'); //ambit is moment + ambit now; | ||
var ambit = require('ambit'); //ambit is moment + ambit now; | ||
var range = ambit.ambit('Spring 2005'); | ||
@@ -12,0 +20,0 @@ ``` |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
35
8124
4
194