@nickyzj2023/utils 1.0.24 → 1.0.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nickyzj2023/utils",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/is.ts CHANGED
@@ -11,7 +11,10 @@ export type Falsy = false | 0 | -0 | 0n | "" | null | undefined;
11
11
 
12
12
  /**
13
13
  * 检测传入的值是否为**普通对象**
14
- * @returns 如果是普通对象,返回 true,否则返回 false
14
+ *
15
+ * @example
16
+ * const obj = { a: 1 };
17
+ * isObject(obj); // true
15
18
  */
16
19
  export const isObject = (value: any): value is object => {
17
20
  return value?.constructor === Object;
@@ -19,7 +22,10 @@ export const isObject = (value: any): value is object => {
19
22
 
20
23
  /**
21
24
  * 检测传入的值是否为**原始值**(number、string、boolean、symbol、bigint、undefined、null)
22
- * @returns 如果是原始值,返回 true,否则返回 false
25
+ *
26
+ * @example
27
+ * isPrimitive(1); // true
28
+ * isPrimitive([]); // false
23
29
  */
24
30
  export const isPrimitive = (value: any): value is Primitive => {
25
31
  return (
@@ -30,14 +36,34 @@ export const isPrimitive = (value: any): value is Primitive => {
30
36
  };
31
37
 
32
38
  /**
33
- * 检测传入的值是否为**false值**(false、0、''、null、undefined、NaN等)
39
+ * 检测传入的值是否为**假值**(false、0、''、null、undefined、NaN等)
40
+ *
41
+ * @example
42
+ * isFalsy(""); // true
43
+ * isFalsy(1); // false
34
44
  */
35
45
  export const isFalsy = (value: any): value is Falsy => {
36
46
  return !value;
37
47
  };
38
48
 
49
+ /**
50
+ * 检测传入的值是否为**真值**
51
+ *
52
+ * @example
53
+ * isTruthy(1); // true
54
+ * isTruthy(""); // false
55
+ */
56
+ export const isTruthy = (value: any): value is any => {
57
+ return !!value;
58
+ };
59
+
39
60
  /**
40
61
  * 检测传入的值是否为**空值**(null、undefined)
62
+ *
63
+ * @example
64
+ * isNil(null); // true
65
+ * isNil(undefined); // true
66
+ * isNil(1); // false
41
67
  */
42
68
  export const isNil = (value: any): value is null | undefined => {
43
69
  return value === null || value === undefined;
package/src/string.ts CHANGED
@@ -40,3 +40,20 @@ export const capitalize = (s: string) => {
40
40
  export const decapitalize = (s: string) => {
41
41
  return s.charAt(0).toLowerCase() + s.slice(1);
42
42
  };
43
+
44
+ /**
45
+ * 图片地址转 base64 数据
46
+ *
47
+ * @example
48
+ * imageUrlToBase64("https://example.com/image.jpg"); // "data:image/jpeg;base64,..."
49
+ */
50
+ export const imageUrlToBase64 = async (imageUrl: string) => {
51
+ if (!imageUrl.startsWith("http")) {
52
+ throw new Error("图片地址必须以http或https开头");
53
+ }
54
+
55
+ const response = await fetch(imageUrl);
56
+ const arrayBuffer = await response.arrayBuffer();
57
+ const base64 = Buffer.from(arrayBuffer).toString("base64");
58
+ return `data:image/jpeg;base64,${base64}`;
59
+ };