@idlebox/common 1.3.4 → 1.3.7

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 (75) hide show
  1. package/docs/package-public.d.ts +40 -0
  2. package/generate/main.ts +56 -0
  3. package/generate/package.json +3 -0
  4. package/generate/tsconfig.json +9 -0
  5. package/lib/_export_all_in_one_index.cjs +3 -1
  6. package/lib/_export_all_in_one_index.cjs.map +1 -1
  7. package/lib/_export_all_in_one_index.js +1 -0
  8. package/lib/_export_all_in_one_index.js.map +1 -1
  9. package/lib/string/concatType.generated.cjs +9 -0
  10. package/lib/string/concatType.generated.cjs.map +10 -0
  11. package/lib/string/concatType.generated.js +5 -0
  12. package/lib/string/concatType.generated.js.map +1 -0
  13. package/package.json +2 -1
  14. package/src/array/arrayDiff.ts +0 -31
  15. package/src/array/arraySame.ts +0 -15
  16. package/src/array/arrayUnique.ts +0 -50
  17. package/src/array/normalizeArray.ts +0 -13
  18. package/src/array/sortAlpha.ts +0 -15
  19. package/src/date/consts.ts +0 -5
  20. package/src/date/isInvalid.ts +0 -6
  21. package/src/date/sibling.ts +0 -28
  22. package/src/date/timeString.ts +0 -150
  23. package/src/date/unix.ts +0 -13
  24. package/src/debugging/tryInspect.ts +0 -37
  25. package/src/error/convertUnknown.ts +0 -10
  26. package/src/error/getFrame.ts +0 -13
  27. package/src/function/asyncCallbackList.ts +0 -75
  28. package/src/function/callbackList.ts +0 -84
  29. package/src/function/delayCallbackList.ts +0 -45
  30. package/src/function/functionName.ts +0 -39
  31. package/src/lifecycle/dispose/bridges/rxjs.ts +0 -6
  32. package/src/lifecycle/dispose/disposableEvent.ts +0 -117
  33. package/src/lifecycle/dispose/disposedError.ts +0 -16
  34. package/src/lifecycle/dispose/lifecycle.async.ts +0 -61
  35. package/src/lifecycle/dispose/lifecycle.global.ts +0 -61
  36. package/src/lifecycle/dispose/lifecycle.sync.ts +0 -79
  37. package/src/lifecycle/dispose/lifecycle.ts +0 -28
  38. package/src/lifecycle/event/event.ts +0 -63
  39. package/src/lifecycle/promise/cancel.ts +0 -16
  40. package/src/lifecycle/promise/cancellationToken/driver.browser.ts +0 -55
  41. package/src/lifecycle/promise/cancellationToken/driver.common.ts +0 -43
  42. package/src/lifecycle/promise/cancellationToken/source.ts +0 -48
  43. package/src/lifecycle/promise/deferredPromise.ts +0 -102
  44. package/src/lifecycle/timeout/timeout.ts +0 -48
  45. package/src/lifecycle/timeout/timeoutError.ts +0 -16
  46. package/src/log/logger.ts +0 -148
  47. package/src/mapSet/customSet.ts +0 -91
  48. package/src/mapSet/extendMap.ts +0 -40
  49. package/src/misc/assertNotNull.ts +0 -21
  50. package/src/object/definePublicConstant.ts +0 -10
  51. package/src/object/initOnRead.ts +0 -27
  52. package/src/object/objectPath.ts +0 -10
  53. package/src/object/objectSame.ts +0 -52
  54. package/src/path/isAbsolute.ts +0 -11
  55. package/src/path/normalizePath.ts +0 -8
  56. package/src/path/pathArray.ts +0 -42
  57. package/src/platform/globalObject.ts +0 -18
  58. package/src/platform/globalSingleton.ts +0 -82
  59. package/src/platform/globalSymbol.ts +0 -36
  60. package/src/platform/os.ts +0 -45
  61. package/src/promise/awaitIterator.ts +0 -19
  62. package/src/promise/finishAllPromise.ts +0 -50
  63. package/src/promise/promiseBool.ts +0 -10
  64. package/src/promise/promisePool.ts +0 -40
  65. package/src/promise/timeoutPromisePool.ts +0 -22
  66. package/src/reflection/classes/hookClass.ts +0 -47
  67. package/src/reflection/classes/singleton.ts +0 -33
  68. package/src/reflection/methods/bind.ts +0 -30
  69. package/src/reflection/methods/initOnRead.ts +0 -11
  70. package/src/reflection/methods/memorize.ts +0 -33
  71. package/src/string/castCase.ts +0 -44
  72. package/src/string/escapeRegexp.ts +0 -4
  73. package/src/string/pad2.ts +0 -11
  74. package/src/string/sizeString.ts +0 -52
  75. package/src/tsconfig.json +0 -12
@@ -1,55 +0,0 @@
1
- import { EventHandler } from '../../event/event';
2
-
3
- import type { CancellationDriver, __CancellationToken } from './source';
4
-
5
- declare const AbortController: any; // TODO
6
- declare type AbortController = any; // TODO
7
-
8
- /** @internal */
9
- export class CancellationDriverBrowser implements CancellationDriver {
10
- private readonly controller: AbortController;
11
- public readonly token: __CancellationToken;
12
- private readonly disposeList: (() => void)[] = [];
13
-
14
- constructor() {
15
- this.controller = new AbortController();
16
-
17
- const signal = this.controller.signal;
18
- signal.addEventListener('abort', () => {
19
- token.isCancellationRequested = true;
20
- });
21
-
22
- const disposeList = this.disposeList;
23
- const token = {
24
- isCancellationRequested: false,
25
- onCancellationRequested(listener: EventHandler<void>) {
26
- const callback = () => listener();
27
-
28
- signal.addEventListener('abort', callback);
29
-
30
- const dispose = () => signal.removeEventListener('abort', callback);
31
- disposeList.push(dispose);
32
-
33
- return { dispose };
34
- },
35
- };
36
-
37
- this.token = token;
38
- }
39
-
40
- cancel() {
41
- if (this.token.isCancellationRequested) {
42
- console.warn('[CancellationTokenSource] is already canceled.');
43
- } else {
44
- this.token.isCancellationRequested = true;
45
- this.controller.abort();
46
- }
47
- }
48
-
49
- dispose() {
50
- for (const removeListener of this.disposeList) removeListener();
51
- if (!this.token.isCancellationRequested) {
52
- this.controller.abort();
53
- }
54
- }
55
- }
@@ -1,43 +0,0 @@
1
- import { IDisposable } from '../../dispose/lifecycle';
2
- import { Emitter, EventHandler } from '../../event/event';
3
-
4
- import type { CancellationDriver, __CancellationToken } from './source';
5
-
6
- /** @internal */
7
- export class CancellationDriverCommon implements CancellationDriver {
8
- private readonly emitter: Emitter<void>;
9
- public readonly token: __CancellationToken;
10
- private readonly disposeList: IDisposable[] = [];
11
-
12
- constructor() {
13
- const emitter = new Emitter<void>();
14
- this.emitter = emitter;
15
-
16
- const disposeList = this.disposeList;
17
- const token = {
18
- isCancellationRequested: false,
19
- onCancellationRequested(callback: EventHandler<void>) {
20
- const ret = emitter.handle(callback);
21
- disposeList.push(ret);
22
- return ret;
23
- },
24
- };
25
- this.token = token;
26
- }
27
-
28
- cancel() {
29
- if (this.token.isCancellationRequested) {
30
- console.warn('[CancellationTokenSource] is already canceled.');
31
- } else {
32
- this.token.isCancellationRequested = true;
33
- this.emitter.fire();
34
- }
35
- }
36
-
37
- dispose() {
38
- for (const listen of this.disposeList) listen.dispose();
39
- if (!this.token.isCancellationRequested) {
40
- this.cancel();
41
- }
42
- }
43
- }
@@ -1,48 +0,0 @@
1
- import { globalObject } from '../../../platform/globalObject';
2
- import { IDisposable } from '../../dispose/lifecycle';
3
- import { DisposableOnce } from '../../dispose/lifecycle.sync';
4
- import { EventHandler } from '../../event/event';
5
- import { CancellationDriverBrowser } from './driver.browser';
6
- import { CancellationDriverCommon } from './driver.common';
7
-
8
- /** @public */
9
- export interface CancellationToken {
10
- readonly isCancellationRequested: boolean;
11
- onCancellationRequested(callback: EventHandler<void>): IDisposable;
12
- }
13
-
14
- /** @internal */
15
- export interface __CancellationToken {
16
- isCancellationRequested: boolean;
17
- onCancellationRequested(callback: EventHandler<void>): IDisposable;
18
- }
19
-
20
- /** @internal */
21
- export interface CancellationDriver extends IDisposable {
22
- readonly token: CancellationToken;
23
- cancel(): void;
24
- }
25
-
26
- /** @public */
27
- export class CancellationTokenSource extends DisposableOnce implements IDisposable {
28
- private readonly driver: CancellationDriver;
29
- public readonly token: CancellationToken;
30
-
31
- constructor() {
32
- super();
33
- if ('AbortController' in globalObject) {
34
- this.driver = new CancellationDriverBrowser();
35
- } else {
36
- this.driver = new CancellationDriverCommon();
37
- }
38
- this.token = this.driver.token;
39
- }
40
-
41
- cancel(): void {
42
- this.driver.cancel();
43
- }
44
-
45
- _dispose(): void {
46
- this.driver.dispose();
47
- }
48
- }
@@ -1,102 +0,0 @@
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.finally(() => {
35
- delete this._progressList;
36
- });
37
- }
38
-
39
- notify(progress: PT): this {
40
- for (const cb of this._progressList!) {
41
- cb(progress);
42
- }
43
- return this;
44
- }
45
-
46
- progress(fn: ProgressCallback<PT>): void {
47
- this._progressList!.push(fn);
48
- }
49
-
50
- get completed(): boolean {
51
- return typeof this._state === 'boolean';
52
- }
53
-
54
- get resolved(): boolean {
55
- return this._state === true;
56
- }
57
-
58
- get rejected(): boolean {
59
- return this._state === false;
60
- }
61
-
62
- /**
63
- * resolve the promise
64
- */
65
- public complete(value: T) {
66
- this._state = true;
67
- this._completeCallback(value);
68
- }
69
-
70
- /**
71
- * reject the promise
72
- */
73
- public error(err: any) {
74
- this._state = false;
75
- this._errorCallback(err);
76
- }
77
-
78
- /**
79
- * reject the promise with CancelError
80
- */
81
- public cancel() {
82
- this._state = false;
83
- this._errorCallback(new CanceledError());
84
- }
85
-
86
- /**
87
- * Convert promise into deferred
88
- * returns a DeferredPromise, resolve when prev resolve, reject when prev reject
89
- */
90
- static wrap(prev: Promise<any>) {
91
- const p = new DeferredPromise();
92
- prev.then(
93
- (d) => {
94
- p.complete(d);
95
- },
96
- (e) => {
97
- p.error(e);
98
- }
99
- );
100
- return p;
101
- }
102
- }
@@ -1,48 +0,0 @@
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
- }
@@ -1,16 +0,0 @@
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
- }
package/src/log/logger.ts DELETED
@@ -1,148 +0,0 @@
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
- }
@@ -1,91 +0,0 @@
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
- }
@@ -1,40 +0,0 @@
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
- }
@@ -1,21 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,27 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }