@idlebox/common 1.4.13 → 1.4.15

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.
Files changed (78) hide show
  1. package/lib/autoindex.d.ts +17 -10
  2. package/lib/autoindex.d.ts.map +1 -1
  3. package/lib/autoindex.js +12 -9
  4. package/lib/autoindex.js.map +1 -1
  5. package/lib/error/getFrame.d.ts +2 -1
  6. package/lib/error/getFrame.d.ts.map +1 -1
  7. package/lib/error/getFrame.js.map +1 -1
  8. package/lib/error/pretty.d.ts.map +1 -1
  9. package/lib/error/pretty.js +14 -2
  10. package/lib/error/pretty.js.map +1 -1
  11. package/lib/error/stackTrace.d.ts +15 -0
  12. package/lib/error/stackTrace.d.ts.map +1 -0
  13. package/lib/error/stackTrace.js +33 -0
  14. package/lib/error/stackTrace.js.map +1 -0
  15. package/lib/error-wellknown/exit.error.d.ts +3 -0
  16. package/lib/error-wellknown/exit.error.d.ts.map +1 -1
  17. package/lib/error-wellknown/exit.error.js +6 -0
  18. package/lib/error-wellknown/exit.error.js.map +1 -1
  19. package/lib/function/functionName.d.ts +4 -0
  20. package/lib/function/functionName.d.ts.map +1 -1
  21. package/lib/function/functionName.js.map +1 -1
  22. package/lib/lifecycle/dispose/disposedError.d.ts +3 -2
  23. package/lib/lifecycle/dispose/disposedError.d.ts.map +1 -1
  24. package/lib/lifecycle/dispose/disposedError.js +3 -0
  25. package/lib/lifecycle/dispose/disposedError.js.map +1 -1
  26. package/lib/lifecycle/dispose/lifecycle.async.d.ts.map +1 -1
  27. package/lib/lifecycle/dispose/lifecycle.async.js +2 -1
  28. package/lib/lifecycle/dispose/lifecycle.async.js.map +1 -1
  29. package/lib/lifecycle/dispose/lifecycle.d.ts +11 -6
  30. package/lib/lifecycle/dispose/lifecycle.d.ts.map +1 -1
  31. package/lib/lifecycle/dispose/lifecycle.js +25 -0
  32. package/lib/lifecycle/dispose/lifecycle.js.map +1 -1
  33. package/lib/lifecycle/dispose/lifecycle.sync.d.ts.map +1 -1
  34. package/lib/lifecycle/dispose/lifecycle.sync.js +3 -2
  35. package/lib/lifecycle/dispose/lifecycle.sync.js.map +1 -1
  36. package/lib/lifecycle/event/event.js +1 -1
  37. package/lib/lifecycle/event/event.js.map +1 -1
  38. package/lib/lifecycle/timeout/interval.d.ts.map +1 -1
  39. package/lib/lifecycle/timeout/interval.js.map +1 -1
  40. package/lib/misc/package.json.d.ts +10 -0
  41. package/lib/misc/package.json.d.ts.map +1 -1
  42. package/lib/misc/package.json.js.map +1 -1
  43. package/lib/object/objectPath.d.ts +24 -0
  44. package/lib/object/objectPath.d.ts.map +1 -1
  45. package/lib/object/objectPath.js +105 -0
  46. package/lib/object/objectPath.js.map +1 -1
  47. package/lib/path/pathArray.d.ts.map +1 -1
  48. package/lib/path/pathArray.js +8 -6
  49. package/lib/path/pathArray.js.map +1 -1
  50. package/lib/platform/globalObject.d.ts +0 -1
  51. package/lib/platform/globalObject.d.ts.map +1 -1
  52. package/lib/platform/globalObject.js +0 -3
  53. package/lib/platform/globalObject.js.map +1 -1
  54. package/lib/string/concatType.generated.d.ts +1 -1
  55. package/lib/string/concatType.generated.js +1 -1
  56. package/lib/tsconfig.tsbuildinfo +1 -1
  57. package/package.json +10 -9
  58. package/src/autoindex.ts +30 -21
  59. package/src/error/getFrame.ts +3 -1
  60. package/src/error/pretty.ts +17 -2
  61. package/src/error/stackTrace.ts +47 -0
  62. package/src/error-wellknown/exit.error.ts +7 -0
  63. package/src/function/functionName.ts +5 -0
  64. package/src/lifecycle/dispose/disposedError.ts +8 -1
  65. package/src/lifecycle/dispose/lifecycle.async.ts +3 -2
  66. package/src/lifecycle/dispose/lifecycle.global.ts +1 -1
  67. package/src/lifecycle/dispose/lifecycle.sync.ts +5 -4
  68. package/src/lifecycle/dispose/lifecycle.ts +30 -8
  69. package/src/lifecycle/event/event.ts +1 -1
  70. package/src/lifecycle/timeout/interval.ts +4 -2
  71. package/src/misc/package.json.ts +13 -0
  72. package/src/object/objectPath.test.ts +46 -0
  73. package/src/object/objectPath.ts +109 -0
  74. package/src/path/pathArray.ts +7 -5
  75. package/src/platform/globalObject.ts +0 -4
  76. package/src/string/concatType.generated.ts +1 -1
  77. package/src/tsconfig.json +2 -2
  78. package/src/global.d.ts +0 -1
@@ -1,3 +1,4 @@
1
+ import type { MaybeNamed } from '../../function/functionName.js';
1
2
  import type { EventRegister } from '../event/event.js';
2
3
 
3
4
  /**
@@ -10,24 +11,18 @@ export interface IDisposableEvents {
10
11
  }
11
12
 
12
13
  /** @public */
13
- export interface IDisposable {
14
+ export interface IDisposable extends MaybeNamed {
14
15
  dispose(): void;
15
16
 
16
- readonly name?: string;
17
- readonly displayName?: string;
18
-
19
17
  // [Symbol.dispose]?(): void;
20
18
  /** @internal do not use/rely */
21
19
  // [Symbol.asyncDispose]?(): Promise<void>;
22
20
  }
23
21
 
24
22
  /** @public */
25
- export interface IAsyncDisposable {
23
+ export interface IAsyncDisposable extends MaybeNamed {
26
24
  dispose(): void | Promise<void>;
27
25
 
28
- name?: string;
29
- displayName?: string;
30
-
31
26
  // [Symbol.dispose]?(): void | Promise<void>;
32
27
  // [Symbol.asyncDispose]?(): void | Promise<void>;
33
28
  }
@@ -70,3 +65,30 @@ export function toDisposable(fn_or_obj: Disposable | AsyncDisposable | (() => vo
70
65
  });
71
66
  }
72
67
  }
68
+
69
+ type ClosableAsync = { close(): Promise<any> } & MaybeNamed;
70
+ type ClosableSync = { close(cb: (e?: Error) => void): void } & MaybeNamed;
71
+ export function closableToDisposable(closable: ClosableAsync | ClosableSync): IDisposable {
72
+ const promised = closable.close.length === 0;
73
+
74
+ return {
75
+ get displayName() {
76
+ return `closableToDisposable[AsyncDisposable](${closable.displayName || closable.name || closable.constructor.name || 'unknown'})`;
77
+ },
78
+ dispose() {
79
+ if (promised) {
80
+ return (closable as ClosableAsync).close();
81
+ } else {
82
+ return new Promise<void>((resolve, reject) => {
83
+ return (closable as ClosableSync).close((error) => {
84
+ if (error) {
85
+ reject(error);
86
+ } else {
87
+ resolve();
88
+ }
89
+ });
90
+ });
91
+ }
92
+ },
93
+ };
94
+ }
@@ -1,4 +1,4 @@
1
- import { AppExit } from '../../autoindex.js';
1
+ import { AppExit } from '../../error-wellknown/exit.error.js';
2
2
  import type { IDisposable } from '../dispose/lifecycle.js';
3
3
 
4
4
  export type EventHandler<T> = (data: T) => void;
@@ -2,13 +2,15 @@ import type { IDisposable } from '../dispose/lifecycle.js';
2
2
  import { Disposable } from '../dispose/lifecycle.sync.js';
3
3
  import { Emitter } from '../event/event.js';
4
4
 
5
+ type TimerT = ReturnType<typeof setInterval>;
6
+
5
7
  /**
6
8
  * @param unref defaults to `false`, when true, call `unref()` on the timer.
7
9
  * can not set to `true` on other platform.
8
10
  * @returns dispose will stop the interval
9
11
  */
10
12
  export function interval(ms: number, action: () => void, unref = false) {
11
- let timer: number | undefined = setInterval(action, ms);
13
+ let timer: TimerT | undefined = setInterval(action, ms);
12
14
  // unref is not supported in browser
13
15
  if (unref) (timer as any).unref();
14
16
 
@@ -23,7 +25,7 @@ export function interval(ms: number, action: () => void, unref = false) {
23
25
 
24
26
  /**
25
27
  * A simple interval class.
26
- *
28
+ *
27
29
  * mainly use for pause/resume several times.
28
30
  */
29
31
  export class Interval extends Disposable {
@@ -31,6 +31,7 @@ export interface IFullExportsField {
31
31
  }
32
32
 
33
33
  export type IExportsField = string | IExportCondition | IExportMap;
34
+ export type IImportsField = IExportCondition | IExportMap;
34
35
 
35
36
  function isPathMap(exportsField: IExportCondition | IExportMap): exportsField is IExportMap {
36
37
  return Object.keys(exportsField).some((e) => e.startsWith('.'));
@@ -110,6 +111,7 @@ export interface IPackageJson {
110
111
  publishConfig: Record<string, any>;
111
112
  workspaces: string[];
112
113
  exports: IExportsField;
114
+ imports: IImportsField;
113
115
  dependencies: Record<string, string>;
114
116
  devDependencies: Record<string, string>;
115
117
  optionalDependencies: Record<string, string>;
@@ -125,6 +127,17 @@ export interface IPackageJson {
125
127
 
126
128
  dist: IPackageJsonNpmDist;
127
129
 
130
+ /* My Addons */
131
+ /**
132
+ * Array of dependencies that will remove from dependencies and devDependencies when resolving build order.
133
+ */
134
+ decoupledDependencies: string | string[];
135
+ /**
136
+ * Array of package names that add this package to it's `decoupledDependencies`.
137
+ */
138
+ decoupledDependents: string | string[];
139
+ /* My Addons End */
140
+
128
141
  [field: string]: any;
129
142
  }
130
143
 
@@ -0,0 +1,46 @@
1
+ import { createRootLogger, EnableLogLevel, logger } from '@idlebox/logger';
2
+ import { ObjectPath } from './objectPath.js';
3
+
4
+ createRootLogger('object-checker');
5
+ logger.enable(EnableLogLevel.debug);
6
+
7
+ const objects = [
8
+ {
9
+ value: {
10
+ a: 1,
11
+ b: {
12
+ x: 1,
13
+ },
14
+ c: {
15
+ x: 1,
16
+ y: 2,
17
+ },
18
+ },
19
+ },
20
+ {
21
+ a: {
22
+ b: {
23
+ c: {
24
+ d: 1,
25
+ },
26
+ },
27
+ },
28
+ },
29
+ ];
30
+
31
+ const check1 = new ObjectPath(objects[1]);
32
+ if (check1.get(['a', 'b', 'c', 'd']) !== 1) {
33
+ logger.fatal`get fail`;
34
+ } else {
35
+ logger.success`get ok`;
36
+ }
37
+
38
+ logger.log`${check1.get(['a', 'b', 'c'])}`;
39
+
40
+ const check2 = new ObjectPath(objects[0]);
41
+
42
+ check2.set(['value', 'a'], 2);
43
+ check2.set(['value', 'b', 'x'], undefined);
44
+ check2.set(['value', 'c', 'x'], undefined);
45
+
46
+ logger.log`${objects[0]}`;
@@ -8,3 +8,112 @@ export function objectPath(obj: object, path: string): any {
8
8
  });
9
9
  return obj;
10
10
  }
11
+
12
+ type KeyType = string | number | symbol;
13
+ /**
14
+ * Mutate an object by path
15
+ *
16
+ * @param object the object to mutate, by reference
17
+ */
18
+ export class ObjectPath {
19
+ constructor(public readonly object: any) {}
20
+
21
+ /**
22
+ * Get a value from the object by path
23
+ * @param path the path to get, must not be empty
24
+ * @returns undefined if any part not found
25
+ */
26
+ public get(path: readonly KeyType[]) {
27
+ if (path.length === 0) throw new Error('path must not be empty');
28
+ const rest = path.slice();
29
+ const last = rest.pop()!;
30
+
31
+ let cursor = this.object;
32
+ for (const p of rest) {
33
+ if (typeof cursor[p] !== 'object') {
34
+ return undefined;
35
+ }
36
+ cursor = cursor[p];
37
+ }
38
+
39
+ return cursor[last];
40
+ }
41
+
42
+ public exists(path: readonly KeyType[]) {
43
+ if (path.length === 0) throw new Error('path must not be empty');
44
+ const rest = path.slice();
45
+ const last = rest.pop()!;
46
+
47
+ let cursor = this.object;
48
+ for (const p of rest) {
49
+ if (typeof cursor[p] !== 'object') {
50
+ return false;
51
+ }
52
+ cursor = cursor[p];
53
+ }
54
+
55
+ return Object.hasOwn(cursor, last);
56
+ }
57
+
58
+ /**
59
+ * Set value on a path
60
+ * @param path the path to set, must not be empty
61
+ * @param value
62
+ */
63
+ public set(path: readonly KeyType[], value: any) {
64
+ if (path.length === 0) throw new Error('path must not be empty');
65
+ function walkSet(cursor: any) {
66
+ const rest = path.slice();
67
+ const last = rest.pop()!;
68
+
69
+ for (const [index, name] of rest.entries()) {
70
+ if (!cursor[name]) {
71
+ const next = path[index + 1];
72
+ if (typeof next === 'number') {
73
+ cursor[name] = [];
74
+ } else {
75
+ cursor[name] = {};
76
+ }
77
+ }
78
+ cursor = cursor[name];
79
+ }
80
+ cursor[last] = value;
81
+ }
82
+ function walkDelete(cursor: any) {
83
+ const rest = path.slice();
84
+ const last = rest.pop()!;
85
+ const objects = [];
86
+ for (const name of rest) {
87
+ objects.unshift({ object: cursor, child: name });
88
+ cursor = cursor[name];
89
+ if (typeof cursor !== 'object') break;
90
+ }
91
+
92
+ if (cursor) {
93
+ if (Array.isArray(cursor) && typeof last === 'number') {
94
+ if (last < cursor.length) {
95
+ cursor.splice(last, 1);
96
+ }
97
+ } else {
98
+ delete cursor[last];
99
+ }
100
+ }
101
+
102
+ for (const { object, child } of objects) {
103
+ if (typeof object[child] !== 'object' || isEmptyObject(object[child])) {
104
+ delete object[child];
105
+ }
106
+ }
107
+ }
108
+
109
+ if (value === undefined) {
110
+ walkDelete(this.object);
111
+ } else {
112
+ walkSet(this.object);
113
+ }
114
+ }
115
+ }
116
+
117
+ function isEmptyObject(obj: any): boolean {
118
+ return Object.keys(obj).length === 0;
119
+ }
@@ -14,12 +14,14 @@ abstract class PathArrayAbstract {
14
14
  init: string | string[] = [],
15
15
  private readonly sep: ':' | ';' = isWindows ? ';' : ':',
16
16
  ) {
17
- if (Array.isArray(init)) {
18
- for (const item of init) {
19
- this.add(item);
17
+ if (init.length) {
18
+ if (Array.isArray(init)) {
19
+ for (const item of init) {
20
+ this.add(item);
21
+ }
22
+ } else {
23
+ this.add(init);
20
24
  }
21
- } else {
22
- this.add(init);
23
25
  }
24
26
  }
25
27
 
@@ -16,7 +16,3 @@ export function ensureGlobalObject<T>(symbol: string, constructor: () => T): T {
16
16
  }
17
17
  return globalObject[sm];
18
18
  }
19
-
20
- export function x() {
21
- return 1;
22
- }
@@ -7,7 +7,7 @@
7
7
  * GENERATED FILE, DO NOT MODIFY
8
8
  * 这是生成的文件,千万不要修改
9
9
  *
10
- * @build-script/codegen 0.0.1 - The Simple Code Generater
10
+ * @build-script/codegen 1.0.2 - The Simple Code Generater
11
11
  * https://github.com/GongT/baobao
12
12
  *
13
13
  ******************************************************************************/
package/src/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "compilerOptions": {
4
4
  "typeRoots": ["../node_modules/@types", "../node_modules"],
5
5
  "outDir": "../lib",
6
- "rootDir": "./",
6
+ "rootDir": "./"
7
7
  },
8
- "exclude": ["**/*.generator.ts"],
8
+ "exclude": ["**/*.generator.ts", "**/*.test.ts"]
9
9
  }
package/src/global.d.ts DELETED
@@ -1 +0,0 @@
1
- /// <reference types="@idlebox/itypes" />