Skip to content

Instantly share code, notes, and snippets.

@colingourlay
Created September 12, 2014 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colingourlay/68971a66fb17240efab8 to your computer and use it in GitHub Desktop.
Save colingourlay/68971a66fb17240efab8 to your computer and use it in GitHub Desktop.
requirebin sketch
var d = require('date.js');
console.log(d('tomorrow afternoon at 4:30pm 1 month from now'));
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var debug=require("debug")("date:date");var _second=1e3;var _minute=60*_second;var _hour=60*_minute;var _day=24*_hour;var _week=7*_day;var _year=56*_week;var _daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];module.exports=date;function date(offset){if(!(this instanceof date))return new date(offset);this._changed={};this.date=new Date(offset)}date.prototype.clone=function(){return new Date(this.date)};date.prototype.changed=function(str){if(this._changed[str]===undefined)return false;return this._changed[str]};date.prototype.second=function(n){var seconds=+n*_second;this.update(seconds);this._changed["seconds"]=true;return this};date.prototype.minute=function(n){var minutes=+n*_minute;this.update(minutes);this._changed["minutes"]=true;return this};date.prototype.hour=function(n){var hours=+n*_hour;this.update(hours);this._changed["hours"]=true;return this};date.prototype.day=function(n){var days=+n*_day;this.update(days);this._changed["days"]=true;return this};date.prototype.week=function(n){var weeks=+n*_week;this.update(weeks);this._changed["weeks"]=true;return this};date.prototype.month=function(n){var d=this.date;var day=d.getDate();d.setDate(1);var month=+n+d.getMonth();d.setMonth(month);var dim=this.daysInMonth(month);d.setDate(Math.min(dim,day));return this};date.prototype.daysInMonth=function(m){var dim=_daysInMonth[m];var leap=leapyear(this.date.getFullYear());return 1==m&&leap?29:28};date.prototype.year=function(n){var yr=this.date.getFullYear();yr+=+n;this.date.setFullYear(yr);this._changed["years"]=true;return this};date.prototype.time=function(h,m,s,meridiem){if(h===false){h=this.date.getHours()}else{h=+h||0;this._changed["hours"]=h}if(m===false){m=this.date.getMinutes()}else{m=+m||0;this._changed["minutes"]=m}if(s===false){s=this.date.getSeconds()}else{s=+s||0;this._changed["seconds"]=s}this.date.setHours(h,m,s);return this};var days=["sunday","monday","tuesday","wednesday","thursday","friday","saturday"];days.forEach(function(day,i){date.prototype[days[i]]=function(n){this._changed["days"]=true;this.updateDay(i,n)}});date.prototype.updateDay=function(d,n){n=+(n||1);var diff=(d-this.date.getDay()+7)%7;if(n>0)--n;diff+=7*n;this.update(diff*_day);return this};date.prototype.update=function(ms){this.date=new Date(this.date.getTime()+ms);return this};function leapyear(yr){return yr%4===0&&yr%100!==0||yr%400===0}},{debug:3}],2:[function(require,module,exports){var date=require("./date");var debug=require("debug")("date:parser");var days=["sunday","monday","tuesday","wednesday","thursday","friday","saturday"];var months=["january","february","march","april","may","june","july","august","september","october","november","december"];var rMeridiem=/^(\d{1,2})([:.](\d{1,2}))?([:.](\d{1,2}))?\s*([ap]m)/;var rHourMinute=/^(\d{1,2})([:.](\d{1,2}))([:.](\d{1,2}))?/;var rAtHour=/^at\s?(\d{1,2})$/;var rDays=/\b(sun(day)?|mon(day)?|tues(day)?|wed(nesday)?|thur(sday|s)?|fri(day)?|sat(urday)?)s?\b/;var rMonths=/^((\d{1,2})(st|nd|rd|th))\sof\s(january|february|march|april|may|june|july|august|september|october|november|december)/;var rPast=/\b(last|yesterday|ago)\b/;var rDayMod=/\b(morning|noon|afternoon|night|evening|midnight)\b/;var rAgo=/^(\d*)\s?\b(second|minute|hour|day|week|month|year)[s]?\b\s?ago$/;module.exports=parser;function parser(str,offset){if(!(this instanceof parser))return new parser(str,offset);if(typeof offset=="string")offset=parser(offset);var d=offset||new Date;this.date=new date(d);this.original=str;this.str=str.toLowerCase();this.stash=[];this.tokens=[];while(this.advance()!=="eos");debug("tokens %j",this.tokens);this.nextTime(d);if(this.date.date==d)throw new Error("Invalid date");return this.date.date}parser.prototype.advance=function(){var tok=this.eos()||this.space()||this._next()||this.last()||this.dayByName()||this.monthByName()||this.timeAgo()||this.ago()||this.yesterday()||this.tomorrow()||this.noon()||this.midnight()||this.night()||this.evening()||this.afternoon()||this.morning()||this.tonight()||this.meridiem()||this.hourminute()||this.athour()||this.week()||this.month()||this.year()||this.second()||this.minute()||this.hour()||this.day()||this.number()||this.string()||this.other();this.tokens.push(tok);return tok};parser.prototype.lookahead=function(n){var fetch=n-this.stash.length;if(fetch==0)return this.lookahead(++n);while(fetch-->0)this.stash.push(this.advance());return this.stash[--n]};parser.prototype.peek=function(){return this.lookahead(1)};parser.prototype.next=function(){var tok=this.stashed()||this.advance();return tok};parser.prototype.stashed=function(){var stashed=this.stash.shift();return stashed};parser.prototype.skip=function(len){this.str=this.str.substr(Array.isArray(len)?len[0].length:len)};parser.prototype.eos=function(){if(this.str.length)return;return"eos"};parser.prototype.space=function(){var captures;if(captures=/^([ \t]+)/.exec(this.str)){this.skip(captures);return this.advance()}};parser.prototype.second=function(){var captures;if(captures=/^s(ec|econd)?s?/.exec(this.str)){this.skip(captures);return"second"}};parser.prototype.minute=function(){var captures;if(captures=/^m(in|inute)?s?/.exec(this.str)){this.skip(captures);return"minute"}};parser.prototype.hour=function(){var captures;if(captures=/^h(r|our)s?/.exec(this.str)){this.skip(captures);return"hour"}};parser.prototype.day=function(){var captures;if(captures=/^d(ay)?s?/.exec(this.str)){this.skip(captures);return"day"}};parser.prototype.dayByName=function(){var captures;var r=new RegExp("^"+rDays.source);if(captures=r.exec(this.str)){var day=captures[1];this.skip(captures);this.date[day](1);return captures[1]}};parser.prototype.monthByName=function(){var captures;if(captures=rMonths.exec(this.str)){var day=captures[2];var month=captures[4];this.date.date.setMonth(months.indexOf(month));if(day)this.date.date.setDate(parseInt(day)-1);this.skip(captures);return captures[0]}};parser.prototype.timeAgo=function(){var captures;if(captures=rAgo.exec(this.str)){var num=captures[1];var mod=captures[2];this.date[mod](-num);this.skip(captures);return"timeAgo"}};parser.prototype.week=function(){var captures;if(captures=/^w(k|eek)s?/.exec(this.str)){this.skip(captures);return"week"}};parser.prototype.month=function(){var captures;if(captures=/^mon(th)?(es|s)?\b/.exec(this.str)){this.skip(captures);return"month"}};parser.prototype.year=function(){var captures;if(captures=/^y(r|ear)s?/.exec(this.str)){this.skip(captures);return"year"}};parser.prototype.meridiem=function(){var captures;if(captures=rMeridiem.exec(this.str)){this.skip(captures);this.time(captures[1],captures[3],captures[5],captures[6]);return"meridiem"}};parser.prototype.hourminute=function(){var captures;if(captures=rHourMinute.exec(this.str)){this.skip(captures);this.time(captures[1],captures[3],captures[5]);return"hourminute"}};parser.prototype.athour=function(){var captures;if(captures=rAtHour.exec(this.str)){this.skip(captures);this.time(captures[1],0,0,this._meridiem);this._meridiem=null;return"athour"}};parser.prototype.time=function(h,m,s,meridiem){var d=this.date;var before=d.clone();if(meridiem){h="pm"==meridiem&&12>h?+h+12:h;h="am"==meridiem&&12==h?0:h}m=!m&&d.changed("minutes")?false:m;s=!s&&d.changed("seconds")?false:s;d.time(h,m,s)};parser.prototype.nextTime=function(before){var d=this.date;var orig=this.original;if(before<=d.date||rPast.test(orig))return this;if(rDays.test(orig))d.day(7);else if((before-d.date)/1e3>60)d.day(1);return this};parser.prototype.yesterday=function(){var captures;if(captures=/^(yes(terday)?)/.exec(this.str)){this.skip(captures);this.date.day(-1);return"yesterday"}};parser.prototype.tomorrow=function(){var captures;if(captures=/^tom(orrow)?/.exec(this.str)){this.skip(captures);this.date.day(1);return"tomorrow"}};parser.prototype.noon=function(){var captures;if(captures=/^noon\b/.exec(this.str)){this.skip(captures);var before=this.date.clone();this.date.date.setHours(12,0,0);return"noon"}};parser.prototype.midnight=function(){var captures;if(captures=/^midnight\b/.exec(this.str)){this.skip(captures);var before=this.date.clone();this.date.date.setHours(0,0,0);return"midnight"}};parser.prototype.night=function(){var captures;if(captures=/^night\b/.exec(this.str)){this.skip(captures);this._meridiem="pm";var before=this.date.clone();this.date.date.setHours(19,0,0);return"night"}};parser.prototype.evening=function(){var captures;if(captures=/^evening\b/.exec(this.str)){this.skip(captures);this._meridiem="pm";var before=this.date.clone();this.date.date.setHours(17,0,0);return"evening"}};parser.prototype.afternoon=function(){var captures;if(captures=/^afternoon\b/.exec(this.str)){this.skip(captures);this._meridiem="pm";var before=this.date.clone();if(this.date.changed("hours"))return"afternoon";this.date.date.setHours(14,0,0);return"afternoon"}};parser.prototype.morning=function(){var captures;if(captures=/^morning\b/.exec(this.str)){this.skip(captures);this._meridiem="am";var before=this.date.clone();if(!this.date.changed("hours"))this.date.date.setHours(8,0,0);return"morning"}};parser.prototype.tonight=function(){var captures;if(captures=/^tonight\b/.exec(this.str)){this.skip(captures);this._meridiem="pm";return"tonight"}};parser.prototype._next=function(){var captures;if(captures=/^next/.exec(this.str)){this.skip(captures);var d=new Date(this.date.date);var mod=this.peek();if(this.date[mod]){this.next();this.date=date(d);this.date[mod](1)}else if(rDayMod.test(mod)){this.date.day(1)}return"next"}};parser.prototype.last=function(){var captures;if(captures=/^last/.exec(this.str)){this.skip(captures);var d=new Date(this.date.date);var mod=this.peek();if(this.date[mod]){this.next();this.date=date(d);this.date[mod](-1)}else if(rDayMod.test(mod)){this.date.day(-1)}return"last"}};parser.prototype.ago=function(){var captures;if(captures=/^ago\b/.exec(this.str)){this.skip(captures);return"ago"}};parser.prototype.number=function(){var captures;if(captures=/^(\d+)/.exec(this.str)){var n=captures[1];this.skip(captures);var mod=this.peek();if(this.date[mod]){if("ago"==this.peek())n=-n;this.date[mod](n)}else if(this._meridiem){this.time(n,0,0,this._meridiem);this._meridiem=null}else if(this.original.indexOf("at")>-1){this.time(n,0,0,this._meridiem);this._meridiem=null}return"number"}};parser.prototype.string=function(){var captures;if(captures=/^\w+/.exec(this.str)){this.skip(captures);return"string"}};parser.prototype.other=function(){var captures;if(captures=/^./.exec(this.str)){this.skip(captures);return"other"}}},{"./date":1,debug:3}],3:[function(require,module,exports){module.exports=debug;function debug(name){if(!debug.enabled(name))return function(){};return function(fmt){fmt=coerce(fmt);var curr=new Date;var ms=curr-(debug[name]||curr);debug[name]=curr;fmt=name+" "+fmt+" +"+debug.humanize(ms);window.console&&console.log&&Function.prototype.apply.call(console.log,console,arguments)}}debug.names=[];debug.skips=[];debug.enable=function(name){try{localStorage.debug=name}catch(e){}var split=(name||"").split(/[\s,]+/),len=split.length;for(var i=0;i<len;i++){name=split[i].replace("*",".*?");if(name[0]==="-"){debug.skips.push(new RegExp("^"+name.substr(1)+"$"))}else{debug.names.push(new RegExp("^"+name+"$"))}}};debug.disable=function(){debug.enable("")};debug.humanize=function(ms){var sec=1e3,min=60*1e3,hour=60*min;if(ms>=hour)return(ms/hour).toFixed(1)+"h";if(ms>=min)return(ms/min).toFixed(1)+"m";if(ms>=sec)return(ms/sec|0)+"s";return ms+"ms"};debug.enabled=function(name){for(var i=0,len=debug.skips.length;i<len;i++){if(debug.skips[i].test(name)){return false}}for(var i=0,len=debug.names.length;i<len;i++){if(debug.names[i].test(name)){return true}}return false};function coerce(val){if(val instanceof Error)return val.stack||val.message;return val}try{if(window.localStorage)debug.enable(localStorage.debug)}catch(e){}},{}],"date.js":[function(require,module,exports){module.exports=require("./lib/parser")},{"./lib/parser":2}]},{},[]);var d=require("date.js");console.log(d("tomorrow afternoon at 4:30pm 1 month from now"));
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"date.js": "0.2.0"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment