decode-formdata
Advanced tools
+22
-16
@@ -21,4 +21,4 @@ "use strict"; | ||
| // src/index.ts | ||
| var src_exports = {}; | ||
| __export(src_exports, { | ||
| var index_exports = {}; | ||
| __export(index_exports, { | ||
| decode: () => decode, | ||
@@ -30,7 +30,7 @@ getFieldDate: () => getFieldDate, | ||
| }); | ||
| module.exports = __toCommonJS(src_exports); | ||
| module.exports = __toCommonJS(index_exports); | ||
| // src/regex.ts | ||
| var DIGIT_REGEX = /^\d+$/u; | ||
| var NUMBER_REGEX = /^-?\d*(\.\d+)?$/u; | ||
| var NUMBER_REGEX = /^-?\d*(?:\.\d+)?$/u; | ||
| var ISO_DATE_REGEX = /^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])$/u; | ||
@@ -173,12 +173,18 @@ var ISO_DATE_TIME_REGEX = /^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])T(?:0\d|1\d|2[0-3]):[0-5]\d$/u; | ||
| const templateName = normlizedPath.replace(/\.\d+\./g, ".$.").replace(/\.\d+$/, ".$"); | ||
| const keys = normlizedPath.split("."); | ||
| const templateKeys = templateName.split("."); | ||
| normlizedPath.split(".").reduce((object, key, index, keys) => { | ||
| let current = values; | ||
| for (let index = 0; index < keys.length; index++) { | ||
| const key = keys[index]; | ||
| if (key === "__proto__" || key === "prototype" || key === "constructor") { | ||
| break; | ||
| } | ||
| if (index < keys.length - 1) { | ||
| if (object[key]) { | ||
| return object[key]; | ||
| if (current[key]) { | ||
| current = current[key]; | ||
| } else { | ||
| const isArray = index < keys.length - 2 ? templateKeys[index + 1] === "$" : info?.arrays?.includes(templateKeys.slice(0, -1).join(".")); | ||
| current = current[key] = isArray ? [] : {}; | ||
| } | ||
| const isArray = index < keys.length - 2 ? templateKeys[index + 1] === "$" : info?.arrays?.includes(templateKeys.slice(0, -1).join(".")); | ||
| return object[key] = isArray ? [] : {}; | ||
| } | ||
| if (!info?.files?.includes(templateName) || input && (typeof input === "string" || input.size)) { | ||
| } else if (!info?.files?.includes(templateName) || input && (typeof input === "string" || input.size)) { | ||
| let output = getFieldValue(info, templateName, input); | ||
@@ -189,12 +195,12 @@ if (transform) { | ||
| if (info?.arrays?.includes(templateName)) { | ||
| if (object[key]) { | ||
| object[key].push(output); | ||
| if (current[key]) { | ||
| current[key].push(output); | ||
| } else { | ||
| object[key] = [output]; | ||
| current[key] = [output]; | ||
| } | ||
| } else { | ||
| object[key] = output; | ||
| current[key] = output; | ||
| } | ||
| } | ||
| }, values); | ||
| } | ||
| } | ||
@@ -201,0 +207,0 @@ if (info?.arrays) { |
+11
-11
| /** | ||
| * Form data entry type. | ||
| */ | ||
| type FormDataEntry = { | ||
| interface FormDataEntry { | ||
| path: string; | ||
| input: File | string; | ||
| output: boolean | Date | File | null | number | string | undefined; | ||
| }; | ||
| } | ||
| /** | ||
@@ -16,9 +16,9 @@ * Form data transform type. | ||
| */ | ||
| type FormDataInfo = Partial<{ | ||
| arrays: string[]; | ||
| booleans: string[]; | ||
| dates: string[]; | ||
| files: string[]; | ||
| numbers: string[]; | ||
| }>; | ||
| interface FormDataInfo { | ||
| arrays?: string[]; | ||
| booleans?: string[]; | ||
| dates?: string[]; | ||
| files?: string[]; | ||
| numbers?: string[]; | ||
| } | ||
@@ -59,5 +59,5 @@ /** | ||
| * | ||
| * @param info The form data info. | ||
| * @param templateName The template name. | ||
| * @param value The field value. | ||
| * @param info The form data info. | ||
| * | ||
@@ -89,2 +89,2 @@ * @returns The decoded value. | ||
| export { FormDataEntry, FormDataInfo, FormDataTransform, decode, getFieldDate, getFieldValue, getPathObject, getValuePaths }; | ||
| export { type FormDataEntry, type FormDataInfo, type FormDataTransform, decode, getFieldDate, getFieldValue, getPathObject, getValuePaths }; |
+11
-11
| /** | ||
| * Form data entry type. | ||
| */ | ||
| type FormDataEntry = { | ||
| interface FormDataEntry { | ||
| path: string; | ||
| input: File | string; | ||
| output: boolean | Date | File | null | number | string | undefined; | ||
| }; | ||
| } | ||
| /** | ||
@@ -16,9 +16,9 @@ * Form data transform type. | ||
| */ | ||
| type FormDataInfo = Partial<{ | ||
| arrays: string[]; | ||
| booleans: string[]; | ||
| dates: string[]; | ||
| files: string[]; | ||
| numbers: string[]; | ||
| }>; | ||
| interface FormDataInfo { | ||
| arrays?: string[]; | ||
| booleans?: string[]; | ||
| dates?: string[]; | ||
| files?: string[]; | ||
| numbers?: string[]; | ||
| } | ||
@@ -59,5 +59,5 @@ /** | ||
| * | ||
| * @param info The form data info. | ||
| * @param templateName The template name. | ||
| * @param value The field value. | ||
| * @param info The form data info. | ||
| * | ||
@@ -89,2 +89,2 @@ * @returns The decoded value. | ||
| export { FormDataEntry, FormDataInfo, FormDataTransform, decode, getFieldDate, getFieldValue, getPathObject, getValuePaths }; | ||
| export { type FormDataEntry, type FormDataInfo, type FormDataTransform, decode, getFieldDate, getFieldValue, getPathObject, getValuePaths }; |
+19
-13
| // src/regex.ts | ||
| var DIGIT_REGEX = /^\d+$/u; | ||
| var NUMBER_REGEX = /^-?\d*(\.\d+)?$/u; | ||
| var NUMBER_REGEX = /^-?\d*(?:\.\d+)?$/u; | ||
| var ISO_DATE_REGEX = /^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])$/u; | ||
@@ -141,12 +141,18 @@ var ISO_DATE_TIME_REGEX = /^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])T(?:0\d|1\d|2[0-3]):[0-5]\d$/u; | ||
| const templateName = normlizedPath.replace(/\.\d+\./g, ".$.").replace(/\.\d+$/, ".$"); | ||
| const keys = normlizedPath.split("."); | ||
| const templateKeys = templateName.split("."); | ||
| normlizedPath.split(".").reduce((object, key, index, keys) => { | ||
| let current = values; | ||
| for (let index = 0; index < keys.length; index++) { | ||
| const key = keys[index]; | ||
| if (key === "__proto__" || key === "prototype" || key === "constructor") { | ||
| break; | ||
| } | ||
| if (index < keys.length - 1) { | ||
| if (object[key]) { | ||
| return object[key]; | ||
| if (current[key]) { | ||
| current = current[key]; | ||
| } else { | ||
| const isArray = index < keys.length - 2 ? templateKeys[index + 1] === "$" : info?.arrays?.includes(templateKeys.slice(0, -1).join(".")); | ||
| current = current[key] = isArray ? [] : {}; | ||
| } | ||
| const isArray = index < keys.length - 2 ? templateKeys[index + 1] === "$" : info?.arrays?.includes(templateKeys.slice(0, -1).join(".")); | ||
| return object[key] = isArray ? [] : {}; | ||
| } | ||
| if (!info?.files?.includes(templateName) || input && (typeof input === "string" || input.size)) { | ||
| } else if (!info?.files?.includes(templateName) || input && (typeof input === "string" || input.size)) { | ||
| let output = getFieldValue(info, templateName, input); | ||
@@ -157,12 +163,12 @@ if (transform) { | ||
| if (info?.arrays?.includes(templateName)) { | ||
| if (object[key]) { | ||
| object[key].push(output); | ||
| if (current[key]) { | ||
| current[key].push(output); | ||
| } else { | ||
| object[key] = [output]; | ||
| current[key] = [output]; | ||
| } | ||
| } else { | ||
| object[key] = output; | ||
| current[key] = output; | ||
| } | ||
| } | ||
| }, values); | ||
| } | ||
| } | ||
@@ -169,0 +175,0 @@ if (info?.arrays) { |
+22
-19
| { | ||
| "name": "decode-formdata", | ||
| "description": "Decodes complex FormData into a JavaScript object", | ||
| "version": "0.8.0", | ||
| "version": "0.9.0", | ||
| "license": "MIT", | ||
@@ -42,25 +42,28 @@ "author": "Fabian Hiller", | ||
| }, | ||
| "devDependencies": { | ||
| "@eslint/js": "^9.23.0", | ||
| "@trivago/prettier-plugin-sort-imports": "^5.2.2", | ||
| "@types/node": "^22.13.13", | ||
| "@vitest/coverage-v8": "^3.0.9", | ||
| "eslint": "^9.23.0", | ||
| "eslint-plugin-import": "^2.31.0", | ||
| "eslint-plugin-jsdoc": "^50.6.9", | ||
| "eslint-plugin-security": "^3.0.1", | ||
| "jsdom": "^26.0.0", | ||
| "prettier": "^3.5.3", | ||
| "tsup": "^8.4.0", | ||
| "typescript": "^5.8.2", | ||
| "typescript-eslint": "^8.28.0", | ||
| "vite": "^6.2.3", | ||
| "vite-tsconfig-paths": "^5.1.4", | ||
| "vitest": "3.0.9" | ||
| }, | ||
| "scripts": { | ||
| "test": "vitest", | ||
| "coverage": "vitest run --coverage", | ||
| "lint": "eslint \"src/**/*.ts*\" && tsc --noEmit", | ||
| "lint": "eslint \"src/**/*.ts*\" && tsc --noEmit && deno check ./src/index.ts", | ||
| "format": "prettier --write ./src", | ||
| "format.check": "prettier --check ./src", | ||
| "build": "tsup", | ||
| "publish": "npm publish" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/eslint": "^8.44.3", | ||
| "@typescript-eslint/eslint-plugin": "^6.7.4", | ||
| "@typescript-eslint/parser": "^6.7.4", | ||
| "@vitest/coverage-v8": "^0.34.6", | ||
| "eslint": "^8.50.0", | ||
| "eslint-plugin-import": "^2.28.1", | ||
| "jsdom": "^22.1.0", | ||
| "prettier": "^3.0.3", | ||
| "tsup": "^7.2.0", | ||
| "typescript": "^5.2.2", | ||
| "vite": "^4.4.10", | ||
| "vitest": "^0.34.6" | ||
| "build": "tsup" | ||
| } | ||
| } | ||
| } |
27489
2.25%526
2.33%16
33.33%