32

GitHub - fjc0k/vtils: 小巧实用的 JavaScript 工具类库。

 4 years ago
source link: https://github.com/fjc0k/vtils?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

vtils

一个面向业务的 JavaScript/TypeScript 实用程序库。

import { md5 } from 'vtils'

console.log(md5('龙'))
// => 682570a229cbd3d67e76ad99b3152060

# npm
npm i vtils

# yarn
yarn add vtils

# pnpm
pnpm add vtils

若在国内,推荐安装 tbify 使用淘宝镜像加速:

# npm
tnpm i vtils

# yarn
tyn add vtils

# pnpm
tpm add vtils

在 NodeJS 中使用

// 基础工具库
import { wait, isNumber, EventBus, base64Encode } from 'vtils'

// 日期时间工具库
import { formatRelative, subDays, zhCN } from 'vtils/date'

// React 工具库
import { useClassName, useToggle, useScrollLoadMore } from 'vtils/react'

// 验证工具库
import { yup } from 'vtils/validator'

// 类型工具库
import { OmitStrict, LiteralUnion, PartialDeep } from 'vtils/types'

// 小程序工具库
import { getTopBarInfo, navigatePageTo, redirectPageTo } from 'vtils/mp'

详细用法见文档:https://fjc0k.github.io/vtils/v3/

在 Taro 3 中使用

Taro 3 中使用前需要修改 Webpack 配置,在 config/index.js 中的 mini 字段下设置:

mini: {
  webpackChain(config) {
    // 该插件会影响 resolve.extensions 的表现,删去
    config.resolve.plugins.delete('MultiPlatformPlugin')
    // 支持 .taro.js 后缀
    config.resolve.extensions.prepend('.taro.js')
  },
}

然后,就可以愉快地使用了:

// React 工具库
import { useSearchParam } from 'vtils/react'

// 小程序工具库
import { useTopBarInfo } from 'vtils/mp'

export default function () {
  const id = +useSearchParam('id')!
  const topBarInfo = useTopBarInfo()
  // ...
}

在 Deno 中使用

Deno 下暂只支持基础工具库。

// 基础工具库
import {
  wait,
  isNumber,
  EventBus,
  base64Encode,
} from 'https://cdn.skypack.dev/[email protected]'

Jay Fong (c) MIT


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK