@mantine/form
Version:
Mantine form management library
1 lines • 2.77 kB
Source Map (JSON)
{"version":3,"file":"change-error-indices.cjs","names":["clearListState"],"sources":["../../src/lists/change-error-indices.ts"],"sourcesContent":["import { clearListState } from './clear-list-state';\n\n/**\n * Gets the part of the key after the path which can be an index\n */\nfunction getIndexFromKeyAfterPath(key: string, path: string): number {\n const split = key.substring(path.length + 1).split('.')[0];\n return parseInt(split, 10);\n}\n\n/**\n * Changes the indices of every error that is after the given `index` with the given `change` at the given `path`.\n * This requires that the errors are in the format of `path.index` and that the index is a number.\n */\nexport function changeErrorIndices<T extends Record<PropertyKey, any>>(\n path: PropertyKey,\n index: number | undefined,\n errors: T,\n change: 1 | -1\n): T {\n if (index === undefined) {\n return errors;\n }\n const pathString = `${String(path)}`;\n let clearedErrors = errors;\n // Remove all errors if the corresponding item was removed\n if (change === -1) {\n clearedErrors = clearListState(`${pathString}.${index}`, clearedErrors);\n }\n\n const cloned = { ...clearedErrors };\n const changedKeys = new Set<string>();\n Object.entries(clearedErrors)\n .filter(([key]) => {\n if (!key.startsWith(`${pathString}.`)) {\n return false;\n }\n const currIndex = getIndexFromKeyAfterPath(key, pathString);\n if (Number.isNaN(currIndex)) {\n return false;\n }\n return currIndex >= index;\n })\n .forEach(([key, value]) => {\n const currIndex = getIndexFromKeyAfterPath(key, pathString);\n\n const newKey: keyof T = key.replace(\n `${pathString}.${currIndex}`,\n `${pathString}.${currIndex + change}`\n );\n cloned[newKey] = value;\n changedKeys.add(newKey);\n if (!changedKeys.has(key)) {\n delete cloned[key];\n }\n });\n\n return cloned;\n}\n"],"mappings":";;;;;;AAKA,SAAS,yBAAyB,KAAa,MAAsB;CACnE,MAAM,QAAQ,IAAI,UAAU,KAAK,SAAS,CAAC,EAAE,MAAM,GAAG,EAAE;CACxD,OAAO,SAAS,OAAO,EAAE;AAC3B;;;;;AAMA,SAAgB,mBACd,MACA,OACA,QACA,QACG;CACH,IAAI,UAAU,KAAA,GACZ,OAAO;CAET,MAAM,aAAa,GAAG,OAAO,IAAI;CACjC,IAAI,gBAAgB;CAEpB,IAAI,WAAW,IACb,gBAAgBA,yBAAAA,eAAe,GAAG,WAAW,GAAG,SAAS,aAAa;CAGxE,MAAM,SAAS,EAAE,GAAG,cAAc;CAClC,MAAM,8BAAc,IAAI,IAAY;CACpC,OAAO,QAAQ,aAAa,EACzB,QAAQ,CAAC,SAAS;EACjB,IAAI,CAAC,IAAI,WAAW,GAAG,WAAW,EAAE,GAClC,OAAO;EAET,MAAM,YAAY,yBAAyB,KAAK,UAAU;EAC1D,IAAI,OAAO,MAAM,SAAS,GACxB,OAAO;EAET,OAAO,aAAa;CACtB,CAAC,EACA,SAAS,CAAC,KAAK,WAAW;EACzB,MAAM,YAAY,yBAAyB,KAAK,UAAU;EAE1D,MAAM,SAAkB,IAAI,QAC1B,GAAG,WAAW,GAAG,aACjB,GAAG,WAAW,GAAG,YAAY,QAC/B;EACA,OAAO,UAAU;EACjB,YAAY,IAAI,MAAM;EACtB,IAAI,CAAC,YAAY,IAAI,GAAG,GACtB,OAAO,OAAO;CAElB,CAAC;CAEH,OAAO;AACT"}