feat: 🎸 初始化项目
This commit is contained in:
85
src/utils/index.ts
Normal file
85
src/utils/index.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* 对象数组深拷贝
|
||||
* @param {Array,Object} source 需要深拷贝的对象数组
|
||||
* @param {Array} noClone 不需要深拷贝的属性集合
|
||||
*/
|
||||
export function deepClone(source: any, noClone: string[] = []): any {
|
||||
if (!source && typeof source !== 'object') {
|
||||
throw new Error('error arguments deepClone')
|
||||
}
|
||||
const targetObj: any = source.constructor === Array ? [] : {}
|
||||
Object.keys(source).forEach((keys: string) => {
|
||||
if (source[keys] && typeof source[keys] === 'object' && noClone.indexOf(keys) === -1) {
|
||||
targetObj[keys] = deepClone(source[keys], noClone)
|
||||
} else {
|
||||
targetObj[keys] = source[keys]
|
||||
}
|
||||
})
|
||||
return targetObj
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找数组对象的某个下标
|
||||
* @param {Array} ary 查找的数组
|
||||
* @param {Functon} fn 判断的方法
|
||||
*/
|
||||
export function findIndex(ary: any, fn: Function): number {
|
||||
if (ary.findIndex) {
|
||||
return ary.findIndex(fn)
|
||||
}
|
||||
let index = -1
|
||||
ary.some((item: any, i: number, ary: any) => {
|
||||
const ret: any = fn(item, i, ary)
|
||||
if (ret) {
|
||||
index = i
|
||||
return ret
|
||||
}
|
||||
})
|
||||
return index
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String|Number} value 要验证的字符串或数值
|
||||
* @param {*} validList 用来验证的列表
|
||||
*/
|
||||
export function oneOf(value: string | number, validList: string[] | number[]): boolean {
|
||||
for (let i = 0; i < validList.length; i++) {
|
||||
if (value === validList[i]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机字符串
|
||||
*/
|
||||
export function toAnyString() {
|
||||
const str: string = 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c: string) => {
|
||||
const r: number = Math.random() * 16 | 0
|
||||
const v: number = c === 'x' ? r : (r & 0x3 | 0x8)
|
||||
return v.toString()
|
||||
})
|
||||
return str
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取URL参数
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function param2Obj(url: string) {
|
||||
const search: string = url.split('?')[1]
|
||||
if (!search) {
|
||||
return {}
|
||||
}
|
||||
return JSON.parse(
|
||||
'{"' +
|
||||
decodeURIComponent(search)
|
||||
.replace(/"/g, '\\"')
|
||||
.replace(/&/g, '","')
|
||||
.replace(/=/g, '":"')
|
||||
.replace(/\+/g, ' ') +
|
||||
'"}'
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user