koishi-plugin-rzgtboeyndxsklmq-commons 1.0.3 → 1.0.6

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/lib/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Context, Schema } from "koishi";
2
2
  export * from "./utils/BeanHelper";
3
- export * from "./utils/File";
3
+ export * from "./utils/Strings";
4
+ export * from "./utils/Files";
4
5
  export * from "./utils/Test";
5
- export * from "./utils/I18n";
6
6
  export declare const name = "rzgtboeyndxsklmq-commons";
7
7
  export interface Config {
8
8
  }
package/lib/index.js CHANGED
@@ -31,41 +31,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
33
  BeanHelper: () => BeanHelper,
34
- BeanType: () => BeanType,
35
34
  Config: () => Config,
35
+ Files: () => Files,
36
+ Strings: () => Strings,
37
+ Test: () => Test,
36
38
  apply: () => apply,
37
- loadLocales: () => loadLocales,
38
- name: () => name,
39
- readDirFiles: () => readDirFiles,
40
- test: () => test,
41
- test2: () => test2
39
+ name: () => name
42
40
  });
43
41
  module.exports = __toCommonJS(src_exports);
44
42
  var import_koishi = require("koishi");
45
43
 
46
44
  // src/utils/BeanHelper.ts
47
- var BeanType = class {
48
- constructor(beanHelper) {
49
- this.beanHelper = beanHelper;
50
- this.ctx = beanHelper.getByName("ctx");
51
- this.config = beanHelper.getByName("config");
52
- return this;
53
- }
54
- static {
55
- __name(this, "BeanType");
56
- }
57
- ctx;
58
- config;
59
- start() {
60
- }
61
- destroy() {
62
- }
63
- };
64
45
  var BeanHelper = class _BeanHelper {
65
46
  static {
66
47
  __name(this, "BeanHelper");
67
48
  }
68
- static buildLazyProxyHandler(getObj, hooks) {
49
+ static buildLazyProxyHandler(getObj, options) {
69
50
  const handlerMap = {};
70
51
  let needInit = true;
71
52
  Reflect.ownKeys(Reflect).forEach((key) => {
@@ -75,17 +56,18 @@ var BeanHelper = class _BeanHelper {
75
56
  needInit = false;
76
57
  Reflect.ownKeys(obj).forEach((k) => target[k] = obj[k]);
77
58
  Reflect.setPrototypeOf(target, Reflect.getPrototypeOf(obj));
59
+ target[_BeanHelper.PlaceholderObject] = true;
78
60
  }
79
61
  if (key === "set") {
80
- Reflect[key].apply(Reflect, [target, args[0], args[1]]);
62
+ Reflect.set(target, args[0], args[1]);
81
63
  } else if (key === "deleteProperty") {
82
- Reflect[key].apply(Reflect, [target, args]);
64
+ Reflect.deleteProperty(target, args[0]);
83
65
  }
84
66
  let res;
85
67
  if (key === "get") {
86
- res = hooks?.get ? hooks.get(obj, args[0]) : Reflect[key].apply(Reflect, [obj, args[0]]);
87
- if (typeof res === "function") {
88
- res = res.bind(args[1]);
68
+ res = options?.hooks?.get ? options.hooks.get(obj, args[0]) : Reflect[key].apply(Reflect, [obj, args[0]]);
69
+ if (typeof res === "function" && !options?.notBind) {
70
+ res = res.bind(options?.bindOriginal ? obj : args[1]);
89
71
  }
90
72
  } else {
91
73
  res = Reflect[key].apply(Reflect, [obj, ...args]);
@@ -95,17 +77,23 @@ var BeanHelper = class _BeanHelper {
95
77
  });
96
78
  return handlerMap;
97
79
  }
98
- static buildLazyProxy(getObj) {
99
- return new Proxy({}, _BeanHelper.buildLazyProxyHandler(getObj));
80
+ static buildLazyProxy(getObj, options) {
81
+ return new Proxy(
82
+ {},
83
+ _BeanHelper.buildLazyProxyHandler(getObj, options)
84
+ );
100
85
  }
101
- static buildLazyRevocableProxy(getObj) {
102
- return Proxy.revocable({}, _BeanHelper.buildLazyProxyHandler(getObj));
86
+ static buildLazyRevocableProxy(getObj, options) {
87
+ return Proxy.revocable(
88
+ {},
89
+ _BeanHelper.buildLazyProxyHandler(getObj, options)
90
+ );
103
91
  }
104
92
  classPool = [];
105
93
  ctx;
106
94
  config;
107
95
  constructor() {
108
- this.proxy(() => this.ctx, "ctx");
96
+ this.proxy(() => this.ctx, "ctx", { notBind: true });
109
97
  this.proxy(() => this.config, "config");
110
98
  }
111
99
  setCtx(ctx, config) {
@@ -125,21 +113,21 @@ var BeanHelper = class _BeanHelper {
125
113
  async start() {
126
114
  this.touchAll();
127
115
  for (const classInfo of this.classPool) {
128
- if (classInfo.proxy instanceof BeanType) {
116
+ if (classInfo.proxy instanceof _BeanHelper.BeanType) {
129
117
  await classInfo.proxy.start();
130
118
  }
131
119
  }
132
120
  }
133
121
  async destroy() {
134
122
  for (const classInfo of this.classPool) {
135
- if (classInfo.proxy instanceof BeanType) {
123
+ if (classInfo.proxy instanceof _BeanHelper.BeanType) {
136
124
  await classInfo.proxy.destroy();
137
125
  }
138
126
  classInfo.proxyRevoke?.();
139
127
  }
140
128
  this.classPool = null;
141
129
  }
142
- instance(clazz) {
130
+ instance(clazz, options) {
143
131
  let classInfo = this.classPool.find(
144
132
  (classInfo2) => classInfo2.class === clazz
145
133
  );
@@ -158,14 +146,14 @@ var BeanHelper = class _BeanHelper {
158
146
  classInfo.instance = new classInfo.class(this);
159
147
  }
160
148
  return classInfo.instance;
161
- });
149
+ }, options);
162
150
  classInfo.proxy = proxyRevocable.proxy;
163
151
  classInfo.proxyRevoke = proxyRevocable.revoke;
164
152
  this.classPool.push(classInfo);
165
153
  return classInfo.proxy;
166
154
  }
167
- proxy(getObj, name2) {
168
- const proxyRevocable = _BeanHelper.buildLazyRevocableProxy(getObj);
155
+ proxy(getObj, name2, options) {
156
+ const proxyRevocable = _BeanHelper.buildLazyRevocableProxy(getObj, options);
169
157
  const classInfo = {
170
158
  name: name2,
171
159
  proxy: proxyRevocable.proxy,
@@ -195,104 +183,145 @@ var BeanHelper = class _BeanHelper {
195
183
  return instance;
196
184
  }
197
185
  };
186
+ ((BeanHelper2) => {
187
+ BeanHelper2.PlaceholderObject = Symbol("PlaceholderObject");
188
+ class BeanType {
189
+ constructor(beanHelper) {
190
+ this.beanHelper = beanHelper;
191
+ this.ctx = beanHelper.getByName("ctx");
192
+ this.config = beanHelper.getByName("config");
193
+ return this;
194
+ }
195
+ static {
196
+ __name(this, "BeanType");
197
+ }
198
+ ctx;
199
+ config;
200
+ start() {
201
+ }
202
+ destroy() {
203
+ }
204
+ }
205
+ BeanHelper2.BeanType = BeanType;
206
+ })(BeanHelper || (BeanHelper = {}));
198
207
 
199
- // src/utils/File.ts
208
+ // src/utils/Strings.ts
209
+ var Strings;
210
+ ((Strings2) => {
211
+ function isEmpty(str) {
212
+ return !str;
213
+ }
214
+ Strings2.isEmpty = isEmpty;
215
+ __name(isEmpty, "isEmpty");
216
+ function isNotEmpty(str) {
217
+ return !Strings2.isEmpty(str);
218
+ }
219
+ Strings2.isNotEmpty = isNotEmpty;
220
+ __name(isNotEmpty, "isNotEmpty");
221
+ function isBlank(str) {
222
+ return Strings2.isEmpty(str) || str.trim().length === 0;
223
+ }
224
+ Strings2.isBlank = isBlank;
225
+ __name(isBlank, "isBlank");
226
+ function isNotBlank(str) {
227
+ return !Strings2.isBlank(str);
228
+ }
229
+ Strings2.isNotBlank = isNotBlank;
230
+ __name(isNotBlank, "isNotBlank");
231
+ })(Strings || (Strings = {}));
232
+
233
+ // src/utils/Files.ts
200
234
  var import_promises = __toESM(require("node:fs/promises"));
201
235
  var import_node_path = __toESM(require("node:path"));
202
- async function readDirFiles(dirPath, needDir = false) {
203
- const files = await import_promises.default.readdir(dirPath, {
204
- recursive: true,
205
- withFileTypes: true
206
- });
207
- const paths = [];
208
- files.forEach((file) => {
209
- if (file.isDirectory()) {
210
- if (!needDir) {
211
- return;
236
+ var Files;
237
+ ((Files2) => {
238
+ async function readDirFiles(dirPath, needDir = false) {
239
+ const files = await import_promises.default.readdir(dirPath, {
240
+ recursive: true,
241
+ withFileTypes: true
242
+ });
243
+ const paths = [];
244
+ files.forEach((file) => {
245
+ if (file.isDirectory()) {
246
+ if (!needDir) {
247
+ return;
248
+ }
249
+ paths.push(import_node_path.default.join(file.parentPath, file.name, import_node_path.default.sep));
250
+ } else if (file.isFile()) {
251
+ paths.push(import_node_path.default.join(file.parentPath, file.name));
212
252
  }
213
- paths.push(import_node_path.default.join(file.parentPath, file.name, import_node_path.default.sep));
214
- } else if (file.isFile()) {
215
- paths.push(import_node_path.default.join(file.parentPath, file.name));
216
- }
217
- });
218
- return paths;
219
- }
220
- __name(readDirFiles, "readDirFiles");
253
+ });
254
+ return paths;
255
+ }
256
+ Files2.readDirFiles = readDirFiles;
257
+ __name(readDirFiles, "readDirFiles");
258
+ })(Files || (Files = {}));
221
259
 
222
260
  // src/utils/Test.ts
223
- async function test(name2, count, serial, run) {
224
- const timeout = setInterval(() => {
225
- process.stdout.write(".");
226
- }, 100);
227
- let timeConsumings = [];
228
- timeConsumings.length = count;
229
- const testStartTime = Date.now();
230
- if (serial) {
231
- for (let i = 0; i < count; i++) {
232
- const time = Date.now();
233
- await run();
234
- timeConsumings[i] = Date.now() - time;
235
- }
236
- } else {
237
- timeConsumings = [...timeConsumings];
238
- await Promise.all(
239
- timeConsumings.map((_, i) => {
240
- return new Promise((resolve) => {
241
- setImmediate(async () => {
242
- const time = Date.now();
243
- await run();
244
- timeConsumings[i] = Date.now() - time;
245
- resolve(0);
261
+ var Test;
262
+ ((Test2) => {
263
+ async function test(name2, count, serial, run) {
264
+ const timeout = setInterval(() => {
265
+ process.stdout.write(".");
266
+ }, 100);
267
+ let timeConsumings = [];
268
+ timeConsumings.length = count;
269
+ const testStartTime = Date.now();
270
+ if (serial) {
271
+ for (let i = 0; i < count; i++) {
272
+ const time = Date.now();
273
+ await run();
274
+ timeConsumings[i] = Date.now() - time;
275
+ }
276
+ } else {
277
+ timeConsumings = [...timeConsumings];
278
+ await Promise.all(
279
+ timeConsumings.map((_, i) => {
280
+ return new Promise((resolve) => {
281
+ setImmediate(async () => {
282
+ const time = Date.now();
283
+ await run();
284
+ timeConsumings[i] = Date.now() - time;
285
+ resolve(0);
286
+ });
246
287
  });
247
- });
248
- })
249
- );
250
- }
251
- const testTime = Date.now() - testStartTime;
252
- clearInterval(timeout);
253
- if (count < 2) {
254
- console.log(`
288
+ })
289
+ );
290
+ }
291
+ const testTime = Date.now() - testStartTime;
292
+ clearInterval(timeout);
293
+ if (count < 2) {
294
+ console.log(`
255
295
  run: ${name2}; total: ${timeConsumings[0]};`);
256
- return;
257
- }
258
- let total = 0;
259
- let average = 0;
260
- let highest = 0;
261
- let lowest = null;
262
- for (let i = 1; i < timeConsumings.length; i++) {
263
- const t = timeConsumings[i];
264
- if (lowest == null || t < lowest) {
265
- lowest = t;
296
+ return;
266
297
  }
267
- highest = Math.max(t, highest);
268
- total += t;
269
- }
270
- average = total / (count - 1);
271
- console.log(
272
- `
273
- run: ${name2}; serial: ${serial}; count: ${count}; testTime: ${testTime}; total: ${total}; first: ${timeConsumings[0]}; average: ${average}; highest: ${highest}; lowest: ${lowest};`
274
- );
275
- }
276
- __name(test, "test");
277
- async function test2(name2, count, run) {
278
- await test(name2, count, true, run);
279
- await test(name2, count, false, run);
280
- }
281
- __name(test2, "test2");
282
-
283
- // src/utils/I18n.ts
284
- var import_node_path2 = __toESM(require("node:path"));
285
- async function loadLocales(ctx, dirPath) {
286
- const files = await readDirFiles(dirPath);
287
- for (const file of files) {
288
- if (!file.endsWith(".yml")) {
289
- continue;
298
+ let total = 0;
299
+ let average = 0;
300
+ let highest = 0;
301
+ let lowest = null;
302
+ for (let i = 1; i < timeConsumings.length; i++) {
303
+ const t = timeConsumings[i];
304
+ if (lowest == null || t < lowest) {
305
+ lowest = t;
306
+ }
307
+ highest = Math.max(t, highest);
308
+ total += t;
290
309
  }
291
- const l = import_node_path2.default.basename(file);
292
- ctx.i18n.define(l, require(file));
310
+ average = total / (count - 1);
311
+ console.log(
312
+ `
313
+ run: ${name2}; serial: ${serial}; count: ${count}; testTime: ${testTime}; total: ${total}; first: ${timeConsumings[0]}; average: ${average}; highest: ${highest}; lowest: ${lowest};`
314
+ );
293
315
  }
294
- }
295
- __name(loadLocales, "loadLocales");
316
+ Test2.test = test;
317
+ __name(test, "test");
318
+ async function test2(name2, count, run) {
319
+ await test(name2, count, true, run);
320
+ await test(name2, count, false, run);
321
+ }
322
+ Test2.test2 = test2;
323
+ __name(test2, "test2");
324
+ })(Test || (Test = {}));
296
325
 
297
326
  // src/index.ts
298
327
  var name = "rzgtboeyndxsklmq-commons";
@@ -303,12 +332,10 @@ __name(apply, "apply");
303
332
  // Annotate the CommonJS export names for ESM import in node:
304
333
  0 && (module.exports = {
305
334
  BeanHelper,
306
- BeanType,
307
335
  Config,
336
+ Files,
337
+ Strings,
338
+ Test,
308
339
  apply,
309
- loadLocales,
310
- name,
311
- readDirFiles,
312
- test,
313
- test2
340
+ name
314
341
  });
@@ -1,26 +1,9 @@
1
1
  import type { Context, Awaitable } from "koishi";
2
- export declare abstract class BeanType<C extends object> {
3
- protected beanHelper: BeanHelper<C>;
4
- protected ctx: Context;
5
- protected config: C;
6
- constructor(beanHelper: BeanHelper<C>);
7
- start(): Awaitable<void>;
8
- destroy(): Awaitable<void>;
9
- }
10
2
  type Constructor<T = any> = new (...args: any[]) => T;
11
- export interface ClassInfo<T> {
12
- name: string;
13
- class?: Constructor<T>;
14
- instance?: T;
15
- proxy?: T;
16
- proxyRevoke?: () => void;
17
- }
18
3
  export declare class BeanHelper<C extends object> {
19
- static buildLazyProxyHandler<T extends object>(getObj: () => T, hooks?: {
20
- get: (obj: any, prop: PropertyKey) => any;
21
- }): ProxyHandler<T>;
22
- static buildLazyProxy<T extends object>(getObj: () => T): T;
23
- static buildLazyRevocableProxy<T extends object>(getObj: () => T): {
4
+ static buildLazyProxyHandler<T extends object>(getObj: () => T, options?: BeanHelper.ProxyOptions): ProxyHandler<T>;
5
+ static buildLazyProxy<T extends object>(getObj: () => T, options?: BeanHelper.ProxyOptions): T;
6
+ static buildLazyRevocableProxy<T extends object>(getObj: () => T, options?: BeanHelper.ProxyOptions): {
24
7
  proxy: T;
25
8
  revoke: () => void;
26
9
  };
@@ -32,9 +15,34 @@ export declare class BeanHelper<C extends object> {
32
15
  private touchAll;
33
16
  start(): Promise<void>;
34
17
  destroy(): Promise<void>;
35
- instance<T extends BeanType<C>>(clazz: Constructor<T>): T;
36
- proxy<T extends object>(getObj: () => T, name: string): T;
18
+ instance<T extends BeanHelper.BeanType<C>>(clazz: Constructor<T>, options?: BeanHelper.ProxyOptions): T;
19
+ proxy<T extends object>(getObj: () => T, name: string, options?: BeanHelper.ProxyOptions): T;
37
20
  getByName<T>(name: string): T;
38
21
  put<T>(instance: T, name?: string, clazz?: Constructor): T;
39
22
  }
23
+ export declare namespace BeanHelper {
24
+ const PlaceholderObject: unique symbol;
25
+ abstract class BeanType<C extends object> {
26
+ protected beanHelper: BeanHelper<C>;
27
+ protected ctx: Context;
28
+ protected config: C;
29
+ constructor(beanHelper: BeanHelper<C>);
30
+ start(): Awaitable<void>;
31
+ destroy(): Awaitable<void>;
32
+ }
33
+ interface ClassInfo<T> {
34
+ name: string;
35
+ class?: Constructor<T>;
36
+ instance?: T;
37
+ proxy?: T;
38
+ proxyRevoke?: () => void;
39
+ }
40
+ interface ProxyOptions {
41
+ notBind?: boolean;
42
+ bindOriginal?: boolean;
43
+ hooks?: {
44
+ get: (obj: any, prop: PropertyKey) => any;
45
+ };
46
+ }
47
+ }
40
48
  export {};
@@ -0,0 +1,3 @@
1
+ export declare namespace Files {
2
+ function readDirFiles(dirPath: string, needDir?: boolean): Promise<string[]>;
3
+ }
@@ -0,0 +1,6 @@
1
+ export declare namespace Strings {
2
+ function isEmpty(str: string): boolean;
3
+ function isNotEmpty(str: string): boolean;
4
+ function isBlank(str: string): boolean;
5
+ function isNotBlank(str: string): boolean;
6
+ }
@@ -1,2 +1,4 @@
1
- export declare function test(name: string, count: number, serial: boolean, run: () => any): Promise<void>;
2
- export declare function test2(name: string, count: number, run: () => any): Promise<void>;
1
+ export declare namespace Test {
2
+ function test(name: string, count: number, serial: boolean, run: () => any): Promise<void>;
3
+ function test2(name: string, count: number, run: () => any): Promise<void>;
4
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-rzgtboeyndxsklmq-commons",
3
3
  "description": "",
4
- "version": "1.0.3",
4
+ "version": "1.0.6",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "scripts": {
@@ -1 +0,0 @@
1
- export declare function readDirFiles(dirPath: string, needDir?: boolean): Promise<string[]>;
@@ -1,2 +0,0 @@
1
- import { Context } from "koishi";
2
- export declare function loadLocales(ctx: Context, dirPath: string): Promise<void>;