UNPKG

ht-data-formatting-view

Version:

A Vue component for data formatting view

78 lines (71 loc) 2.6 kB
import { sha256 } from "js-sha256"; import { has, merge, cloneDeep, intersection, set } from "lodash"; export const htccEncrypt = function (data, prefix = "h") { let inputData = ""; if (typeof data !== "string" && !(data instanceof Buffer)) { console.error("转换的数据类型错误 :>> ", data); } else { inputData = data; } return prefix + sha256(inputData); }; export const debounce = function (func, wait) { let startTime = Date.now(); let timer; return (...args) => { if (Date.now() - startTime < wait && timer) { clearTimeout(timer); } timer = setTimeout(() => { func(...args); }, wait); startTime = Date.now(); }; }; /** * 导出一个函数,用于获取对象中所有键值的索引信息,并可选择性地对值进行处理。 * @param {Object} data - 需要处理的对象。 * @param {Function} upValue - 可选参数,一个回调函数,用于对值进行进一步处理。 * @returns {Object} - 返回一个对象,包含两个属性:keys和newObj。keys是处理后对象的所有键组成的数组,newObj是经过处理后的新对象。 */ export function getObjKeysWithIndex(data, anchor, upValue) { let obj = JSON.parse(JSON.stringify(data)); function format(obj, path = "") { let result = {}; for (let key in obj) { let currentPath = path ? `${path}['${key}']` : `['${key}']`; if (obj[key] === null) { obj[key] = ""; // 将null转换为空字符串 } else if (Array.isArray(obj[key])) { // 处理数组,包含数组元素的键为索引 obj[key].forEach((item, index) => { let arrayPath = `${currentPath}[${index}]`; if (typeof item === "object") { Object.assign(result, format(item, arrayPath)); } else { result[arrayPath] = trim(item); } }); } else if (typeof obj[key] === "object" && obj[key] !== null) { // 递归处理对象 Object.assign(result, format(obj[key], currentPath)); } else { // 处理基本类型值 result[currentPath] = trim(obj[key]); } } return result; } let result = format(obj); console.log("result :>> ", result); Object.keys(result).forEach((key) => { let value = upValue ? upValue(result[key], key) : result[key]; let arr = value.split(anchor); console.log("value :>> ", value); arr[1] = arr[1].replace(/\['/g, `[?`).replace(/'\]/g, `?]`); if (set) { set(obj, key, arr.join(anchor)); } }); return { keys: Object.keys(result), newObj: obj }; }