Skip to content

Instantly share code, notes, and snippets.

@jdeagle
Created November 28, 2014 18:03
Show Gist options
  • Save jdeagle/3bff5106f9f13a6bdd91 to your computer and use it in GitHub Desktop.
Save jdeagle/3bff5106f9f13a6bdd91 to your computer and use it in GitHub Desktop.
requirebin sketch
// example using the raf module from npm. try changing some values!
var requestAnimationFrame = require("raf")
var canvas = document.createElement("canvas")
var Router = require('routes');
var router = Router();
var callback = function () {
console.log("woot");
};
router.addRoute("/articles/:title?", callback);
router.addRoute("/:controller/:action/:id.:format?", callback);
console.log(router.match("/articles"));
console.log(router.match("/articles/never-gonna-let-you-down"));
console.log(router.match("/posts/show/1.json"));
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}({routes:[function(require,module,exports){(function(global){!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.routes=e()}}(function(){var define,module,exports;return 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);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.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(_dereq_,module,exports){var localRoutes=[];var Route=function(path){var src,re,keys=[];if(path instanceof RegExp){re=path;src=path.toString()}else{re=pathToRegExp(path,keys);src=path}return{re:re,src:path.toString(),keys:keys}};var pathToRegExp=function(path,keys){path=path.concat("/?").replace(/\/\(/g,"(?:/").replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?|\*/g,function(_,slash,format,key,capture,optional){if(_==="*"){keys.push(undefined);return _}keys.push(key);slash=slash||"";return""+(optional?"":slash)+"(?:"+(optional?slash:"")+(format||"")+(capture||"([^/]+?)")+")"+(optional||"")}).replace(/([\/.])/g,"\\$1").replace(/\*/g,"(.*)");return new RegExp("^"+path+"$","i")};var match=function(routes,uri,startAt){var captures,i=startAt||0;for(var len=routes.length;i<len;++i){var route=routes[i],re=route.re,keys=route.keys,splats=[],params={};if(captures=uri.match(re)){for(var j=1,len=captures.length;j<len;++j){var key=keys[j-1],val=typeof captures[j]==="string"?unescape(captures[j]):captures[j];if(key){params[key]=val}else{splats.push(val)}}return{params:params,splats:splats,route:route.src,next:i+1}}}};var Router=function(){return{routes:[],routeMap:{},addRoute:function(path,fn){if(!path)throw new Error(" route requires a path");if(!fn)throw new Error(" route "+path.toString()+" requires a callback");if(this.routeMap[path]){throw new Error("path is already defined: "+path)}var route=Route(path);route.fn=fn;this.routes.push(route);this.routeMap[path]=fn},match:function(pathname,startAt){var route=match(this.routes,pathname,startAt);if(route){route.fn=this.routeMap[route.route];route.next=this.match.bind(this,pathname,route.next)}return route}}};Router.Route=Route;Router.pathToRegExp=pathToRegExp;Router.match=match;Router.Router=Router;module.exports=Router},{}]},{},[1])(1)})}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}]},{},[]);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 process=module.exports={};process.nextTick=function(){var canSetImmediate=typeof window!=="undefined"&&window.setImmediate;var canPost=typeof window!=="undefined"&&window.postMessage&&window.addEventListener;if(canSetImmediate){return function(f){return window.setImmediate(f)}}if(canPost){var queue=[];window.addEventListener("message",function(ev){var source=ev.source;if((source===window||source===null)&&ev.data==="process-tick"){ev.stopPropagation();if(queue.length>0){var fn=queue.shift();fn()}}},true);return function nextTick(fn){queue.push(fn);window.postMessage("process-tick","*")}}return function nextTick(fn){setTimeout(fn,0)}}();process.title="browser";process.browser=true;process.env={};process.argv=[];function noop(){}process.on=noop;process.addListener=noop;process.once=noop;process.off=noop;process.removeListener=noop;process.removeAllListeners=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")}},{}],2:[function(require,module,exports){(function(process){(function(){var getNanoSeconds,hrtime,loadTime;if(typeof performance!=="undefined"&&performance!==null&&performance.now){module.exports=function(){return performance.now()}}else if(typeof process!=="undefined"&&process!==null&&process.hrtime){module.exports=function(){return(getNanoSeconds()-loadTime)/1e6};hrtime=process.hrtime;getNanoSeconds=function(){var hr;hr=hrtime();return hr[0]*1e9+hr[1]};loadTime=getNanoSeconds()}else if(Date.now){module.exports=function(){return Date.now()-loadTime};loadTime=Date.now()}else{module.exports=function(){return(new Date).getTime()-loadTime};loadTime=(new Date).getTime()}}).call(this)}).call(this,require("_process"))},{_process:1}],raf:[function(require,module,exports){var now=require("performance-now"),global=typeof window==="undefined"?{}:window,vendors=["moz","webkit"],suffix="AnimationFrame",raf=global["request"+suffix],caf=global["cancel"+suffix]||global["cancelRequest"+suffix],isNative=true;for(var i=0;i<vendors.length&&!raf;i++){raf=global[vendors[i]+"Request"+suffix];caf=global[vendors[i]+"Cancel"+suffix]||global[vendors[i]+"CancelRequest"+suffix]}if(!raf||!caf){isNative=false;var last=0,id=0,queue=[],frameDuration=1e3/60;raf=function(callback){if(queue.length===0){var _now=now(),next=Math.max(0,frameDuration-(_now-last));last=next+_now;setTimeout(function(){var cp=queue.slice(0);queue.length=0;for(var i=0;i<cp.length;i++){if(!cp[i].cancelled){try{cp[i].callback(last)}catch(e){setTimeout(function(){throw e},0)}}}},Math.round(next))}queue.push({handle:++id,callback:callback,cancelled:false});return id};caf=function(handle){for(var i=0;i<queue.length;i++){if(queue[i].handle===handle){queue[i].cancelled=true}}}}module.exports=function(fn){if(!isNative){return raf.call(global,fn)}return raf.call(global,function(){try{fn.apply(this,arguments)}catch(e){setTimeout(function(){throw e},0)}})};module.exports.cancel=function(){caf.apply(global,arguments)}},{"performance-now":2}]},{},[]);var requestAnimationFrame=require("raf");var canvas=document.createElement("canvas");var Router=require("routes");var router=Router();var callback=function(){console.log("woot")};router.addRoute("/articles/:title?",callback);router.addRoute("/:controller/:action/:id.:format?",callback);console.log(router.match("/articles"));console.log(router.match("/articles/never-gonna-let-you-down"));console.log(router.match("/posts/show/1.json"));
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"routes": "2.0.0",
"raf": "2.0.4"
}
}
<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