quality_frontend/packages/common/javascript/string.js

150 lines
3.9 KiB
JavaScript
Raw Permalink 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.

/**
* @description: 深复制对象, 利用JSON序列化实现一个深拷贝,继承的属性会丢失
* @param {Object} 必选
* @return: {Object} 返回复制的新对象
*/
export function deepCloneByJson (obj) {
if (typeof obj === 'object') {
return JSON.parse(JSON.stringify(obj))
} else {
return obj
}
}
/**
* @description: 筛选table数据
* @param {Object} 必填
* @return: {Object} 返回筛选后的新对象
*/
export function fliterTableData (val) {
let data = deepCloneByJson(val)
if (data) {
return [
data.label
]
}
return []
}
// 根据路径获取数组
/**
* @description: 根据字符串路径获取对象层级下的数据eg: getValueByPath({a:{b:{c:123}}}, 'a.b.c') => 123
* @param {Object} 需要获取数据的对象
* @param {String} 对应的key路径eg:'a.b.c'
* @return: 返回对象对应路径下的值
*/
export function getValueByPath (object, prop) {
prop = prop || ''
let paths = prop.split('.')
let current = object
let result = null
for (let i = 0, j = paths.length; i < j; i++) {
let path = paths[i]
if (!current) break
if (i === j - 1) {
result = current[path]
break
}
current = current[path]
}
return result
}
/**
* @description: 数组去重
* @param {Array} 必选,需要去重的数组
* @param {Boolean} 可选默认true, 当false时表示数组内的对象key/value相同时则为相同
* @return: 返回去重后的数组
*/
export function unique (array, isStrict = true) {
if (!(array instanceof Array)) { // 输入非数组,返回[]
return []
}
let newArray = [...new Set(array)]
if (!isStrict) {
let n = []
for (let i = 0, len = newArray.length; i < len; i++) {
if (typeof newArray[i] === 'object') {
let str = JSON.stringify(newArray[i])
let isEqual = n.filter(item => {
return JSON.stringify(item) === str
}).length
if (!isEqual) {
n.push(newArray[i])
}
} else {
n.push(newArray[i])
}
}
newArray = n
}
return newArray
}
/**
* @description: 地址栏搜索字符转化为json对象
* @param {String} 可选,格式为 a=1&b=2&c=3
* @return: {Object} 返回对象
*/
export function searchToJson (search = window.location.search) {
if (!search || !(typeof search === 'string')) { // 输入非字符串返回undefined
return undefined
} else {
let index = search.indexOf('?')
search = index > -1 ? search.substr(index + 1) : search // 存在问号,则删除
let searchJson = {}
let searchArr = search.split('&')
for (let i = 0, len = searchArr.length; i < len; i++) {
let tempArr = searchArr[i].split('=')
if (tempArr.length > 1) {
searchJson[tempArr[0]] = tempArr[1]
}
}
return searchJson
}
}
/**
* @description: json对象转化为地址栏搜索字符
* @param {Object} 必选
* @return: {String} 返回字符串,格式为 a=1&b=2&c=3
*/
export function jsonToSearch (json) {
if (typeof json !== 'object') { // 输入非字符串返回undefined
return ''
} else {
let str = ''
for (let i in json) {
str += i + '=' + json[i] + '&'
}
if (str) {
str = str.substr(0, str.length - 1)
}
return str
}
}
/**
* @description: 首字符为字母,则返回首字母大写的字符串
* @param {String} 必选
* @return: {String} 返回转化后的字符串
*/
export function firstLetterUppercase (str) {
if (!str || typeof str !== 'string') {
return str
}
return str.toLocaleUpperCase().slice(0, 1) + str.slice(1)
}
/**
* @description: 若字符串存在字母,则返回首字母大写,其余小写的字符串
* @param {String} 必选
* @return: {String} 返回转化后的字符串
*/
export function onlyFirstLetterUppercase (str) {
if (!str || typeof str !== 'string') {
return str
}
return str.toLocaleUpperCase().slice(0, 1) + str.toLocaleLowerCase().slice(1)
}