Last active
August 29, 2015 14:10
-
-
Save lamchau/83604d8059071c24cf41 to your computer and use it in GitHub Desktop.
fixture pattern matcher
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
| 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; | |
| } |
Author
lamchau
commented
Nov 25, 2014
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment