export function parse(format: string, data: object): string {
const tokens = [] as tokenProps[]
let position = 0
let alltext = ''
let text = ''
while (position < format.length) {
let char = format[position++]
if (char === '{') {
if (text) {
tokens.push({
value: text,
type: 'text',
})
}
text = ''
let sub = ''
char = format[position++]
while (char !== undefined && char !== '}') {
sub += char
char = format[position++]
}
tokens.push({
value: sub,
type: 'key',
})
} else {
text += char
}
}
text &&
tokens.push({
type: 'text',
value: text,
})
tokens.forEach((res) => {
if (res.type === 'text') {
alltext += res.value
} else {
alltext += data[res.value]
}
})
return alltext
}
const SET_IR_ONOROFF = 'SET IR {onoroff}'
parse(SET_IR_ONOROFF,{onoroff:'OFF'}) // 调用
字符串插值函数记录
于 2023-08-01 17:44:11 首次发布