pen-it 1.0.7 → 1.0.9

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 CHANGED
@@ -578,7 +578,12 @@ import { isArray, formatFull, deepClone, unique } from 'pen-it'
578
578
 
579
579
  ## 更新日志
580
580
 
581
- ### v1.0.7
581
+ ### v1.0.9
582
+
583
+ - **构建优化** — rolldown 开启 `minify: true`压缩 JS/CJS(注释/空白移除 + 变量名缩短)
584
+ - **类型声明优化** — 移除 `.d.ts` 中冗余的 JSDoc 注释
585
+
586
+ ### v1.0.7 & v1.0.8
582
587
 
583
588
  - **体积优化** — 移除JSDoc `@example` 示例注释(共 294 行),减少包体积
584
589
  - **文档** — README 底部新增更新日志模块
@@ -1,119 +1,12 @@
1
- /**
2
- * Set 去重
3
- * @param arr - 数组
4
- * @returns 去重后的数组
5
- * @example
6
- * unique([1, 2, 2, 3])
7
- * // => [1, 2, 3]
8
- */
9
1
  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
2
  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
3
  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
4
  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
5
  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
6
  export declare const toArray: <T>(arrayLike: ArrayLike<T>) => T[];
58
- /**
59
- * 合并多个数组
60
- * @param arrays - 一个或多个数组
61
- * @returns 合并后的新数组
62
- * @example
63
- * mergeArrays([1, 2], [3, 4], [5])
64
- * // => [1, 2, 3, 4, 5]
65
- */
66
7
  export declare const mergeArrays: <T>(...arrays: T[][]) => T[];
67
- /**
68
- * 将多维数组扁平化到指定层级
69
- * @param arr - 多维数组
70
- * @param depth - 扁平化深度,默认 1(仅展开一层)。传 Infinity 可完全扁平化为一维
71
- * @returns 扁平化后的数组
72
- * @example
73
- * flatten([1, [2, [3, 4]], 5])
74
- * // => [1, 2, [3, 4], 5]
75
- * @example
76
- * flatten([1, [2, [3, 4]], 5], Infinity)
77
- * // => [1, 2, 3, 4, 5]
78
- */
79
8
  export declare const flatten: <T>(arr: any[], depth?: number) => T[];
80
- /**
81
- * 在对象数组中按 key-value 查找第一个匹配项
82
- * @param arr - 对象数组
83
- * @param key - 要匹配的键名
84
- * @param value - 要匹配的键值
85
- * @returns 第一个匹配的对象,无匹配则返回 undefined
86
- * @example
87
- * arrFind([{ id: 1, name: "a" }, { id: 2, name: "b" }], "id", 2)
88
- * // => { id: 2, name: "b" }
89
- */
90
9
  export declare const arrFind: <T extends Record<string, any>>(arr: T[], key: keyof T, value: T[keyof T]) => T | undefined;
91
- /**
92
- * 将对象数组按指定属性分组
93
- * @param arr - 对象数组
94
- * @param key - 用于分组的属性名
95
- * @returns 以属性值为键、对应数组为值的对象
96
- * @example
97
- * groupBy([{ type: "fruit", name: "apple" }, { type: "vegetable", name: "carrot" }], "type")
98
- * // => { fruit: [{ type: "fruit", name: "apple" }], vegetable: [{ type: "vegetable", name: "carrot" }] }
99
- */
100
10
  export declare const groupBy: <T extends Record<string, any>>(arr: T[], key: keyof T) => Record<string, T[]>;
101
- /**
102
- * 移除对象中值为空(null / undefined / 空字符串)的属性
103
- * @param obj - 待过滤的对象
104
- * @returns 过滤后的新对象
105
- * @example
106
- * filterEmptyValues({ a: 1, b: "", c: null, d: 3 })
107
- * // => { a: 1, d: 3 }
108
- */
109
11
  export declare const filterEmptyValues: <T extends Record<string, any>>(obj: T) => Partial<T>;
110
- /**
111
- * 根据长度和映射函数快速生成数组
112
- * @param length - 数组长度
113
- * @param mapFn - 映射函数,接收索引返回元素值,默认为返回索引
114
- * @returns 生成的数组
115
- * @example
116
- * createRange(5)
117
- * // => [0, 1, 2, 3, 4]
118
- */
119
12
  export declare const createRange: <T = number>(length: number, mapFn?: (index: number) => T) => T[];
@@ -1,93 +1,10 @@
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
1
  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
2
  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
3
  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
4
  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
5
  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
6
  export declare const exportJSON: (data: any, filename?: string) => void;
56
- /**
57
- * 滚动到顶部
58
- * @param behavior - 滚动行为,"smooth" 平滑 / "auto" 瞬间,默认 "smooth"
59
- * @example
60
- * scrollToTop()
61
- */
62
7
  export declare const scrollToTop: (behavior?: ScrollBehavior) => void;
63
- /**
64
- * 滚动到底部
65
- * @param behavior - 滚动行为,"smooth" 平滑 / "auto" 瞬间,默认 "smooth"
66
- * @example
67
- * scrollToBottom()
68
- */
69
8
  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
9
  export declare const onScroll: (callback: (scrollY: number) => void) => () => void;
79
- /**
80
- * 监听目标元素是否进入/离开可视区域
81
- * @param target - 目标 DOM 元素
82
- * @param onEnter - 元素进入可视区域时的回调
83
- * @param onLeave - 元素离开可视区域时的回调(可选)
84
- * @param options - IntersectionObserver 配置项
85
- * @returns 清理函数,调用后停止观察
86
- * @example
87
- * const cleanup = observeIntersection(
88
- * document.querySelector(".footer")!,
89
- * () => loadMore(10),
90
- * );
91
- * cleanup(); // 停止观察
92
- */
93
10
  export declare const observeIntersection: (target: Element, onEnter: (entry: IntersectionObserverEntry) => void, onLeave?: (entry: IntersectionObserverEntry) => void, options?: IntersectionObserverInit) => () => void;
@@ -1,33 +1,9 @@
1
- /**
2
- * 防抖 —— 在事件被触发 n 秒后再执行,如果 n 秒内再次触发则重新计时
3
- * @param fn - 需要防抖的函数
4
- * @param delay - 延迟时间(毫秒),默认 300
5
- * @param options.leading - 首次调用是否立即执行,默认 false
6
- * @param options.trailing - 停止调用后是否延迟执行,默认 true
7
- * @returns 防抖后的函数,附带 cancel 方法
8
- * @example
9
- * const fn = debounce((val: string) => console.log(val), 500)
10
- * fn("a"); fn("b"); fn("c")
11
- * // => "c"
12
- */
13
1
  export declare const debounce: <T extends (...args: any[]) => any>(fn: T, delay?: number, { leading, trailing }?: {
14
2
  leading?: boolean;
15
3
  trailing?: boolean;
16
4
  }) => ((...args: Parameters<T>) => void) & {
17
5
  cancel: () => void;
18
6
  };
19
- /**
20
- * 节流 —— 固定间隔内最多执行一次,超出频率的调用被忽略
21
- * @param fn - 需要节流的函数
22
- * @param interval - 间隔时间(毫秒),默认 300
23
- * @param options.leading - 首次调用是否立即执行,默认 true
24
- * @param options.trailing - 最后一次调用后是否尾部执行,默认 true
25
- * @returns 节流后的函数,附带 cancel 方法
26
- * @example
27
- * const fn = throttle((val: string) => console.log(val), 500)
28
- * fn("a"); fn("b"); fn("c")
29
- * // => "a",500ms 后输出 "c"
30
- */
31
7
  export declare const throttle: <T extends (...args: any[]) => any>(fn: T, interval?: number, { leading, trailing }?: {
32
8
  leading?: boolean;
33
9
  trailing?: boolean;
@@ -1,25 +1,3 @@
1
- /**
2
- * 设置 Cookie
3
- * @param name - Cookie 名称
4
- * @param value - Cookie 值
5
- * @param days - 有效天数,默认 7 天
6
- * @example
7
- * setCookie("token", "abc123", 30)
8
- */
9
1
  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
2
  export declare const getCookie: (name: string) => string | null;
19
- /**
20
- * 删除 Cookie
21
- * @param name - Cookie 名称
22
- * @example
23
- * delCookie("token")
24
- */
25
3
  export declare const delCookie: (name: string) => void;
@@ -1,35 +1,3 @@
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
1
  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
2
  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
3
  export declare function shallowClone<T>(obj: T): T;
@@ -1,211 +1,25 @@
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
1
  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
2
  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
3
  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
4
  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
5
  export declare const formatRmb: (value: number, options: {
47
6
  type: "zh-CN";
48
7
  currency: "CNY";
49
8
  }) => string;
50
- /**
51
- * 千位分隔符(数字格式化),中文(逗号分隔)
52
- * @param value - 数值
53
- * @returns 格式化后的千位分隔数字字符串
54
- * @example
55
- * formatNum(1234567)
56
- * // => "1,234,567"
57
- */
58
9
  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
10
  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
11
  export declare const compactEN: (value: number) => string;
78
- /**
79
- * 紧凑计数法(大数简化)_ 中文缩写
80
- * @param value - 数值
81
- * @returns 格式化后的紧凑数字字符串(中文缩写)
82
- * @example
83
- * compactCN(12345)
84
- * // => "1.2万"
85
- */
86
12
  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
13
  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
14
  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
15
  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
16
  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
17
  export declare const kebabToCamel: (str: string) => string;
136
- /**
137
- * 大驼峰或小驼峰 _ 转为 ‘-’ 短横线连接
138
- * @param str - 字符串
139
- * @returns 大驼峰或小驼峰命名格式化的字符串
140
- * @example
141
- * camelToKebab("helloWorld")
142
- * // => "hello-world"
143
- */
144
18
  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
19
  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
20
  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
21
  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
22
  export declare const toCamel: (str: string) => string;
185
- /**
186
- * 首字母大写
187
- * @param str - 字符串
188
- * @returns 首字母大写后的字符串
189
- * @example
190
- * firstUpper("hello")
191
- * // => "Hello"
192
- */
193
23
  export declare const firstUpper: (str: string) => string;
194
- /**
195
- * 首字母小写
196
- * @param str - 字符串
197
- * @returns 首字母小写后的字符串
198
- * @example
199
- * firstLower("Hello")
200
- * // => "hello"
201
- */
202
24
  export declare const firstLower: (str: string) => string;
203
- /**
204
- * 反转字符串
205
- * @param str - 字符串
206
- * @returns 反转后的字符串
207
- * @example
208
- * reverse("hello")
209
- * // => "olleh"
210
- */
211
25
  export declare const reverse: (str: string) => string;