UNPKG

@antdv/pro-utils

Version:

@antdv/pro-utils

156 lines (155 loc) 6.27 kB
var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; import { get, merge, set as namePathSet } from "@antdv/pro-provider"; import { merge as deepMerge } from "lodash-es"; import { isNil, isValidElement } from "../is/index.mjs"; function isPlainObj(itemValue) { if (typeof itemValue !== "object") return false; if (itemValue === null) return true; if (isValidElement(itemValue)) return false; if (itemValue.constructor === RegExp) return false; if (itemValue instanceof Map) return false; if (itemValue instanceof Set) return false; if (itemValue instanceof HTMLElement) return false; if (itemValue instanceof Blob) return false; if (itemValue instanceof File) return false; if (Array.isArray(itemValue)) return false; return true; } function transformKeySubmitValue(values, dataFormatMapRaw, omit = true) { const dataFormatMap = Object.keys(dataFormatMapRaw).reduce((ret, key) => { const value = dataFormatMapRaw[key]; if (!isNil(value)) { ret[key] = value; } return ret; }, {}); if (Object.keys(dataFormatMap).length < 1) { return values; } if (typeof window === "undefined") return values; if (typeof values !== "object" || isNil(values) || values instanceof Blob) { return values; } let finalValues = Array.isArray(values) ? [] : {}; const gen = (tempValues, parentsKey) => { const isArrayValues = Array.isArray(tempValues); let result = isArrayValues ? [] : {}; if (tempValues == null || tempValues === void 0) { return result; } Object.keys(tempValues).forEach((entityKey) => { const transformForArray = (transformList, subItemValue) => { if (!Array.isArray(transformList)) return entityKey; transformList.forEach( (transform2, idx) => { if (!transform2) return; const subTransformItem = subItemValue == null ? void 0 : subItemValue[idx]; if (typeof transform2 === "function") { subItemValue[idx] = transform2( subItemValue, entityKey, tempValues ); } if (typeof transform2 === "object" && !Array.isArray(transform2)) { Object.keys(transform2).forEach((transformArrayItem) => { const subTransformItemValue = subTransformItem == null ? void 0 : subTransformItem[transformArrayItem]; if (typeof transform2[transformArrayItem] === "function" && subTransformItemValue) { const res = transform2[transformArrayItem]( subTransformItem[transformArrayItem], entityKey, tempValues ); subTransformItem[transformArrayItem] = typeof res === "object" ? res[transformArrayItem] : res; } else if (typeof transform2[transformArrayItem] === "object" && Array.isArray(transform2[transformArrayItem]) && subTransformItemValue) { transformForArray( transform2[transformArrayItem], subTransformItemValue ); } }); } if (typeof transform2 === "object" && Array.isArray(transform2) && subTransformItem) { transformForArray(transform2, subTransformItem); } } ); return entityKey; }; const key = parentsKey ? [parentsKey, entityKey].flat(1) : [entityKey].flat(1); const itemValue = tempValues[entityKey]; const transformFunction = get(dataFormatMap, key); const transform = () => { let tempKey; let transformedResult; let isTransformedResultPrimitive = false; if (typeof transformFunction === "function") { transformedResult = transformFunction == null ? void 0 : transformFunction( itemValue, entityKey, tempValues ); const typeOfResult = typeof transformedResult; if (typeOfResult !== "object" && typeOfResult !== "undefined") { tempKey = entityKey; isTransformedResultPrimitive = true; } else { tempKey = transformedResult; } } else { tempKey = transformForArray(transformFunction, itemValue); } if (Array.isArray(tempKey)) { result = namePathSet(result, tempKey, itemValue); return; } if (typeof tempKey === "object" && !Array.isArray(finalValues)) { finalValues = deepMerge(finalValues, tempKey); } else if (typeof tempKey === "object" && Array.isArray(finalValues)) { result = __spreadValues(__spreadValues({}, result), tempKey); } else if (tempKey !== null || tempKey !== void 0) { result = namePathSet( result, [tempKey], isTransformedResultPrimitive ? transformedResult : itemValue ); } }; if (transformFunction && typeof transformFunction === "function") { transform(); } if (typeof window === "undefined") return; if (isPlainObj(itemValue)) { const genValues = gen(itemValue, key); if (Object.keys(genValues).length < 1) { return; } result = namePathSet(result, [entityKey], genValues); return; } transform(); }); return omit ? result : tempValues; }; finalValues = Array.isArray(values) && Array.isArray(finalValues) ? [...gen(values)] : merge({}, gen(values), finalValues); return finalValues; } export { isPlainObj, transformKeySubmitValue };