UNPKG

eslink-ui-plus

Version:

vue3 component library, css framework

110 lines (105 loc) 3.3 kB
import { initKeyConfig, keyConfigType } from "./commonProps"; export const typeOf = (obj: any): string => { return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); }; export const isObject = (obj: any): boolean => { return typeOf(obj) === "object"; }; export const isArray = (arr: any): boolean => { return typeOf(arr) === "array"; }; export const isSet = (set: any): boolean => { return typeOf(set) === "set"; }; export const isString = (str: any): boolean => { return typeof str === "string"; }; export const equalSet = (as: any, bs: any) => { if (as.size !== bs.size) return false; for (const a of as) if (!bs.has(a)) return false; return true; }; export const merge = (source: any = {}, target: any = {}) => { for (const key in source) { if (source.hasOwnProperty(key)) { if (isObject(target[key]) && isObject(source[key])) { merge(source[key], target[key]); } else { target[key] = source[key]; } } } return target; }; export const toRestFullResult = ( params: { [key: string]: string }, url: string ) => { const otherParams: { [key: string]: string } = {}; Object.keys(params).forEach((key) => { const mainKey = `\${${key}}`; if (url.includes(mainKey)) { url = url.replace(`\${${key}}`, params[key]); } else { otherParams[key] = params[key]; } }); return { otherParams, url }; }; export const toRestFullFormDataResult = ( params: { [key: string]: string }, url: string ) => { const form = new FormData(); Object.keys(params).forEach((key) => { const mainKey = `\${${key}}`; if (url.includes(mainKey)) { url = url.replace(`\${${key}}`, params[key]); } else { form.append(key, params[key]); } }); return { otherParams: form, url }; }; export const initSource = ( source: unknown[] = [], keyConfig = initKeyConfig, parent: { path: string[]; leaf: boolean; parent: any; labelPath: string[]; key: string; } | null = null, pathMap: { [key: string]: string } = {}, valueMap: { [key: string]: any } = {} ) => { const { valueKey, labelKey, childrenKey } = keyConfig; if (!source.length) { return { source, pathMap, valueMap }; } source.forEach((item: any) => { const value = item[valueKey]; const label = item[labelKey]; item.parent = parent; item.path = parent ? [...parent.path, value] : [value]; item.labelPath = parent ? [...parent.labelPath, label] : [label]; item.leaf = !item?.[childrenKey]?.length; item.leafLabel = item.labelPath.join("/"); item[`_${labelKey}`] = item[labelKey]; item[childrenKey] = item?.[childrenKey] ? initSource(item[childrenKey], keyConfig, item, pathMap, valueMap).source : []; pathMap[value] = item.path; valueMap[value] = item; }); return { source, pathMap, valueMap }; }; export const getKeyConfig = ( source: keyConfigType ): keyConfigType & Required<Pick<keyConfigType, "valueKey" | "labelKey">> => { return { ...initKeyConfig, ...source }; }; export const getCssUnit = (value: string | number): string => { return value.toString().includes("px") ? (value as string) : value + "px"; };