github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/utils/index.ts (about)

     1  // import type { Plugin } from 'vue'
     2  
     3  /**
     4   *
     5   * @param component 需要注册的组件
     6   * @param alias 组件别名
     7   * @returns any
     8   */
     9  export const withInstall = <T>(component: T, alias?: string) => {
    10    const comp = component as any
    11    comp.install = (app: any) => {
    12      app.component(comp.name || comp.displayName, component)
    13      if (alias) {
    14        app.config.globalProperties[alias] = component
    15      }
    16    }
    17    return component as T & Plugin
    18  }
    19  
    20  /**
    21   * @param str 需要转下划线的驼峰字符串
    22   * @returns 字符串下划线
    23   */
    24  export const humpToUnderline = (str: string): string => {
    25    return str.replace(/([A-Z])/g, '-$1').toLowerCase()
    26  }
    27  
    28  /**
    29   * @param str 需要转驼峰的下划线字符串
    30   * @returns 字符串驼峰
    31   */
    32  export const underlineToHump = (str: string): string => {
    33    if (!str) return ''
    34    return str.replace(/\-(\w)/g, (_, letter: string) => {
    35      return letter.toUpperCase()
    36    })
    37  }
    38  
    39  export const setCssVar = (prop: string, val: any, dom = document.documentElement) => {
    40    dom.style.setProperty(prop, val)
    41  }
    42  
    43  /**
    44   * 查找数组对象的某个下标
    45   * @param {Array} ary 查找的数组
    46   * @param {Functon} fn 判断的方法
    47   */
    48  // eslint-disable-next-line
    49  export const findIndex = <T = Recordable>(ary: Array<T>, fn: Fn): number => {
    50    if (ary.findIndex) {
    51      return ary.findIndex(fn)
    52    }
    53    let index = -1
    54    ary.some((item: T, i: number, ary: Array<T>) => {
    55      const ret: T = fn(item, i, ary)
    56      if (ret) {
    57        index = i
    58        return ret
    59      }
    60    })
    61    return index
    62  }
    63  
    64  export const trim = (str: string) => {
    65    return str.replace(/(^\s*)|(\s*$)/g, '')
    66  }
    67  
    68  /**
    69   * @param {Date | number | string} time 需要转换的时间
    70   * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
    71   */
    72  export function formatTime(time: Date | number | string, fmt: string) {
    73    if (!time) return ''
    74    else {
    75      const date = new Date(time)
    76      const o = {
    77        'M+': date.getMonth() + 1,
    78        'd+': date.getDate(),
    79        'H+': date.getHours(),
    80        'm+': date.getMinutes(),
    81        's+': date.getSeconds(),
    82        'q+': Math.floor((date.getMonth() + 3) / 3),
    83        S: date.getMilliseconds()
    84      }
    85      if (/(y+)/.test(fmt)) {
    86        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    87      }
    88      for (const k in o) {
    89        if (new RegExp('(' + k + ')').test(fmt)) {
    90          fmt = fmt.replace(
    91            RegExp.$1,
    92            RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
    93          )
    94        }
    95      }
    96      return fmt
    97    }
    98  }
    99  
   100  /**
   101   * 生成随机字符串
   102   */
   103  export function toAnyString() {
   104    const str: string = 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c: string) => {
   105      const r: number = (Math.random() * 16) | 0
   106      const v: number = c === 'x' ? r : (r & 0x3) | 0x8
   107      return v.toString()
   108    })
   109    return str
   110  }
   111  
   112  export const parseTime = (
   113      time?: object | string | number | null,
   114      cFormat?: string
   115  ): string | null => {
   116    if (time === undefined || !time) {
   117      return null
   118    }
   119    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
   120    let date: Date
   121    if (typeof time === 'object') {
   122      date = time as Date
   123    } else {
   124      if (typeof time === 'string') {
   125        if (/^[0-9]+$/.test(time)) {
   126          // support "1548221490638"
   127        } else {
   128          // '2012-07-04T18:10:00.000+09:00'
   129          time = Date.parse(time)
   130        }
   131      }
   132      if (typeof time === 'number' && time.toString().length === 10) {
   133        time = time * 1000
   134      }
   135      date = new Date(time)
   136    }
   137    const formatObj: { [key: string]: number } = {
   138      y: date.getFullYear(),
   139      m: date.getMonth() + 1,
   140      d: date.getDate(),
   141      h: date.getHours(),
   142      i: date.getMinutes(),
   143      s: date.getSeconds(),
   144      a: date.getDay()
   145    }
   146    const timeStr = format.replace(/{([ymdhisa])+}/g, (result, key) => {
   147      const value = formatObj[key]
   148      // Note: getDay() returns 0 on Sunday
   149      if (key === 'a') {
   150        return ['日', '一', '二', '三', '四', '五', '六'][value]
   151      }
   152      return value.toString().padStart(2, '0')
   153    })
   154    return timeStr
   155  }