Skip to content

Commit

Permalink
Merge pull request #2 from connectk12:refactor/reorder
Browse files Browse the repository at this point in the history
Refactor/reorder
  • Loading branch information
ngnathan authored Aug 6, 2024
2 parents ac16fe2 + d6f2f34 commit 9d34771
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
23 changes: 17 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parse } from "path";
import type { FormItem } from "./schema";

type SanitizeTextOpts = {
Expand Down Expand Up @@ -64,17 +65,27 @@ export const sanitizeText = <T extends string | (string | undefined)>(
*
* @returns {string | undefined} - The value of the specified field, optionally sanitized.
*/
export const getValueFromField = (
export const getValueFromField = <T extends string | number | undefined>(
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;
};

/**
Expand Down

0 comments on commit 9d34771

Please sign in to comment.