A simple calculator using js expressions

Open js-expression in Script Kit

// Name: JS Expression
// Description: I prefer to define it as a simple calculator
// Global Objects
let arr = [1, 2, 3, 4, 5]
let obj = {name: 'Mike', age: 20}
// Global Functions
let {
ceil, floor, round, trunc, abs, PI,
sin, cos, tan, log, log2, log10, exp, sqrt, cbrt, pow
} = Math
// Factorial
let fact = num => _.reduce(_.range(1, num + 1), (acc, i) => acc * i, 1)
let selected = await arg({
placeholder: 'Expression ...',
enter: 'Copy & Exit',
shortcuts: [{
name: 'Repalce', key: 'cmd+r', bar: 'right', onPress: async (input, {focused}) => {
setInput(evalExp(input))
}
}]
}, async (input) => {
let res = input ? evalExp(input) : ''
return md(`~~~javascript\n${res}\n~~~`)
})
if (selected) await copy(evalExp(selected))
function evalExp(input) {
let value = eval(`(${input})`)
if (typeof value == 'number') return (value % 1 != 0 ? value.toFixed(2) : value) + ''
if (typeof value == 'array') return JSON.stringify(value, null, 2)
if (typeof value == 'object') return JSON.stringify(value, null, 2)
if (typeof value == 'function') return ''
}
image