zoo-frontend/src/utils/common.js

35 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//深拷贝任何值
export function copy(value) {
let result = JSON.stringify(value)
return JSON.parse(result)
}
//分割字符串
export function splitKeyWords(keyword) {
let keyset = keyword.split(' ')//提取关键字
keyset = Array.from(new Set(keyset))//关键字去重
console.log('切割去重好的关键字', keyset)
return keyset;
}
//模糊匹配传入一个包含多个关键字的数组、一个对象若匹配则true
export function fuzzyMatching(object, keyset) {
//一个对象的所有属性能匹配所有关键字就返回trueevery():遇到false就返回false所有true就返回true
return keyset.every(key => {
return keywordMatching(object, key)
})
}
//传入一个关键字一个对象只有关键字能匹配对象的某个属性则true
function keywordMatching(object, keyword) {
//该关键字为空,肯定能匹配上
if (keyword === '') {
console.log('keywordMatching关键字为空', object, keyword, true)
return true
}
//只要对象的任意属性能匹配一次关键字就返回truesome():遇到true就返回true所有false就返回false
return Object.keys(object).some(key => {
return (object[key] + '').includes(keyword)
})
}