Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created August 22, 2014 20:12
Show Gist options
  • Save bdkosher/994c6b26f4b8d78ffcd1 to your computer and use it in GitHub Desktop.
Save bdkosher/994c6b26f4b8d78ffcd1 to your computer and use it in GitHub Desktop.
Crude Groovy closure that can take a URI and strip off the query string and fragment, returning just the path part
def extractPathOnly = { path ->
switch (path) {
case '/':
path = 'index.html'
break
case { it.contains('?') }:
path = path.split('\\?')[0]
break
case { it.contains('#') }:
path = path.split('#')[0]
}
path
}
assert extractPathOnly('window.html?foo=bar') == 'window.html'
assert extractPathOnly('window.html?foo=bar#hash') == 'window.html'
assert extractPathOnly('window.html?#hash') == 'window.html'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment