export const ascSortData = arr => arr.sort((a, b) => a.value - b.value)
export const descSortData = arr => isArray(arr) ? arr.sort((a, b) => b.value - a.value) : arr
export const firstUpperCase = str => str.toLowerCase().replace(/^\S/g, s => s.toUpperCase())
export const lowercaseCase = str => str.toLocaleLowerCase()
export function findArrayIndex(arr, key, value) {
return isArray(arr) ? arr.findIndex(item => item[key] === value) : -1
}
export function intersect(original, arr) {
if (!isArray(original) || !isArray(original)) {
return original
}
const packageData = new Set(arr)
return original.filter(el => packageData.has(el))
}
export function minus(original, arr) {
if (!isArray(original) || !isArray(original)) {
return original
}
const packageData = new Set(arr)
return original.filter(el => !packageData.has(el))
}
export function complement(original, arr) {
if (!isArray(original) || !isArray(original)) {
return original
}
const packageOriginal = minus(original, arr)
const packageData = minus(arr, original)
return [...packageOriginal, ...packageData]
}
export function unionSet(original, arr) {
if (!isArray(original) || !isArray(original)) {
return original
}
return Array.from(new Set([...original, ...arr]))
}
export function dataEmptyFilter(str) {
if (!str && str !== 0) {
return '-'
}
return str
}
export const arrayConversion = (value, result) => {
if (isArray(value)) {
value = []
return result
}
}
export const arrayConversionToObject = options =>
options.map(el => el.key).reduce((acc, cur) => ({ ...acc, [cur]: 0 }), {})
export const objectConversionToInitData = options =>
Object.keys(options).reduce((acc, cur) => ({ ...acc, [cur]: 0 }), {})
export const arrayToHeavy = (data) => Array.from(new Set(data))
export const flattenArray = (data) => data.reduce((flat, toFlat) => flat.concat(Array.isArray(toFlat) ? flattenArray(toFlat) : toFlat), [])
export const updateArrayData = (data, result) => {
remove(result)
data.map((el, index) => result.splice(index, 1, el))
return result
}
export const fillZero = (num) => `${num}`.padStart(2, '0')
export const sum = (arr) => (arr.reduce((acc, cur) => (acc += cur.value), 0))
export const numberWithCommas = (x) => x.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,')
export const isObject = function(arg) {
return Object.prototype.toString.call(arg) === '[object Object]'
}
export const isEmptyObject = (data) => {
return !isEmpty(data) && !Object.keys(data).length
}