@bettergi/utils 0.1.1 → 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 CHANGED
@@ -149,3 +149,33 @@ const body2 = await postForBody("https://example.com/", undefined, { "User-Agent
149
149
  log.info(`GET 请求响应体内容${body1}`);
150
150
  log.info(`POST 请求响应体内容${body2}`);
151
151
  ```
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
+
163
+ ### 日期时间
164
+
165
+ ```ts
166
+ // 获取下一个(含当日)凌晨4点的时间
167
+ const d1 = getNextDay4AM();
168
+ // 获取下一个(含当日)周一凌晨4点的时间
169
+ const d2 = getNextMonday4AM();
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/game.js CHANGED
@@ -101,10 +101,11 @@ export const setTimeTo = async (hour, minute, options) => {
101
101
  .map(rad => ({ x: radius * Math.cos(rad), y: radius * Math.sin(rad) }))
102
102
  // 计算绝对坐标
103
103
  .map(p => ({ x: Math.round(p.x + centerX), y: Math.round(p.y + centerY) })));
104
- // 3.拨动指针
104
+ // 3.调整时间
105
105
  await withGameMetrics(1920, 1080, 1.5, async () => {
106
+ // 拨动指针
106
107
  await mouseMoveAlongWaypoints(waypoints, { shouldDrag: true });
107
- // 3.点击确认按钮,等待调整结束
108
+ // 点击确认按钮,等待调整结束
108
109
  await assertRegionAppearing(() => findTextInDirection("时间少于", "south-east", { contains: true }), "调整时间超时", () => {
109
110
  findTextInDirection("确认", "south-east")?.click();
110
111
  }, { maxAttempts: 20, retryInterval: 1000 });
@@ -120,12 +121,12 @@ export const setTimeTo = async (hour, minute, options) => {
120
121
  export const setTime = async (period, options) => {
121
122
  switch (period) {
122
123
  case "night":
123
- return setTimeTo(0, 0, options);
124
+ return setTimeTo(0, 5, options);
124
125
  case "morning":
125
- return setTimeTo(6, 0, options);
126
+ return setTimeTo(6, 5, options);
126
127
  case "noon":
127
- return setTimeTo(12, 0, options);
128
+ return setTimeTo(12, 5, options);
128
129
  case "evening":
129
- return setTimeTo(18, 0, options);
130
+ return setTimeTo(18, 5, options);
130
131
  }
131
132
  };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,20 @@
1
+ /** 断言 */
1
2
  export * from "./asserts";
2
- export * from "./workflow";
3
+ /** 文件操作 */
4
+ export * from "./file";
5
+ /** 游戏操作 */
3
6
  export * from "./game";
7
+ /** HTTP 请求 */
4
8
  export * from "./http";
9
+ /** 杂项 */
10
+ export * from "./misc";
11
+ /** 鼠标操作 */
5
12
  export * from "./mouse";
13
+ /** 图像识别 */
6
14
  export * from "./ocr";
15
+ /** 数据存储 */
7
16
  export * from "./store";
17
+ /** 日期时间 */
18
+ export * from "./time";
19
+ /** 流程控制 */
20
+ export * from "./workflow";
package/dist/index.js CHANGED
@@ -1,7 +1,20 @@
1
+ /** 断言 */
1
2
  export * from "./asserts";
2
- export * from "./workflow";
3
+ /** 文件操作 */
4
+ export * from "./file";
5
+ /** 游戏操作 */
3
6
  export * from "./game";
7
+ /** HTTP 请求 */
4
8
  export * from "./http";
9
+ /** 杂项 */
10
+ export * from "./misc";
11
+ /** 鼠标操作 */
5
12
  export * from "./mouse";
13
+ /** 图像识别 */
6
14
  export * from "./ocr";
15
+ /** 数据存储 */
7
16
  export * from "./store";
17
+ /** 日期时间 */
18
+ export * from "./time";
19
+ /** 流程控制 */
20
+ export * from "./workflow";
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
+ };
package/dist/time.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 获取下一个(含当日)凌晨4点的时间
3
+ */
4
+ export declare const getNextDay4AM: () => Date;
5
+ /**
6
+ * 获取下一个(含当日)周一凌晨4点的时间
7
+ */
8
+ export declare const getNextMonday4AM: () => Date;
package/dist/time.js ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 获取下一个(含当日)凌晨4点的时间
3
+ */
4
+ export const getNextDay4AM = () => {
5
+ const now = new Date();
6
+ const result = new Date(now);
7
+ result.setHours(4, 0, 0, 0);
8
+ // 如果当前时间在4点前,则返回今天4点,否则返回明天4点
9
+ const daysUntilNextDay = now.getHours() < 4 ? 0 : 1;
10
+ result.setDate(now.getDate() + daysUntilNextDay);
11
+ return result;
12
+ };
13
+ /**
14
+ * 获取下一个(含当日)周一凌晨4点的时间
15
+ */
16
+ export const getNextMonday4AM = () => {
17
+ const now = new Date();
18
+ const result = new Date(now);
19
+ result.setHours(4, 0, 0, 0);
20
+ // 如果当前为周一且时间在4点前,则返回今天4点,否则返回下一个周一的4点
21
+ const currentDay = now.getDay();
22
+ const daysUntilNextMonday = currentDay === 1 && now.getHours() < 4 ? 0 : (8 - currentDay) % 7;
23
+ result.setDate(now.getDate() + daysUntilNextMonday);
24
+ return result;
25
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",