简介(Introduction)
nex-lib 是一个轻量的 JavaScript/TypeScript 前端工具库,专注常用业务场景:
- 时间:格式化输出与时间戳获取
- URL:主域名解析、查询参数解析/追加、协议/路径/端口获取
- 校验:邮箱、URL、手机号、日期、邮编、身份证等常见输入验证
- 对象与字符串:深合并、深比较、类型判断、树过滤、驼峰化、反转、省略、随机串、词频统计
- 彩色日志:ANSI 彩色输出,提升调试可读性
特性:TypeScript 友好、支持 Tree‑shaking、零运行时依赖、ESM/CommonJS 双格式、稳定 API(遵循接口不随意变更)。
亮点(Highlights)
- 轻量与按需:零运行时依赖 + Tree‑shaking
- 完整类型:TypeScript 友好,IDE 智能提示
- 全场景覆盖:时间 / URL / 校验 / 对象 / 字符串 / 彩色日志
控制台(Console Ready)
- 文档站已注入常用对象:
window.browserUtils、window.Http、window.NumberUtils、window.StorageUtils - DevTools 直接尝试:
browserUtils.getLocaleInfo('zh-CN')、browserUtils.getCommonLocaleTable()、browserUtils.getPublicIP()
安装(Install)
bash
npm install nex-libbash
pnpm add nex-libbash
yarn add nex-lib快速开始(Quick Start)
ts
import { format, getTimestamp, createWURL, ObjectUtils, StringUtils, ValidationUtils, EchoUtils } from 'nex-lib';
format(); // 'YYYY-MM-DD HH:mm:ss'
getTimestamp(); // 毫秒时间戳
const u = createWURL('https://sub.example.com/path?foo=bar');
u.getMainDomain(); // 'example.com'
u.parseQueryParams(); // { foo: 'bar' }
u.addParamsToURL({ q: 'x' });
ValidationUtils.isValidEmail('a@b.com');
ObjectUtils.deepMerge({ a: 1 }, { b: 2 });
StringUtils.toCamelCase('hello_world');
EchoUtils.green('OK', { id: 1 });ts
import { format, getTimestamp } from 'nex-lib'
format(0)
getTimestamp(0)text
1970-01-01 00:00:00
0CommonJS:
js
const { format, getTimestamp, createWURL, ObjectUtils, StringUtils, ValidationUtils, EchoUtils } = require('nex-lib');兼容说明(Compatibility)
- 统一使用命名导出:
format、getTimestamp。 - 双格式产物:
dist/bundle.cjs.js与dist/bundle.esm.js,支持 Node.js 与各类打包器。
文档(Docs)
- 在线文档:https://webkubor.github.io/nex-lib/
- API:
要求(Requirements)
- Node.js 18+ / 20+
- 任意现代打包器(Vite/Rollup/Webpack)均可使用
快速导航(Quick Navigation)
Time
格式化、UTC、时区、毫秒/秒时间戳、ISO8601。
URL
主域名、查询参数解析/追加、协议/路径/端口。
Solutions
场景解决方案:代码节省对比与最佳实践。
Vue
Composition API 场景:搜索、防抖、倒计时、分享。
React
Hooks 场景:搜索、防抖、倒计时、分享。
HTTP
轻量 fetch 封装:超时/重试/查询/FormData。
Number
货币/数字格式化、百分比、范围与取整。
Color
颜色转换、亮/暗处理、对比色计算。
Object
深合并、深比较、类型判断、树过滤、键/值。
String
驼峰化、反转、省略、随机串、词频统计。
Validation
邮箱、URL、手机号、日期、邮编、身份证。
Echo
ANSI 彩色日志,调试更友好。