mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-16 09:43:18 +01:00
add helper functions for tanstack form and cent conversions
This commit is contained in:
@@ -43,6 +43,13 @@ export function formatCents(
|
|||||||
return formatMoney(amount / 100, currency, format, currencySymbol, numberFormat);
|
return formatMoney(amount / 100, currency, format, currencySymbol, numberFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Converts a major-unit amount (e.g. 19.99) to integer hundredths (i.e. cents)
|
||||||
|
*/
|
||||||
|
export function toCents(value: number | undefined | null): number {
|
||||||
|
return Math.round((value || 0) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
export function getOrganizationCurrencySymbol(currency: string) {
|
export function getOrganizationCurrencySymbol(currency: string) {
|
||||||
return (0)
|
return (0)
|
||||||
.toLocaleString('de-DE', {
|
.toLocaleString('de-DE', {
|
||||||
|
|||||||
@@ -23,6 +23,21 @@ export function getApiValidationFieldErrors(error: unknown): Record<string, stri
|
|||||||
return fieldErrors;
|
return fieldErrors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Like getApiValidationFieldErrors, but with Laravel's dot-notation array
|
||||||
|
* indices (entries.0.unit_price) converted to the bracket notation of
|
||||||
|
* TanStack Form field names (entries[0].unit_price), so that the errors
|
||||||
|
* attach to the mounted fields.
|
||||||
|
*/
|
||||||
|
export function getApiValidationFormFieldErrors(error: unknown): Record<string, string> {
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(getApiValidationFieldErrors(error)).map(([field, message]) => [
|
||||||
|
field.replace(/\.(\d+)(?=\.|$)/g, '[$1]'),
|
||||||
|
message,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function getApiValidationMessage(error: unknown, fallback: string): string {
|
export function getApiValidationMessage(error: unknown, fallback: string): string {
|
||||||
if (!isApiValidationError(error)) {
|
if (!isApiValidationError(error)) {
|
||||||
return fallback;
|
return fallback;
|
||||||
|
|||||||
Reference in New Issue
Block a user