Last active
December 7, 2020 15:25
-
-
Save leemason/50056e4ac4ca10ac27088464c21ded27 to your computer and use it in GitHub Desktop.
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
// Existing solution (result not implementation) | |
const existingWay = theUrlConstructor([ '/api/v1/resource/{0}/{1}', 'my string, with unsafe', 'url +characters+' ]) | |
// /api/v1/resource/my%20string%2C%20with%20unsafe/url%20%2Bcharacters%2B | |
// New solution | |
const url = (strings, ...values) => { | |
return strings.reduce((compiled, string, index) => { | |
return compiled.concat(string, encodeURIComponent(values[index] || '')) | |
}, []).join('') | |
} | |
const first = 'my string, with unsafe' | |
const second = 'url +characters+' | |
const newWay = url`/api/v1/resource/${first}/${second}` | |
// /api/v1/resource/my%20string%2C%20with%20unsafe/url%20%2Bcharacters%2B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment