@beecode/msh-util 1.0.1-alpha

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/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/lib/class-factory-pattern.d.ts +25 -0
  4. package/lib/class-factory-pattern.d.ts.map +1 -0
  5. package/lib/class-factory-pattern.js +30 -0
  6. package/lib/class-factory-pattern.js.map +1 -0
  7. package/lib/express/error-handler.d.ts +17 -0
  8. package/lib/express/error-handler.d.ts.map +1 -0
  9. package/lib/express/error-handler.js +28 -0
  10. package/lib/express/error-handler.js.map +1 -0
  11. package/lib/index.d.ts +14 -0
  12. package/lib/index.d.ts.map +1 -0
  13. package/lib/index.js +30 -0
  14. package/lib/index.js.map +1 -0
  15. package/lib/joi-util.d.ts +47 -0
  16. package/lib/joi-util.d.ts.map +1 -0
  17. package/lib/joi-util.js +63 -0
  18. package/lib/joi-util.js.map +1 -0
  19. package/lib/memoize-factory.d.ts +16 -0
  20. package/lib/memoize-factory.d.ts.map +1 -0
  21. package/lib/memoize-factory.js +28 -0
  22. package/lib/memoize-factory.js.map +1 -0
  23. package/lib/object-util.d.ts +70 -0
  24. package/lib/object-util.d.ts.map +1 -0
  25. package/lib/object-util.js +114 -0
  26. package/lib/object-util.js.map +1 -0
  27. package/lib/regex-util.d.ts +12 -0
  28. package/lib/regex-util.d.ts.map +1 -0
  29. package/lib/regex-util.js +15 -0
  30. package/lib/regex-util.js.map +1 -0
  31. package/lib/single-threshold-promise.d.ts +31 -0
  32. package/lib/single-threshold-promise.d.ts.map +1 -0
  33. package/lib/single-threshold-promise.js +49 -0
  34. package/lib/single-threshold-promise.js.map +1 -0
  35. package/lib/singleton/async.d.ts +50 -0
  36. package/lib/singleton/async.d.ts.map +1 -0
  37. package/lib/singleton/async.js +78 -0
  38. package/lib/singleton/async.js.map +1 -0
  39. package/lib/singleton/pattern.d.ts +34 -0
  40. package/lib/singleton/pattern.d.ts.map +1 -0
  41. package/lib/singleton/pattern.js +45 -0
  42. package/lib/singleton/pattern.js.map +1 -0
  43. package/lib/string-util.d.ts +10 -0
  44. package/lib/string-util.d.ts.map +1 -0
  45. package/lib/string-util.js +19 -0
  46. package/lib/string-util.js.map +1 -0
  47. package/lib/time-util.d.ts +74 -0
  48. package/lib/time-util.d.ts.map +1 -0
  49. package/lib/time-util.js +97 -0
  50. package/lib/time-util.js.map +1 -0
  51. package/lib/timeout.d.ts +15 -0
  52. package/lib/timeout.d.ts.map +1 -0
  53. package/lib/timeout.js +21 -0
  54. package/lib/timeout.js.map +1 -0
  55. package/lib/type-util.d.ts +50 -0
  56. package/lib/type-util.d.ts.map +1 -0
  57. package/lib/type-util.js +57 -0
  58. package/lib/type-util.js.map +1 -0
  59. package/lib/types/any-function/index.d.ts +2 -0
  60. package/lib/types/any-function/index.d.ts.map +1 -0
  61. package/lib/types/any-function/index.js +3 -0
  62. package/lib/types/any-function/index.js.map +1 -0
  63. package/lib/types/any-function/no-params.d.ts +2 -0
  64. package/lib/types/any-function/no-params.d.ts.map +1 -0
  65. package/lib/types/any-function/no-params.js +3 -0
  66. package/lib/types/any-function/no-params.js.map +1 -0
  67. package/lib/types/any-function/promise-no-params.d.ts +2 -0
  68. package/lib/types/any-function/promise-no-params.d.ts.map +1 -0
  69. package/lib/types/any-function/promise-no-params.js +3 -0
  70. package/lib/types/any-function/promise-no-params.js.map +1 -0
  71. package/lib/types/any-function/promise.d.ts +2 -0
  72. package/lib/types/any-function/promise.d.ts.map +1 -0
  73. package/lib/types/any-function/promise.js +3 -0
  74. package/lib/types/any-function/promise.js.map +1 -0
  75. package/package.json +127 -0
@@ -0,0 +1,31 @@
1
+ import { AnyFunctionPromiseNoParams } from './types/any-function/promise-no-params';
2
+ /**
3
+ * SingleThresholdPromise returns a single promise, and subsequent calls made before the promise resolves will return the same promise.
4
+ * @example
5
+ * export const refreshTokenSingleThreshold = new SingleThresholdPromise(async () => {
6
+ * const oldRefreshToken = await refreshTokenService.get()
7
+ * const { accessToken, refreshToken } = await authService.refreshToken({
8
+ * refreshToken: oldRefreshToken,
9
+ * })
10
+ * return { accessToken, refreshToken }
11
+ * })
12
+ *
13
+ * export const authService = {
14
+ * refreshToken: async (): Promise<{ accessToken: string; refreshToken:string }> => {
15
+ * return refreshTokenSingleThreshold.promise()
16
+ * }
17
+ * }
18
+ */
19
+ export declare class SingleThresholdPromise<T> {
20
+ protected _cache: {
21
+ promises?: {
22
+ resolve: (value: T | PromiseLike<T>) => void;
23
+ reject: (reason?: any) => void;
24
+ }[];
25
+ };
26
+ protected _factoryFn: AnyFunctionPromiseNoParams<T>;
27
+ constructor(factoryFn: AnyFunctionPromiseNoParams<T>);
28
+ protected _rejectPromises(): void;
29
+ promise(): Promise<T>;
30
+ }
31
+ //# sourceMappingURL=single-threshold-promise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"single-threshold-promise.d.ts","sourceRoot":"","sources":["../src/single-threshold-promise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,0CAA0C,CAAA;AAErF;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,sBAAsB,CAAC,CAAC;IACnC,SAAS,CAAC,MAAM,EAAE;QAChB,QAAQ,CAAC,EAAE;YAAE,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;SAAE,EAAE,CAAA;KAC9F,CAAK;IAEN,SAAS,CAAC,UAAU,EAAE,0BAA0B,CAAC,CAAC,CAAC,CAAA;gBAEvC,SAAS,EAAE,0BAA0B,CAAC,CAAC,CAAC;IAIpD,SAAS,CAAC,eAAe,IAAI,IAAI;IAO3B,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;CAkB5B"}
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SingleThresholdPromise = void 0;
4
+ /**
5
+ * SingleThresholdPromise returns a single promise, and subsequent calls made before the promise resolves will return the same promise.
6
+ * @example
7
+ * export const refreshTokenSingleThreshold = new SingleThresholdPromise(async () => {
8
+ * const oldRefreshToken = await refreshTokenService.get()
9
+ * const { accessToken, refreshToken } = await authService.refreshToken({
10
+ * refreshToken: oldRefreshToken,
11
+ * })
12
+ * return { accessToken, refreshToken }
13
+ * })
14
+ *
15
+ * export const authService = {
16
+ * refreshToken: async (): Promise<{ accessToken: string; refreshToken:string }> => {
17
+ * return refreshTokenSingleThreshold.promise()
18
+ * }
19
+ * }
20
+ */
21
+ class SingleThresholdPromise {
22
+ constructor(factoryFn) {
23
+ this._cache = {};
24
+ this._factoryFn = factoryFn;
25
+ }
26
+ _rejectPromises() {
27
+ if (this._cache.promises) {
28
+ this._cache.promises.forEach((promise) => promise.reject(new Error('Cache was cleaned')));
29
+ }
30
+ delete this._cache.promises;
31
+ }
32
+ async promise() {
33
+ if ('promises' in this._cache) {
34
+ return new Promise((resolve, reject) => {
35
+ this._cache.promises.push({ resolve, reject });
36
+ });
37
+ }
38
+ this._cache.promises = [];
39
+ const result = await this._factoryFn().catch((err) => {
40
+ this._rejectPromises();
41
+ throw err;
42
+ });
43
+ this._cache.promises.forEach((promise) => promise.resolve(result));
44
+ delete this._cache.promises;
45
+ return result;
46
+ }
47
+ }
48
+ exports.SingleThresholdPromise = SingleThresholdPromise;
49
+ //# sourceMappingURL=single-threshold-promise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"single-threshold-promise.js","sourceRoot":"","sources":["../src/single-threshold-promise.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAa,sBAAsB;IAOjC,YAAY,SAAwC;QAN1C,WAAM,GAEZ,EAAE,CAAA;QAKJ,IAAI,CAAC,UAAU,GAAG,SAAS,CAAA;IAC7B,CAAC;IAES,eAAe;QACvB,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE;YACxB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAA;SAC1F;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAA;IAC7B,CAAC;IAED,KAAK,CAAC,OAAO;QACX,IAAI,UAAU,IAAI,IAAI,CAAC,MAAM,EAAE;YAC7B,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACxC,IAAI,CAAC,MAAM,CAAC,QAAS,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAA;YACjD,CAAC,CAAC,CAAA;SACH;QAED,IAAI,CAAC,MAAM,CAAC,QAAQ,GAAG,EAAE,CAAA;QACzB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACnD,IAAI,CAAC,eAAe,EAAE,CAAA;YACtB,MAAM,GAAG,CAAA;QACX,CAAC,CAAC,CAAA;QAEF,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAA;QAClE,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAA;QAE3B,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AApCD,wDAoCC"}
@@ -0,0 +1,50 @@
1
+ import { AnyFunctionPromiseNoParams } from '../types/any-function/promise-no-params';
2
+ /**
3
+ * This is a singleton wrapper that is used to wrap around async function. We have additional functionality to clear the cache
4
+ * and reject any subscriptions to initial promise. And we can also check if there is anything i cache
5
+ * @example
6
+ * export const configSingleton = new SingletonAsync(async () => {
7
+ * await timeout(3000)
8
+ * return {
9
+ * env: process.env.NODE_ENV
10
+ * } as const
11
+ * })
12
+ *
13
+ * // using
14
+ * // cache value before we call promise
15
+ * console.log(configSingleton().cache()) // undefined
16
+ * console.log('NODE_ENV: ', await configSingleton().promise().env) // NODE_ENV: prod
17
+ * // cache value after we call promise
18
+ * console.log(configSingleton().cache()) // { env: 'prod' }
19
+ */
20
+ export declare class SingletonAsync<T> {
21
+ protected _cache: {
22
+ singleton?: T;
23
+ promises?: {
24
+ resolve: (value: T | PromiseLike<T>) => void;
25
+ reject: (reason?: any) => void;
26
+ }[];
27
+ };
28
+ protected _factory: AnyFunctionPromiseNoParams<T>;
29
+ constructor(factory: AnyFunctionPromiseNoParams<T>);
30
+ /**
31
+ * Empty cached value and reject any subscribed promise that is waiting for the initial promise to be resolved.
32
+ */
33
+ cleanCache(): void;
34
+ protected _rejectPromises(params: {
35
+ error: Error;
36
+ }): void;
37
+ /**
38
+ * Return singleton value in a promise. If there is no cached value then try to get it from factory.
39
+ * @template T
40
+ * @returns {Promise<T>}
41
+ */
42
+ promise(): Promise<T>;
43
+ /**
44
+ * Return cached value, if there is no value cached return undefined.
45
+ * @template T
46
+ * @returns {T | undefined}
47
+ */
48
+ cached(): T | undefined;
49
+ }
50
+ //# sourceMappingURL=async.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/singleton/async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,0CAA0C,CAAA;AAErF;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAc,CAAC,CAAC;IAC3B,SAAS,CAAC,MAAM,EAAE;QAChB,SAAS,CAAC,EAAE,CAAC,CAAA;QACb,QAAQ,CAAC,EAAE;YAAE,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;SAAE,EAAE,CAAA;KAC9F,CAAK;IAEN,SAAS,CAAC,QAAQ,EAAE,0BAA0B,CAAC,CAAC,CAAC,CAAA;gBAErC,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC;IAIlD;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB,SAAS,CAAC,eAAe,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,KAAK,CAAA;KAAE,GAAG,IAAI;IASzD;;;;OAIG;IACG,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;IAuB3B;;;;OAIG;IACH,MAAM,IAAI,CAAC,GAAG,SAAS;CAOxB"}
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SingletonAsync = void 0;
4
+ /**
5
+ * This is a singleton wrapper that is used to wrap around async function. We have additional functionality to clear the cache
6
+ * and reject any subscriptions to initial promise. And we can also check if there is anything i cache
7
+ * @example
8
+ * export const configSingleton = new SingletonAsync(async () => {
9
+ * await timeout(3000)
10
+ * return {
11
+ * env: process.env.NODE_ENV
12
+ * } as const
13
+ * })
14
+ *
15
+ * // using
16
+ * // cache value before we call promise
17
+ * console.log(configSingleton().cache()) // undefined
18
+ * console.log('NODE_ENV: ', await configSingleton().promise().env) // NODE_ENV: prod
19
+ * // cache value after we call promise
20
+ * console.log(configSingleton().cache()) // { env: 'prod' }
21
+ */
22
+ class SingletonAsync {
23
+ constructor(factory) {
24
+ this._cache = {};
25
+ this._factory = factory;
26
+ }
27
+ /**
28
+ * Empty cached value and reject any subscribed promise that is waiting for the initial promise to be resolved.
29
+ */
30
+ cleanCache() {
31
+ delete this._cache.singleton;
32
+ this._rejectPromises({ error: new Error('Cache was cleaned') });
33
+ }
34
+ _rejectPromises(params) {
35
+ const { error } = params;
36
+ if (this._cache.promises) {
37
+ this._cache.promises.forEach((promise) => promise.reject(error));
38
+ }
39
+ delete this._cache.promises;
40
+ }
41
+ /**
42
+ * Return singleton value in a promise. If there is no cached value then try to get it from factory.
43
+ * @template T
44
+ * @returns {Promise<T>}
45
+ */
46
+ async promise() {
47
+ if ('singleton' in this._cache) {
48
+ return this._cache.singleton;
49
+ }
50
+ if ('promises' in this._cache) {
51
+ return new Promise((resolve, reject) => {
52
+ this._cache.promises.push({ resolve, reject });
53
+ });
54
+ }
55
+ this._cache.promises = [];
56
+ const result = await this._factory().catch((error) => {
57
+ this._rejectPromises({ error });
58
+ throw error;
59
+ });
60
+ this._cache.singleton = result;
61
+ this._cache.promises.forEach((promise) => promise.resolve(result));
62
+ delete this._cache.promises;
63
+ return result;
64
+ }
65
+ /**
66
+ * Return cached value, if there is no value cached return undefined.
67
+ * @template T
68
+ * @returns {T | undefined}
69
+ */
70
+ cached() {
71
+ if ('singleton' in this._cache) {
72
+ return this._cache.singleton;
73
+ }
74
+ return undefined;
75
+ }
76
+ }
77
+ exports.SingletonAsync = SingletonAsync;
78
+ //# sourceMappingURL=async.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async.js","sourceRoot":"","sources":["../../src/singleton/async.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,cAAc;IAQzB,YAAY,OAAsC;QAPxC,WAAM,GAGZ,EAAE,CAAA;QAKJ,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;IACzB,CAAC;IAED;;OAEG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAA;QAC5B,IAAI,CAAC,eAAe,CAAC,EAAE,KAAK,EAAE,IAAI,KAAK,CAAC,mBAAmB,CAAC,EAAE,CAAC,CAAA;IACjE,CAAC;IAES,eAAe,CAAC,MAAwB;QAChD,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAA;QAExB,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE;YACxB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC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nyFunctionNoParams } from '../types/any-function/no-params';
2
+ /**
3
+ * Singleton patter wrapper function
4
+ * @param {AnyFunctionNoParams<R>} factoryFn Factory function that is used to generate value that is going to be cached and return by
5
+ * singleton.
6
+ * @return {AnyFunctionNoParams<R>} Function result that returns cached value.
7
+ * @example
8
+ * export class SomeClass {
9
+ * constructor(protected _param: string){ }
10
+ * get param(): string {
11
+ * return this._param
12
+ * }
13
+ * }
14
+ * export const someClassSingleton = singletonPattern((): SomeClass => {
15
+ * return new SomeClass('some param value')
16
+ * })
17
+ *
18
+ * // using
19
+ * console.log('param: ', someClassSingleton().param) // param: some param value
20
+ *
21
+ * ///////////////////////////////////////////
22
+ * // Or we can use it with simple function //
23
+ * ///////////////////////////////////////////
24
+ * export const config = singletonPattern(() => {
25
+ * return {
26
+ * env: process.NODE_ENV,
27
+ * } as const
28
+ * })
29
+ *
30
+ * // using
31
+ * console.log('NODE_ENV: ', config().env) // NODE_ENV: prod
32
+ */
33
+ export declare const singletonPattern: <R>(factoryFn: AnyFunctionNoParams<R>) => AnyFunctionNoParams<R>;
34
+ //# sourceMappingURL=pattern.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pattern.d.ts","sourceRoot":"","sources":["../../src/singleton/pattern.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAA;AAEtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,gBAAgB,kEAU5B,CAAA"}
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.singletonPattern = void 0;
4
+ /**
5
+ * Singleton patter wrapper function
6
+ * @param {AnyFunctionNoParams<R>} factoryFn Factory function that is used to generate value that is going to be cached and return by
7
+ * singleton.
8
+ * @return {AnyFunctionNoParams<R>} Function result that returns cached value.
9
+ * @example
10
+ * export class SomeClass {
11
+ * constructor(protected _param: string){ }
12
+ * get param(): string {
13
+ * return this._param
14
+ * }
15
+ * }
16
+ * export const someClassSingleton = singletonPattern((): SomeClass => {
17
+ * return new SomeClass('some param value')
18
+ * })
19
+ *
20
+ * // using
21
+ * console.log('param: ', someClassSingleton().param) // param: some param value
22
+ *
23
+ * ///////////////////////////////////////////
24
+ * // Or we can use it with simple function //
25
+ * ///////////////////////////////////////////
26
+ * export const config = singletonPattern(() => {
27
+ * return {
28
+ * env: process.NODE_ENV,
29
+ * } as const
30
+ * })
31
+ *
32
+ * // using
33
+ * console.log('NODE_ENV: ', config().env) // NODE_ENV: prod
34
+ */
35
+ const singletonPattern = (factoryFn) => {
36
+ const cache = {};
37
+ return () => {
38
+ if ('singleton' in cache) {
39
+ return cache.singleton;
40
+ }
41
+ return (cache.singleton = factoryFn());
42
+ };
43
+ };
44
+ exports.singletonPattern = singletonPattern;
45
+ //# sourceMappingURL=pattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pattern.js","sourceRoot":"","sources":["../../src/singleton/pattern.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACI,MAAM,gBAAgB,GAAG,CAAI,SAAiC,EAA0B,EAAE;IAC/F,MAAM,KAAK,GAAsB,EAAE,CAAA;IAEnC,OAAO,GAAM,EAAE;QACb,IAAI,WAAW,IAAI,KAAK,EAAE;YACxB,OAAO,KAAK,CAAC,SAAU,CAAA;SACxB;QAED,OAAO,CAAC,KAAK,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC,CAAA;IACxC,CAAC,CAAA;AACH,CAAC,CAAA;AAVY,QAAA,gBAAgB,oBAU5B"}
@@ -0,0 +1,10 @@
1
+ export declare const stringUtil: {
2
+ /**
3
+ * Generate random UUID
4
+ * @return {string}
5
+ * @example
6
+ * console.log(stringUtil.uuid()) // "69bfda25-df3f-46b4-8bbb-955cf5193426"
7
+ */
8
+ generateUUID: () => string;
9
+ };
10
+ //# sourceMappingURL=string-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string-util.d.ts","sourceRoot":"","sources":["../src/string-util.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU;IACrB;;;;;OAKG;wBACe,MAAM;CAQzB,CAAA"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stringUtil = void 0;
4
+ exports.stringUtil = {
5
+ /**
6
+ * Generate random UUID
7
+ * @return {string}
8
+ * @example
9
+ * console.log(stringUtil.uuid()) // "69bfda25-df3f-46b4-8bbb-955cf5193426"
10
+ */
11
+ generateUUID: () => {
12
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
13
+ const r = (Math.random() * 16) | 0;
14
+ const v = c == 'x' ? r : (r & 0x3) | 0x8;
15
+ return v.toString(16);
16
+ });
17
+ },
18
+ };
19
+ //# sourceMappingURL=string-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string-util.js","sourceRoot":"","sources":["../src/string-util.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAG;IACxB;;;;;OAKG;IACH,YAAY,EAAE,GAAW,EAAE;QACzB,OAAO,sCAAsC,CAAC,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC;YACxE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAA;YAClC,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAA;YAExC,OAAO,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;QACvB,CAAC,CAAC,CAAA;IACJ,CAAC;CACF,CAAA"}
@@ -0,0 +1,74 @@
1
+ export declare enum DurationUnit {
2
+ MILLISECOND = "MILLISECOND",
3
+ SECOND = "SECOND",
4
+ MINUTE = "MINUTE",
5
+ HOUR = "HOUR",
6
+ DAY = "DAY",
7
+ WEEK = "WEEK",
8
+ MONTH = "MONTH",
9
+ YEAR = "YEAR"
10
+ }
11
+ export type DurationUnitType = `${DurationUnit}`;
12
+ export declare class TimeUtil {
13
+ /**
14
+ * return date object with the current time
15
+ * @return {Date}
16
+ * @example
17
+ * console.log(new TimeUtil().now().toISOString()) // 2023-03-08T19:45:01.991Z
18
+ */
19
+ now(): Date;
20
+ /**
21
+ * Convert date object to unix timestamp (milliseconds)
22
+ * @param {Date} date
23
+ * @return {number}
24
+ * @example
25
+ * // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
26
+ * const timeUtil = new TimeUtil()
27
+ * console.log(timeUtil.dateToUnix(timeUtil.now())) // 1678304701991
28
+ */
29
+ dateToUnix(date: Date): number;
30
+ /**
31
+ * Convert date object to unix timestamp (seconds)
32
+ * @param {Date} date
33
+ * @return {number}
34
+ * @example
35
+ * // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
36
+ * const timeUtil = new TimeUtil()
37
+ * console.log(timeUtil.dateToUnix(timeUtil.now())) // 1678304701
38
+ */
39
+ dateToUnixSec(date: Date): number;
40
+ /**
41
+ * Convert unix timestamp (milliseconds) to date object
42
+ * @param {number} unix
43
+ * @return {Date}
44
+ * @example
45
+ * const timeUtil = new TimeUtil()
46
+ * console.log(timeUtil.unixToDate(1678304701991).toISOString()) // 2023-03-08T19:45:01.991Z
47
+ */
48
+ unixToDate(unix: number): Date;
49
+ /**
50
+ * Convert unix timestamp (seconds) to date object
51
+ * @param {number} unix
52
+ * @return {Date}
53
+ * @example
54
+ * const timeUtil = new TimeUtil()
55
+ * console.log(timeUtil.unixToDate(1678304701).toISOString()) // 2023-03-08T19:45:01.000Z
56
+ */
57
+ unixSecToDate(unix: number): Date;
58
+ /**
59
+ * Change the value of date by unit/value pare.
60
+ * @param {{units: DurationUnitType, value: number, date: Date}} params
61
+ * @return {Date}
62
+ * @example
63
+ * // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
64
+ * const timeUtil = new TimeUtil()
65
+ * console.log(timeUtil.addToDate({date: timeUtil.now(), unit: 'DAY', value: 1 }).toISOString()) // 2023-03-09T19:45:01.991Z
66
+ * console.log(timeUtil.addToDate({date: timeUtil.now(), unit: DurationUnit.MONTH, value: -1 }).toISOString()) //2023-02-08T19:45:01.991Z
67
+ */
68
+ addToDate(params: {
69
+ unit: DurationUnitType | DurationUnit;
70
+ value: number;
71
+ date: Date;
72
+ }): Date;
73
+ }
74
+ //# sourceMappingURL=time-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time-util.d.ts","sourceRoot":"","sources":["../src/time-util.ts"],"names":[],"mappings":"AAKA,oBAAY,YAAY;IACtB,WAAW,gBAAgB;IAC3B,MAAM,WAAW;IACjB,MAAM,WAAW;IACjB,IAAI,SAAS;IACb,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,KAAK,UAAU;IACf,IAAI,SAAS;CACd;AAED,MAAM,MAAM,gBAAgB,GAAG,GAAG,YAAY,EAAE,CAAA;AAEhD,qBAAa,QAAQ;IACnB;;;;;OAKG;IACH,GAAG,IAAI,IAAI;IAIX;;;;;;;;OAQG;IACH,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAI9B;;;;;;;;OAQG;IACH,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAIjC;;;;;;;OAOG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAI9B;;;;;;;OAOG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAIjC;;;;;;;;;OASG;IACH,SAAS,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,gBAAgB,GAAG,YAAY,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,IAAI,CAAA;KAAE,GAAG,IAAI;CAQ9F"}
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TimeUtil = exports.DurationUnit = void 0;
7
+ const add_1 = __importDefault(require("date-fns/add"));
8
+ const addMilliseconds_1 = __importDefault(require("date-fns/addMilliseconds"));
9
+ const format_1 = __importDefault(require("date-fns/format"));
10
+ const parse_1 = __importDefault(require("date-fns/parse"));
11
+ var DurationUnit;
12
+ (function (DurationUnit) {
13
+ DurationUnit["MILLISECOND"] = "MILLISECOND";
14
+ DurationUnit["SECOND"] = "SECOND";
15
+ DurationUnit["MINUTE"] = "MINUTE";
16
+ DurationUnit["HOUR"] = "HOUR";
17
+ DurationUnit["DAY"] = "DAY";
18
+ DurationUnit["WEEK"] = "WEEK";
19
+ DurationUnit["MONTH"] = "MONTH";
20
+ DurationUnit["YEAR"] = "YEAR";
21
+ })(DurationUnit = exports.DurationUnit || (exports.DurationUnit = {}));
22
+ class TimeUtil {
23
+ /**
24
+ * return date object with the current time
25
+ * @return {Date}
26
+ * @example
27
+ * console.log(new TimeUtil().now().toISOString()) // 2023-03-08T19:45:01.991Z
28
+ */
29
+ now() {
30
+ return new Date();
31
+ }
32
+ /**
33
+ * Convert date object to unix timestamp (milliseconds)
34
+ * @param {Date} date
35
+ * @return {number}
36
+ * @example
37
+ * // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
38
+ * const timeUtil = new TimeUtil()
39
+ * console.log(timeUtil.dateToUnix(timeUtil.now())) // 1678304701991
40
+ */
41
+ dateToUnix(date) {
42
+ return +(0, format_1.default)(date, 'T');
43
+ }
44
+ /**
45
+ * Convert date object to unix timestamp (seconds)
46
+ * @param {Date} date
47
+ * @return {number}
48
+ * @example
49
+ * // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
50
+ * const timeUtil = new TimeUtil()
51
+ * console.log(timeUtil.dateToUnix(timeUtil.now())) // 1678304701
52
+ */
53
+ dateToUnixSec(date) {
54
+ return +(0, format_1.default)(date, 't');
55
+ }
56
+ /**
57
+ * Convert unix timestamp (milliseconds) to date object
58
+ * @param {number} unix
59
+ * @return {Date}
60
+ * @example
61
+ * const timeUtil = new TimeUtil()
62
+ * console.log(timeUtil.unixToDate(1678304701991).toISOString()) // 2023-03-08T19:45:01.991Z
63
+ */
64
+ unixToDate(unix) {
65
+ return (0, parse_1.default)(unix.toString(), 'T', this.now());
66
+ }
67
+ /**
68
+ * Convert unix timestamp (seconds) to date object
69
+ * @param {number} unix
70
+ * @return {Date}
71
+ * @example
72
+ * const timeUtil = new TimeUtil()
73
+ * console.log(timeUtil.unixToDate(1678304701).toISOString()) // 2023-03-08T19:45:01.000Z
74
+ */
75
+ unixSecToDate(unix) {
76
+ return (0, parse_1.default)(unix.toString(), 't', this.now());
77
+ }
78
+ /**
79
+ * Change the value of date by unit/value pare.
80
+ * @param {{units: DurationUnitType, value: number, date: Date}} params
81
+ * @return {Date}
82
+ * @example
83
+ * // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
84
+ * const timeUtil = new TimeUtil()
85
+ * console.log(timeUtil.addToDate({date: timeUtil.now(), unit: 'DAY', value: 1 }).toISOString()) // 2023-03-09T19:45:01.991Z
86
+ * console.log(timeUtil.addToDate({date: timeUtil.now(), unit: DurationUnit.MONTH, value: -1 }).toISOString()) //2023-02-08T19:45:01.991Z
87
+ */
88
+ addToDate(params) {
89
+ const { date, unit, value } = params;
90
+ if (`${unit}` === 'MILLISECOND') {
91
+ return (0, addMilliseconds_1.default)(date, value);
92
+ }
93
+ return (0, add_1.default)(date, { [`${unit.toLowerCase()}s`]: value });
94
+ }
95
+ }
96
+ exports.TimeUtil = TimeUtil;
97
+ //# sourceMappingURL=time-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time-util.js","sourceRoot":"","sources":["../src/time-util.ts"],"names":[],"mappings":";;;;;;AAAA,uDAA8B;AAC9B,+EAAsD;AACtD,6DAAoC;AACpC,2DAAkC;AAElC,IAAY,YASX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AAE,CAAC,CAAA;IACzD,CAAC;CACF;AA/ED,4BA+EC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ *
3
+ * @param {number} ms The time, in milliseconds that the timer should wait before the specified function or code is executed. If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle.
4
+ * @return {Promise<void>}
5
+ * @example
6
+ * const lightBlink = (): void => {
7
+ * turnLightOn()
8
+ * timeout(3000) // wait for 3 seconds
9
+ * turnLightOff()
10
+ * timeout(3000) // wait for 3 seconds
11
+ * turnLightOn()
12
+ * }
13
+ */
14
+ export declare const timeout: (ms: number) => Promise<void>;
15
+ //# sourceMappingURL=timeout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,OAAO,OAAQ,MAAM,KAAG,QAAQ,IAAI,CAEhD,CAAA"}
package/lib/timeout.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timeout = void 0;
4
+ /**
5
+ *
6
+ * @param {number} ms The time, in milliseconds that the timer should wait before the specified function or code is executed. If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle.
7
+ * @return {Promise<void>}
8
+ * @example
9
+ * const lightBlink = (): void => {
10
+ * turnLightOn()
11
+ * timeout(3000) // wait for 3 seconds
12
+ * turnLightOff()
13
+ * timeout(3000) // wait for 3 seconds
14
+ * turnLightOn()
15
+ * }
16
+ */
17
+ const timeout = (ms) => {
18
+ return new Promise((resolve) => setTimeout(resolve, ms));
19
+ };
20
+ exports.timeout = timeout;
21
+ //# sourceMappingURL=timeout.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timeout.js","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;GAYG;AACI,MAAM,OAAO,GAAG,CAAC,EAAU,EAAiB,EAAE;IACnD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAA;AAC1D,CAAC,CAAA;AAFY,QAAA,OAAO,WAEnB"}
@@ -0,0 +1,50 @@
1
+ export declare const typeUtil: {
2
+ /**
3
+ * In TypeScript, exhaustiveMessage is a technique that can be used with switch statements to ensure that all possible cases are handled.
4
+ *
5
+ * When using switch statements, it is common to have a default case that handles any unanticipated cases. However, sometimes it is important to ensure that all cases are explicitly handled to avoid potential errors or bugs in the code.
6
+ * @param {string} message
7
+ * @param {never} _
8
+ * @return {string}
9
+ * @example
10
+ * export type Animal = 'cat' | 'dog' | 'bird';
11
+ *
12
+ * export const makeSound = (animal: Animal): string => {
13
+ * switch (animal) {
14
+ * case 'cat':
15
+ * return 'Meow'
16
+ * case 'dog':
17
+ * return 'Woof'
18
+ * case 'bird':
19
+ * return 'Tweet'
20
+ * default:
21
+ * console.error(new TypeUtil().exhaustiveMessage('Unknown animal [animal]', animal))
22
+ * return 'unknown sound'
23
+ * }
24
+ * }
25
+ */
26
+ exhaustiveMessage: (message: string, _: never) => string;
27
+ /**
28
+ * This is the similar to exhaustiveMessage, but instead of message we are returning error so we can throw it
29
+ * @param {string} message
30
+ * @param {never} _
31
+ * @return {Error}
32
+ * @example
33
+ * export type Animal = 'cat' | 'dog' | 'bird';
34
+ *
35
+ * export const makeSound = (animal: Animal): string => {
36
+ * switch (animal) {
37
+ * case 'cat':
38
+ * return 'Meow'
39
+ * case 'dog':
40
+ * return 'Woof'
41
+ * case 'bird':
42
+ * return 'Tweet'
43
+ * default:
44
+ * throw typeUtil.exhaustiveError('Unknown animal [animal]', animal)
45
+ * }
46
+ * }
47
+ */
48
+ exhaustiveError: (message: string, _: never) => Error;
49
+ };
50
+ //# sourceMappingURL=type-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-util.d.ts","sourceRoot":"","sources":["../src/type-util.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ;IACnB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;iCAC0B,MAAM,KAAK,KAAK,KAAG,MAAM;IAItD;;;;;;;;;;;;;;;;;;;;OAoBG;+BACwB,MAAM,KAAK,KAAK,KAAG,KAAK;CAGpD,CAAA"}