UNPKG

linkmore-design

Version:

🌈 🚀lm组件库。🚀

119 lines (109 loc) 4.8 kB
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _typeof from "@babel/runtime/helpers/esm/typeof"; import dayjs from 'dayjs'; // 检查是否存在内容 export function getIsHas(val) { // 检查是否数字类型, 是否布尔类型 var flag = !!val || typeof val === 'number' || typeof val === 'boolean'; // 检查空对象和空数组 if (!!val && _typeof(val) === 'object') { var _Object$keys; flag = Array.isArray(val) ? !!val.length : !!((_Object$keys = Object.keys(val)) !== null && _Object$keys !== void 0 && _Object$keys.length); } return flag; } /* * 数据(布尔值、数值)转字符串后对比是否相等 str, strs, type * @params {Array | String} strs * @params {String} type 比对类型 * @type includes 是否包含在数组内 */ export function toStringIsEqual(str, strs, type) { if (type === 'includes') { return strs === null || strs === void 0 ? void 0 : strs.some(function (v) { return toStringIsEqual(str, v); }); } return String(str) === String(strs); } // 根据类型返回对应的值 export function getValueForType(type) { if (['checkbox', 'cascader'].includes(type)) { return []; } return ''; } // 数组对象去重 export function uniqueFunc(arr, guid) { var res = new Map(); return arr.filter(function (item) { var isHas = (item === null || item === void 0 ? void 0 : item[guid]) || typeof (item === null || item === void 0 ? void 0 : item[guid]) === 'number'; var resetItem = isHas ? item === null || item === void 0 ? void 0 : item[guid] : item; return !res.has(resetItem) && res.set(resetItem, 1); }); } // 获取最近的时间段 export function getRanges() { return { 近一周: [dayjs().subtract(7, 'day').add(1, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')], 近一月: [dayjs().subtract(1, 'month').add(1, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')], 近一年: [dayjs().subtract(1, 'year').add(1, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')] }; } // 级联选择器获取深层结构数据 type: 遍历子/父结构 export function getFlatItem(getItem, checkedValue) { var _getItem$componentPro, _getItem$componentPro2; var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var filterValues = getItem !== null && getItem !== void 0 && (_getItem$componentPro = getItem.componentProps) !== null && _getItem$componentPro !== void 0 && _getItem$componentPro.multiple ? checkedValue : [checkedValue]; var originArr = (getItem === null || getItem === void 0 ? void 0 : getItem.options) || []; // 用于自定义读取的字段 var fieldNames = _objectSpread({ label: 'label', value: 'value', children: 'children' }, (getItem === null || getItem === void 0 ? void 0 : (_getItem$componentPro2 = getItem.componentProps) === null || _getItem$componentPro2 === void 0 ? void 0 : _getItem$componentPro2.fieldNames) || {}); var getFlatChildren = function getFlatChildren(item) { var _item$fieldNames$chil; if (type === 'parent') return item; if (item !== null && item !== void 0 && (_item$fieldNames$chil = item[fieldNames.children]) !== null && _item$fieldNames$chil !== void 0 && _item$fieldNames$chil.length) { return item === null || item === void 0 ? void 0 : item[fieldNames.children].map(function (v) { return getFlatChildren(v); }); } return item; }; // 查找匹配项 var matchChildren = function matchChildren(arr, str) { return (arr || []).find(function (item) { return toStringIsEqual(item === null || item === void 0 ? void 0 : item[fieldNames.value], str); }); }; /** 匹配末级选项 */ var matchLastItem = function matchLastItem(arr) { return arr === null || arr === void 0 ? void 0 : arr.reduce(function (pre, cur, index, array) { var matchItem = matchChildren(pre, cur); var isLast = index + 1 === array.length; return isLast ? matchItem : matchItem === null || matchItem === void 0 ? void 0 : matchItem[fieldNames.children]; }, originArr); }; var result = filterValues.reduce(function (pre, cur) { var findItem = matchLastItem(cur); if (!findItem) return pre; return pre.concat(getFlatChildren(findItem)); }, []); return result; } // 返回过滤掉隐藏字段后的查询结构 () => {...} export function getFilterShow(basicFilter, dataSource) { return Object.keys(basicFilter).reduce(function (pre, cur) { var value = basicFilter[cur]; if (dataSource.some(function (v) { return v.field === cur && (v.show || typeof v.show !== 'boolean'); })) { // eslint-disable-next-line no-param-reassign pre[cur] = value; } return pre; }, {}); } export default {};