Options
All
  • Public
  • Public/Protected
  • All
Menu

Module vx-tools

常用工具函数

Index

References

assert

Renames and exports vx-tools/assert

Functions

asyncFormat

  • asyncFormat<T, U>(promise: Promise<T>): Promise<[T, null] | [null, U]>
  • async 异步包装

    summary

    将 aysnce await 错误作为返回值的处理方式

    example

    asynce load(){

     const [ res, err ] = await asyncFormat( api(...) )
    
     if(res === null){
       console.log(err)
       return
     }
    

    }

    Type parameters

    • T

    • U = any

    Parameters

    • promise: Promise<T>

      被包装promise

    Returns Promise<[T, null] | [null, U]>

    包装后的promise

buildGetItem

  • buildGetItem(storage: typeof localStorage | typeof sessionStorage): <T>(key: string) => T | null
  • Parameters

    • storage: typeof localStorage | typeof sessionStorage

    Returns <T>(key: string) => T | null

      • <T>(key: string): T | null
      • Type parameters

        • T = any

        Parameters

        • key: string

        Returns T | null

buildSetItem

  • buildSetItem(storage: typeof localStorage | typeof sessionStorage): <T>(key: string, data: T) => void
  • Parameters

    • storage: typeof localStorage | typeof sessionStorage

    Returns <T>(key: string, data: T) => void

      • <T>(key: string, data: T): void
      • Type parameters

        • T = any

        Parameters

        • key: string
        • data: T

        Returns void

delay

  • delay<T>(time: number, cb?: () => T): Promise<T | null>
  • 延时器

    summary

    通过promise 将函数推入任务队列,延时执行

    example
      // 模式一
      // 利用 async 等待delay执行完成, 此时delay将向任务队列中插叙空函数 
      async function load(){
        // 延时获取数据
        
        await delay(2)
        const data = await API.getData(...)
        ...
      }
    
      // 模式二
      // 直接将回调函数作物,delay的延时执行函数
      delay(3, () => API.getData(...)).then(data => console.log('data: ', data))
    
    

    Type parameters

    • T = any

    Parameters

    • time: number
    • Optional cb: () => T
        • (): T
        • Returns T

    Returns Promise<T | null>

joinUrlParams

  • joinUrlParams<T>(url: string, obj: T): string
  • 拼接路由参数

    example
     const parmas = {
       id: 1,
       name: 'Co'
     }
    
     const url = joinUrlParams('/users', params)
     // url = /users?id=1&name=Co
    

    Type parameters

    • T: {}

    Parameters

    • url: string
    • obj: T

      参数对象

    Returns string

objectFilter

  • objectFilter<T, U>(keys: U[], source: T): {[ key in U]: any }
  • 对象过滤器

    summary

    通过关键字列表,抽取对象对应字段值,生成新的对象. 只做浅层映射

    example
    const source = {
      id: 12,
      name: 'coco',
      job: 'IT'
    }
    const keys = ['name', 'job']
    const target = objectFilter(keys, source) 
    => { name: 'coco', job: 'IT' }
    

    Type parameters

    • T: {}

    • U: string | number | symbol

    Parameters

    • keys: U[]
    • source: T

    Returns {[ key in U]: any }

registSaveFn

  • registSaveFn(): void
  • 本地缓存包装函数

    summary

    为本地缓存对象 localStorage,sessionStorage 添加 setItem getItem 包装函数 包装函数将原字符参数或返回值通过JSON转换为对象

    remind

    因为使用JSON对数据做转换, 数据格式必须服务json格式, 所以报错纯字符时将报错, 此时可以使用原生方法

    example
    import { registSaveFn } from '@micro/utils'
    registSaveFn() //全局调用一次
     
    const saveKey = 'USER'
    localStorage.$setItem(saveKey, { name: 'copy', sex: 'man' })
    const user = localStorage.$getItem(saveKey)
    console.log(user.name)
    

    Returns void

Generated using TypeDoc