@bettergi/utils 0.1.2 → 0.1.3
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 +21 -0
- package/dist/file.d.ts +29 -0
- package/dist/file.js +43 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/misc.d.ts +11 -0
- package/dist/misc.js +26 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -150,6 +150,16 @@ log.info(`GET 请求响应体内容${body1}`);
|
|
|
150
150
|
log.info(`POST 请求响应体内容${body2}`);
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
+
### 文件操作
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
// 列出指定文件夹内所有文件路径
|
|
157
|
+
const files = listFiles("assets");
|
|
158
|
+
|
|
159
|
+
// 读取文件文本行(非空白、去除首尾空白、去重)
|
|
160
|
+
const lines = readLinesSync("assets/data.txt", { notBlank: true, trim: true, distinct: true });
|
|
161
|
+
```
|
|
162
|
+
|
|
153
163
|
### 日期时间
|
|
154
164
|
|
|
155
165
|
```ts
|
|
@@ -158,3 +168,14 @@ const d1 = getNextDay4AM();
|
|
|
158
168
|
// 获取下一个(含当日)周一凌晨4点的时间
|
|
159
169
|
const d2 = getNextMonday4AM();
|
|
160
170
|
```
|
|
171
|
+
|
|
172
|
+
### 杂项
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
// 生成UUID(不带连字符)
|
|
176
|
+
const uuid = generateUUID(false);
|
|
177
|
+
|
|
178
|
+
// 数组洗牌
|
|
179
|
+
const arr = [1, 2, 3, 4, 5];
|
|
180
|
+
const shuffled = shuffleArray(arr);
|
|
181
|
+
```
|
package/dist/file.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 读取指定文件夹内所有文件的路径
|
|
3
|
+
* @param folderPath 文件夹路径(相对于脚本根目录)
|
|
4
|
+
* @param recursive 是否递归子文件夹(默认:false)
|
|
5
|
+
* @returns 文件路径数组
|
|
6
|
+
*/
|
|
7
|
+
export declare const listFiles: (folderPath: string, recursive?: boolean) => string[];
|
|
8
|
+
export type ReadLinesOptions = {
|
|
9
|
+
/** 是否过滤仅含空白的行 */
|
|
10
|
+
notBlank?: boolean;
|
|
11
|
+
/** 是否过滤空行 */
|
|
12
|
+
notEmpty?: boolean;
|
|
13
|
+
/** 是否去除每行首尾空白 */
|
|
14
|
+
trim?: boolean;
|
|
15
|
+
/** 是否去重 */
|
|
16
|
+
distinct?: boolean;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* 同步读取文件文本行
|
|
20
|
+
* @param path 文件路径(相对于脚本根目录)
|
|
21
|
+
* @param options 读取选项
|
|
22
|
+
*/
|
|
23
|
+
export declare const readLinesSync: (path: string, options: ReadLinesOptions) => string[];
|
|
24
|
+
/**
|
|
25
|
+
* 读取文件文本行
|
|
26
|
+
* @param path 文件路径(相对于脚本根目录)
|
|
27
|
+
* @param options 读取选项
|
|
28
|
+
*/
|
|
29
|
+
export declare const readLines: (path: string, options: ReadLinesOptions) => Promise<string[]>;
|
package/dist/file.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 读取指定文件夹内所有文件的路径
|
|
3
|
+
* @param folderPath 文件夹路径(相对于脚本根目录)
|
|
4
|
+
* @param recursive 是否递归子文件夹(默认:false)
|
|
5
|
+
* @returns 文件路径数组
|
|
6
|
+
*/
|
|
7
|
+
export const listFiles = (folderPath, recursive) => {
|
|
8
|
+
return [...file.readPathSync(folderPath)].flatMap(path => recursive && file.isFolder(path) ? listFiles(path, recursive) : file.isFolder(path) ? [] : path);
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* 同步读取文件文本行
|
|
12
|
+
* @param path 文件路径(相对于脚本根目录)
|
|
13
|
+
* @param options 读取选项
|
|
14
|
+
*/
|
|
15
|
+
export const readLinesSync = (path, options) => {
|
|
16
|
+
const { notBlank = false, notEmpty = false, trim = false, distinct = false } = options || {};
|
|
17
|
+
return file
|
|
18
|
+
.readTextSync(path)
|
|
19
|
+
.replaceAll("\r\n", "\n")
|
|
20
|
+
.split("\n")
|
|
21
|
+
.filter(line => (!notBlank || line.trim().length > 0) && (!notEmpty || line.length > 0))
|
|
22
|
+
.map(line => (trim ? line.trim() : line))
|
|
23
|
+
.reduce((acc, line) => {
|
|
24
|
+
return distinct ? (acc.includes(line) ? acc : [...acc, line]) : [...acc, line];
|
|
25
|
+
}, []);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 读取文件文本行
|
|
29
|
+
* @param path 文件路径(相对于脚本根目录)
|
|
30
|
+
* @param options 读取选项
|
|
31
|
+
*/
|
|
32
|
+
export const readLines = async (path, options) => {
|
|
33
|
+
const { notBlank = false, notEmpty = false, trim = false, distinct = false } = options || {};
|
|
34
|
+
const text = await file.readText(path);
|
|
35
|
+
return text
|
|
36
|
+
.replaceAll("\r\n", "\n")
|
|
37
|
+
.split("\n")
|
|
38
|
+
.filter(line => (!notBlank || line.trim().length > 0) && (!notEmpty || line.length > 0))
|
|
39
|
+
.map(line => (trim ? line.trim() : line))
|
|
40
|
+
.reduce((acc, line) => {
|
|
41
|
+
return distinct ? (acc.includes(line) ? acc : [...acc, line]) : [...acc, line];
|
|
42
|
+
}, []);
|
|
43
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/** 断言 */
|
|
2
2
|
export * from "./asserts";
|
|
3
|
+
/** 文件操作 */
|
|
4
|
+
export * from "./file";
|
|
3
5
|
/** 游戏操作 */
|
|
4
6
|
export * from "./game";
|
|
5
7
|
/** HTTP 请求 */
|
|
6
8
|
export * from "./http";
|
|
9
|
+
/** 杂项 */
|
|
10
|
+
export * from "./misc";
|
|
7
11
|
/** 鼠标操作 */
|
|
8
12
|
export * from "./mouse";
|
|
9
13
|
/** 图像识别 */
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/** 断言 */
|
|
2
2
|
export * from "./asserts";
|
|
3
|
+
/** 文件操作 */
|
|
4
|
+
export * from "./file";
|
|
3
5
|
/** 游戏操作 */
|
|
4
6
|
export * from "./game";
|
|
5
7
|
/** HTTP 请求 */
|
|
6
8
|
export * from "./http";
|
|
9
|
+
/** 杂项 */
|
|
10
|
+
export * from "./misc";
|
|
7
11
|
/** 鼠标操作 */
|
|
8
12
|
export * from "./mouse";
|
|
9
13
|
/** 图像识别 */
|
package/dist/misc.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 生成UUID
|
|
3
|
+
* @param withDashes 是否包含连字符
|
|
4
|
+
*/
|
|
5
|
+
export declare const generateUUID: (withDashes?: boolean) => string;
|
|
6
|
+
/**
|
|
7
|
+
* Fisher-Yates 洗牌算法
|
|
8
|
+
* @param array 待洗牌数组
|
|
9
|
+
* @returns 洗牌后的新数组
|
|
10
|
+
*/
|
|
11
|
+
export declare const shuffleArray: <T>(array: T[]) => T[];
|
package/dist/misc.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 生成UUID
|
|
3
|
+
* @param withDashes 是否包含连字符
|
|
4
|
+
*/
|
|
5
|
+
export const generateUUID = (withDashes = true) => {
|
|
6
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
|
7
|
+
.replace(/[xy]/g, c => {
|
|
8
|
+
const r = (Math.random() * 16) | 0;
|
|
9
|
+
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
|
|
10
|
+
})
|
|
11
|
+
.replace(/-/g, withDashes ? "-" : "");
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Fisher-Yates 洗牌算法
|
|
15
|
+
* @param array 待洗牌数组
|
|
16
|
+
* @returns 洗牌后的新数组
|
|
17
|
+
*/
|
|
18
|
+
export const shuffleArray = (array) => {
|
|
19
|
+
const shuffled = [...array];
|
|
20
|
+
let i = shuffled.length;
|
|
21
|
+
while (i) {
|
|
22
|
+
const j = Math.floor(Math.random() * i--);
|
|
23
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
24
|
+
}
|
|
25
|
+
return shuffled;
|
|
26
|
+
};
|