better type signature for undefinedIfEmpty
This commit is contained in:
parent
80ab8ccce9
commit
a69e559890
@ -22,6 +22,7 @@ import { h, VNode, Fragment } from "preact";
|
|||||||
import { useCallback, useState } from "preact/hooks";
|
import { useCallback, useState } from "preact/hooks";
|
||||||
import { Translate, Translator, useTranslator } from "../../i18n/index.js";
|
import { Translate, Translator, useTranslator } from "../../i18n/index.js";
|
||||||
import { COUNTRY_TABLE } from "../../utils/constants.js";
|
import { COUNTRY_TABLE } from "../../utils/constants.js";
|
||||||
|
import { undefinedIfEmpty } from "../../utils/table.js";
|
||||||
import { FormErrors, FormProvider } from "./FormProvider.js";
|
import { FormErrors, FormProvider } from "./FormProvider.js";
|
||||||
import { Input } from "./Input.js";
|
import { Input } from "./Input.js";
|
||||||
import { InputGroup } from "./InputGroup.js";
|
import { InputGroup } from "./InputGroup.js";
|
||||||
@ -154,12 +155,6 @@ const targets = [
|
|||||||
const noTargetValue = targets[0];
|
const noTargetValue = targets[0];
|
||||||
const defaultTarget = { target: noTargetValue, options: {} };
|
const defaultTarget = { target: noTargetValue, options: {} };
|
||||||
|
|
||||||
function undefinedIfEmpty<T>(obj: T): T | undefined {
|
|
||||||
return Object.keys(obj).some((k) => (obj as any)[k] !== undefined)
|
|
||||||
? obj
|
|
||||||
: undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function InputPaytoForm<T>({
|
export function InputPaytoForm<T>({
|
||||||
name,
|
name,
|
||||||
readonly,
|
readonly,
|
||||||
|
@ -33,6 +33,7 @@ import { Translate, useTranslator } from "../../../i18n/index.js";
|
|||||||
import { DefaultInstanceFormFields } from "../../../components/instance/DefaultInstanceFormFields.js";
|
import { DefaultInstanceFormFields } from "../../../components/instance/DefaultInstanceFormFields.js";
|
||||||
import { INSTANCE_ID_REGEX, PAYTO_REGEX } from "../../../utils/constants.js";
|
import { INSTANCE_ID_REGEX, PAYTO_REGEX } from "../../../utils/constants.js";
|
||||||
import { Amounts } from "@gnu-taler/taler-util";
|
import { Amounts } from "@gnu-taler/taler-util";
|
||||||
|
import { undefinedIfEmpty } from "../../../utils/table.js";
|
||||||
|
|
||||||
export type Entity = MerchantBackend.Instances.InstanceConfigurationMessage & {
|
export type Entity = MerchantBackend.Instances.InstanceConfigurationMessage & {
|
||||||
auth_token?: string;
|
auth_token?: string;
|
||||||
@ -54,12 +55,6 @@ function with_defaults(id?: string): Partial<Entity> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function undefinedIfEmpty<T>(obj: T): T | undefined {
|
|
||||||
return Object.keys(obj).some((k) => (obj as any)[k] !== undefined)
|
|
||||||
? obj
|
|
||||||
: undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function CreatePage({ onCreate, onBack, forceId }: Props): VNode {
|
export function CreatePage({ onCreate, onBack, forceId }: Props): VNode {
|
||||||
const [value, valueHandler] = useState(with_defaults(forceId));
|
const [value, valueHandler] = useState(with_defaults(forceId));
|
||||||
const [isTokenSet, updateIsTokenSet] = useState<boolean>(false);
|
const [isTokenSet, updateIsTokenSet] = useState<boolean>(false);
|
||||||
|
@ -42,6 +42,7 @@ import { InventoryProductForm } from "../../../../components/product/InventoryPr
|
|||||||
import { NonInventoryProductFrom } from "../../../../components/product/NonInventoryProductForm.js";
|
import { NonInventoryProductFrom } from "../../../../components/product/NonInventoryProductForm.js";
|
||||||
import { InputNumber } from "../../../../components/form/InputNumber.js";
|
import { InputNumber } from "../../../../components/form/InputNumber.js";
|
||||||
import { InputBoolean } from "../../../../components/form/InputBoolean.js";
|
import { InputBoolean } from "../../../../components/form/InputBoolean.js";
|
||||||
|
import { undefinedIfEmpty } from "../../../../utils/table.js";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
onCreate: (d: MerchantBackend.Orders.PostOrderRequest) => void;
|
onCreate: (d: MerchantBackend.Orders.PostOrderRequest) => void;
|
||||||
@ -126,12 +127,6 @@ const stringIsValidJSON = (value: string) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function undefinedIfEmpty<T>(obj: T): T | undefined {
|
|
||||||
return Object.keys(obj).some((k) => (obj as any)[k] !== undefined)
|
|
||||||
? obj
|
|
||||||
: undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function CreatePage({
|
export function CreatePage({
|
||||||
onCreate,
|
onCreate,
|
||||||
onBack,
|
onBack,
|
||||||
|
@ -34,6 +34,7 @@ import { Translate, useTranslator } from "../../../i18n/index.js";
|
|||||||
import { DefaultInstanceFormFields } from "../../../components/instance/DefaultInstanceFormFields.js";
|
import { DefaultInstanceFormFields } from "../../../components/instance/DefaultInstanceFormFields.js";
|
||||||
import { PAYTO_REGEX } from "../../../utils/constants.js";
|
import { PAYTO_REGEX } from "../../../utils/constants.js";
|
||||||
import { Amounts } from "@gnu-taler/taler-util";
|
import { Amounts } from "@gnu-taler/taler-util";
|
||||||
|
import { undefinedIfEmpty } from "../../../utils/table.js";
|
||||||
|
|
||||||
type Entity = MerchantBackend.Instances.InstanceReconfigurationMessage & {
|
type Entity = MerchantBackend.Instances.InstanceReconfigurationMessage & {
|
||||||
auth_token?: string;
|
auth_token?: string;
|
||||||
@ -70,12 +71,6 @@ function getTokenValuePart(t?: string): string | undefined {
|
|||||||
return match[1];
|
return match[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
function undefinedIfEmpty<T>(obj: T): T | undefined {
|
|
||||||
return Object.keys(obj).some((k) => (obj as any)[k] !== undefined)
|
|
||||||
? obj
|
|
||||||
: undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function UpdatePage({
|
export function UpdatePage({
|
||||||
onUpdate,
|
onUpdate,
|
||||||
onChangeAuth,
|
onChangeAuth,
|
||||||
|
@ -35,3 +35,19 @@ export function buildActions<T extends WithId>(instances: T[], selected: string[
|
|||||||
.filter(notEmpty)
|
.filter(notEmpty)
|
||||||
.map(id => ({ element: id, type: action }))
|
.map(id => ({ element: id, type: action }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For any object or array, return the same object if is not empty.
|
||||||
|
* not empty:
|
||||||
|
* - for arrays: at least one element not undefined
|
||||||
|
* - for objects: at least one property not undefined
|
||||||
|
* @param obj
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export function undefinedIfEmpty<T extends Record<string, unknown>|Array<unknown>>(obj: T): T | undefined {
|
||||||
|
if (obj === undefined) return undefined
|
||||||
|
return Object.values(obj).some((v) => v !== undefined)
|
||||||
|
? obj
|
||||||
|
: undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user