decode-formdata
Advanced tools
Comparing version 0.6.0 to 0.7.0
/** | ||
* Form data entry type. | ||
*/ | ||
type FormDataEntry = { | ||
path: string; | ||
input: File | string; | ||
output: boolean | Date | File | null | number | string | undefined; | ||
}; | ||
/** | ||
* Form data transform type. | ||
*/ | ||
type FormDataTransform = (entry: FormDataEntry) => any; | ||
/** | ||
* Form data info type. | ||
@@ -18,6 +30,17 @@ */ | ||
* @param info The form data info. | ||
* @param transform The value transformation. | ||
* | ||
* @returns The decoded form values. | ||
*/ | ||
declare function decode<TOutput extends Record<string, any> = Record<string, unknown>>(formData: FormData, info?: FormDataInfo): TOutput; | ||
declare function decode<TOutput extends Record<string, any> = Record<string, unknown>>(formData: FormData, info: FormDataInfo, transform?: FormDataTransform): TOutput; | ||
/** | ||
* Decodes the form data entries. Information that is lost during the transfer | ||
* via HTTP can be supplemented. | ||
* | ||
* @param formData The form data object. | ||
* @param transform The value transformation. | ||
* | ||
* @returns The decoded form values. | ||
*/ | ||
declare function decode<TOutput extends Record<string, any> = Record<string, unknown>>(formData: FormData, transform?: FormDataTransform): TOutput; | ||
@@ -65,2 +88,2 @@ /** | ||
export { FormDataInfo, decode, getFieldDate, getFieldValue, getPathObject, getValuePaths }; | ||
export { FormDataEntry, FormDataInfo, FormDataTransform, decode, getFieldDate, getFieldValue, getPathObject, getValuePaths }; |
@@ -120,5 +120,6 @@ // src/regex.ts | ||
// src/decode.ts | ||
function decode(formData, info) { | ||
function decode(formData, arg2, arg3) { | ||
const [info, transform] = typeof arg2 === "function" ? [void 0, arg2] : [arg2, arg3]; | ||
const values = {}; | ||
for (const [path, value] of formData.entries()) { | ||
for (const [path, input] of formData.entries()) { | ||
const templateName = path.replace(/.\d+./g, ".$."); | ||
@@ -134,12 +135,15 @@ const templateKeys = templateName.split("."); | ||
} | ||
if (!info?.files?.includes(templateName) || value && (typeof value === "string" || value.size)) { | ||
const fieldValue = getFieldValue(info, templateName, value); | ||
if (!info?.files?.includes(templateName) || input && (typeof input === "string" || input.size)) { | ||
let output = getFieldValue(info, templateName, input); | ||
if (transform) { | ||
output = transform({ path, input, output }); | ||
} | ||
if (info?.arrays?.includes(templateName)) { | ||
if (object[key]) { | ||
object[key].push(fieldValue); | ||
object[key].push(output); | ||
} else { | ||
object[key] = [fieldValue]; | ||
object[key] = [output]; | ||
} | ||
} else { | ||
object[key] = fieldValue; | ||
object[key] = output; | ||
} | ||
@@ -146,0 +150,0 @@ } |
{ | ||
"name": "decode-formdata", | ||
"description": "Decodes complex FormData into a JavaScript object", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"license": "MIT", | ||
@@ -42,2 +42,11 @@ "author": "Fabian Hiller", | ||
}, | ||
"scripts": { | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage", | ||
"lint": "eslint \"src/**/*.ts*\" && tsc --noEmit", | ||
"format": "prettier --write ./src", | ||
"format.check": "prettier --check ./src", | ||
"build": "tsup", | ||
"publish": "npm publish" | ||
}, | ||
"devDependencies": { | ||
@@ -56,11 +65,3 @@ "@types/eslint": "^8.44.3", | ||
"vitest": "^0.34.6" | ||
}, | ||
"scripts": { | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage", | ||
"lint": "eslint \"src/**/*.ts*\" && tsc --noEmit", | ||
"format": "prettier --write ./src", | ||
"format.check": "prettier --check ./src", | ||
"build": "tsup" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25958
482