Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {};
export { convertFromCents, convertToCents, round } from './money';
export { getNestedValue } from './object';
47 changes: 47 additions & 0 deletions packages/core/src/money.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export function round(value: number): number {
return Math.round((value + Number.EPSILON) * 100) / 100;
}

function convertToValidCost(value: string) {
return parseFloat(value.replace(/,/g, ''));
}

export function convertToCents(
amount?: number | string | string[] | null | boolean,
) {
if (
amount == null ||
amount === '' ||
Number.isNaN(amount) ||
Array.isArray(amount) ||
typeof amount === 'boolean'
) {
return null;
}

let validAmount: number;

if (typeof amount === 'string') {
validAmount = convertToValidCost(amount);
} else {
validAmount = amount;
}

return round(validAmount * 100);
}

export function convertFromCents(amount?: number | string | null) {
if (amount == null || Number.isNaN(amount)) {
return null;
}

let normalizedValue: number;

if (typeof amount === 'string') {
normalizedValue = convertToValidCost(amount || '0');
} else {
normalizedValue = amount;
}

return round(normalizedValue / 100);
}
29 changes: 29 additions & 0 deletions packages/core/src/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Gets a nested value from an object using a dot-notation path string.
* Replacement for lodash.get with support for nested property access.
* @param obj - The object to query
* @param path - The path to the property (e.g., 'user.address.city')
* @param defaultValue - The value to return if the resolved value is undefined
* @returns The resolved value or defaultValue
*/
export function getNestedValue<T = unknown>(
obj: unknown,
path: string,
defaultValue?: T,
): T | undefined {
if (obj == null || typeof path !== 'string') {
return defaultValue;
}

const keys = path.split('.');
let result: unknown = obj;

for (const key of keys) {
if (result == null || typeof result !== 'object') {
return defaultValue;
}
result = (result as Record<string, unknown>)[key];
}

return (result === undefined ? defaultValue : result) as T | undefined;
}
60 changes: 11 additions & 49 deletions src/components/form/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
nextMonday,
} from 'date-fns';
import equal from 'fast-deep-equal';
import { getNestedValue } from '@/src/lib/utils';
import {
convertFromCents,
convertToCents,
getNestedValue,
} from '@remoteoss/remote-flows-core';
import { UploadedFileReference } from '@/src/types/fields';

const textInputTypes = {
Expand Down Expand Up @@ -53,54 +57,6 @@ const supportedTypes = {
...checkboxTypes,
} as const;

export function round(value: number): number {
return Math.round((value + Number.EPSILON) * 100) / 100;
}

function convertToValidCost(value: string) {
return parseFloat(value.replace(/,/g, ''));
}

export function convertToCents(
amount?: number | string | string[] | null | boolean,
) {
if (
amount == null ||
amount === '' ||
Number.isNaN(amount) ||
Array.isArray(amount) ||
typeof amount === 'boolean'
) {
return null;
}

let validAmount: number;

if (typeof amount === 'string') {
validAmount = convertToValidCost(amount);
} else {
validAmount = amount;
}

return round(validAmount * 100);
}

export function convertFromCents(amount?: number | string | null) {
if (amount == null || Number.isNaN(amount)) {
return null;
}

let normalizedValue: number;

if (typeof amount === 'string') {
normalizedValue = convertToValidCost(amount || '0');
} else {
normalizedValue = amount;
}

return round(normalizedValue / 100);
}

const trimStringValues = (values: Record<string, $TSFixMe>) =>
Object.entries(values || {}).reduce<Record<string, $TSFixMe>>(
(result, [key, value]) => {
Expand Down Expand Up @@ -1054,3 +1010,9 @@ export function isDateDisabled(
}
return false;
}

export {
convertFromCents,
convertToCents,
round,
} from '@remoteoss/remote-flows-core';
34 changes: 3 additions & 31 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
NormalizedFieldError,
normalizeFieldErrors,
} from '@/src/lib/mutations';
import { convertFromCents } from '@/src/components/form/utils';
import { convertFromCents, getNestedValue } from '@remoteoss/remote-flows-core';

export function cn(...inputs: ClassValue[]) {
return mergeClasses(...inputs);
Expand Down Expand Up @@ -392,36 +392,6 @@ export function handleStepError(
};
}

/**
* Gets a nested value from an object using a dot-notation path string.
* Replacement for lodash.get with support for nested property access.
* @param obj - The object to query
* @param path - The path to the property (e.g., 'user.address.city')
* @param defaultValue - The value to return if the resolved value is undefined
* @returns The resolved value or defaultValue
*/
export function getNestedValue<T = unknown>(
obj: unknown,
path: string,
defaultValue?: T,
): T | undefined {
if (obj == null || typeof path !== 'string') {
return defaultValue;
}

const keys = path.split('.');
let result: unknown = obj;

for (const key of keys) {
if (result == null || typeof result !== 'object') {
return defaultValue;
}
result = (result as Record<string, unknown>)[key];
}

return (result === undefined ? defaultValue : result) as T | undefined;
}

/**
* Sets backend validation errors into react-hook-form state
* Converts backend field paths to react-hook-form paths and sets errors
Expand Down Expand Up @@ -460,3 +430,5 @@ export function setFormFieldErrors(
}
});
}

export { getNestedValue };
Loading