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.
@@ -0,0 +1,286 @@
1
+ /**
2
+ * 判断一个值是否为数组
3
+ * @param value - 要判断的值
4
+ * @returns 如果是数组返回 true,否则返回 false
5
+ * @example
6
+ * isArray([1, 2, 3])
7
+ * // => true
8
+ * isArray("hello")
9
+ * // => false
10
+ */
11
+ export declare const isArray: (value: any) => boolean;
12
+ /**
13
+ * 判断一个值是否为对象(但不是数组)
14
+ * @param value - 要判断的值
15
+ * @returns 如果是对象且不是数组返回 true,否则返回 false
16
+ * @example
17
+ * isObject({ a: 1 })
18
+ * // => true
19
+ * isObject([1, 2])
20
+ * // => false
21
+ */
22
+ export declare const isObject: (value: any) => boolean;
23
+ /**
24
+ * 判断数据类型(内部工具函数)
25
+ * @param val - 要判断的数据
26
+ * @param type - 数据类型字符串,如 "Array"、"Function"
27
+ * @returns 如果数据是指定类型返回 true
28
+ * @example
29
+ * is([], "Array")
30
+ * // => true
31
+ */
32
+ export declare const is: (val: unknown, type: string) => boolean;
33
+ /**
34
+ * 判断一个值是否为函数
35
+ * @param val - 要判断的数据
36
+ * @returns 如果是函数返回 true
37
+ * @example
38
+ * isFunction(() => {})
39
+ * // => true
40
+ */
41
+ export declare const isFunction: (val: any) => val is Function;
42
+ /**
43
+ * 判断一个值是否为 Date 对象
44
+ * @param val - 要判断的数据
45
+ * @returns 如果是 Date 对象返回 true
46
+ * @example
47
+ * isDate(new Date())
48
+ * // => true
49
+ */
50
+ export declare const isDate: (val: any) => val is Date;
51
+ /**
52
+ * 判断一个值是否为数字(NaN 和 Infinity 也视为数字)
53
+ * @param val - 要判断的数据
54
+ * @returns 如果是数字返回 true
55
+ * @example
56
+ * isNumber(42)
57
+ * // => true
58
+ * isNumber("42")
59
+ * // => false
60
+ */
61
+ export declare const isNumber: (val: any) => val is number;
62
+ /**
63
+ * 判断一个值是否为整数
64
+ * @param val - 要判断的数据
65
+ * @returns 如果是整数返回 true
66
+ * @example
67
+ * isInt(42)
68
+ * // => true
69
+ * isInt(3.14)
70
+ * // => false
71
+ */
72
+ export declare const isInt: (val: any) => val is number;
73
+ /**
74
+ * 判断一个值是否为浮点数(有限小数)
75
+ * @param val - 要判断的数据
76
+ * @returns 如果是浮点数返回 true
77
+ * @example
78
+ * isFloat(3.14)
79
+ * // => true
80
+ * isFloat(42)
81
+ * // => false
82
+ */
83
+ export declare const isFloat: (val: any) => val is number;
84
+ /**
85
+ * 判断一个值是否为异步函数
86
+ * @param val - 要判断的数据
87
+ * @returns 如果是异步函数返回 true
88
+ * @example
89
+ * isAsyncFunction(async () => {})
90
+ * // => true
91
+ */
92
+ export declare const isAsyncFunction: (val: unknown) => val is (...args: any[]) => Promise<any>;
93
+ /**
94
+ * 判断一个值是否为 Promise
95
+ * @param value - 要判断的数据
96
+ * @returns 如果是 Promise 返回 true
97
+ * @example
98
+ * isPromise(Promise.resolve())
99
+ * // => true
100
+ */
101
+ export declare const isPromise: (value: any) => value is Promise<any>;
102
+ /**
103
+ * 判断一个值是否为字符串
104
+ * @param val - 要判断的数据
105
+ * @returns 如果是字符串返回 true
106
+ * @example
107
+ * isString("hello")
108
+ * // => true
109
+ * isString(42)
110
+ * // => false
111
+ */
112
+ export declare const isString: (val: unknown) => val is string;
113
+ /**
114
+ * 判断一个值是否为布尔值
115
+ * @param val - 要判断的数据
116
+ * @returns 如果是布尔值返回 true
117
+ * @example
118
+ * isBoolean(true)
119
+ * // => true
120
+ * isBoolean(1)
121
+ * // => false
122
+ */
123
+ export declare const isBoolean: (val: unknown) => val is boolean;
124
+ /**
125
+ * 判断是否为 PC 端(浏览器环境)
126
+ * @returns 如果在浏览器环境中返回 true
127
+ * @example
128
+ * isPC()
129
+ * // => true(在浏览器中)
130
+ */
131
+ export declare const isPC: () => boolean;
132
+ /**
133
+ * 判断一个值是否为 window 对象
134
+ * @param val - 要判断的数据
135
+ * @returns 如果是 window 对象返回 true
136
+ * @example
137
+ * isWindow(window)
138
+ * // => true(在浏览器中)
139
+ */
140
+ export declare const isWindow: (val: any) => val is Window;
141
+ /**
142
+ * 判断一个值是否为 DOM 元素
143
+ * @param val - 要判断的数据
144
+ * @returns 如果是 DOM 元素返回 true
145
+ * @example
146
+ * isElement(document.body)
147
+ * // => true
148
+ */
149
+ export declare const isElement: (val: unknown) => val is Element;
150
+ /**
151
+ * 判断一个值是否为 null
152
+ * @param val - 要判断的数据
153
+ * @returns 如果是 null 返回 true
154
+ * @example
155
+ * isNull(null)
156
+ * // => true
157
+ */
158
+ export declare const isNull: (val: unknown) => val is null;
159
+ /**
160
+ * 判断一个值是否为十六进制颜色
161
+ * @param str - 要判断的字符串
162
+ * @returns 如果是有效的十六进制颜色值返回 true
163
+ * @example
164
+ * isHexColor("#fff")
165
+ * // => true
166
+ * isHexColor("#ff0000")
167
+ * // => true
168
+ */
169
+ export declare const isHexColor: (str: string) => boolean;
170
+ /**
171
+ * 判断设备是否为 iOS
172
+ * @returns 如果是 iOS 设备返回 true
173
+ * @example
174
+ * isIOS()
175
+ * // => true(在 iPhone/iPad 上)
176
+ */
177
+ export declare const isIOS: () => boolean;
178
+ /**
179
+ * 判断一个值是否为有效的电子邮件
180
+ * @param email - 要判断的字符串
181
+ * @returns 如果是有效的电子邮件地址返回 true
182
+ * @example
183
+ * isValidEmail("test@example.com")
184
+ * // => true
185
+ */
186
+ export declare const isValidEmail: (email: string) => boolean;
187
+ /**
188
+ * 判断一个值是否已定义(不是 undefined)
189
+ * @param val - 要判断的数据
190
+ * @returns 如果不是 undefined 返回 true
191
+ * @example
192
+ * isDef("hello")
193
+ * // => true
194
+ * isDef(undefined)
195
+ * // => false
196
+ */
197
+ export declare const isDef: <T>(val: T | undefined) => val is T;
198
+ /**
199
+ * 判断一个值是否为 undefined
200
+ * @param val - 要判断的数据
201
+ * @returns 如果是 undefined 返回 true
202
+ * @example
203
+ * isUnDef(undefined)
204
+ * // => true
205
+ */
206
+ export declare const isUnDef: <T>(val: T | undefined) => val is undefined;
207
+ /**
208
+ * 判断一个值是否为 null 或 undefined
209
+ * @param val - 要判断的数据
210
+ * @returns 如果是 null 或 undefined 返回 true
211
+ * @example
212
+ * isNullOrUnDef(null)
213
+ * // => true
214
+ * isNullOrUnDef(undefined)
215
+ * // => true
216
+ * isNullOrUnDef("hello")
217
+ * // => false
218
+ */
219
+ export declare const isNullOrUnDef: <T>(val: T | null | undefined) => val is null | undefined;
220
+ /**
221
+ * 判断一个值是否为 symbol
222
+ * @param value - 要判断的数据
223
+ * @returns 如果是 symbol 返回 true
224
+ * @example
225
+ * isSymbol(Symbol("foo"))
226
+ * // => true
227
+ */
228
+ export declare const isSymbol: (value: any) => value is symbol;
229
+ /**
230
+ * 判断一个值是否为原始类型
231
+ * 原始类型包括:number、string、boolean、symbol、bigint、undefined、null
232
+ * @param value - 要判断的值
233
+ * @returns 如果是原始类型返回 true
234
+ * @example
235
+ * isPrimitive(42)
236
+ * // => true
237
+ * isPrimitive({})
238
+ * // => false
239
+ */
240
+ export declare const isPrimitive: (value: any) => boolean;
241
+ /**
242
+ * 判断一个值是否为空
243
+ * 空值包括:null、undefined、空字符串、空数组、空对象、空 Map/Set、无效 Date
244
+ * @param value - 要判断的数据
245
+ * @returns 如果为空返回 true
246
+ * @example
247
+ * isEmpty("")
248
+ * // => true
249
+ * isEmpty([])
250
+ * // => true
251
+ * isEmpty(0)
252
+ * // => false
253
+ */
254
+ export declare const isEmpty: (value: any) => boolean;
255
+ /**
256
+ * 判断一个值是否为空(简化版)
257
+ * 支持:字符串、数组、对象、Map、Set、类型化数组
258
+ * @param value - 要检查的值
259
+ * @returns 如果为空返回 true
260
+ * @example
261
+ * isEmptySv("")
262
+ * // => true
263
+ * isEmptySv(null)
264
+ * // => true
265
+ * isEmptySv([])
266
+ * // => true
267
+ * isEmptySv({})
268
+ * // => true
269
+ * isEmptySv(new Set())
270
+ * // => true
271
+ * isEmptySv(new Map())
272
+ * // => true
273
+ */
274
+ export declare function isEmptySv(value: string | object | null | undefined): boolean;
275
+ /**
276
+ * 判断两个值是否深度相等
277
+ * @param x - 要比较的第一个值
278
+ * @param y - 要比较的第二个值
279
+ * @returns 如果值深度相等返回 true
280
+ * @example
281
+ * isEqual({ a: 1 }, { a: 1 })
282
+ * // => true
283
+ * isEqual({ a: 1 }, { a: 2 })
284
+ * // => false
285
+ */
286
+ export declare const isEqual: <T>(x: T, y: T) => boolean;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * 获取 localStorage
3
+ * @param key - 存储键名
4
+ * @returns 解析后的值,解析失败则返回原始字符串,键不存在返回 null
5
+ * @example
6
+ * localGet("user")
7
+ * // => { name: "张三" }
8
+ */
9
+ export declare const localGet: <T = any>(key: string) => T | null;
10
+ /**
11
+ * 设置 localStorage
12
+ * @param key - 存储键名
13
+ * @param value - 存储值(会自动 JSON 序列化)
14
+ * @example
15
+ * localSet("user", { name: "张三" })
16
+ */
17
+ export declare const localSet: (key: string, value: unknown) => void;
18
+ /**
19
+ * 移除指定 localStorage
20
+ * @param key - 存储键名
21
+ * @example
22
+ * localRm("user")
23
+ */
24
+ export declare const localRm: (key: string) => void;
25
+ /**
26
+ * 清除所有 localStorage
27
+ * @example
28
+ * localClear()
29
+ */
30
+ export declare const localClear: () => void;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "pen-it",
3
+ "version": "1.0.0",
4
+ "description": "用笔锚定记忆中的前端常用工具函数",
5
+ "type": "module",
6
+ "main": "dist/index.cjs",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "build": "rimraf dist && rolldown -c rolldown.config.js",
18
+ "test": "vitest"
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "keywords": [
24
+ "utils",
25
+ "pen-it",
26
+ "toolkit",
27
+ "helpers"
28
+ ],
29
+ "author": "newborn_calf",
30
+ "license": "MIT",
31
+ "packageManager": "pnpm@10.19.0",
32
+ "devDependencies": {
33
+ "@rollup/plugin-typescript": "^12.3.0",
34
+ "@vitest/coverage-v8": "^4.1.8",
35
+ "jsdom": "^29.1.1",
36
+ "rimraf": "^6.1.3",
37
+ "rolldown": "^1.0.3",
38
+ "tslib": "^2.8.1",
39
+ "typescript": "^6.0.3",
40
+ "vitest": "^4.1.8"
41
+ }
42
+ }