made with requirebin
Created
June 2, 2015 14:17
-
-
Save joshski/d1e4980663a0c84fcfa5 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var routism = require('routism'); | |
var routes = routism.compile([{ pattern: '/fruit/:name', route: 'fruit' }]); | |
var path = '/fruit/granny+smith'; | |
document.body.innerHTML = JSON.stringify(routes.recognise(path)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}({routism:[function(require,module,exports){(function(){var self=this;var variableRegex,splatVariableRegex,escapeRegex,addGroupForTo,addVariablesInTo,compile,recogniseIn,extractParamsForFromAfter;variableRegex=/(\:([a-z\-_]+))/gi;splatVariableRegex=/(\:([a-z\-_]+)\\\*)/gi;escapeRegex=function(pattern){return pattern.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")};exports.table=function(){var self=this;var rows;rows=[];return{add:function(pattern,route){var self=this;return rows.push({pattern:pattern,route:route})},compile:function(){var self=this;return exports.compile(rows)}}};exports.compile=function(routeTable){var self=this;var groups,regexen,gen1_items,gen2_i,row;groups=[];regexen=[];gen1_items=routeTable;for(gen2_i=0;gen2_i<gen1_items.length;++gen2_i){row=gen1_items[gen2_i];addGroupForTo(row,groups);regexen.push("("+compile(row.pattern)+")")}return{regex:new RegExp("^("+regexen.join("|")+")$"),groups:groups,recognise:function(input){var self=this;return recogniseIn(self.regex.exec(input)||[],self.groups)}}};addGroupForTo=function(row,groups){var group;group={route:row.route,params:[]};groups.push(group);return addVariablesInTo(row.pattern,group)};addVariablesInTo=function(pattern,group){var match;while(match=variableRegex.exec(pattern)){group.params.push(match[2])}return void 0};compile=function(pattern){return escapeRegex(pattern).replace(splatVariableRegex,"(.+)").replace(variableRegex,"([^\\/]+)")};recogniseIn=function(match,groups){var g,i,gen3_forResult;g=0;for(i=2;i<match.length;i=i+groups[g-1].params.length+1){gen3_forResult=void 0;if(function(i){if(typeof match[i]!=="undefined"){gen3_forResult={route:groups[g].route,params:extractParamsForFromAfter(groups[g],match,i)};return true}g=g+1}(i)){return gen3_forResult}}return false};extractParamsForFromAfter=function(group,match,i){var params,p;params=[];for(p=0;p<group.params.length;p=p+1){params.push([group.params[p],match[p+i+1]])}return params}}).call(this)},{}]},{},[]);var routism=require("routism");var routes=routism.compile([{pattern:"/fruit/:name",route:"fruit"}]);var path="/fruit/granny+smith";document.body.innerHTML=JSON.stringify(routes.recognise(path)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"routism": "1.0.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment