pen-it 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,200 @@
1
+ # pen-it
2
+
3
+ > 前端常用工具函数,笔笔记下,随手即用
4
+
5
+ 一个轻量级的前端工具函数库,涵盖类型判断、日期格式化、数字格式化、字符串处理、存储操作、深拷贝、数组操作、Cookie 操作、浏览器 API 等常用场景。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ pnpm add pen-it
11
+ ```
12
+
13
+ ## 使用
14
+
15
+ ```ts
16
+ import { isArray, formatFull, deepClone, unique } from 'pen-it'
17
+ ```
18
+
19
+ ---
20
+
21
+ ## API
22
+
23
+ ### 类型判断 — `is`
24
+
25
+ | 函数 | 说明 | 示例 |
26
+ |------|------|------|
27
+ | `isArray(value)` | 判断是否为数组 | `isArray([1,2,3]) // true` |
28
+ | `isObject(value)` | 判断是否为对象(不含数组) | `isObject({a:1}) // true` |
29
+ | `is(val, type)` | 通用类型判断(内部工具) | `is([], "Array") // true` |
30
+ | `isFunction(val)` | 判断是否为函数 | `isFunction(()=>{}) // true` |
31
+ | `isAsyncFunction(val)` | 判断是否为异步函数 | `isAsyncFunction(async ()=>{}) // true` |
32
+ | `isPromise(value)` | 判断是否为 Promise | `isPromise(Promise.resolve()) // true` |
33
+ | `isDate(val)` | 判断是否为 Date 对象 | `isDate(new Date()) // true` |
34
+ | `isNumber(val)` | 判断是否为数字(含 NaN/Infinity) | `isNumber(42) // true` |
35
+ | `isInt(val)` | 判断是否为整数 | `isInt(42) // true` |
36
+ | `isFloat(val)` | 判断是否为浮点数 | `isFloat(3.14) // true` |
37
+ | `isString(val)` | 判断是否为字符串 | `isString("hello") // true` |
38
+ | `isBoolean(val)` | 判断是否为布尔值 | `isBoolean(true) // true` |
39
+ | `isSymbol(value)` | 判断是否为 Symbol | `isSymbol(Symbol("foo")) // true` |
40
+ | `isPrimitive(value)` | 判断是否为原始类型 | `isPrimitive(42) // true` |
41
+ | `isNull(val)` | 判断是否为 null | `isNull(null) // true` |
42
+ | `isDef(val)` | 判断是否不是 undefined | `isDef("hello") // true` |
43
+ | `isUnDef(val)` | 判断是否为 undefined | `isUnDef(undefined) // true` |
44
+ | `isNullOrUnDef(val)` | 判断是否为 null 或 undefined | `isNullOrUnDef(null) // true` |
45
+ | `isEmpty(value)` | 判断是否为空(完备版:支持 Date/Map/Set 等) | `isEmpty("") // true` |
46
+ | `isEmptySv(value)` | 判断是否为空(简化版) | `isEmptySv([]) // true` |
47
+ | `isEqual(x, y)` | 深度相等比较(支持 Date/RegExp) | `isEqual({a:1}, {a:1}) // true` |
48
+ | `isHexColor(str)` | 判断是否为十六进制颜色 | `isHexColor("#fff") // true` |
49
+ | `isValidEmail(email)` | 判断是否为有效邮箱 | `isValidEmail("a@b.com") // true` |
50
+ | `isPC()` | 判断是否为浏览器环境 | `isPC() // true` |
51
+ | `isWindow(val)` | 判断是否为 window 对象 | `isWindow(window) // true` |
52
+ | `isElement(val)` | 判断是否为 DOM 元素 | `isElement(document.body) // true` |
53
+ | `isIOS()` | 判断设备是否为 iOS | `isIOS() // true` |
54
+
55
+ ---
56
+
57
+ ### 日期时间格式化 — `format`
58
+
59
+ | 函数 | 说明 | 示例 |
60
+ |------|------|------|
61
+ | `formatFull(date)` | 完整日期时间(斜杠分隔) | `"2026/06/03 14:30:45"` |
62
+ | `formatFullReplace(date)` | 完整日期时间(短横线分隔) | `"2026-06-03 14:30:45"` |
63
+ | `formatYMD(date)` | 中文年月日 | `"2026年6月3日"` |
64
+ | `formatWeek(date)` | 星期几 | `"星期三"` |
65
+
66
+ ---
67
+
68
+ ### 数字与货币格式化 — `format`
69
+
70
+ | 函数 | 说明 | 示例 |
71
+ |------|------|------|
72
+ | `formatRmb(value, options)` | 货币格式化(人民币) | `formatRmb(1234.56, { type: "zh-CN", currency: "CNY" }) // "¥1,234.56"` |
73
+ | `formatNum(value)` | 千位分隔符 | `formatNum(1234567) // "1,234,567"` |
74
+ | `percentCN(value, digit)` | 百分比格式化 | `percentCN(0.1234, 2) // "12.34%"` |
75
+ | `compactEN(value)` | 大数简化(英文缩写) | `compactEN(12345) // "12K"` |
76
+ | `compactCN(value)` | 大数简化(中文缩写) | `compactCN(12345) // "1.2万"` |
77
+ | `signed(value, digit)` | 带正负号显示 | `signed(42, 1) // "+42.0"` |
78
+
79
+ ---
80
+
81
+ ### 字符串处理 — `format`
82
+
83
+ | 函数 | 说明 | 示例 |
84
+ |------|------|------|
85
+ | `maskPhone(phone)` | 手机号脱敏(隐藏中间4位) | `maskPhone("13812345678") // "138****5678"` |
86
+ | `spacePhone(phone)` | 手机号空格分隔 | `spacePhone("13812345678") // "138 1234 5678"` |
87
+ | `capitalize(str)` | 每个单词首字母大写 | `capitalize("hello world") // "Hello World"` |
88
+ | `kebabToCamel(str)` | 短横线转小驼峰 | `kebabToCamel("hello-world") // "helloWorld"` |
89
+ | `camelToKebab(str)` | 驼峰转短横线 | `camelToKebab("helloWorld") // "hello-world"` |
90
+ | `toCamel(str)` | 下划线转驼峰 | `toCamel("hello_world") // "helloWorld"` |
91
+ | `firstUpper(str)` | 首字母大写 | `firstUpper("hello") // "Hello"` |
92
+ | `firstLower(str)` | 首字母小写 | `firstLower("Hello") // "hello"` |
93
+ | `reverse(str)` | 反转字符串 | `reverse("hello") // "olleh"` |
94
+ | `trimAll(str)` | 去除所有空格 | `trimAll(" h e l lo ") // "hello"` |
95
+ | `truncate(str, max, suffix?)` | 超长文本截断 | `truncate("Hello World", 8) // "Hello..."` |
96
+ | `truncateByWords(str, max, suffix?)` | 按字数截断(中文友好) | `truncateByWords("你好世界欢迎你", 4) // "你好世界..."` |
97
+
98
+ ---
99
+
100
+ ### 存储 — `storage`
101
+
102
+ | 函数 | 说明 | 示例 |
103
+ |------|------|------|
104
+ | `localGet<T>(key)` | 获取 localStorage(自动 JSON 解析) | `localGet("user") // { name: "张三" }` |
105
+ | `localSet(key, value)` | 设置 localStorage(自动 JSON 序列化) | `localSet("user", { name: "张三" })` |
106
+ | `localRm(key)` | 移除指定 localStorage | `localRm("user")` |
107
+ | `localClear()` | 清除所有 localStorage | `localClear()` |
108
+
109
+ ---
110
+
111
+ ### 拷贝 — `copy`
112
+
113
+ | 函数 | 说明 | 示例 |
114
+ |------|------|------|
115
+ | `deepClone(obj)` | 递归深拷贝(支持 Date/RegExp/Map/Set/循环引用) | `deepClone({a:1, b:{c:2}})` |
116
+ | `deepCloneWithJSON(obj)` | JSON 深拷贝(仅 JSON 安全类型) | `deepCloneWithJSON({a:1})` |
117
+ | `shallowClone(obj)` | 浅拷贝(仅第一层) | `shallowClone({a:1, b:{c:2}})` |
118
+
119
+ ---
120
+
121
+ ### 数组 — `array`
122
+
123
+ | 函数 | 说明 | 示例 |
124
+ |------|------|------|
125
+ | `unique(arr)` | Set 去重 | `unique([1,2,2,3]) // [1,2,3]` |
126
+ | `uniqueByKey(arr, key)` | 对象数组按 key 去重 | `uniqueByKey([{id:1},{id:1}], "id")` |
127
+ | `sortNumAsc(arr)` | 数值升序 | `sortNumAsc([3,1,2]) // [1,2,3]` |
128
+ | `sortNumDesc(arr)` | 数值降序 | `sortNumDesc([1,3,2]) // [3,2,1]` |
129
+ | `sortByKey(arr, key, order?)` | 按对象属性排序 | `sortByKey([{age:30},{age:20}], "age")` |
130
+ | `toArray(arrayLike)` | 类数组转数组 | `toArray(document.querySelectorAll("div"))` |
131
+
132
+ ---
133
+
134
+ ### Cookie — `cookie`
135
+
136
+ | 函数 | 说明 | 示例 |
137
+ |------|------|------|
138
+ | `setCookie(name, value, days?)` | 设置 Cookie(默认7天) | `setCookie("token", "abc123", 30)` |
139
+ | `getCookie(name)` | 获取 Cookie | `getCookie("token") // "abc123"` |
140
+ | `delCookie(name)` | 删除 Cookie | `delCookie("token")` |
141
+
142
+ ---
143
+
144
+ ### 浏览器工具 — `browser`
145
+
146
+ #### URL 参数
147
+
148
+ | 函数 | 说明 | 示例 |
149
+ |------|------|------|
150
+ | `getUrlParams(url?)` | 获取 URL 参数对象 | `getUrlParams("?a=1&b=2") // {a:"1",b:"2"}` |
151
+ | `getUrlParam(key, url?)` | 获取单个 URL 参数 | `getUrlParam("a") // "1"` |
152
+ | `toQueryString(params)` | 对象转 URL 参数字符串 | `toQueryString({a:1,b:"hello"}) // "a=1&b=hello"` |
153
+
154
+ #### 剪贴板与文件
155
+
156
+ | 函数 | 说明 | 示例 |
157
+ |------|------|------|
158
+ | `copyToClipboard(text)` | 复制文本到剪贴板(支持降级) | `await copyToClipboard("Hello")` |
159
+ | `downloadFile(content, filename, mimeType?)` | 下载文件(Blob) | `downloadFile("Hello", "hello.txt")` |
160
+ | `exportJSON(data, filename?)` | 导出 JSON 为文件 | `exportJSON({name:"张三"})` |
161
+
162
+ #### 页面滚动
163
+
164
+ | 函数 | 说明 | 示例 |
165
+ |------|------|------|
166
+ | `scrollToTop(behavior?)` | 滚动到顶部(默认平滑) | `scrollToTop()` |
167
+ | `scrollToBottom(behavior?)` | 滚动到底部(默认平滑) | `scrollToBottom()` |
168
+ | `onScroll(callback)` | 监听滚动(rAF 节流),返回清理函数 | `const off = onScroll(y => console.log(y))` |
169
+
170
+ ---
171
+
172
+ ## 项目结构
173
+
174
+ ```
175
+ pen-it/
176
+ ├── dist/ # 构建输出(CJS + ESM + .d.ts)
177
+ ├── src/ # 源码
178
+ │ ├── index.ts # 统一导出入口
179
+ │ ├── is/ # 类型判断(28个函数)
180
+ │ ├── format/ # 日期/数字/字符串格式化(22个函数)
181
+ │ ├── storage/ # localStorage 操作(4个函数)
182
+ │ ├── copy/ # 深拷贝/浅拷贝(3个函数)
183
+ │ ├── array/ # 去重/排序/转换(6个函数)
184
+ │ ├── cookie/ # Cookie 操作(3个函数)
185
+ │ └── browser/ # URL参数/剪贴板/文件下载/滚动(9个函数)
186
+ ├── tests/ # 单元测试
187
+ │ ├── is.test.ts
188
+ │ ├── format.test.ts
189
+ │ ├── storage.test.ts
190
+ │ ├── copy.test.ts
191
+ │ ├── array.test.ts
192
+ │ ├── cookie.test.ts
193
+ │ └── browser.test.ts
194
+ ├── package.json # 包配置(入口、脚本、依赖)
195
+ ├── tsconfig.json # TypeScript 编译配置
196
+ ├── rolldown.config.js # 打包配置(Rolldown)
197
+ ├── vitest.config.ts # 测试配置(Vitest)
198
+ ├── pnpm-lock.yaml
199
+ └── dist/ # 构建输出(CJS + ESM + .d.ts)
200
+ ```
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Set 去重
3
+ * @param arr - 数组
4
+ * @returns 去重后的数组
5
+ * @example
6
+ * unique([1, 2, 2, 3])
7
+ * // => [1, 2, 3]
8
+ */
9
+ export declare const unique: <T>(arr: T[]) => T[];
10
+ /**
11
+ * 对象数组按 key 去重
12
+ * @param arr - 对象数组
13
+ * @param key - 用于去重判定的键名
14
+ * @returns 去重后的数组
15
+ * @example
16
+ * uniqueByKey([{ id: 1, name: "a" }, { id: 1, name: "b" }], "id")
17
+ * // => [{ id: 1, name: "a" }]
18
+ */
19
+ export declare const uniqueByKey: <T extends Record<string, any>>(arr: T[], key: keyof T) => T[];
20
+ /**
21
+ * 数值升序
22
+ * @param arr - 数值数组
23
+ * @returns 升序排序后的新数组
24
+ * @example
25
+ * sortNumAsc([3, 1, 2])
26
+ * // => [1, 2, 3]
27
+ */
28
+ export declare const sortNumAsc: (arr: number[]) => number[];
29
+ /**
30
+ * 数值降序
31
+ * @param arr - 数值数组
32
+ * @returns 降序排序后的新数组
33
+ * @example
34
+ * sortNumDesc([1, 3, 2])
35
+ * // => [3, 2, 1]
36
+ */
37
+ export declare const sortNumDesc: (arr: number[]) => number[];
38
+ /**
39
+ * 按对象属性排序
40
+ * @param arr - 对象数组
41
+ * @param key - 用于排序的属性名
42
+ * @param order - 排序方向,"asc" 升序 / "desc" 降序,默认 "asc"
43
+ * @returns 排序后的新数组
44
+ * @example
45
+ * sortByKey([{ age: 30 }, { age: 20 }], "age", "asc")
46
+ * // => [{ age: 20 }, { age: 30 }]
47
+ */
48
+ export declare const sortByKey: <T extends Record<string, any>>(arr: T[], key: keyof T, order?: "asc" | "desc") => T[];
49
+ /**
50
+ * 类数组转数组
51
+ * @param arrayLike - 类数组对象(如 NodeList、arguments)
52
+ * @returns 转换后的数组
53
+ * @example
54
+ * toArray(document.querySelectorAll("div"))
55
+ * // => [div, div, ...]
56
+ */
57
+ export declare const toArray: <T>(arrayLike: ArrayLike<T>) => T[];
@@ -0,0 +1,78 @@
1
+ /**
2
+ * 获取 URL 参数对象
3
+ * @param url - URL 字符串,默认使用当前页面地址
4
+ * @returns 参数键值对对象
5
+ * @example
6
+ * getUrlParams("https://example.com?a=1&b=2")
7
+ * // => { a: "1", b: "2" }
8
+ */
9
+ export declare const getUrlParams: (url?: string) => Record<string, string>;
10
+ /**
11
+ * 获取单个 URL 参数
12
+ * @param key - 参数名
13
+ * @param url - URL 字符串,默认使用当前页面地址
14
+ * @returns 参数值,不存在则返回 null
15
+ * @example
16
+ * getUrlParam("a", "https://example.com?a=1")
17
+ * // => "1"
18
+ */
19
+ export declare const getUrlParam: (key: string, url?: string) => string | null;
20
+ /**
21
+ * 对象转 URL 参数字符串
22
+ * @param params - 参数键值对对象,值为 null/undefined 的项会被过滤
23
+ * @returns URL 参数字符串(不含 ? 前缀)
24
+ * @example
25
+ * toQueryString({ a: 1, b: "hello", c: null })
26
+ * // => "a=1&b=hello"
27
+ */
28
+ export declare const toQueryString: (params: Record<string, any>) => string;
29
+ /**
30
+ * 复制文本到剪贴板
31
+ * 优先使用 Clipboard API,失败时降级为 execCommand
32
+ * @param text - 要复制的文本
33
+ * @returns 复制成功返回 true,失败返回 false
34
+ * @example
35
+ * await copyToClipboard("Hello World")
36
+ * // => true
37
+ */
38
+ export declare const copyToClipboard: (text: string) => Promise<boolean>;
39
+ /**
40
+ * 下载文件(Blob)
41
+ * @param content - 文件内容
42
+ * @param filename - 文件名
43
+ * @param mimeType - MIME 类型,默认 "text/plain"
44
+ * @example
45
+ * downloadFile("Hello", "hello.txt")
46
+ */
47
+ export declare const downloadFile: (content: string | BlobPart, filename: string, mimeType?: string) => void;
48
+ /**
49
+ * 导出 JSON 为文件
50
+ * @param data - 要导出的数据
51
+ * @param filename - 文件名,默认 "data.json"
52
+ * @example
53
+ * exportJSON({ name: "张三", age: 30 })
54
+ */
55
+ export declare const exportJSON: (data: any, filename?: string) => void;
56
+ /**
57
+ * 滚动到顶部
58
+ * @param behavior - 滚动行为,"smooth" 平滑 / "auto" 瞬间,默认 "smooth"
59
+ * @example
60
+ * scrollToTop()
61
+ */
62
+ export declare const scrollToTop: (behavior?: ScrollBehavior) => void;
63
+ /**
64
+ * 滚动到底部
65
+ * @param behavior - 滚动行为,"smooth" 平滑 / "auto" 瞬间,默认 "smooth"
66
+ * @example
67
+ * scrollToBottom()
68
+ */
69
+ export declare const scrollToBottom: (behavior?: ScrollBehavior) => void;
70
+ /**
71
+ * 监听页面滚动(requestAnimationFrame 节流)
72
+ * @param callback - 滚动回调,接收当前 scrollY 值
73
+ * @returns 清理函数,调用后移除事件监听
74
+ * @example
75
+ * const cleanup = onScroll((y) => console.log(y));
76
+ * cleanup(); // 停止监听
77
+ */
78
+ export declare const onScroll: (callback: (scrollY: number) => void) => () => void;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 设置 Cookie
3
+ * @param name - Cookie 名称
4
+ * @param value - Cookie 值
5
+ * @param days - 有效天数,默认 7 天
6
+ * @example
7
+ * setCookie("token", "abc123", 30)
8
+ */
9
+ export declare const setCookie: (name: string, value: string, days?: number) => void;
10
+ /**
11
+ * 获取 Cookie
12
+ * @param name - Cookie 名称
13
+ * @returns Cookie 值,不存在则返回 null
14
+ * @example
15
+ * getCookie("token")
16
+ * // => "abc123"
17
+ */
18
+ export declare const getCookie: (name: string) => string | null;
19
+ /**
20
+ * 删除 Cookie
21
+ * @param name - Cookie 名称
22
+ * @example
23
+ * delCookie("token")
24
+ */
25
+ export declare const delCookie: (name: string) => void;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * 递归深拷贝
3
+ * 支持 Date、RegExp、Map、Set、Array、Object 以及循环引用
4
+ * @param obj - 要拷贝的值
5
+ * @param hash - 内部 WeakMap 用于处理循环引用,调用方无需传入
6
+ * @returns 深拷贝后的值
7
+ * @example
8
+ * const obj = { a: 1, b: { c: 2 }, d: new Date() };
9
+ * const cloned = deepClone(obj);
10
+ * // cloned.b !== obj.b
11
+ * // cloned.d !== obj.d
12
+ */
13
+ export declare function deepClone<T>(obj: T, hash?: WeakMap<any, any>): T;
14
+ /**
15
+ * JSON 深拷贝
16
+ * 使用 JSON 序列化与反序列化实现,仅支持 JSON 安全的数据类型
17
+ * (不支持函数、undefined、Date、RegExp、Map、Set 等)
18
+ * @param obj - 要拷贝的 JSON 安全值
19
+ * @returns 深拷贝后的值
20
+ * @example
21
+ * deepCloneWithJSON({ a: 1, b: [2, 3] })
22
+ * // => { a: 1, b: [2, 3] }
23
+ */
24
+ export declare function deepCloneWithJSON<T>(obj: T): T;
25
+ /**
26
+ * 浅拷贝
27
+ * 仅拷贝第一层属性,嵌套对象仍共享引用
28
+ * @param obj - 要拷贝的对象或数组
29
+ * @returns 浅拷贝后的值
30
+ * @example
31
+ * const obj = { a: 1, b: { c: 2 } };
32
+ * const cloned = shallowClone(obj);
33
+ * // cloned.b === obj.b(共享引用)
34
+ */
35
+ export declare function shallowClone<T>(obj: T): T;
@@ -0,0 +1,211 @@
1
+ /**
2
+ * 完整日期时间(年/月/日 时分秒)_ 斜杠
3
+ * @param date - Date 对象
4
+ * @returns 格式化后的完整日期时间字符串,如 "2026/06/03 14:30:45"
5
+ * @example
6
+ * formatFull(new Date("2026-06-03T14:30:45"))
7
+ * // => "2026/06/03 14:30:45"
8
+ */
9
+ export declare const formatFull: (date: Date) => string;
10
+ /**
11
+ * 完整日期时间(年-月-日 时分秒)_ 短横线
12
+ * @param date - Date 对象
13
+ * @returns 格式化后的完整日期时间字符串,如 "2026-06-03 14:30:45"
14
+ * @example
15
+ * formatFullReplace(new Date("2026-06-03T14:30:45"))
16
+ * // => "2026-06-03 14:30:45"
17
+ */
18
+ export declare const formatFullReplace: (date: Date) => string;
19
+ /**
20
+ * 中文年月日
21
+ * @param date - Date 对象
22
+ * @returns 格式化后的中文日期字符串,如 "2026年6月3日"
23
+ * @example
24
+ * formatYMD(new Date("2026-06-03"))
25
+ * // => "2026年6月3日"
26
+ */
27
+ export declare const formatYMD: (date: Date) => string;
28
+ /**
29
+ * 星期几
30
+ * @param date - Date 对象
31
+ * @returns 中文星期字符串,如 "星期三"
32
+ * @example
33
+ * formatWeek(new Date("2026-06-03"))
34
+ * // => "星期三"
35
+ */
36
+ export declare const formatWeek: (date: Date) => string;
37
+ /**
38
+ * 货币格式化
39
+ * @param value - 数值
40
+ * @param options - 格式化选项
41
+ * @returns 格式化后的货币字符串
42
+ * @example
43
+ * formatRmb(1234.56, { type: "zh-CN", currency: "CNY" })
44
+ * // => "¥1,234.56"
45
+ */
46
+ export declare const formatRmb: (value: number, options: {
47
+ type: "zh-CN";
48
+ currency: "CNY";
49
+ }) => string;
50
+ /**
51
+ * 千位分隔符(数字格式化),中文(逗号分隔)
52
+ * @param value - 数值
53
+ * @returns 格式化后的千位分隔数字字符串
54
+ * @example
55
+ * formatNum(1234567)
56
+ * // => "1,234,567"
57
+ */
58
+ export declare const formatNum: (value: number) => string;
59
+ /**
60
+ * 百分比格式化
61
+ * @param value - 百分比数值
62
+ * @param digit - 保留的小数位数,默认为 0
63
+ * @returns 格式化后的百分比字符串
64
+ * @example
65
+ * percentCN(0.1234, 2)
66
+ * // => "12.34%"
67
+ */
68
+ export declare const percentCN: (value: number, digit?: number) => string;
69
+ /**
70
+ * 紧凑计数法(大数简化)_ 英文缩写
71
+ * @param value - 数值
72
+ * @returns 格式化后的紧凑数字字符串(英文缩写)
73
+ * @example
74
+ * compactEN(12345)
75
+ * // => "12K"
76
+ */
77
+ export declare const compactEN: (value: number) => string;
78
+ /**
79
+ * 紧凑计数法(大数简化)_ 中文缩写
80
+ * @param value - 数值
81
+ * @returns 格式化后的紧凑数字字符串(中文缩写)
82
+ * @example
83
+ * compactCN(12345)
84
+ * // => "1.2万"
85
+ */
86
+ export declare const compactCN: (value: number) => string;
87
+ /**
88
+ * 带符号的正负数显示
89
+ * @param value - 数值
90
+ * @param digit - 保留的小数位数,默认为 0
91
+ * @returns 格式化后的带正负号的数值显示
92
+ * @example
93
+ * signed(42, 1)
94
+ * // => "+42.0"
95
+ * @example
96
+ * signed(-5)
97
+ * // => "-5"
98
+ */
99
+ export declare const signed: (value: number, digit?: number) => string;
100
+ /**
101
+ * 手机号格式化脱敏 _ 隐藏中间 4 位数
102
+ * @param phone - 手机号码
103
+ * @returns 手机号格式化脱敏后的手机号
104
+ * @example
105
+ * maskPhone("13812345678")
106
+ * // => "138****5678"
107
+ */
108
+ export declare const maskPhone: (phone: string) => string;
109
+ /**
110
+ * 手机号格式化 _ 空格分隔
111
+ * @param phone - 手机号
112
+ * @returns 手机号空格格式化的手机号
113
+ * @example
114
+ * spacePhone("13812345678")
115
+ * // => "138 1234 5678"
116
+ */
117
+ export declare const spacePhone: (phone: string) => string;
118
+ /**
119
+ * 每个单词首字母大写
120
+ * @param str - 字符串
121
+ * @returns 每个单词首字母大写后的字符串
122
+ * @example
123
+ * capitalize("hello world")
124
+ * // => "Hello World"
125
+ */
126
+ export declare const capitalize: (str: string) => string;
127
+ /**
128
+ * 短横线连接转小驼峰(kebab-case → camelCase)
129
+ * @param str - 字符串
130
+ * @returns 转换后的小驼峰字符串
131
+ * @example
132
+ * kebabToCamel("hello-world")
133
+ * // => "helloWorld"
134
+ */
135
+ export declare const kebabToCamel: (str: string) => string;
136
+ /**
137
+ * 大驼峰或小驼峰 _ 转为 ‘-’ 短横线连接
138
+ * @param str - 字符串
139
+ * @returns 大驼峰或小驼峰命名格式化的字符串
140
+ * @example
141
+ * camelToKebab("helloWorld")
142
+ * // => "hello-world"
143
+ */
144
+ export declare const camelToKebab: (str: string) => string;
145
+ /**
146
+ * 超长文本截断
147
+ * @param str - 内容
148
+ * @param maxLength - 截断位置
149
+ * @param suffix - 自定义的替换截断后的内容
150
+ * @returns 超长文本截断后内容
151
+ * @example
152
+ * truncate("Hello World", 8)
153
+ * // => "Hello..."
154
+ */
155
+ export declare const truncate: (str: string, maxLength: number, suffix?: string) => string;
156
+ /**
157
+ * 按字数截断(中文友好)
158
+ * @param str - 内容
159
+ * @param maxWords - 截断位置
160
+ * @param suffix - 自定义的替换截断后的内容
161
+ * @returns 超长文本截断后内容
162
+ * @example
163
+ * truncateByWords("你好世界欢迎你", 4)
164
+ * // => "你好世界..."
165
+ */
166
+ export declare const truncateByWords: (str: string, maxWords: number, suffix?: string) => string;
167
+ /**
168
+ * 去除所有空格 _ 前后中间
169
+ * @param str - 内容
170
+ * @returns 去除空格后的内容
171
+ * @example
172
+ * trimAll(" hello world ")
173
+ * // => "helloworld"
174
+ */
175
+ export declare const trimAll: (str: string) => string;
176
+ /**
177
+ * 下划线转驼峰(snake_case → camelCase)
178
+ * @param str - 字符串
179
+ * @returns 转换后的驼峰字符串
180
+ * @example
181
+ * toCamel("hello_world")
182
+ * // => "helloWorld"
183
+ */
184
+ export declare const toCamel: (str: string) => string;
185
+ /**
186
+ * 首字母大写
187
+ * @param str - 字符串
188
+ * @returns 首字母大写后的字符串
189
+ * @example
190
+ * firstUpper("hello")
191
+ * // => "Hello"
192
+ */
193
+ export declare const firstUpper: (str: string) => string;
194
+ /**
195
+ * 首字母小写
196
+ * @param str - 字符串
197
+ * @returns 首字母小写后的字符串
198
+ * @example
199
+ * firstLower("Hello")
200
+ * // => "hello"
201
+ */
202
+ export declare const firstLower: (str: string) => string;
203
+ /**
204
+ * 反转字符串
205
+ * @param str - 字符串
206
+ * @returns 反转后的字符串
207
+ * @example
208
+ * reverse("hello")
209
+ * // => "olleh"
210
+ */
211
+ export declare const reverse: (str: string) => string;