magiccube-vue3
Version:
vue3-js版组件库
79 lines (71 loc) • 2.25 kB
JavaScript
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
dayjs.locale('zh-cn')
export const deepCopy = data => {
let d
if (typeof data === 'object') {
if (Array.isArray(data)) {
d = []
for (let i = 0, len = data.length; i < len; i++) d[i] = deepCopy(data[i])
} else if (data === null) {
d = null
} else if (data.constructor === RegExp) {
d = data
} else if (Object.keys(data).length) {
d = {}
for (const key in data) d[key] = deepCopy(data[key])
} else {
d = data
}
} else {
d = data
}
return d
}
export const getParentScrollElement = (root) => {
if (!root) return []
let arr = []
const style = getComputedStyle(root, null)
if (root && root !== document.body) {
if (style['overflowY'] === 'auto' || style['overflow-y'] === 'auto' || style['overflowX'] === 'auto' || style['overflow-x'] === 'auto') {
arr.push(root)
} else {
arr = [...arr, ...getParentScrollElement(root.parentNode)]
}
}
return arr
}
export const _uuid = () => {
let s = Date.now() + parseInt(Math.random() * 365 * 1000)
s = s.toString()
return s.substr(s.length - 5)
}
export const dateFormat = (time, format) => {
if(time){
return dayjs(time).format(format)
} else {
return ''
}
}
export const cityCodeToData = (data, _citys, keyName = 'value') => {
if (typeof data === 'object') {
if (Array.isArray(data)) {
return _citys.filter(city => data.includes(city[keyName]))
} else {
return data.value ? _citys.find(city => city[keyName] === data[keyName]) : []
}
} else {
return _citys.filter(city => city[keyName] === data)
}
}
export const jobFuncCodeToData = (data, options, keyName = 'value') => {
if (typeof data === 'object') {
if (Array.isArray(data)) {
return options.filter(item => data.includes(item[keyName]))
} else {
return data.value ? options.find(item => item[keyName] === data[keyName]) : []
}
} else {
return options.filter(item => item[keyName] === data)
}
}