mirror of
https://github.com/gethomepage/homepage.git
synced 2025-12-07 09:35:54 -08:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user