ergogen/src/operation.js

25 lines
715 B
JavaScript
Raw Normal View History

2021-01-01 00:50:04 +01:00
const op_prefix = exports.op_prefix = str => {
2022-05-29 20:25:52 +02:00
const prefix = str[0]
2021-01-01 00:50:04 +01:00
const suffix = str.slice(1)
2022-05-29 20:25:52 +02:00
const result = {name: suffix, operation: 'add'}
if (prefix == '+') ; // noop
else if (prefix == '-') result.operation = 'subtract'
else if (prefix == '~') result.operation = 'intersect'
else if (prefix == '^') result.operation = 'stack'
else result.name = str // no prefix, so the name was the whole string
return result
2021-01-01 00:50:04 +01:00
}
exports.operation = (str, choices={}, order=Object.keys(choices)) => {
let res = op_prefix(str)
for (const key of order) {
if (choices[key].includes(res.name)) {
res.what = key
2021-01-01 00:50:04 +01:00
break
}
}
return res
}