Last active
June 30, 2021 07:36
-
-
Save joeldbirch/c6b05cab0fb813169a153d8a79fefa3c 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
// moustache replacement | |
const soupStrainer = (template, values) => | |
[...template.matchAll(/\{\{([^}]*)\}\}/gi)].reduce( | |
(acc, [full, path]) => | |
acc.replace( | |
full, | |
path.split(`.`).reduce((acc, curr) => acc[curr], values) | |
), | |
template | |
) | |
// your values to swap in | |
const values = { | |
dealership: { | |
name: `Joel's Shonky Dealership`, | |
street: `Shonk Avenue`, | |
}, | |
} | |
// your template string | |
const template = `At {{dealership.name}} located on {{dealership.street}}` | |
// how to use soupStrainer | |
const result = soupStrainer(template, values) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment