Feature: Add size formatter

The `size` formatter returns the length of an array or string, or the
number of keys in an object.

Some APIs do not return a summary of their contents but only the full
list of contents. The `size` formatter can be used to still show such
count on a widget.
This commit is contained in:
Jacobo de Vera
2024-08-26 19:36:53 +01:00
parent 8ff68dd6bf
commit 4318cd6537
2 changed files with 23 additions and 1 deletions

View File

@@ -4,6 +4,16 @@ import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
function getLength(data) {
if ('length' in data) {
return data.length;
} else if (typeof data === 'object' && data !== null) {
return Object.keys(data).length;
} else {
return NaN;
}
}
function getValue(field, data) {
let value = data;
let lastField = field;
@@ -85,6 +95,10 @@ function formatValue(t, mapping, rawValue) {
numeric: mapping?.numeric,
});
break;
case "size":
value = getLength(value);
value = t("common.number", { value });
break;
case "text":
default:
// nothing