@antdv/pro-utils
Version:
@antdv/pro-utils
173 lines (172 loc) • 7.23 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
isPlainObj: () => isPlainObj,
transformKeySubmitValue: () => transformKeySubmitValue
});
module.exports = __toCommonJS(stdin_exports);
var import_pro_provider = require("@antdv/pro-provider");
var import_lodash_es = require("lodash-es");
var import_is = require("../is");
function isPlainObj(itemValue) {
if (typeof itemValue !== "object") return false;
if (itemValue === null) return true;
if ((0, import_is.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 (!(0, import_is.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" || (0, import_is.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 = (0, import_pro_provider.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 = (0, import_pro_provider.set)(result, tempKey, itemValue);
return;
}
if (typeof tempKey === "object" && !Array.isArray(finalValues)) {
finalValues = (0, import_lodash_es.merge)(finalValues, tempKey);
} else if (typeof tempKey === "object" && Array.isArray(finalValues)) {
result = __spreadValues(__spreadValues({}, result), tempKey);
} else if (tempKey !== null || tempKey !== void 0) {
result = (0, import_pro_provider.set)(
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 = (0, import_pro_provider.set)(result, [entityKey], genValues);
return;
}
transform();
});
return omit ? result : tempValues;
};
finalValues = Array.isArray(values) && Array.isArray(finalValues) ? [...gen(values)] : (0, import_pro_provider.merge)({}, gen(values), finalValues);
return finalValues;
}