Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit 7d4a03f46454eb7c67d5b55c94bd5d5374476ee6
parent b5fb7be281ac7008e25eeecb35b28e63d93f8b3a
Author: knutsen <samuel@knutsen.co>
Date:   Mon, 19 Apr 2021 16:43:49 +0100

Better regex for !metric command + clean up.

Diffstat:
Mlib/commands/metric.ts | 33+++++++++++++++++++++------------
1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/lib/commands/metric.ts b/lib/commands/metric.ts @@ -17,7 +17,8 @@ const UNITS: Conversions = { "yd": n => [0.9144 * n, "m"], "acre": n => [4046.873 * n, "m^2"], "pint": n => [473.176 * n, "ml"], - "quart": n => [0.946 * n, "l"], + "fl": n => [29.573 * n, " ml"], + "quart": n => [0.946 * n, "l"], "gallon": n => [3.785 * n, "l"], "oz" : n => [28.349 * n, "g"], "ounce": n => [28.349 * n, "g"], @@ -25,23 +26,31 @@ const UNITS: Conversions = { "lb": n => [0.453 * n, "kg"] }; -const QUANTITY_REGEX = /[\d.]+(F |F$|\s(mile|inch|feet|foot|ft|ounce|gallon|yard|oz|fl.?.?oz|yd|acre|pint|quart|pound|lb|fahrenheit))/gi; +const QUANTITY_REGEX = /(\d*\.?\d+)\s*(mile|inch|feet|foot|ft|ounce|gallon|yard|oz|fl(?:.?.?oz)|yd|acre|pint|quart|pound|lb|fahrenheit|f)(?:es|s)?\b/gi; export default async (homescope: HomeScope) => { const { message, args } = homescope; - const msg = args.join(" ").match(QUANTITY_REGEX).map(s => { - s = s.toLowerCase(); - if (s[s.length - 1] === "f") { - const c = Math.round(ftoc(Number(s.slice(0, -1)))[0]); - if (c) return s + " = " + c + "C"; - } - if (s.split(" ")[1].startsWith("fl")) { - return s + " = " + Math.round(29.573 * Number(s.split(" ")[0])) + " ml" + const sentence = args.join(" "); + const matches: [string, string][] = []; // Pairs of a quantity with its unit. + + let match = undefined; + while (match = QUANTITY_REGEX.exec(sentence)) + matches.push([match[1], match[2]]); + + const msg = matches.map(pair => { + let [quantity, unit] = [Number(pair[0]), pair[1].toLowerCase()]; + + if (unit === "f") { + const c = ftoc(quantity); + if (c) return `${quantity}${unit} = ${c[0]}${c[1]}`; } - const out = (UNITS[s.split(" ")[1]] || noop)(Number(s.split(" ")[0])); + if (unit.startsWith("fl")) + unit = "fl"; + + const out = (UNITS[unit] || noop)(quantity); if (out && !isNaN(out[0])) { - return `${s} = ${Math.round(out[0] * 10) / 10} ${out[1]}`; + return `${quantity} ${unit} = ${Math.round(out[0] * 10) / 10} ${out[1]}`; } else { return ""; }