diff --git a/package.json b/package.json index ba2ca8b..99afb26 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,14 @@ "release": "pnpm run build && changeset publish", "lint": "tsc" }, + "dependencies": { + "node-fetch": "^3.3.2", + "zod": "^3.23.8" + }, "devDependencies": { "@changesets/cli": "^2.27.7", "@types/node": "^22.0.0", "tsup": "^8.2.3", "typescript": "^5.5.4" - }, - "dependencies": { - "node-fetch": "^3.3.2", - "zod": "^3.23.8" } } \ No newline at end of file diff --git a/src/helpers.ts b/src/helpers.ts index 2c66bae..4e5230d 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,3 +1,4 @@ +import { parse } from "path"; import type { FormItem } from "./schema"; type SanitizeTextOpts = { @@ -64,17 +65,27 @@ export const sanitizeText = ( * * @returns {string | undefined} - The value of the specified field, optionally sanitized. */ -export const getValueFromField = ( +export const getValueFromField = ( form: FormItem, fieldNumber: number, - opts?: { sanitize: boolean; sanitizeOpts?: SanitizeTextOpts } -) => { + opts?: { + sanitize: boolean; + sanitizeOpts?: SanitizeTextOpts; + castValueType?: "string" | "number"; + } +): T => { const field = form.fields.find((field) => field.number === fieldNumber); - if (!field) return; + if (!field) return undefined as T; + let value: string | number = field.value; if (opts?.sanitize) { - return sanitizeText(field.value); + value = sanitizeText(field.value); + return value as T; + } + if (field.value && opts?.castValueType === "number") { + value = parseFloat(field.value); + return value as T; } - return field.value; + return value as T; }; /**