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.

32 lines
954 B

3 years ago
exports.printVersion = () => {
console.log('Staples v0.0.1')
}
exports.compile = (template, input) => {
let index = 0
while(index < template.length) {
const nextOpen = template.indexOf('{{', index + 1)
if(nextOpen == index || nextOpen == -1) {
index = template.length
} else {
index = nextOpen
const nextClose = template.indexOf('}}', index + 1)
if(nextClose == index || nextClose == -1) {
index = template.length
} else {
index = nextOpen+2
const tagContent = template.substring(nextOpen+2, nextClose)
const tagContentSplit = tagContent.split('.')
let varLevel = input
tagContentSplit.forEach( (level) => {
const isIndex = Number.isInteger(parseInt(level))
varLevel = isIndex ? varLevel[parseInt(level)] : varLevel[level]
})
template = template.replace(tagContent, varLevel)
}
}
}
console.log(template)
}