{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type StructuredFormValue =\n\t| string\n\t| boolean\n\t| number\n\t| File\n\t| StructuredFormValue[];\n\nexport type StructuredFormData = Record<string, StructuredFormValue>;\n\nexport interface Configuration<T> {\n\t/**\n\t * Can be a partial object of `T` type.\n\t *\n\t * This is useful if you want to always guarantee that certain fields are\n\t * present in the output.\n\t */\n\tdefaults?: Partial<T>;\n\n\t/**\n\t * What values to use when determining a \"falsy\" value to coerce to `false`.\n\t *\n\t * Examples:\n\t * - `[\"false\", \"no\", \"n\", \"0\"]`\n\t *\n\t * Default is `\"false\"`\n\t */\n\tfalsy?: string | string[];\n\n\t/**\n\t * What values to use when determining a \"truthy\" value to coerce to `true`.\n\t *\n\t * Examples:\n\t * - `[\"on\", \"true\", \"yes\", \"y\", \"1\"]`\n\t *\n\t * Default is `[\"on\", \"true\"]`\n\t */\n\ttruthy?: string | string[];\n}\n\nexport function parseFormData<T extends StructuredFormData>(\n\tdata?: FormData,\n\tconfiguration?: Configuration<T>\n): Partial<T> {\n\t// TODO: throw or return empty object/undefined/null?\n\tif (!data || !data?.entries) return {} as T;\n\n\tconst config = {\n\t\ttruthy: [\"on\", \"true\"],\n\t\tfalsy: \"false\",\n\t\tdefaults: {},\n\t\t...configuration,\n\t} as Required<Configuration<T>>;\n\tconst truthy = to_array(config.truthy);\n\tconst falsy = to_array(config.falsy);\n\tconst defaults = config.defaults;\n\n\treturn [...data.entries()].reduce(\n\t\t(data, [k, v]) => {\n\t\t\t/**\n\t\t\t * Coerce the values to their primitive types.\n\t\t\t *\n\t\t\t * Start with booleans before we move onto numbers and nested fields.\n\t\t\t */\n\t\t\tlet value: StructuredFormValue = v;\n\n\t\t\tconst isString = typeof v === \"string\";\n\n\t\t\tif (v === \"\") value = v;\n\t\t\telse if (isString && truthy.includes(v)) value = true;\n\t\t\telse if (isString && falsy.includes(v)) value = false;\n\t\t\telse if (!isNaN(Number(v))) value = Number(v);\n\n\t\t\t/**\n\t\t\t * If the key exists in the defaults object, we check to see if\n\t\t\t * it is not an array value because we don't want to accidentally\n\t\t\t * create an array of values when the expectation is it will return\n\t\t\t * a single value.\n\t\t\t *\n\t\t\t * For example, this call:\n\t\t\t *  const data = new FormData();\n\t\t\t * \tdata.append(\"a\", \"foo\");\n\t\t\t * \tparseFormData(data, { defaults: { a: \"bar\" } })\n\t\t\t *\n\t\t\t * Should result in this output:\n\t\t\t * \t{ a: \"foo\" }\n\t\t\t *\n\t\t\t * Not:\n\t\t\t * \t{ a: [\"foo\", \"bar\"] }\n\t\t\t */\n\t\t\tconst is_in_defaults = k in defaults;\n\t\t\tconst is_default_array_value = Array.isArray(defaults[k]);\n\t\t\tconst is_equal_to_default = defaults[k] === data[k];\n\t\t\tif (k in data) {\n\t\t\t\tif (is_default_array_value && is_equal_to_default) {\n\t\t\t\t\t/**\n\t\t\t\t\t * If the defaults contain the given key and the default value is an\n\t\t\t\t\t * array and the current value equals the default, then we empty\n\t\t\t\t\t * out the `defaults` array and set the value to the first item in\n\t\t\t\t\t * the data array. Whew, that was a mouthful!\n\t\t\t\t\t *\n\t\t\t\t\t * This way we prevent situations where the default values are\n\t\t\t\t\t * appended to the data values.\n\t\t\t\t\t */\n\t\t\t\t\tvalue = [value];\n\t\t\t\t} else if (!is_in_defaults || is_default_array_value) {\n\t\t\t\t\t/**\n\t\t\t\t\t * For grouped fields like multi-selects and checkboxes, we need to\n\t\t\t\t\t * store the values in an array.\n\t\t\t\t\t *\n\t\t\t\t\t * For example, this call:\n\t\t\t\t\t *  const data = new FormData();\n\t\t\t\t\t * \tdata.append(\"a\", \"foo\");\n\t\t\t\t\t * \tdata.append(\"a\", \"bar\");\n\t\t\t\t\t * \tparseFormData(data)\n\t\t\t\t\t *\n\t\t\t\t\t * Should result in this output:\n\t\t\t\t\t * \t{ a: [\"foo\", \"bar\"] }\n\t\t\t\t\t *\n\t\t\t\t\t * Not:\n\t\t\t\t\t * \t{ a: \"foo\" } or { a: \"bar\" }\n\t\t\t\t\t */\n\t\t\t\t\tconst val = data[k];\n\t\t\t\t\tvalue = Array.isArray(val) ? [...val, value] : [val, value];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t/**\n\t\t\t * Do some TypeScript magic to get the correct type for the output.\n\t\t\t */\n\t\t\tdata[k as keyof T] = value as T[keyof T];\n\n\t\t\treturn data;\n\t\t},\n\t\t{ ...defaults } as Partial<T>\n\t);\n}\n\nfunction to_array(val: string | string[]): string[] {\n\treturn Array.isArray(val) ? val : [val];\n}\n"],"mappings":";AAuCO,SAAS,cACf,MACA,eACa;AAEb,MAAI,CAAC,QAAQ,EAAC,6BAAM;AAAS,WAAO,CAAC;AAErC,QAAM,SAAS;AAAA,IACd,QAAQ,CAAC,MAAM,MAAM;AAAA,IACrB,OAAO;AAAA,IACP,UAAU,CAAC;AAAA,IACX,GAAG;AAAA,EACJ;AACA,QAAM,SAAS,SAAS,OAAO,MAAM;AACrC,QAAM,QAAQ,SAAS,OAAO,KAAK;AACnC,QAAM,WAAW,OAAO;AAExB,SAAO,CAAC,GAAG,KAAK,QAAQ,CAAC,EAAE;AAAA,IAC1B,CAACA,OAAM,CAAC,GAAG,CAAC,MAAM;AAMjB,UAAI,QAA6B;AAEjC,YAAM,WAAW,OAAO,MAAM;AAE9B,UAAI,MAAM;AAAI,gBAAQ;AAAA,eACb,YAAY,OAAO,SAAS,CAAC;AAAG,gBAAQ;AAAA,eACxC,YAAY,MAAM,SAAS,CAAC;AAAG,gBAAQ;AAAA,eACvC,CAAC,MAAM,OAAO,CAAC,CAAC;AAAG,gBAAQ,OAAO,CAAC;AAmB5C,YAAM,iBAAiB,KAAK;AAC5B,YAAM,yBAAyB,MAAM,QAAQ,SAAS,EAAE;AACxD,YAAM,sBAAsB,SAAS,OAAOA,MAAK;AACjD,UAAI,KAAKA,OAAM;AACd,YAAI,0BAA0B,qBAAqB;AAUlD,kBAAQ,CAAC,KAAK;AAAA,QACf,WAAW,CAAC,kBAAkB,wBAAwB;AAiBrD,gBAAM,MAAMA,MAAK;AACjB,kBAAQ,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,KAAK,KAAK,IAAI,CAAC,KAAK,KAAK;AAAA,QAC3D;AAAA,MACD;AAKA,MAAAA,MAAK,KAAgB;AAErB,aAAOA;AAAA,IACR;AAAA,IACA,EAAE,GAAG,SAAS;AAAA,EACf;AACD;AAEA,SAAS,SAAS,KAAkC;AACnD,SAAO,MAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,GAAG;AACvC;","names":["data"]}