Skip to content

Utils

内置多种便捷工具函数。

  • 复制数据到剪切板

    ts
    import { copyDataToClipboard } from '@fs/lib'
    
    function copy() {
      copyDataToClipboard('fs.com').then(() => {
        console.log('success')
      })
    }
  • 针对文件大小格式化

    ts
    import { formatKB } from '@fs/lib'
    
    formatKB(600) // 600KB
    formatKB(1024 * 2) // 2MB
    formatKB(1024 * 1024 * 2) // 2GB
  • 处理style可能会用到

    ts
    import { formatSize, unitToNumber } from '@fs/lib'
    
    formatSize(16) // 16px
    unitToNumber('16px') // 16
  • 时区处理(浏览器环境)

    ts
    import { timeZone } from '@fs/lib'
    
    timeZone(new Date())
  • 缓存

    适用于一些数据基本不变的情况,如全局可用的下拉项

    ts
    import { http, memo, withHadlerResponse } from '@fs/lib'
    
    // 获取所有国家(数据源:ERP)
    export const queryCountries = memo(
      withHadlerResponse(() =>
        http.get({
          url: '/wms/basic/data/getAllCountry',
        }),
      ),
    )
  • sessionStorage & localStorage

    ts
    import { cacheStorage } from '@fs/lib'
    
    cacheStorage.set('Key_Name', 'libai')
    cacheStorage.get('Key_Name') // libai
    
    // 或者如下方式
    const oneStorage = new CacheStorage(`Key_Name`)
    oneStorage.set('libai')
    oneStorage.get() // libai
  • TreeData 处理

    ts
    import { treeEach, treeFind } from '@fs/lib'
    
    const treeData = [
      {
        label: 'A',
        value: 'a',
        children: [
          {
            label: 'A-1',
            value: 'a-1',
            children: [
              {
                label: 'A-1-1',
                value: 'a-1-1',
              },
            ],
          },
          {
            label: 'A-2',
            value: 'a-2',
          },
        ],
      },
    ]
    
    treeEach(
      treeData,
      (v) => {
        console.log(v)
      },
      v => v.children,
    )
    
    const res = treeFind(
      treeData,
      v => v.value === 'a-1-1',
      v => v.children,
    )
    console.log(res) // { label: 'A-1-1', value: 'a-1-1' }
  • loadScript

    如引用某些第三方sdk,可在适当的时机引用,而不是全局

    ts
    import { loadScript } from '@fs/lib'
    
    loadScript('https://cdn.xx.umd.js')