SOURCE

// 计数器
function calculator(equation = "", ...args) {
    const computeSign = '[\\+\\-\\*\\/]'
    const numberReg = /\d+/
    const signReg = new RegExp(computeSign)
    const filterLastSignReg = new RegExp(`\\s?${computeSign}\\s?$`)
    const list = equation.split(/\s+/)
    const params = [...args]
    const targetReturn = list.map((k, i) => (signReg.test(k) || numberReg.test(k)) ? k : params.shift())
        .join(' ').replace(filterLastSignReg, '')
    return new Function(`return ${targetReturn}`)()
}

const foo = calculator('a + b * c', 4, 2, 3)
console.log(foo)    // 4 + 2 * 3 = 10

const bar = calculator('a * 2 / c', 10, 8)
console.log(bar)    // 10 * 2 / 8 = 2.5
console 命令行工具 X clear

                    
>
console