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