@be-link/utils
v1.0.3
Published
@be-link utils - 通用工具函数库
Readme
@be-link/utils
通用工具函数库,提供常用的类型检测、对象合并、URL 参数处理等功能。
安装
npm install @be-link/utils lodash-es
npm install @types/lodash-es -D
# 或
pnpm add @be-link/utils lodash-es
pnpm install @types/lodash-es -D依赖说明
| 依赖 | 类型 | 说明 |
| ----------- | -------------- | -------------------------- |
| lodash-es | peerDependency | 必须安装,用于深度合并功能 |
功能模块
1. 类型检测 (var-type-detector)
提供精确的 JavaScript 类型检测函数。
import {
is,
isDef,
isUnDef,
isNull,
isNullOrUnDef,
isObject,
isString,
isNumber,
isBoolean,
isArray,
isDate,
isRegExp,
isFunction,
isPromise,
isElement,
isMap,
isSet,
isSymbol,
isEmpty,
isUrl,
isMobile,
} from '@be-link/utils';
// 基础类型检测
is('hello', 'String'); // true
is(123, 'Number'); // true
is([], 'Array'); // true
// null/undefined 检测
isDef(value); // value !== undefined
isUnDef(value); // value === undefined
isNull(value); // value === null
isNullOrUnDef(value); // value === null || value === undefined
// 对象类型检测
isObject({}); // true
isObject([]); // false (数组不是纯对象)
isArray([]); // true
// 特殊类型检测
isPromise(Promise.resolve()); // true
isElement(document.body); // true
isMap(new Map()); // true
isSet(new Set()); // true
// 空值检测(支持多种类型)
isEmpty(''); // true
isEmpty([]); // true
isEmpty({}); // true
isEmpty(new Map()); // true
isEmpty(new Set()); // true
isEmpty(null); // true
isEmpty(undefined); // true
// 格式验证
isUrl('https://example.com'); // true
isMobile('13812345678'); // true2. 深度合并 (merge)
基于 lodash 的深度合并,支持自定义合并策略。
import { deepMerge } from '@be-link/utils';
// 基础合并
const result = deepMerge({ a: 1, b: { c: 2 } }, { b: { d: 3 }, e: 4 });
// { a: 1, b: { c: 2, d: 3 }, e: 4 }
// 数组合并(默认替换)
deepMerge({ arr: [1, 2] }, { arr: [3, 4] });
// { arr: [3, 4] }
// 多对象合并
deepMerge(obj1, obj2, obj3, obj4);3. URL 参数处理 (url)
将对象转换为 URL 查询参数。
import { setObjToUrlParams } from '@be-link/utils';
// 基础用法
setObjToUrlParams('https://example.com', { a: 1, b: 2 });
// 'https://example.com?a=1&b=2'
// 自动过滤 null/undefined
setObjToUrlParams('https://example.com', { a: 1, b: null, c: undefined });
// 'https://example.com?a=1'
// 已有参数时追加
setObjToUrlParams('https://example.com?x=1', { a: 2 });
// 'https://example.com?x=1&a=2'
// 自动编码特殊字符
setObjToUrlParams('https://example.com', { q: '你好 world' });
// 'https://example.com?q=%E4%BD%A0%E5%A5%BD%20world'API 参考
类型检测函数
| 函数 | 说明 | 返回值 |
| -------------------- | ------------------------ | --------- |
| is(val, type) | 检测值的类型 | boolean |
| isDef(val) | 是否已定义 | boolean |
| isUnDef(val) | 是否未定义 | boolean |
| isNull(val) | 是否为 null | boolean |
| isNullOrUnDef(val) | 是否为 null 或 undefined | boolean |
| isObject(val) | 是否为纯对象 | boolean |
| isString(val) | 是否为字符串 | boolean |
| isNumber(val) | 是否为数字 | boolean |
| isBoolean(val) | 是否为布尔值 | boolean |
| isArray(val) | 是否为数组 | boolean |
| isDate(val) | 是否为日期 | boolean |
| isRegExp(val) | 是否为正则表达式 | boolean |
| isFunction(val) | 是否为函数 | boolean |
| isPromise(val) | 是否为 Promise | boolean |
| isElement(val) | 是否为 DOM 元素 | boolean |
| isMap(val) | 是否为 Map | boolean |
| isSet(val) | 是否为 Set | boolean |
| isSymbol(val) | 是否为 Symbol | boolean |
| isEmpty(val) | 是否为空值 | boolean |
| isUrl(val) | 是否为有效 URL | boolean |
| isMobile(val) | 是否为中国手机号 | boolean |
工具函数
| 函数 | 说明 | 返回值 |
| --------------------------------- | --------------------- | -------- |
| deepMerge(...objects) | 深度合并多个对象 | object |
| setObjToUrlParams(baseUrl, obj) | 将对象转换为 URL 参数 | string |
TypeScript 支持
import type {
// 类型检测
is,
isDef,
isUnDef,
// ... 其他函数
} from '@be-link/utils';License
MIT
