-
-
Save trygve-lie/4255138406b9b5969ce3c5da7a0711fb to your computer and use it in GitHub Desktop.
uri.matcher.js
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
const toUri = (strings, ...keys) => { | |
return (source, regx) => { | |
const values = source.match(regx).splice(1); | |
const dict = values[values.length - 1] || {}; | |
return [strings[0]].concat(keys.map((key, index) => { | |
const value = Number.isInteger(key) ? values[key] : dict[key]; | |
return `${value}${strings[index + 1]}` | |
})).join(''); | |
}; | |
} | |
const input = '/some-magic/path/12122211/22112211'; | |
const match = /([1-9]{8})\/([1-9]{8})/i; | |
const parser = toUri`http://www.${0}.bar.no/${1}/${0}/`; | |
console.log(parser(input, match)); // http://www.12122211.bar.no/22112211/12122211/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment