Skip to content

Instantly share code, notes, and snippets.

@lamchau
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save lamchau/83604d8059071c24cf41 to your computer and use it in GitHub Desktop.

Select an option

Save lamchau/83604d8059071c24cf41 to your computer and use it in GitHub Desktop.
fixture pattern matcher
function parseUrl(url) {
url = String(url);
// expects api calls to be "/some/api/v(#)/some/path" with optional query params
// examples: http://regex101.com/r/vO8mU5/1
var regex = /.*\/v(\d+)((?:\/\w+)+)\??(.+)?$/i;
var match = url.match(regex);
console.assert("Invalid URL '" + url + "'", match);
var result = {
"url": url,
"version": +match[1],
"endpoint": match[2],
"params": match[3]
};
console.assert("API version '" + result.version + "' must greater than 0", result.version > 0);
return result;
}
@lamchau
Copy link
Author

lamchau commented Nov 25, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment