@zwa73/utils 1.0.14 → 1.0.16

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,16 @@
1
+ /**用于打印方法的调用
2
+ * @returns {void}
3
+ */
4
+ export declare function DLogger(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
5
+ /**用于打印异步方法的调用
6
+ * @returns {void}
7
+ */
8
+ export declare function DLoggerAsync(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
9
+ /**用于捕获方法中的错误
10
+ * @returns {void}
11
+ */
12
+ export declare function DCatchErrors(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
13
+ /**用于捕获异步方法中的错误
14
+ * @returns {void}
15
+ */
16
+ export declare function DCatchErrorsAsync(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DCatchErrorsAsync = exports.DCatchErrors = exports.DLoggerAsync = exports.DLogger = void 0;
4
+ /**用于打印方法的调用
5
+ * @returns {void}
6
+ */
7
+ function DLogger() {
8
+ return function (target, propertyKey, descriptor) {
9
+ const originalMethod = descriptor.value;
10
+ descriptor.value = function (...args) {
11
+ let result = originalMethod.apply(this, args);
12
+ console.log(`Call: ${propertyKey}(${args}) => ${result}`);
13
+ return result;
14
+ };
15
+ };
16
+ }
17
+ exports.DLogger = DLogger;
18
+ /**用于打印异步方法的调用
19
+ * @returns {void}
20
+ */
21
+ function DLoggerAsync() {
22
+ return function (target, propertyKey, descriptor) {
23
+ const originalMethod = descriptor.value;
24
+ descriptor.value = async function (...args) {
25
+ let result = await originalMethod.apply(this, args);
26
+ console.log(`Call: ${propertyKey}(${args}) => ${result}`);
27
+ return result;
28
+ };
29
+ };
30
+ }
31
+ exports.DLoggerAsync = DLoggerAsync;
32
+ /**用于捕获方法中的错误
33
+ * @returns {void}
34
+ */
35
+ function DCatchErrors() {
36
+ return function (target, propertyKey, descriptor) {
37
+ const originalMethod = descriptor.value;
38
+ descriptor.value = function (...args) {
39
+ try {
40
+ originalMethod.apply(this, args);
41
+ }
42
+ catch (err) {
43
+ console.log(`Error in method ${propertyKey}: ${err}`);
44
+ }
45
+ };
46
+ };
47
+ }
48
+ exports.DCatchErrors = DCatchErrors;
49
+ /**用于捕获异步方法中的错误
50
+ * @returns {void}
51
+ */
52
+ function DCatchErrorsAsync() {
53
+ return function (target, propertyKey, descriptor) {
54
+ const originalMethod = descriptor.value;
55
+ descriptor.value = async function (...args) {
56
+ try {
57
+ await originalMethod.apply(this, args);
58
+ }
59
+ catch (err) {
60
+ console.log(`Error in method ${propertyKey}: ${err}`);
61
+ }
62
+ };
63
+ };
64
+ }
65
+ exports.DCatchErrorsAsync = DCatchErrorsAsync;
@@ -48,45 +48,29 @@ declare class SFfmpegTool {
48
48
  */
49
49
  static resample(inputWavPath: string, outputWavPath: string, rate?: number): Promise<boolean>;
50
50
  /**wav转ogg多线程
51
- * @param {IOMap} iomap - 输入输出路径映射
51
+ * @param {IOMap} ioMap - 输入输出路径映射
52
52
  * @param {number} quality - 质量
53
53
  * @param {number} cpCount - 并发数
54
54
  */
55
- static wav2oggMP(iomap: IOMap, quality?: number, cpCount?: number): Promise<void>;
56
- /**wav转ogg子线程
57
- * @param {IOMap} iomap - 输入输出路径映射
58
- * @param {number} quality - 质量
59
- */
60
- private static wav2oggCP;
55
+ static wav2oggMP(ioMap: IOMap, quality?: number, cpCount?: number): Promise<void>;
61
56
  /**flac转ogg多线程
62
- * @param {IOMap} iomap - 输入输出路径映射
63
- * @param {number} quality - 质量
64
- * @param {number} cpCount - 并发数
65
- */
66
- static flac2oggMP(iomap: IOMap, quality?: number, cpCount?: number): Promise<void>;
67
- /**flac转ogg子线程
68
- * @param {IOMap} iomap - 输入输出路径映射
57
+ * @param {IOMap} ioMap - 输入输出路径映射
69
58
  * @param {number} quality - 质量
70
59
  * @param {number} cpCount - 并发数
71
60
  */
72
- private static flac2oggCP;
61
+ static flac2oggMP(ioMap: IOMap, quality?: number, cpCount?: number): Promise<void>;
73
62
  /**删除静音多线程
74
- * @param {IOMap} iomap - 输入输出路径映射
63
+ * @param {IOMap} ioMap - 输入输出路径映射
75
64
  * @param {number} threshold - 静音阈值/dB
76
65
  * @param {number} silence - 保留静音时长
77
66
  */
78
- static trimSilenceMP(iomap: IOMap, threshold?: number, silence?: number, cpCount?: number): Promise<void>;
79
- /**删除静音子线程
80
- */
81
- private static trimSilenceCP;
67
+ static trimSilenceMP(ioMap: IOMap, threshold?: number, silence?: number, cpCount?: number): Promise<void>;
82
68
  /**重采样多线程
83
- * @param {IOMap} iomap - 输入输出路径映射
69
+ * @param {IOMap} ioMap - 输入输出路径映射
84
70
  * @param {number} rate - 采样率
85
71
  * @param {number} cpCount - 并发数
86
72
  */
87
73
  static resampleMP(ioMap: IOMap, rate?: number, cpCount?: number): Promise<void>;
88
- /**重采样子线程*/
89
- private static resampleCP;
90
74
  }
91
75
  export default SFfmpegTool;
92
76
  export { SFfmpegTool };
@@ -4,6 +4,7 @@ exports.SFfmpegTool = void 0;
4
4
  const fluentFfmpeg = require("fluent-ffmpeg");
5
5
  const path = require("path");
6
6
  const fs = require("fs");
7
+ const UtilClass_1 = require("./UtilClass");
7
8
  /**ffmpeg工具类
8
9
  */
9
10
  class SFfmpegTool {
@@ -124,84 +125,60 @@ class SFfmpegTool {
124
125
  }
125
126
  //多线程处理
126
127
  /**wav转ogg多线程
127
- * @param {IOMap} iomap - 输入输出路径映射
128
+ * @param {IOMap} ioMap - 输入输出路径映射
128
129
  * @param {number} quality - 质量
129
130
  * @param {number} cpCount - 并发数
130
131
  */
131
- static async wav2oggMP(iomap, quality = 10, cpCount = 16) {
132
- let cpList = MPClip(iomap, cpCount);
133
- for (let cpMap of cpList)
134
- SFfmpegTool.wav2oggCP(cpMap, quality);
135
- }
136
- /**wav转ogg子线程
137
- * @param {IOMap} iomap - 输入输出路径映射
138
- * @param {number} quality - 质量
139
- */
140
- static async wav2oggCP(iomap, quality = 10) {
141
- for (let inPath in iomap) {
142
- let outpath = iomap[inPath];
143
- console.log("正在处理:" + outpath);
144
- await SFfmpegTool.wav2ogg(inPath, outpath, quality);
145
- }
132
+ static async wav2oggMP(ioMap, quality = 10, cpCount = 16) {
133
+ await new UtilClass_1.SList(Object.entries(ioMap))
134
+ .toSStream(cpCount)
135
+ .map(async ([inPath, outPath]) => {
136
+ console.log("正在处理:" + outPath);
137
+ await SFfmpegTool.wav2ogg(inPath, outPath, quality);
138
+ })
139
+ .appendOperations();
146
140
  }
147
141
  /**flac转ogg多线程
148
- * @param {IOMap} iomap - 输入输出路径映射
149
- * @param {number} quality - 质量
150
- * @param {number} cpCount - 并发数
151
- */
152
- static async flac2oggMP(iomap, quality = 10, cpCount = 16) {
153
- let cpList = MPClip(iomap, cpCount);
154
- for (let cpMap of cpList)
155
- SFfmpegTool.flac2oggCP(cpMap, quality);
156
- }
157
- /**flac转ogg子线程
158
- * @param {IOMap} iomap - 输入输出路径映射
142
+ * @param {IOMap} ioMap - 输入输出路径映射
159
143
  * @param {number} quality - 质量
160
144
  * @param {number} cpCount - 并发数
161
145
  */
162
- static async flac2oggCP(iomap, quality = 10) {
163
- for (let inPath in iomap) {
164
- let outpath = iomap[inPath];
165
- console.log("正在处理:" + outpath);
166
- await SFfmpegTool.flac2ogg(inPath, outpath, quality);
167
- }
146
+ static async flac2oggMP(ioMap, quality = 10, cpCount = 16) {
147
+ await new UtilClass_1.SList(Object.entries(ioMap))
148
+ .toSStream(cpCount)
149
+ .map(async ([inPath, outPath]) => {
150
+ console.log("正在处理:" + outPath);
151
+ await SFfmpegTool.flac2ogg(inPath, outPath, quality);
152
+ })
153
+ .appendOperations();
168
154
  }
169
155
  /**删除静音多线程
170
- * @param {IOMap} iomap - 输入输出路径映射
156
+ * @param {IOMap} ioMap - 输入输出路径映射
171
157
  * @param {number} threshold - 静音阈值/dB
172
158
  * @param {number} silence - 保留静音时长
173
159
  */
174
- static async trimSilenceMP(iomap, threshold = -50, silence = 0.2, cpCount = 16) {
175
- let cpList = MPClip(iomap, cpCount);
176
- for (let cpMap of cpList)
177
- SFfmpegTool.trimSilenceCP(cpMap, threshold, silence);
178
- }
179
- /**删除静音子线程
180
- */
181
- static async trimSilenceCP(iomap, threshold = -50, silence = 0.2) {
182
- for (let inPath in iomap) {
183
- let outpath = iomap[inPath];
184
- console.log("正在处理:" + outpath);
185
- await SFfmpegTool.trimSilence(inPath, outpath, threshold, silence);
186
- }
160
+ static async trimSilenceMP(ioMap, threshold = -50, silence = 0.2, cpCount = 16) {
161
+ await new UtilClass_1.SList(Object.entries(ioMap))
162
+ .toSStream(cpCount)
163
+ .map(async ([inPath, outPath]) => {
164
+ console.log("正在处理:" + outPath);
165
+ await SFfmpegTool.trimSilence(inPath, outPath, threshold, silence);
166
+ })
167
+ .appendOperations();
187
168
  }
188
169
  /**重采样多线程
189
- * @param {IOMap} iomap - 输入输出路径映射
170
+ * @param {IOMap} ioMap - 输入输出路径映射
190
171
  * @param {number} rate - 采样率
191
172
  * @param {number} cpCount - 并发数
192
173
  */
193
174
  static async resampleMP(ioMap, rate = 22050, cpCount = 16) {
194
- let cpList = MPClip(ioMap, cpCount);
195
- for (let cpMap of cpList)
196
- SFfmpegTool.resampleCP(cpMap, rate);
197
- }
198
- /**重采样子线程*/
199
- static async resampleCP(ioMap, rate = 22050) {
200
- for (let inPath in ioMap) {
201
- let outpath = ioMap[inPath];
202
- console.log("正在处理:" + outpath);
203
- await SFfmpegTool.resample(inPath, outpath, rate);
204
- }
175
+ await new UtilClass_1.SList(Object.entries(ioMap))
176
+ .toSStream(cpCount)
177
+ .map(async ([inPath, outPath]) => {
178
+ console.log("正在处理:" + outPath);
179
+ await SFfmpegTool.resample(inPath, outPath, rate);
180
+ })
181
+ .appendOperations();
205
182
  }
206
183
  }
207
184
  exports.SFfmpegTool = SFfmpegTool;
@@ -1,12 +1,50 @@
1
- import { JObject } from './UtilInterfaces';
1
+ import { JObject } from "./UtilInterfaces";
2
+ /**获取当前时间戳
3
+ * number ()
4
+ * @returns {number} 时间戳
5
+ */
2
6
  export declare function getTime(): number;
3
- export declare function initField(obj: Record<string, any>, field: string, defaultVal: any): void;
4
- /**加载json文件
7
+ /**初始化对象的字段
8
+ * void (Object,string,any)
9
+ * @param {Record<string,T>} obj - 所要初始化的对象
10
+ * @param {string} field - 所要初始化的字段
11
+ * @param {T} defaultVal - 默认值
12
+ * @returns {T} - 最终值
13
+ */
14
+ export declare function initField<T>(obj: Record<string, T>, field: string, defaultVal: T): T;
15
+ /**验证路径 文件或文件夹 是否存在 异步
16
+ * @param {string} filePath - 待验证的路径
17
+ * @returns {Promise<boolean>} - 是否存在
18
+ */
19
+ export declare function pathExists(filePath: string): Promise<boolean>;
20
+ /**验证路径 文件或文件夹 是否存在 同步
21
+ * @param {string} filePath - 待验证的路径
22
+ * @returns {boolean} - 是否存在
23
+ */
24
+ export declare function pathExistsSync(filePath: string): boolean;
25
+ /**创建路径 path.sep 结尾时创建文件夹 异步
26
+ * @param {string} filePath - 待创建的路径
27
+ * @returns {Promise<boolean>} - 是否成功创建
28
+ */
29
+ export declare function createPath(filePath: string): Promise<boolean>;
30
+ /**创建路径 path.sep 结尾时创建文件夹 同步
31
+ * @param {string} filePath - 待创建的路径
32
+ * @returns {boolean} - 是否成功创建
33
+ */
34
+ export declare function createPathSync(filePath: string): boolean;
35
+ /**加载json文件 同步
5
36
  * Object (string)
6
37
  * @param {string} filePath - 文件路径
7
38
  * @returns {JObject}
8
39
  */
9
- export declare function loadJSONFile(filePath: string): JObject;
40
+ export declare function loadJSONFileSync(filePath: string): JObject;
41
+ /**加载json文件 异步
42
+ * Object (string)
43
+ * @async
44
+ * @param {string} filePath - 文件路径
45
+ * @returns {JObject}
46
+ */
47
+ export declare function loadJSONFile(filePath: string): Promise<JObject>;
10
48
  /**写入JSON文件
11
49
  * void (string,Object)
12
50
  * @async
@@ -1,37 +1,131 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fileSearch = exports.sleep = exports.isSafeNumber = exports.deepClone = exports.genUUID = exports.writeJSONFile = exports.loadJSONFile = exports.initField = exports.getTime = void 0;
3
+ exports.fileSearch = exports.sleep = exports.isSafeNumber = exports.deepClone = exports.genUUID = exports.writeJSONFile = exports.loadJSONFile = exports.loadJSONFileSync = exports.createPathSync = exports.createPath = exports.pathExistsSync = exports.pathExists = exports.initField = exports.getTime = void 0;
4
4
  const fs = require("fs");
5
5
  const crypto = require("crypto");
6
6
  const path = require("path");
7
+ /**获取当前时间戳
8
+ * number ()
9
+ * @returns {number} 时间戳
10
+ */
7
11
  function getTime() {
8
12
  return new Date().getTime();
9
13
  }
10
14
  exports.getTime = getTime;
15
+ /**初始化对象的字段
16
+ * void (Object,string,any)
17
+ * @param {Record<string,T>} obj - 所要初始化的对象
18
+ * @param {string} field - 所要初始化的字段
19
+ * @param {T} defaultVal - 默认值
20
+ * @returns {T} - 最终值
21
+ */
11
22
  function initField(obj, field, defaultVal) {
12
23
  if (!(field in obj))
13
24
  obj[field] = defaultVal;
25
+ return obj[field];
14
26
  }
15
27
  exports.initField = initField;
16
- /**加载json文件
28
+ /**验证路径 文件或文件夹 是否存在 异步
29
+ * @param {string} filePath - 待验证的路径
30
+ * @returns {Promise<boolean>} - 是否存在
31
+ */
32
+ async function pathExists(filePath) {
33
+ try {
34
+ const stats = await fs.promises.stat(filePath);
35
+ await fs.promises.access(filePath);
36
+ return true;
37
+ }
38
+ catch (e) {
39
+ return false;
40
+ }
41
+ }
42
+ exports.pathExists = pathExists;
43
+ /**验证路径 文件或文件夹 是否存在 同步
44
+ * @param {string} filePath - 待验证的路径
45
+ * @returns {boolean} - 是否存在
46
+ */
47
+ function pathExistsSync(filePath) {
48
+ try {
49
+ fs.accessSync(filePath);
50
+ return true;
51
+ }
52
+ catch (e) {
53
+ return false;
54
+ }
55
+ }
56
+ exports.pathExistsSync = pathExistsSync;
57
+ /**创建路径 path.sep 结尾时创建文件夹 异步
58
+ * @param {string} filePath - 待创建的路径
59
+ * @returns {Promise<boolean>} - 是否成功创建
60
+ */
61
+ async function createPath(filePath) {
62
+ try {
63
+ if (filePath.endsWith(path.sep)) {
64
+ await fs.promises.mkdir(filePath, { recursive: true });
65
+ return true;
66
+ }
67
+ await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
68
+ await fs.promises.open(filePath, 'w');
69
+ return true;
70
+ }
71
+ catch (e) {
72
+ console.log("createPath 错误");
73
+ console.log(e);
74
+ return false;
75
+ }
76
+ }
77
+ exports.createPath = createPath;
78
+ /**创建路径 path.sep 结尾时创建文件夹 同步
79
+ * @param {string} filePath - 待创建的路径
80
+ * @returns {boolean} - 是否成功创建
81
+ */
82
+ function createPathSync(filePath) {
83
+ try {
84
+ if (filePath.endsWith(path.sep)) {
85
+ fs.mkdirSync(filePath, { recursive: true });
86
+ return true;
87
+ }
88
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
89
+ fs.openSync(filePath, 'w');
90
+ return true;
91
+ }
92
+ catch (e) {
93
+ console.log("createPath 错误");
94
+ console.log(e);
95
+ return false;
96
+ }
97
+ }
98
+ exports.createPathSync = createPathSync;
99
+ /**加载json文件 同步
17
100
  * Object (string)
18
101
  * @param {string} filePath - 文件路径
19
102
  * @returns {JObject}
20
103
  */
21
- function loadJSONFile(filePath) {
22
- if (filePath.indexOf(".json") == -1)
23
- filePath += ".json";
104
+ function loadJSONFileSync(filePath) {
105
+ if (path.extname(filePath) !== '.json')
106
+ filePath += '.json';
107
+ let str = "";
24
108
  // 判断文件路径是否存在
25
- if (!fs.existsSync(filePath)) {
26
- // 如果路径不存在,创建文件夹
27
- try {
28
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
29
- }
30
- catch (e) { }
31
- // 创建文件
32
- fs.writeFileSync(filePath, '');
33
- }
34
- let str = fs.readFileSync(filePath);
109
+ if (pathExistsSync(filePath))
110
+ str = fs.readFileSync(filePath, "utf-8");
111
+ if (str == "" || str == null)
112
+ str = "{}";
113
+ return JSON.parse(str);
114
+ }
115
+ exports.loadJSONFileSync = loadJSONFileSync;
116
+ /**加载json文件 异步
117
+ * Object (string)
118
+ * @async
119
+ * @param {string} filePath - 文件路径
120
+ * @returns {JObject}
121
+ */
122
+ async function loadJSONFile(filePath) {
123
+ if (path.extname(filePath) !== '.json')
124
+ filePath += '.json';
125
+ let str = "";
126
+ // 判断文件路径是否存在
127
+ if (await pathExists(filePath))
128
+ str = await fs.promises.readFile(filePath, "utf-8");
35
129
  if (str == "" || str == null)
36
130
  str = "{}";
37
131
  return JSON.parse(str);
@@ -46,35 +140,20 @@ exports.loadJSONFile = loadJSONFile;
46
140
  */
47
141
  async function writeJSONFile(filePath, obj) {
48
142
  let str = JSON.stringify(obj, null, "\t");
49
- if (filePath.indexOf(".json") === -1)
50
- filePath += ".json";
51
- // 判断文件路径是否存在
52
- await new Promise((resolve, reject) => {
53
- fs.exists(filePath, (ex) => {
54
- if (!ex) {
55
- try {
56
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
57
- }
58
- catch (e) {
59
- console.log("创建文件错误:" + e);
60
- }
61
- }
62
- resolve(true);
63
- });
64
- });
65
- return new Promise((resolve, reject) => {
66
- fs.writeFile(filePath, str, function (err) {
67
- if (err === null) {
68
- console.log(`${filePath} writeJSONFile 成功`);
69
- resolve();
70
- }
71
- else {
72
- console.log(`${filePath} writeJSONFile 错误`);
73
- console.log(err);
74
- reject(err);
75
- }
76
- });
77
- });
143
+ if (path.extname(filePath) !== '.json')
144
+ filePath += '.json';
145
+ // 判断文件路径是否存在 不存在则创建
146
+ if (!(await pathExists(filePath)))
147
+ await createPath(filePath);
148
+ // 写入文件
149
+ try {
150
+ await fs.promises.writeFile(filePath, str);
151
+ console.log(`${filePath} writeJSONFile 成功`);
152
+ }
153
+ catch (err) {
154
+ console.log(`${filePath} writeJSONFile 错误`);
155
+ console.log(err);
156
+ }
78
157
  }
79
158
  exports.writeJSONFile = writeJSONFile;
80
159
  /**生成一串uuid
@@ -82,7 +161,7 @@ exports.writeJSONFile = writeJSONFile;
82
161
  * @returns {string} uuid
83
162
  */
84
163
  function genUUID() {
85
- return crypto.randomBytes(16).toString('hex');
164
+ return crypto.randomBytes(16).toString("hex");
86
165
  }
87
166
  exports.genUUID = genUUID;
88
167
  /**深克隆 序列化并反序列化
@@ -101,7 +180,9 @@ exports.deepClone = deepClone;
101
180
  function isSafeNumber(num) {
102
181
  if (num === undefined || num == null || isNaN(num))
103
182
  return false;
104
- return true;
183
+ if (typeof num === 'number')
184
+ return true;
185
+ return false;
105
186
  }
106
187
  exports.isSafeNumber = isSafeNumber;
107
188
  /**等待 timeMs 毫秒
@@ -131,7 +212,7 @@ function fileSearch(folder, traitRegex) {
131
212
  let stat = fs.lstatSync(subFilePath);
132
213
  //判断是否是文件夹,递归调用
133
214
  if (stat.isDirectory()) {
134
- let subMap = fileSearch(path.join(subFilePath, "/"), traitRegex);
215
+ let subMap = fileSearch(path.join(subFilePath, path.sep), traitRegex);
135
216
  for (let key in subMap)
136
217
  outMap[key] = subMap[key];
137
218
  continue;
package/dist/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './UtilClass';
4
4
  export * from './UtilCom';
5
5
  export * from './UtilCodecs';
6
6
  export * from './UtilFfmpegTools';
7
+ export * from './UtilDecorators';
package/dist/index.js CHANGED
@@ -20,3 +20,4 @@ __exportStar(require("./UtilClass"), exports);
20
20
  __exportStar(require("./UtilCom"), exports);
21
21
  __exportStar(require("./UtilCodecs"), exports);
22
22
  __exportStar(require("./UtilFfmpegTools"), exports);
23
+ __exportStar(require("./UtilDecorators"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {