Package Exports
- ut2
- ut2/es/index.js
- ut2/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ut2) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ut2
一个现代 JavaScript 实用工具库。点击查看在线文档。
比较
与其他工具库对比(lodash、underscore)
- 大部分是基于 ES2015+ 标准实现的纯函数,没有过多的包装嵌套。
- 使用 TypeScript 实现,提供类型定义。
- 导出
es、cjs、umd模块格式,支持不同开发环境使用。
兼容性
兼容现代浏览器 Firefox、Chrome、Safari、Opera,以及 Node.js >= 10 。
如果要兼容 IE 系列浏览器,需要提供相应的 Polyfill 支持,建议使用 @babel/preset-env 设置对应的 targets 。
使用
es 或 node 开发环境
安装依赖
npm install ut2yarn add ut2pnpm add ut2浏览器原生开发环境
如果你的项目使用的是原生方式开发,可以在浏览器中使用 script 标签直接引入文件,并使用全局变量 ut2 。
npm 包的 ut2/dist 目录下提供了 UMD 包 ut2.js 以及 ut2.min.js。你也可以通过 UNPKG 下载到本地进行使用。或者直接使用 UNPKG 线上版本 注意版本 。
示例
所有方法都在 ut2 模块中。
import { debounce } from 'ut2';
const debounced = debounce(() => {
// do something
}, 300);API
下列方法全部在
ut2模块直接导出,分类只是为了可读性。你可以点击查看在线文档,了解更多信息。
import { throttle, chunk, max } from 'ut2'
- Array 数组
- Collection 集合
- Function 函数
- Language 语言
- isArguments -
arguments对象。 - isArray -
Array对象。 - isArrayBuffer -
ArrayBuffer对象。 - isArrayLike - 类数组。
- isArrayLikeObject - 类数组对象。
- isBigInt -
bigint类型或对象。 - isBlob -
Blob对象。 - isBoolean - 布尔类型或对象。
- isBuffer -
Buffer对象。 - isDataView -
DataView对象。 - isDate -
Date对象。 - isElement -
Dom元素。 - isEmpty - 空对象、数组、
Map、Set。 - isEqual - 深比较。
- isError -
Error对象。 - isFinite - 有限数字。
- isFunction -
Function对象。 - isInteger - 整数。
- isLength - 有效的类数组长度。
- isMap -
Map对象。 - isMatch - 普通对象部分匹配的深比较。
- isNaN -
NaN。 - isNil -
undefined或null。 - isNull -
null。 - isNumber - 数字类型或对象。
- isObject - 对象。
- isObjectLike - 类对象。
- isPlainObject - 普通对象。
- isPromiseLike - 类
Promise对象。 - isRegExp -
RegExp对象。 - isSafeInteger - 安全整数。
- isSet -
Set对象。 - isString - 字符串类型或对象。
- isSymbol -
Symbol类型或对象。 - isTypedArray - 类型化数组。
- isUndefined -
undefined。 - isWeakMap -
WeakMap对象。 - isWeakSet -
WeakSet对象。
- isArguments -
- Math 数学
- Number 数字
- Object 对象
- String 字符串
- camelCase - 驼峰小写。
- capitalize - 首字母大写,其余小写。
- escape - 转义
&<>"'为 HTML 实体字符。 - escapeRegExp - 转义为
RegExp字符串中的特殊字符。 - kebabCase - 连接符分隔小写。
- lowerCase - 空格分隔小写。
- lowerFirst - 首字母小写。
- pascalCase - 帕斯卡小写。又名大驼峰。
- snakeCase - 下划线分隔小写。
- unescape - 转义字符串中的 HTML 实体字符
&<"'。 - upperCase - 空格分隔大写。
- upperFirst - 首字母大写。
- words - 拆分词组。
- Util 实用工具
- castArray - 包装成数组。
- conforms - 创建断言对象属性的方法。
- conformsTo - 断言对象属性。
- constant - 返回自身的函数。
- defaultTo - 默认值。
- eq - 等于。
- gt - 大于。
- gte - 大于等于
- identity - 返回第一个参数的函数。
- lt - 小于。
- lte - 小于等于。
- noop - 空函数。
- nthArg - 返回指定位置参数的函数。
- range - 创建升序或降序的数字数组。
- sleep - 返回
promise延迟。 - times - 迭代执行次数。
- toFinite - 转为有限数字。
- toInteger - 转为整数。
- toLength - 转为数组长度整数。
- toNumber - 转为数字。
- toSafeInteger - 转为安全整数。
- toString - 转为字符串。
- uniqueId - 唯一 ID。