@nickyzj2023/utils 1.0.42 → 1.0.44
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/AGENTS.md +131 -0
- package/biome.json +35 -34
- package/dist/index.js +1 -1
- package/dist/network.d.ts +2 -0
- package/dist/string.d.ts +11 -2
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/functions/camelToSnake.html +1 -1
- package/docs/functions/capitalize.html +1 -1
- package/docs/functions/compactStr.html +1 -1
- package/docs/functions/debounce.html +1 -1
- package/docs/functions/decapitalize.html +1 -1
- package/docs/functions/fetcher.html +1 -1
- package/docs/functions/getRealURL.html +175 -0
- package/docs/functions/imageUrlToBase64.html +9 -3
- package/docs/functions/isFalsy.html +1 -1
- package/docs/functions/isNil.html +1 -1
- package/docs/functions/isObject.html +1 -1
- package/docs/functions/isPrimitive.html +1 -1
- package/docs/functions/isTruthy.html +1 -1
- package/docs/functions/loopUntil.html +1 -1
- package/docs/functions/mapKeys.html +1 -1
- package/docs/functions/mapValues.html +1 -1
- package/docs/functions/mergeObjects.html +1 -1
- package/docs/functions/randomInt.html +1 -1
- package/docs/functions/sleep.html +1 -1
- package/docs/functions/snakeToCamel.html +1 -1
- package/docs/functions/throttle.html +1 -1
- package/docs/functions/timeLog.html +1 -1
- package/docs/functions/to.html +1 -1
- package/docs/functions/withCache.html +3 -3
- package/docs/modules.html +1 -1
- package/docs/types/CamelToSnake.html +1 -1
- package/docs/types/Capitalize.html +1 -1
- package/docs/types/Decapitalize.html +1 -1
- package/docs/types/DeepMapKeys.html +1 -1
- package/docs/types/DeepMapValues.html +1 -1
- package/docs/types/Falsy.html +1 -1
- package/docs/types/Primitive.html +1 -1
- package/docs/types/RequestInit.html +1 -1
- package/docs/types/SetTtl.html +1 -1
- package/docs/types/SnakeToCamel.html +1 -1
- package/package.json +25 -24
- package/src/network.ts +17 -0
- package/src/string.ts +27 -5
package/package.json
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
2
|
+
"name": "@nickyzj2023/utils",
|
|
3
|
+
"version": "1.0.44",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"module": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"docs": "typedoc src/index.ts --plugin typedoc-material-theme",
|
|
9
|
+
"build": "bun build --target=bun --outdir ./dist --minify ./src/index.ts --packages external && tsc && bun run docs"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@biomejs/biome": "^2.3.14",
|
|
13
|
+
"@types/bun": "^1.3.8",
|
|
14
|
+
"typedoc": "^0.28.16"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"typescript": "^5.9.3"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/Nickyzj628/utils.git"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"sharp": "^0.34.5",
|
|
25
|
+
"typedoc-material-theme": "^1.4.1"
|
|
26
|
+
}
|
|
26
27
|
}
|
package/src/network.ts
CHANGED
|
@@ -125,3 +125,20 @@ export const to = async <T, E = Error>(
|
|
|
125
125
|
return [e as E, undefined];
|
|
126
126
|
}
|
|
127
127
|
};
|
|
128
|
+
|
|
129
|
+
/** 从 url 响应头获取真实链接 */
|
|
130
|
+
export const getRealURL = async (originURL: string) => {
|
|
131
|
+
const [error, response] = await to(
|
|
132
|
+
fetch(originURL, {
|
|
133
|
+
method: "HEAD", // 用 HEAD 减少数据传输
|
|
134
|
+
redirect: "manual", // 手动处理重定向
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
if (error) {
|
|
139
|
+
return originURL;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const location = response.headers.get("location");
|
|
143
|
+
return location || originURL;
|
|
144
|
+
};
|
package/src/string.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import sharp from "sharp";
|
|
1
2
|
import { fetcher } from "./network";
|
|
2
3
|
|
|
3
4
|
export type SnakeToCamel<S extends string> =
|
|
@@ -75,25 +76,46 @@ export const decapitalize = <S extends string>(s: S): Decapitalize<S> => {
|
|
|
75
76
|
/**
|
|
76
77
|
* 图片地址转 base64 数据
|
|
77
78
|
*
|
|
79
|
+
* @param imageUrl 图片地址
|
|
80
|
+
* @param options 可选配置
|
|
81
|
+
* @param options.quality 压缩比率,默认 0.92
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* imageUrlToBase64("https://example.com/image.gif"); // "data:image/gif;base64,..."
|
|
85
|
+
*
|
|
78
86
|
* @example
|
|
79
|
-
* imageUrlToBase64("https://example.com/image.jpg"); //
|
|
87
|
+
* imageUrlToBase64("https://example.com/image.jpg", { quality: 0.8 }); // 压缩至 80% 质量
|
|
80
88
|
*/
|
|
81
|
-
export const imageUrlToBase64 = async (
|
|
89
|
+
export const imageUrlToBase64 = async (
|
|
90
|
+
imageUrl: string,
|
|
91
|
+
{ quality = 0.92 }: { quality?: number } = {},
|
|
92
|
+
) => {
|
|
82
93
|
if (!imageUrl.startsWith("http")) {
|
|
83
94
|
throw new Error("图片地址必须以http或https开头");
|
|
84
95
|
}
|
|
85
96
|
|
|
86
97
|
let mime = "";
|
|
87
98
|
const response = await fetcher().get<ArrayBuffer>(imageUrl, {
|
|
88
|
-
parser: (response) => {
|
|
99
|
+
parser: async (response) => {
|
|
89
100
|
mime = response.headers.get("Content-Type") || "image/jpeg";
|
|
90
101
|
return response.arrayBuffer();
|
|
91
102
|
},
|
|
92
103
|
});
|
|
104
|
+
|
|
93
105
|
const buffer = Buffer.from(response);
|
|
106
|
+
let compressedBuffer: Buffer;
|
|
107
|
+
|
|
108
|
+
if (mime === "image/png") {
|
|
109
|
+
compressedBuffer = await sharp(buffer)
|
|
110
|
+
.png({ compressionLevel: Math.round(quality * 9) })
|
|
111
|
+
.toBuffer();
|
|
112
|
+
} else if (mime === "image/gif") {
|
|
113
|
+
compressedBuffer = await sharp(buffer).gif().toBuffer();
|
|
114
|
+
} else {
|
|
115
|
+
compressedBuffer = await sharp(buffer).jpeg({ quality }).toBuffer();
|
|
116
|
+
}
|
|
94
117
|
|
|
95
|
-
|
|
96
|
-
return `data:${mime};base64,${base64}`;
|
|
118
|
+
return `data:${mime};base64,${compressedBuffer.toString("base64")}`;
|
|
97
119
|
};
|
|
98
120
|
|
|
99
121
|
/**
|