fallback finder

node v14.19.3
version: 4.0.0
endpointsharetweet
function findDefaultMerges(content, mergeFields) { const startPattern = '\\{\\{\\s*' const endPattern = '\\s*\\|\\s*([^\}\{\|]+)\\s*\\}\\}' const defaultValues = mergeFields.reduce((acc, key) => { const fallbackFinder = new RegExp(startPattern + '(' + key + ')' + endPattern) const matches = fallbackFinder.exec(sampleText); if (matches && matches.length >= 3) { acc[key] = matches[2].trim() content = content.replace(fallbackFinder, `\{\{ ${key} \}\}`) } return acc }, {}) return { defaultValues, content } }
const mergeFields = ['first_name', 'last_4', 'card_brand'] const sampleText = `Hello {{ first_name | multi-word fall back }} card {{ last_4 | 23094 }} and more.`
findDefaultMerges(sampleText, mergeFields)
Loading…

no comments

    sign in to comment