Files
wekan/imports/lib/customNumberFormat.js
T
Lauri OjansivuandClaude Opus 4.8 98315c26cd Fix #2091: custom number field shows NaN when cleared after being set
A number-type Custom Field rendered the literal 'NaN' once its value was cleared.
The submit handler now stores '' instead of NaN for empty input, and a shared
formatNumberValue helper normalizes empty/null/non-numeric values to an empty
string for display. Helper unit-tested.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 19:17:01 +03:00

25 lines
1.1 KiB
JavaScript

// Pure, dependency-free helper for rendering custom-field number values (#2091).
//
// A "number" type Custom Field can be given a value and then cleared (set to
// blank). Clearing the input parses to NaN, and naive rendering then displays
// the literal string "NaN" instead of an empty value. This helper normalizes
// any empty / null / undefined / non-numeric value (including NaN) to an empty
// string, and otherwise returns the numeric value as a string.
//
// Lives under /imports/lib so it is isomorphic (shared by the client card
// detail / minicard renderers) and free of any Meteor/Mongo runtime
// dependency, so it can also be unit tested directly.
export function formatNumberValue(value) {
// Reject blank/empty-ish values up front so '' / null / undefined render empty.
if (value === '' || value === null || value === undefined) {
return '';
}
const num = Number(value);
// Number('') is 0, but we've already excluded ''. Anything that is not a
// finite number (NaN, Infinity, 'abc', ...) renders as empty.
if (!Number.isFinite(num)) {
return '';
}
return String(num);
}