Skip to content

Instantly share code, notes, and snippets.

@trygve-lie
Created February 22, 2017 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trygve-lie/4255138406b9b5969ce3c5da7a0711fb to your computer and use it in GitHub Desktop.
Save trygve-lie/4255138406b9b5969ce3c5da7a0711fb to your computer and use it in GitHub Desktop.
uri.matcher.js
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