You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
523 B
26 lines
523 B
2 years ago
|
const assert = require('assert')
|
||
|
|
||
|
module.exports = maxstache
|
||
|
|
||
|
// Minimalist mustache template replacement
|
||
|
// (str, obj) -> null
|
||
|
function maxstache (str, ctx) {
|
||
|
ctx = ctx || {}
|
||
|
|
||
|
assert.equal(typeof str, 'string')
|
||
|
assert.equal(typeof ctx, 'object')
|
||
|
|
||
|
const tokens = str.split(/\{\{|\}\}/)
|
||
|
const res = tokens.map(parse(ctx))
|
||
|
return res.join('')
|
||
|
}
|
||
|
|
||
|
// parse a token
|
||
|
// obj -> (str, num) -> str
|
||
|
function parse (ctx) {
|
||
|
return function parse (token, i) {
|
||
|
if (i % 2 === 0) return token
|
||
|
return ctx[token]
|
||
|
}
|
||
|
}
|