Skip to content

Instantly share code, notes, and snippets.

@dariocravero
Created September 27, 2016 16:20
Show Gist options
  • Save dariocravero/ac329c55265053adc988e6f86ecaf8f7 to your computer and use it in GitHub Desktop.
Save dariocravero/ac329c55265053adc988e6f86ecaf8f7 to your computer and use it in GitHub Desktop.
router with dynamic app naming
const cleanPath = s => /\?/.test(s) ? s.match(/(.*?)\?/)[1] : s
const routes = [
'https://my-review-cert.hrw.com/content/selenas-blog--thing?q=1',
'https://my-review-cert.hrw.com/toc',
'https://very-specific.com/toc/super-toc/thing',
'https://my-review-cert.hrw.com/content/selenas-blog--thing/https://my-review-cert.hrw.com/toc',
'https://panels.com/thing/that'
]
const panels = /^https?:\/\/([a-zA-Z0-9\-\_\.]+)()(\/.*)/
const custom = /^https?:\/\/((my-review-cert\.hrw\.com|int\.hmhone\.app\.hmhco\.com|www-review-cert-tc1\.thinkcentral\.com)\/[a-zA-Z0-9\-\_]+)(\/.*)/
const verySpecific = /^https?:\/\/(very-specific\.com\/[a-zA-Z0-9\-\_]+\/[a-zA-Z0-9\-\_]+)()(\/.*)/
const parsers = [
verySpecific,
custom,
panels
]
routes.forEach(r => {
let app
let path
let i = 0
for (i; i < parsers.length; i++) {
result = r.match(parsers[i])
if (result) {
app = result[1]
path = cleanPath(result[3] || '/')
break
}
}
console.log('used parser:', i, '\nroute:', r, '\n app:', app, '\n path:', path, '\n\n')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment