Support string templating for key-level attributes
This commit is contained in:
parent
6225013828
commit
2b98b502d6
10 changed files with 459 additions and 47 deletions
14
src/utils.js
14
src/utils.js
|
@ -18,6 +18,20 @@ const deep = exports.deep = (obj, key, val) => {
|
|||
return obj
|
||||
}
|
||||
|
||||
exports.template = (str, vals={}) => {
|
||||
const regex = /\{\{([^}]*)\}\}/g
|
||||
let res = str
|
||||
let shift = 0
|
||||
for (const match of str.matchAll(regex)) {
|
||||
const replacement = deep(vals, match[1]) || ''
|
||||
res = res.substring(0, match.index + shift)
|
||||
+ replacement
|
||||
+ res.substring(match.index + match[0].length)
|
||||
shift += replacement.length - match[0].length
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
const eq = exports.eq = (a=[], b=[]) => {
|
||||
return a[0] === b[0] && a[1] === b[1]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue