All files data.ts

89.47% Statements 85/95
100% Branches 27/27
88.23% Functions 15/17
89.47% Lines 85/95

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 961x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 3x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 9x 9x 8x 8x 2x 2x 9x 1x 1x 9x 4x 5x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x                      
import clonedeep from 'lodash.clonedeep'
/**
 * @description 'For simple data, you use it is better.(Object who not includes object type data)'
 * @param target
 */
export function deepCloneByStringfy<T extends object>(target: T): T {
  return target && JSON.parse(JSON.stringify(target))
}
 
/**
 * @description 'For complicated data, you use it is better.(Object who includes object type data)'
 */
 
export const deepClone = clonedeep
 
export function jsonToObject<T>(target: string, defV: unknown): T | typeof defV {
  return target ? JSON.parse(target) : defV
}
 
/**
 * @description notice: max >= min is need
 * @param min
 * @param max
 */
export function getRandomNumber(min: number, max: number) {
  return min + Math.floor((max - min) * Math.random())
}
 
/**
 * Will create a new arr, deep clone is deal
 * @param arr
 */
export function getRandomOneInArr(arr: any[]) {
  if (arr.length >= 1) {
    return deepClone(arr)[getRandomNumber(0, arr.length - 1)]
  } else {
    throw 'Array has at least one element'
  }
}
 
/**
 * Will create a new arr, deep clone is deal
 * @param arr
 */
export function getRandomArr<T extends unknown[]>(arr: T): T {
  return deepClone(arr).sort(() => 0.5 - Math.random())
}
 
/**
 * @description Map key operation for object.
 * @param target
 * @param mapKeys example ['name-value','age-label'], It means the 'name' key will change as 'value' key, and the 'age' key will change as 'label' key.
 * @param replace @default(false)||true:will replace origin key to new key. false:will add new key that will just inherit old keys' value.
 */
export function getMapkeyedObj<T>(target: { [key: string]: any }, mapKeys: string[], replace = false): T {
  const deepTarget = deepClone(target)
  for (const mapKeyStr of mapKeys) {
    const mapKeyArr = mapKeyStr.split('-')
    if (mapKeyArr[0] in deepTarget) {
      deepTarget[mapKeyArr[1]] = deepTarget[mapKeyArr[0]]
      if (replace) {
        delete deepTarget[mapKeyArr[0]]
      }
    } else {
      throw `Can't find '${mapKeyArr[0]}' key in origin target.`
    }
  }
  return deepTarget as T
}
 
/**
 * @description Generate an array of specified length.
 * @param length
 * @param callback
 */
// eslint-disable-next-line no-unused-vars
export function getLengthArr<T = undefined>(length: number, callback?: (index?: number) => T): T[] {
  return callback ? Array.from(Array(length), (value, index) => callback(index)) : Array.from(Array(length))
}
 
/**
 * @description Prefer RFC 5897 to make the download result the same as the URL directly through the A tag.
 * @param contentDisposition
 */
export function analysisFilename(contentDisposition: string): string {
  let regex = /filename\*=\S+?''(.+?)(;|$)/
  if (regex.test(contentDisposition)) {
    return RegExp.$1
  }
  regex = /filename="{0,1}([\S\s]+?)"{0,1}(;|$)/
  if (regex.test(contentDisposition)) {
    return RegExp.$1
  }
  return 'File name get exception.'
}