mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 21:29:59 -05:00
Allow arithmetic expressions in inventory number fields
A user can type a calculation into any numeric inventory field (number, quantity magnitude, calories) and have it evaluated in place — e.g. weigh a 400 g box of nails, enter '400 - 20', and Tab/Enter swaps in 380. The result feeds dependent logic like count-by-weight automatically. - evaluateExpression() (units.js) reuses the scoped mathjs instance, guarded by a strict regex so only bare arithmetic (digits, + - * / ( ) and spaces) is evaluated; anything with a letter, a plain number, or unparseable input is left untouched. Float noise is rounded off. - FieldCell evaluates on blur (Tab/click-away) and on Enter: in a numeric field the first Enter resolves a pending expression, a second submits the row. - NUMERIC_TYPES is exported once from units.js and shared by the table and cell editors. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
305c73921e
commit
e7c737fe0c
5 changed files with 138 additions and 7 deletions
|
|
@ -15,6 +15,10 @@ export const UNIT_GROUPS = [
|
|||
|
||||
export const ALL_UNITS = UNIT_GROUPS.flatMap(g => g.units);
|
||||
|
||||
// Field types whose value is a number the user may type as an arithmetic expression (e.g. "400 - 20"). Shared
|
||||
// so the table editor and cell editor can't drift apart when a new numeric type is added.
|
||||
export const NUMERIC_TYPES = ['number', 'quantity', 'calories'];
|
||||
|
||||
// Compaction ladders (ascending): a summed quantity is displayed in the largest unit on a compatible ladder where
|
||||
// the magnitude is still >= 1 (mirrors the old backend `compact_unit`, e.g. 2000 lb -> 1 ton, 16 oz + 1 lb -> 2 lb).
|
||||
const COMPACTION_LADDERS = [
|
||||
|
|
@ -41,6 +45,34 @@ function toUnit(value, unit) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a simple arithmetic expression typed into a numeric field to its numeric string, so a user can do math
|
||||
* inline — e.g. "400 - 20" -> "380" (weigh a 400 g box of nails, subtract the 20 g box). Only bare arithmetic is
|
||||
* allowed (digits, `. + - * / ( )` and spaces); anything containing a letter is rejected, so there are no function
|
||||
* calls or constants to evaluate. A plain number, a bare negative, or anything unparseable is returned unchanged.
|
||||
* Float noise is rounded off (0.1 + 0.2 -> 0.3).
|
||||
*/
|
||||
export function evaluateExpression(value) {
|
||||
if (typeof value !== 'string') {
|
||||
return value;
|
||||
}
|
||||
const expr = value.trim();
|
||||
// Must look like arithmetic AND contain an operator beyond an optional leading sign (so "380" and "-20" are
|
||||
// left alone rather than needlessly rewritten).
|
||||
if (expr === '' || !/^[\d\s.+\-*/()]+$/.test(expr) || !/[+\-*/]/.test(expr.replace(/^[+-]/, ''))) {
|
||||
return value;
|
||||
}
|
||||
try {
|
||||
const result = math.evaluate(expr);
|
||||
if (typeof result === 'number' && Number.isFinite(result)) {
|
||||
return String(Math.round(result * 1e6) / 1e6);
|
||||
}
|
||||
} catch {
|
||||
// Not a valid expression (e.g. "400-") — leave it as typed.
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// Convert a {value, unit} to another unit. Returns a number, or null when incompatible/unparseable.
|
||||
export function convert(value, fromUnit, toUnitName) {
|
||||
const u = toUnit(value, fromUnit);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue