Skip to content

Instantly share code, notes, and snippets.

@nim23
Created August 10, 2016 15:55
Show Gist options
  • Save nim23/6d1e06c0eca36efff8aeef85306dfcfb to your computer and use it in GitHub Desktop.
Save nim23/6d1e06c0eca36efff8aeef85306dfcfb to your computer and use it in GitHub Desktop.
requirebin sketch
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
const p = require('path-to-regexp')
console.log(p('/beta/search(?:([^\/]+?))', []))
setTimeout(function(){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){module.exports=Array.isArray||function(arr){return Object.prototype.toString.call(arr)=="[object Array]"}},{}],"path-to-regexp":[function(require,module,exports){var isarray=require("isarray");module.exports=pathToRegexp;module.exports.parse=parse;module.exports.compile=compile;module.exports.tokensToFunction=tokensToFunction;module.exports.tokensToRegExp=tokensToRegExp;var PATH_REGEXP=new RegExp(["(\\\\.)","([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))"].join("|"),"g");function parse(str){var tokens=[];var key=0;var index=0;var path="";var res;while((res=PATH_REGEXP.exec(str))!=null){var m=res[0];var escaped=res[1];var offset=res.index;path+=str.slice(index,offset);index=offset+m.length;if(escaped){path+=escaped[1];continue}var next=str[index];var prefix=res[2];var name=res[3];var capture=res[4];var group=res[5];var modifier=res[6];var asterisk=res[7];if(path){tokens.push(path);path=""}var partial=prefix!=null&&next!=null&&next!==prefix;var repeat=modifier==="+"||modifier==="*";var optional=modifier==="?"||modifier==="*";var delimiter=res[2]||"/";var pattern=capture||group||(asterisk?".*":"[^"+delimiter+"]+?");tokens.push({name:name||key++,prefix:prefix||"",delimiter:delimiter,optional:optional,repeat:repeat,partial:partial,asterisk:!!asterisk,pattern:escapeGroup(pattern)})}if(index<str.length){path+=str.substr(index)}if(path){tokens.push(path)}return tokens}function compile(str){return tokensToFunction(parse(str))}function encodeURIComponentPretty(str){return encodeURI(str).replace(/[\/?#]/g,function(c){return"%"+c.charCodeAt(0).toString(16).toUpperCase()})}function encodeAsterisk(str){return encodeURI(str).replace(/[?#]/g,function(c){return"%"+c.charCodeAt(0).toString(16).toUpperCase()})}function tokensToFunction(tokens){var matches=new Array(tokens.length);for(var i=0;i<tokens.length;i++){if(typeof tokens[i]==="object"){matches[i]=new RegExp("^(?:"+tokens[i].pattern+")$")}}return function(obj,opts){var path="";var data=obj||{};var options=opts||{};var encode=options.pretty?encodeURIComponentPretty:encodeURIComponent;for(var i=0;i<tokens.length;i++){var token=tokens[i];if(typeof token==="string"){path+=token;continue}var value=data[token.name];var segment;if(value==null){if(token.optional){if(token.partial){path+=token.prefix}continue}else{throw new TypeError('Expected "'+token.name+'" to be defined')}}if(isarray(value)){if(!token.repeat){throw new TypeError('Expected "'+token.name+'" to not repeat, but received `'+JSON.stringify(value)+"`")}if(value.length===0){if(token.optional){continue}else{throw new TypeError('Expected "'+token.name+'" to not be empty')}}for(var j=0;j<value.length;j++){segment=encode(value[j]);if(!matches[i].test(segment)){throw new TypeError('Expected all "'+token.name+'" to match "'+token.pattern+'", but received `'+JSON.stringify(segment)+"`")}path+=(j===0?token.prefix:token.delimiter)+segment}continue}segment=token.asterisk?encodeAsterisk(value):encode(value);if(!matches[i].test(segment)){throw new TypeError('Expected "'+token.name+'" to match "'+token.pattern+'", but received "'+segment+'"')}path+=token.prefix+segment}return path}}function escapeString(str){return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g,"\\$1")}function escapeGroup(group){return group.replace(/([=!:$\/()])/g,"\\$1")}function attachKeys(re,keys){re.keys=keys;return re}function flags(options){return options.sensitive?"":"i"}function regexpToRegexp(path,keys){var groups=path.source.match(/\((?!\?)/g);if(groups){for(var i=0;i<groups.length;i++){keys.push({name:i,prefix:null,delimiter:null,optional:false,repeat:false,partial:false,asterisk:false,pattern:null})}}return attachKeys(path,keys)}function arrayToRegexp(path,keys,options){var parts=[];for(var i=0;i<path.length;i++){parts.push(pathToRegexp(path[i],keys,options).source)}var regexp=new RegExp("(?:"+parts.join("|")+")",flags(options));return attachKeys(regexp,keys)}function stringToRegexp(path,keys,options){var tokens=parse(path);var re=tokensToRegExp(tokens,options);for(var i=0;i<tokens.length;i++){if(typeof tokens[i]!=="string"){keys.push(tokens[i])}}return attachKeys(re,keys)}function tokensToRegExp(tokens,options){options=options||{};var strict=options.strict;var end=options.end!==false;var route="";var lastToken=tokens[tokens.length-1];var endsWithSlash=typeof lastToken==="string"&&/\/$/.test(lastToken);for(var i=0;i<tokens.length;i++){var token=tokens[i];if(typeof token==="string"){route+=escapeString(token)}else{var prefix=escapeString(token.prefix);var capture="(?:"+token.pattern+")";if(token.repeat){capture+="(?:"+prefix+capture+")*"}if(token.optional){if(!token.partial){capture="(?:"+prefix+"("+capture+"))?"}else{capture=prefix+"("+capture+")?"}}else{capture=prefix+"("+capture+")"}route+=capture}}if(!strict){route=(endsWithSlash?route.slice(0,-2):route)+"(?:\\/(?=$))?"}if(end){route+="$"}else{route+=strict&&endsWithSlash?"":"(?=\\/|$)"}return new RegExp("^"+route,flags(options))}function pathToRegexp(path,keys,options){keys=keys||[];if(!isarray(keys)){options=keys;keys=[]}else if(!options){options={}}if(path instanceof RegExp){return regexpToRegexp(path,keys)}if(isarray(path)){return arrayToRegexp(path,keys,options)}return stringToRegexp(path,keys,options)}},{isarray:1}]},{},[]);const p=require("path-to-regexp");console.log(p("/beta/search(?:([^/]+?))",[]))},0);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"path-to-regexp": "1.5.3"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- 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