@idlebox/common 1.3.14 → 1.3.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.
Files changed (121) hide show
  1. package/lib/array/arrayDiff.cjs +2 -2
  2. package/lib/array/arrayDiff.cjs.map +1 -1
  3. package/lib/array/arrayDiff.js +2 -2
  4. package/lib/array/arrayDiff.js.map +1 -1
  5. package/lib/array/arraySame.cjs.map +1 -1
  6. package/lib/array/arraySame.js.map +1 -1
  7. package/lib/array/arrayUnique.cjs.map +1 -1
  8. package/lib/array/arrayUnique.js.map +1 -1
  9. package/{dist → lib}/common-alpha.d.ts +57 -13
  10. package/{dist → lib}/common-beta.d.ts +57 -13
  11. package/{dist → lib}/common-public.d.ts +57 -13
  12. package/{dist → lib}/common.d.ts +57 -13
  13. package/lib/function/callbackList.cjs +4 -0
  14. package/lib/function/callbackList.cjs.map +1 -1
  15. package/lib/function/callbackList.js +4 -0
  16. package/lib/function/callbackList.js.map +1 -1
  17. package/lib/string/concatType.generated.cjs +8 -0
  18. package/lib/string/concatType.generated.cjs.map +1 -1
  19. package/lib/string/concatType.generated.js +8 -0
  20. package/lib/string/concatType.generated.js.map +1 -1
  21. package/{dist → lib}/tsdoc-metadata.json +1 -1
  22. package/lib/{typingHeler/deep.cjs → typingHelper/deep.partial.cjs} +1 -2
  23. package/lib/typingHelper/deep.partial.cjs.map +1 -0
  24. package/lib/typingHelper/deep.partial.js +2 -0
  25. package/lib/typingHelper/deep.partial.js.map +1 -0
  26. package/lib/typingHelper/deep.readonly.cjs +3 -0
  27. package/lib/typingHelper/deep.readonly.cjs.map +1 -0
  28. package/lib/typingHelper/deep.readonly.js +2 -0
  29. package/lib/typingHelper/deep.readonly.js.map +1 -0
  30. package/lib/typingHelper/deep.required.cjs +3 -0
  31. package/lib/typingHelper/deep.required.cjs.map +1 -0
  32. package/lib/typingHelper/deep.required.js +2 -0
  33. package/lib/typingHelper/deep.required.js.map +1 -0
  34. package/lib/typingHelper/deep.writable.cjs +3 -0
  35. package/lib/typingHelper/deep.writable.cjs.map +1 -0
  36. package/lib/typingHelper/deep.writable.js +2 -0
  37. package/lib/typingHelper/deep.writable.js.map +1 -0
  38. package/lib/typingHelper/literal.cjs +3 -0
  39. package/lib/typingHelper/literal.cjs.map +1 -0
  40. package/lib/typingHelper/literal.js +2 -0
  41. package/lib/typingHelper/literal.js.map +1 -0
  42. package/package.json +12 -12
  43. package/src/array/arrayDiff.ts +31 -0
  44. package/src/array/arraySame.ts +15 -0
  45. package/src/array/arrayUnique.ts +50 -0
  46. package/src/array/normalizeArray.ts +13 -0
  47. package/src/array/sortAlpha.ts +15 -0
  48. package/src/date/consts.ts +5 -0
  49. package/src/date/isInvalid.ts +6 -0
  50. package/src/date/sibling.ts +28 -0
  51. package/src/date/timeString.ts +150 -0
  52. package/src/date/unix.ts +13 -0
  53. package/src/debugging/serializable.ts +146 -0
  54. package/src/debugging/tryInspect.ts +37 -0
  55. package/src/error/convertUnknown.ts +10 -0
  56. package/src/error/getFrame.ts +13 -0
  57. package/src/function/asyncCallbackList.ts +75 -0
  58. package/src/function/callbackList.ts +88 -0
  59. package/src/function/delayCallbackList.ts +45 -0
  60. package/src/function/functionName.ts +39 -0
  61. package/src/index.generated.ts +289 -0
  62. package/src/lifecycle/dispose/bridges/rxjs.ts +6 -0
  63. package/src/lifecycle/dispose/disposableEvent.ts +117 -0
  64. package/src/lifecycle/dispose/disposedError.ts +16 -0
  65. package/src/lifecycle/dispose/lifecycle.async.ts +61 -0
  66. package/src/lifecycle/dispose/lifecycle.global.ts +61 -0
  67. package/src/lifecycle/dispose/lifecycle.sync.ts +79 -0
  68. package/src/lifecycle/dispose/lifecycle.ts +28 -0
  69. package/src/lifecycle/event/event.ts +81 -0
  70. package/src/lifecycle/event/memorized.ts +39 -0
  71. package/src/lifecycle/promise/cancel.ts +16 -0
  72. package/src/lifecycle/promise/cancellationToken/driver.browser.ts +55 -0
  73. package/src/lifecycle/promise/cancellationToken/driver.common.ts +43 -0
  74. package/src/lifecycle/promise/cancellationToken/source.ts +48 -0
  75. package/src/lifecycle/promise/deferredPromise.ts +104 -0
  76. package/src/lifecycle/timeout/timeout.ts +48 -0
  77. package/src/lifecycle/timeout/timeoutError.ts +16 -0
  78. package/src/log/logger.ts +148 -0
  79. package/src/mapSet/customSet.ts +91 -0
  80. package/src/mapSet/extendMap.ts +40 -0
  81. package/src/misc/assertNotNull.ts +21 -0
  82. package/src/object/definePublicConstant.ts +10 -0
  83. package/src/object/initOnRead.ts +27 -0
  84. package/src/object/objectPath.ts +10 -0
  85. package/src/object/objectSame.ts +52 -0
  86. package/src/path/isAbsolute.ts +11 -0
  87. package/src/path/normalizePath.ts +8 -0
  88. package/src/path/pathArray.ts +42 -0
  89. package/src/platform/globalObject.ts +22 -0
  90. package/src/platform/globalSingleton.ts +82 -0
  91. package/src/platform/globalSymbol.ts +36 -0
  92. package/src/platform/os.ts +46 -0
  93. package/src/promise/awaitIterator.ts +19 -0
  94. package/src/promise/finishAllPromise.ts +50 -0
  95. package/src/promise/promiseBool.ts +10 -0
  96. package/src/promise/promisePool.ts +40 -0
  97. package/src/promise/timeoutPromisePool.ts +22 -0
  98. package/src/reflection/classes/hookClass.ts +47 -0
  99. package/src/reflection/classes/singleton.ts +33 -0
  100. package/src/reflection/methods/bind.ts +30 -0
  101. package/src/reflection/methods/initOnRead.ts +11 -0
  102. package/src/reflection/methods/memorize.ts +33 -0
  103. package/src/string/castCase.ts +44 -0
  104. package/src/string/concatType.generated.ts +265 -0
  105. package/src/string/concatType.generator.ts +31 -0
  106. package/src/string/escapeRegexp.ts +4 -0
  107. package/src/string/pad2.ts +11 -0
  108. package/src/string/sizeString.ts +52 -0
  109. package/src/tsconfig.json +14 -0
  110. package/src/typingHelper/deep.partial.ts +16 -0
  111. package/src/typingHelper/deep.readonly.ts +16 -0
  112. package/src/typingHelper/deep.required.ts +16 -0
  113. package/src/typingHelper/deep.writable.ts +16 -0
  114. package/src/typingHelper/literal.ts +1 -0
  115. package/docs/common.api.json +0 -21737
  116. package/docs/common.api.md +0 -1139
  117. package/docs/package-public.d.ts +0 -952
  118. package/docs/tsdoc-metadata.json +0 -11
  119. package/lib/typingHeler/deep.cjs.map +0 -1
  120. package/lib/typingHeler/deep.js +0 -3
  121. package/lib/typingHeler/deep.js.map +0 -1
@@ -0,0 +1,104 @@
1
+ import { CanceledError } from './cancel';
2
+
3
+ export type ValueCallback<T = any> = (value: T | Promise<T>) => void;
4
+ export type ProgressCallback<T = any> = (value: T) => void;
5
+
6
+ export interface IProgressHolder<T, PT> {
7
+ progress(fn: ProgressCallback<PT>): Promise<T> & IProgressHolder<T, PT>;
8
+ }
9
+
10
+ /**
11
+ * a promise can resolve or reject later
12
+ * @public
13
+ */
14
+ export class DeferredPromise<T, PT = any> {
15
+ public readonly p: Promise<T> & IProgressHolder<T, PT>;
16
+ private declare _completeCallback: ValueCallback<T>;
17
+ private declare _errorCallback: (err: any) => void;
18
+ private _state: boolean | null = null;
19
+ private _progressList?: ProgressCallback<PT>[] = [];
20
+
21
+ constructor() {
22
+ this.p = Object.assign(
23
+ new Promise<any>((c, e) => {
24
+ this._completeCallback = c;
25
+ this._errorCallback = e;
26
+ }),
27
+ {
28
+ progress: (fn: ProgressCallback<PT>) => {
29
+ this.progress(fn);
30
+ return this.p;
31
+ },
32
+ }
33
+ );
34
+ this.p
35
+ .finally(() => {
36
+ delete this._progressList;
37
+ })
38
+ .catch(() => void 0);
39
+ }
40
+
41
+ notify(progress: PT): this {
42
+ for (const cb of this._progressList!) {
43
+ cb(progress);
44
+ }
45
+ return this;
46
+ }
47
+
48
+ progress(fn: ProgressCallback<PT>): void {
49
+ this._progressList!.push(fn);
50
+ }
51
+
52
+ get completed(): boolean {
53
+ return typeof this._state === 'boolean';
54
+ }
55
+
56
+ get resolved(): boolean {
57
+ return this._state === true;
58
+ }
59
+
60
+ get rejected(): boolean {
61
+ return this._state === false;
62
+ }
63
+
64
+ /**
65
+ * resolve the promise
66
+ */
67
+ public complete(value: T) {
68
+ this._state = true;
69
+ this._completeCallback(value);
70
+ }
71
+
72
+ /**
73
+ * reject the promise
74
+ */
75
+ public error(err: any) {
76
+ this._state = false;
77
+ this._errorCallback(err);
78
+ }
79
+
80
+ /**
81
+ * reject the promise with CancelError
82
+ */
83
+ public cancel() {
84
+ this._state = false;
85
+ this._errorCallback(new CanceledError());
86
+ }
87
+
88
+ /**
89
+ * Convert promise into deferred
90
+ * returns a DeferredPromise, resolve when prev resolve, reject when prev reject
91
+ */
92
+ static wrap(prev: Promise<any>) {
93
+ const p = new DeferredPromise();
94
+ prev.then(
95
+ (d) => {
96
+ p.complete(d);
97
+ },
98
+ (e) => {
99
+ p.error(e);
100
+ }
101
+ );
102
+ return p;
103
+ }
104
+ }
@@ -0,0 +1,48 @@
1
+ import { TimeoutError } from './timeoutError';
2
+ import { DeferredPromise } from '../promise/deferredPromise';
3
+
4
+ /**
5
+ * @returns promise reject with TimeoutError after specific time
6
+ */
7
+ export function timeout(ms: number, error = 'no response'): Promise<never> {
8
+ return new Promise((_, reject) => {
9
+ setTimeout(() => {
10
+ reject(new TimeoutError(ms, error));
11
+ }, ms);
12
+ });
13
+ }
14
+
15
+ /**
16
+ * @returns promise resolve after specific time
17
+ */
18
+ export function sleep(ms: number): Promise<void> {
19
+ return new Promise((resolve) => {
20
+ setTimeout(() => {
21
+ resolve();
22
+ }, ms);
23
+ });
24
+ }
25
+
26
+ export function timeoutPromise<T>(ms: number, p: Promise<T>): Promise<T>;
27
+ export function timeoutPromise<T>(ms: number, message: string, p: Promise<T>): Promise<T>;
28
+ export function timeoutPromise<T, PT = any>(ms: number, p: DeferredPromise<T, PT>): DeferredPromise<T, PT>;
29
+ export function timeoutPromise<T, PT = any>(
30
+ ms: number,
31
+ message: string,
32
+ p: DeferredPromise<T, PT>
33
+ ): DeferredPromise<T, PT>;
34
+
35
+ export function timeoutPromise<T>(ms: number, message: string | T, p?: T): T {
36
+ let msg: string | undefined;
37
+ if (typeof message !== 'string') {
38
+ p = message;
39
+ msg = undefined;
40
+ } else {
41
+ msg = message;
42
+ }
43
+ if (p instanceof DeferredPromise) {
44
+ return Promise.race([p.p, timeout(ms, msg)]).then(() => p) as any;
45
+ } else {
46
+ return Promise.race([p, timeout(ms, msg)]).then(() => p) as any;
47
+ }
48
+ }
@@ -0,0 +1,16 @@
1
+ import { humanDate } from '../../date/timeString';
2
+
3
+ /**
4
+ * Error when timeout() done
5
+ * @public
6
+ */
7
+ export class TimeoutError extends Error {
8
+ constructor(time: number, what: string = 'no response') {
9
+ super(`Timeout: ${what} in ${humanDate.deltaTiny(time)}`);
10
+ }
11
+ }
12
+
13
+ /** @public */
14
+ export function isTimeoutError(error: Error): error is TimeoutError {
15
+ return error instanceof TimeoutError;
16
+ }
@@ -0,0 +1,148 @@
1
+ import { nameFunction } from '../function/functionName';
2
+ import { isWeb } from '../platform/os';
3
+
4
+ export enum ColorKind {
5
+ DISABLE,
6
+ TERMINAL,
7
+ WEB,
8
+ DETECT,
9
+ }
10
+
11
+ export interface WrappedConsoleOptions {
12
+ parent?: Console;
13
+ bind?: boolean;
14
+ }
15
+
16
+ export abstract class WrappedConsole {
17
+ public declare info: Console['info'];
18
+ public declare log: Console['log'];
19
+ public declare success: Console['log'];
20
+ public declare debug: Console['debug'];
21
+ public declare error: Console['error'];
22
+ public declare trace: Console['trace'];
23
+ public declare warn: Console['warn'];
24
+ public declare assert: Console['assert'];
25
+
26
+ public declare time: Console['time'];
27
+ public declare timeEnd: Console['timeEnd'];
28
+ public declare timeLog: Console['timeLog'];
29
+ public declare count: Console['count'];
30
+ public declare countReset: Console['countReset'];
31
+ public declare group: Console['group'];
32
+ public declare groupCollapsed: Console['groupCollapsed'];
33
+ public declare groupEnd: Console['groupEnd'];
34
+
35
+ public declare table: Console['table'];
36
+ public declare dir: Console['dir'];
37
+ public declare clear: Console['clear'];
38
+
39
+ protected readonly title: string;
40
+ protected readonly parent: Console;
41
+ protected readonly bind: boolean;
42
+
43
+ constructor(title: string, { parent, bind }: WrappedConsoleOptions = {}) {
44
+ this.title = title;
45
+ this.parent = parent || console;
46
+ this.bind = bind || false;
47
+
48
+ this.info = this.wrapMessageAt('info', 0);
49
+ this.log = this.wrapMessageAt('log', 0);
50
+ this.success = this.wrapMessageAt('log', 0);
51
+ this.debug = this.wrapMessageAt('debug', 0);
52
+ this.error = this.wrapMessageAt('error', 0);
53
+ this.trace = this.wrapMessageAt('trace', 0);
54
+ this.warn = this.wrapMessageAt('warn', 0);
55
+ this.assert = this.wrapMessageAt('assert', 1);
56
+
57
+ this.time = this.wrapMessageAt('time', 0);
58
+ this.timeEnd = this.wrapMessageAt('timeEnd', 0);
59
+ this.timeLog = this.wrapMessageAt('timeLog', 0);
60
+ this.count = this.wrapMessageAt('count', 0);
61
+ this.countReset = this.wrapMessageAt('countReset', 0);
62
+ this.group = this.wrapMessageAt('group', 0);
63
+ this.groupCollapsed = this.wrapMessageAt('groupCollapsed', 0);
64
+
65
+ this.groupEnd = this.wrapSimple('groupEnd');
66
+ this.table = this.wrapExtra('table', ' <<table>>');
67
+ this.dir = this.wrapExtra('dir', ' <<dir>>');
68
+ this.clear = this.wrapExtra('clear', ' <<clear>>');
69
+ }
70
+
71
+ protected wrap<T extends keyof Omit<Console & { Console: any }, 'Console'>>(original: T): Function {
72
+ if (this.bind) {
73
+ return this.parent[original].bind(this.parent) as any;
74
+ } else {
75
+ return this.parent[original];
76
+ }
77
+ }
78
+
79
+ private wrapSimple<T extends keyof Omit<Console & { Console: any }, 'Console'>>(original: T): Console[T] {
80
+ return nameFunction('console:' + original, this.wrap(original) as any);
81
+ }
82
+
83
+ private wrapExtra<T extends keyof Omit<Console & { Console: any }, 'Console'>>(
84
+ original: T,
85
+ exMessage: string
86
+ ): Console[T] {
87
+ const bindedFn = this.wrap(original);
88
+ return nameFunction('console:' + original, (...args: any[]) => {
89
+ this.log(exMessage);
90
+ bindedFn(...args);
91
+ });
92
+ }
93
+
94
+ protected createPrefix(message: string) {
95
+ let prefix = `[${this.title}]`;
96
+ if (message) {
97
+ prefix += `[${message}] `;
98
+ } else {
99
+ prefix += ' ';
100
+ }
101
+ return prefix;
102
+ }
103
+
104
+ private wrapMessageAt<T extends keyof Omit<Console & { Console: any }, 'Console'>>(
105
+ original: T,
106
+ messageLoc: number,
107
+ additionalPrefix?: string
108
+ ): Console[T] {
109
+ let prefix = `[${this.title}]`;
110
+ if (additionalPrefix) {
111
+ prefix += `[${additionalPrefix}] `;
112
+ } else {
113
+ prefix += ' ';
114
+ }
115
+
116
+ const fn = this.wrap(original);
117
+
118
+ return nameFunction('console:' + original, (...args: any[]) => {
119
+ this.convertObjectArg(args, messageLoc);
120
+ this.processColorLabel(args, messageLoc, original, prefix);
121
+ fn(...args);
122
+ });
123
+ }
124
+
125
+ private convertObjectArg(args: any[], pos: number) {
126
+ const msg = args[pos];
127
+ if (args.length > pos) {
128
+ if (typeof msg === 'string') {
129
+ return;
130
+ }
131
+
132
+ args.splice(pos, 0, (isWeb ? ' %o' : ' %j').repeat(args.length - pos).substr(1));
133
+ } else {
134
+ args[pos] = '';
135
+ }
136
+ }
137
+
138
+ protected abstract processColorLabel(
139
+ normalizedArguments: any[],
140
+ messageLoc: number,
141
+ level: string,
142
+ prefix: string
143
+ ): void;
144
+
145
+ protected uncolor(args: any[], pos: number, prefix: string, postfix: string) {
146
+ args[pos] = prefix + args[pos] + postfix;
147
+ }
148
+ }
@@ -0,0 +1,91 @@
1
+ /** Find the index of given item */
2
+ export interface Finder<Type> {
3
+ (this: Type[], item: Type): number;
4
+ }
5
+
6
+ type MyFinder<Type> = (item: Type) => number;
7
+
8
+ export function RegexpFinder(this: RegExp[], item: RegExp): number {
9
+ return this.findIndex((e) => e.toString() === item.toString());
10
+ }
11
+
12
+ /**
13
+ * Like a Set, but use custom compare function insteadof ===
14
+ */
15
+ export class CustomSet<Type = string> {
16
+ protected registry: Type[];
17
+ private finder: MyFinder<Type>;
18
+
19
+ constructor(finder: Finder<Type> = Array.prototype.indexOf) {
20
+ this.registry = [];
21
+ this.finder = finder.bind(this.registry);
22
+ }
23
+
24
+ setFinder(finder: Finder<Type>) {
25
+ this.finder = finder.bind(this.registry);
26
+ }
27
+
28
+ has(item: Type): boolean {
29
+ return this.finder(item) !== -1;
30
+ }
31
+
32
+ add(item: Type): boolean {
33
+ const index = this.finder(item);
34
+ if (index === -1) {
35
+ this.registry.push(item);
36
+ return true;
37
+ } else {
38
+ return false;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * @returns all added values
44
+ */
45
+ addAll(items: Type[]): Type[] {
46
+ return items.filter((e) => this.add(e));
47
+ }
48
+
49
+ delete(item: Type): boolean {
50
+ const index = this.finder(item);
51
+ if (index === -1) {
52
+ return false;
53
+ } else {
54
+ this.registry.splice(index, 1);
55
+ return true;
56
+ }
57
+ }
58
+
59
+ /**
60
+ * @returns all deleted values
61
+ */
62
+ deleteAll(items: Type[]): Type[] {
63
+ return items.filter((e) => this.delete(e));
64
+ }
65
+
66
+ clear() {
67
+ this.registry.length = 0;
68
+ }
69
+
70
+ get length() {
71
+ return this.registry.length;
72
+ }
73
+
74
+ get size() {
75
+ return this.registry.length;
76
+ }
77
+
78
+ [Symbol.iterator](): Iterator<Type> {
79
+ return this.registry[Symbol.iterator]();
80
+ }
81
+ keys(): Iterator<Type> {
82
+ return this.registry[Symbol.iterator]();
83
+ }
84
+ values(): Iterator<Type> {
85
+ return this.registry[Symbol.iterator]();
86
+ }
87
+
88
+ toArray() {
89
+ return this.registry.slice();
90
+ }
91
+ }
@@ -0,0 +1,40 @@
1
+ export interface MapLike<V> {
2
+ [id: string]: V;
3
+ }
4
+
5
+ /**
6
+ * A map, will throw error when try to get not exists key
7
+ */
8
+ export class ExtendMap<K, V> extends Map<K, V> {
9
+ /**
10
+ * Get value from map, if not exists, throw an error
11
+ */
12
+ public get(id: K): V;
13
+ /**
14
+ * Get value from map, if not exists, return def instead (not insert it into map)
15
+ */
16
+ public get(id: K, def: V): V;
17
+
18
+ public get(id: K, def?: V): V {
19
+ if (super.has(id)) {
20
+ return super.get(id)!;
21
+ } else if (arguments.length === 2) {
22
+ return def!;
23
+ } else {
24
+ throw new Error(`Unknown key {${id}} in map.`);
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Get a value, if not exists, call init() and set to map
30
+ */
31
+ public entry(id: K, init: (id: K) => V): V {
32
+ if (super.has(id)) {
33
+ return super.get(id)!;
34
+ } else {
35
+ const nv = init(id);
36
+ super.set(id, nv);
37
+ return nv;
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * assert value is not null or undefined or NaN
3
+ * @public
4
+ */
5
+ export function assertNotNull<T>(val: T | null | undefined): T {
6
+ console.assert(val !== undefined && val !== null, 'AssertValue failed, got %s(%s).', typeof val, val);
7
+ return val!;
8
+ }
9
+
10
+ /**
11
+ * assert value is not null or undefined or NaN
12
+ * @throws Value is null or undefined
13
+ * @public
14
+ */
15
+ export function throwNull<T>(val: T) {
16
+ if (val === undefined || val === null) {
17
+ return val!;
18
+ } else {
19
+ throw new Error('Value is ' + typeof val);
20
+ }
21
+ }
@@ -0,0 +1,10 @@
1
+ export function definePublicConstant(object: any, propertyKey: string | symbol, value: any) {
2
+ if (object[propertyKey] === value) return;
3
+ delete object[propertyKey];
4
+ Object.defineProperty(object, propertyKey, {
5
+ configurable: false,
6
+ enumerable: true,
7
+ writable: false,
8
+ value,
9
+ });
10
+ }
@@ -0,0 +1,27 @@
1
+ /** @public */
2
+ export interface InitFunc<O, T> {
3
+ (this: O): T;
4
+ }
5
+
6
+ /**
7
+ * Define property on target, call init it when first use, them memorize
8
+ * @public
9
+ */
10
+ export function initOnRead<O, T extends keyof O>(target: any, propertyKey: T, init: InitFunc<O, O[T]>) {
11
+ if (target.hasOwnProperty(propertyKey)) {
12
+ return;
13
+ }
14
+ Object.defineProperty(target, propertyKey, {
15
+ configurable: true,
16
+ get: function (): O[T] {
17
+ const data = init.call(this);
18
+ delete target[propertyKey];
19
+ target[propertyKey] = data;
20
+ return data;
21
+ },
22
+ set(v: T) {
23
+ delete target[propertyKey];
24
+ target[propertyKey] = v;
25
+ },
26
+ });
27
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Get deep child property of an object
3
+ * @param path object path seprate by "."
4
+ */
5
+ export function objectPath(obj: object, path: string): any {
6
+ path.split('.').every((name) => {
7
+ return (obj = (obj as any)[name]);
8
+ });
9
+ return obj;
10
+ }
@@ -0,0 +1,52 @@
1
+ import { isArraySame } from '../array/arraySame';
2
+
3
+ /**
4
+ * Should ensure a and b is none-null before call this
5
+ * @returns true when a and b has EXACTLY same keys and values
6
+ */
7
+ export function isObjectSame(a: any, b: any) {
8
+ if (a === b) {
9
+ return true;
10
+ }
11
+ const aks = Object.keys(a);
12
+ if (!isArraySame(aks, Object.keys(b))) {
13
+ return false;
14
+ }
15
+
16
+ for (const k of aks) {
17
+ if (a[k] !== b[k]) {
18
+ return false;
19
+ }
20
+ }
21
+ return true;
22
+ }
23
+
24
+ /**
25
+ * Should ensure a and b is none-null before call this
26
+ * @returns true when a and b has EXACTLY same keys and values, recursive compare all object values
27
+ */
28
+ export function isObjectSameRecursive(a: any, b: any) {
29
+ const aks = Object.keys(a);
30
+ if (!isArraySame(aks, Object.keys(b))) {
31
+ return false;
32
+ }
33
+
34
+ for (const k of aks) {
35
+ if (a[k] !== b[k]) {
36
+ const av = a[k],
37
+ bv = b[k];
38
+ if (av && bv && typeof av === 'object' && typeof bv === 'object') {
39
+ if (Array.isArray(av)) {
40
+ if (Array.isArray(bv)) {
41
+ if (isArraySame(av, bv)) continue;
42
+ }
43
+ } else if (!Array.isArray(bv)) {
44
+ if (isObjectSameRecursive(a[k], b[k])) continue;
45
+ }
46
+ }
47
+
48
+ return false;
49
+ }
50
+ }
51
+ return true;
52
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * return true if a path is absolute:
3
+ * - /xxxx
4
+ * - \xxxx
5
+ * - c:/
6
+ * - c:\
7
+ * - http://
8
+ */
9
+ export function isAbsolute(path: string) {
10
+ return /^\/|^\\|^[a-z]:[\\/]|^[^:]{1,10}:\/\//i.test(path);
11
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * replace // to /
3
+ * replace \ to /
4
+ * remove ending /
5
+ */
6
+ export function normalizePath(p: string) {
7
+ return p.replace(/\\+/, '/').replace(/\/\/+/, '/').replace(/\/+$/, '');
8
+ }
@@ -0,0 +1,42 @@
1
+ import { normalizePath } from './normalizePath';
2
+
3
+ /**
4
+ * Work on "PATH"-like values
5
+ */
6
+ export class PathArray extends Set<string> {
7
+ constructor(init: string, private readonly sep: ':' | ';') {
8
+ super();
9
+ if (init) this.add(init);
10
+ }
11
+
12
+ add(paths: string) {
13
+ for (const p of paths.split(this.sep)) {
14
+ if (!p) continue;
15
+ super.add(normalizePath(p));
16
+ }
17
+ return this;
18
+ }
19
+
20
+ delete(paths: string) {
21
+ let anyRet = false;
22
+ for (const p of paths.split(this.sep)) {
23
+ anyRet = anyRet || super.delete(normalizePath(p));
24
+ }
25
+ return anyRet;
26
+ }
27
+
28
+ has(path: string): boolean {
29
+ return super.has(normalizePath(path));
30
+ }
31
+
32
+ toString() {
33
+ return [...this.values()].join(this.sep);
34
+ }
35
+
36
+ /**
37
+ * @returns an array with `part` append to every element
38
+ */
39
+ join(part: string) {
40
+ return [...this.values()].map((p) => normalizePath(p + '/' + part));
41
+ }
42
+ }
@@ -0,0 +1,22 @@
1
+ declare const global: any;
2
+ declare const window: any;
3
+
4
+ /**
5
+ * globalThis when supported.
6
+ * if not, window in browser, global in nodejs
7
+ * @public
8
+ */
9
+ export const globalObject: any =
10
+ typeof globalThis === 'undefined' ? (typeof window === 'undefined' ? global : window) : globalThis;
11
+
12
+ export function ensureGlobalObject<T>(symbol: string, constructor: () => T): T {
13
+ const sm = Symbol.for(symbol);
14
+ if (!globalObject[sm]) {
15
+ globalObject[sm] = constructor();
16
+ }
17
+ return globalObject[sm];
18
+ }
19
+
20
+ export function x() {
21
+ return 1;
22
+ }