@telperion/js-utils 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +621 -0
- package/array/arrayize.cjs +22 -0
- package/array/arrayize.d.ts +17 -0
- package/array/arrayize.js +21 -0
- package/array/arrayize.js.map +1 -0
- package/array/async-map.cjs +40 -0
- package/array/async-map.d.ts +34 -0
- package/array/async-map.js +39 -0
- package/array/async-map.js.map +1 -0
- package/array/compact.cjs +34 -0
- package/array/compact.d.ts +28 -0
- package/array/compact.js +33 -0
- package/array/compact.js.map +1 -0
- package/array/difference.cjs +16 -0
- package/array/difference.d.ts +45 -0
- package/array/difference.js +15 -0
- package/array/difference.js.map +1 -0
- package/array/find-by-key.cjs +35 -0
- package/array/find-by-key.d.ts +30 -0
- package/array/find-by-key.js +34 -0
- package/array/find-by-key.js.map +1 -0
- package/array/index.cjs +14 -0
- package/array/index.d.ts +11 -0
- package/array/index.js +13 -0
- package/array/index.js.map +1 -0
- package/array/intersection.cjs +16 -0
- package/array/intersection.d.ts +45 -0
- package/array/intersection.js +15 -0
- package/array/intersection.js.map +1 -0
- package/array/limit.cjs +37 -0
- package/array/limit.d.ts +29 -0
- package/array/limit.js +36 -0
- package/array/limit.js.map +1 -0
- package/array/proto/async-map.cjs +6 -0
- package/array/proto/async-map.d.ts +25 -0
- package/array/proto/async-map.js +6 -0
- package/array/proto/async-map.js.map +1 -0
- package/array/proto/compact.cjs +6 -0
- package/array/proto/compact.d.ts +15 -0
- package/array/proto/compact.js +6 -0
- package/array/proto/compact.js.map +1 -0
- package/array/proto/difference.cjs +6 -0
- package/array/proto/difference.d.ts +27 -0
- package/array/proto/difference.js +6 -0
- package/array/proto/difference.js.map +1 -0
- package/array/proto/find-by-key.cjs +6 -0
- package/array/proto/find-by-key.d.ts +25 -0
- package/array/proto/find-by-key.js +6 -0
- package/array/proto/find-by-key.js.map +1 -0
- package/array/proto/intersection.cjs +6 -0
- package/array/proto/intersection.d.ts +27 -0
- package/array/proto/intersection.js +6 -0
- package/array/proto/intersection.js.map +1 -0
- package/array/proto/limit.cjs +6 -0
- package/array/proto/limit.d.ts +24 -0
- package/array/proto/limit.js +6 -0
- package/array/proto/limit.js.map +1 -0
- package/array/proto/remove.cjs +6 -0
- package/array/proto/remove.d.ts +32 -0
- package/array/proto/remove.js +6 -0
- package/array/proto/remove.js.map +1 -0
- package/array/proto/replace.cjs +6 -0
- package/array/proto/replace.d.ts +72 -0
- package/array/proto/replace.js +6 -0
- package/array/proto/replace.js.map +1 -0
- package/array/proto/uniquify-by-key.cjs +6 -0
- package/array/proto/uniquify-by-key.d.ts +24 -0
- package/array/proto/uniquify-by-key.js +6 -0
- package/array/proto/uniquify-by-key.js.map +1 -0
- package/array/proto/uniquify.cjs +6 -0
- package/array/proto/uniquify.d.ts +23 -0
- package/array/proto/uniquify.js +6 -0
- package/array/proto/uniquify.js.map +1 -0
- package/array/remove.cjs +46 -0
- package/array/remove.d.ts +33 -0
- package/array/remove.js +45 -0
- package/array/remove.js.map +1 -0
- package/array/replace.cjs +69 -0
- package/array/replace.d.ts +139 -0
- package/array/replace.js +68 -0
- package/array/replace.js.map +1 -0
- package/array/static/arrayize.cjs +4 -0
- package/array/static/arrayize.d.ts +6 -0
- package/array/static/arrayize.js +4 -0
- package/array/static/arrayize.js.map +1 -0
- package/array/uniquify-by-key.cjs +41 -0
- package/array/uniquify-by-key.d.ts +29 -0
- package/array/uniquify-by-key.js +40 -0
- package/array/uniquify-by-key.js.map +1 -0
- package/array/uniquify.cjs +32 -0
- package/array/uniquify.d.ts +27 -0
- package/array/uniquify.js +31 -0
- package/array/uniquify.js.map +1 -0
- package/boolean/static/is-falsy.cjs +4 -0
- package/boolean/static/is-falsy.d.ts +23 -0
- package/boolean/static/is-falsy.js +4 -0
- package/boolean/static/is-falsy.js.map +1 -0
- package/boolean/static/is-truthy.cjs +4 -0
- package/boolean/static/is-truthy.d.ts +23 -0
- package/boolean/static/is-truthy.js +4 -0
- package/boolean/static/is-truthy.js.map +1 -0
- package/class/index.cjs +4 -0
- package/class/index.d.ts +1 -0
- package/class/index.js +3 -0
- package/class/index.js.map +1 -0
- package/class/mixin.cjs +60 -0
- package/class/mixin.d.ts +50 -0
- package/class/mixin.js +59 -0
- package/class/mixin.js.map +1 -0
- package/compact.cjs +39 -0
- package/compact.d.ts +2 -0
- package/compact.js +38 -0
- package/compact.js.map +1 -0
- package/function/call.cjs +6 -0
- package/function/call.d.ts +1 -0
- package/function/call.js +5 -0
- package/function/call.js.map +1 -0
- package/function/debounce.cjs +85 -0
- package/function/debounce.d.ts +44 -0
- package/function/debounce.js +82 -0
- package/function/debounce.js.map +1 -0
- package/function/defer.cjs +42 -0
- package/function/defer.d.ts +26 -0
- package/function/defer.js +41 -0
- package/function/defer.js.map +1 -0
- package/function/index.cjs +8 -0
- package/function/index.d.ts +5 -0
- package/function/index.js +7 -0
- package/function/index.js.map +1 -0
- package/function/noop.cjs +23 -0
- package/function/noop.d.ts +20 -0
- package/function/noop.js +22 -0
- package/function/noop.js.map +1 -0
- package/function/of.cjs +44 -0
- package/function/of.d.ts +31 -0
- package/function/of.js +43 -0
- package/function/of.js.map +1 -0
- package/function/proto/debounce.cjs +6 -0
- package/function/proto/debounce.d.ts +30 -0
- package/function/proto/debounce.js +6 -0
- package/function/proto/debounce.js.map +1 -0
- package/function/static/call.cjs +4 -0
- package/function/static/call.d.ts +6 -0
- package/function/static/call.js +4 -0
- package/function/static/call.js.map +1 -0
- package/function/static/defer.cjs +4 -0
- package/function/static/defer.d.ts +23 -0
- package/function/static/defer.js +4 -0
- package/function/static/defer.js.map +1 -0
- package/function/static/noop.cjs +4 -0
- package/function/static/noop.d.ts +18 -0
- package/function/static/noop.js +4 -0
- package/function/static/noop.js.map +1 -0
- package/function/static/of.cjs +4 -0
- package/function/static/of.d.ts +26 -0
- package/function/static/of.js +4 -0
- package/function/static/of.js.map +1 -0
- package/index.cjs +31 -0
- package/index.d.ts +14 -0
- package/index.js +17 -0
- package/index.js.map +1 -0
- package/is-falsy.cjs +33 -0
- package/is-falsy.d.ts +28 -0
- package/is-falsy.js +32 -0
- package/is-falsy.js.map +1 -0
- package/is-truthy.cjs +33 -0
- package/is-truthy.d.ts +28 -0
- package/is-truthy.js +32 -0
- package/is-truthy.js.map +1 -0
- package/limit.cjs +34 -0
- package/limit.d.ts +2 -0
- package/limit.js +33 -0
- package/limit.js.map +1 -0
- package/map/index.cjs +4 -0
- package/map/index.d.ts +1 -0
- package/map/index.js +3 -0
- package/map/index.js.map +1 -0
- package/map/merge.cjs +33 -0
- package/map/merge.d.ts +24 -0
- package/map/merge.js +32 -0
- package/map/merge.js.map +1 -0
- package/math/index.cjs +4 -0
- package/math/index.d.ts +1 -0
- package/math/index.js +3 -0
- package/math/index.js.map +1 -0
- package/math/min-max.cjs +29 -0
- package/math/min-max.d.ts +24 -0
- package/math/min-max.js +28 -0
- package/math/min-max.js.map +1 -0
- package/math/static/min-max.cjs +4 -0
- package/math/static/min-max.d.ts +20 -0
- package/math/static/min-max.js +4 -0
- package/math/static/min-max.js.map +1 -0
- package/object/clone.cjs +157 -0
- package/object/clone.d.ts +161 -0
- package/object/clone.js +156 -0
- package/object/clone.js.map +1 -0
- package/object/compact.cjs +49 -0
- package/object/compact.d.ts +37 -0
- package/object/compact.js +48 -0
- package/object/compact.js.map +1 -0
- package/object/deep-freeze.cjs +16 -0
- package/object/deep-freeze.d.ts +1 -0
- package/object/deep-freeze.js +15 -0
- package/object/deep-freeze.js.map +1 -0
- package/object/deepest.cjs +55 -0
- package/object/deepest.d.ts +47 -0
- package/object/deepest.js +54 -0
- package/object/deepest.js.map +1 -0
- package/object/index.cjs +7 -0
- package/object/index.d.ts +4 -0
- package/object/index.js +6 -0
- package/object/index.js.map +1 -0
- package/object/static/clone.cjs +4 -0
- package/object/static/clone.d.ts +43 -0
- package/object/static/clone.js +4 -0
- package/object/static/clone.js.map +1 -0
- package/object/static/compact.cjs +4 -0
- package/object/static/compact.d.ts +26 -0
- package/object/static/compact.js +4 -0
- package/object/static/compact.js.map +1 -0
- package/object/static/deep-freeze.cjs +4 -0
- package/object/static/deep-freeze.d.ts +6 -0
- package/object/static/deep-freeze.js +4 -0
- package/object/static/deep-freeze.js.map +1 -0
- package/object/static/deepest.cjs +4 -0
- package/object/static/deepest.d.ts +32 -0
- package/object/static/deepest.js +4 -0
- package/object/static/deepest.js.map +1 -0
- package/open-promise.cjs +115 -0
- package/open-promise.d.ts +80 -0
- package/open-promise.js +113 -0
- package/open-promise.js.map +1 -0
- package/package.json +667 -0
- package/promise/chain.cjs +12 -0
- package/promise/chain.d.ts +7 -0
- package/promise/chain.js +11 -0
- package/promise/chain.js.map +1 -0
- package/promise/index.cjs +10 -0
- package/promise/index.d.ts +7 -0
- package/promise/index.js +9 -0
- package/promise/index.js.map +1 -0
- package/promise/never.cjs +41 -0
- package/promise/never.d.ts +34 -0
- package/promise/never.js +39 -0
- package/promise/never.js.map +1 -0
- package/promise/promisify.cjs +12 -0
- package/promise/promisify.d.ts +7 -0
- package/promise/promisify.js +11 -0
- package/promise/promisify.js.map +1 -0
- package/promise/proto/revert.cjs +6 -0
- package/promise/proto/revert.d.ts +29 -0
- package/promise/proto/revert.js +6 -0
- package/promise/proto/revert.js.map +1 -0
- package/promise/proto/try-catch.cjs +7 -0
- package/promise/proto/try-catch.d.ts +30 -0
- package/promise/proto/try-catch.js +7 -0
- package/promise/proto/try-catch.js.map +1 -0
- package/promise/revert.cjs +43 -0
- package/promise/revert.d.ts +38 -0
- package/promise/revert.js +42 -0
- package/promise/revert.js.map +1 -0
- package/promise/static/chain.cjs +4 -0
- package/promise/static/chain.d.ts +10 -0
- package/promise/static/chain.js +4 -0
- package/promise/static/chain.js.map +1 -0
- package/promise/static/never.cjs +5 -0
- package/promise/static/never.d.ts +39 -0
- package/promise/static/never.js +5 -0
- package/promise/static/never.js.map +1 -0
- package/promise/static/timeout.cjs +4 -0
- package/promise/static/timeout.d.ts +21 -0
- package/promise/static/timeout.js +4 -0
- package/promise/static/timeout.js.map +1 -0
- package/promise/static/try-one-by-one.cjs +4 -0
- package/promise/static/try-one-by-one.d.ts +27 -0
- package/promise/static/try-one-by-one.js +4 -0
- package/promise/static/try-one-by-one.js.map +1 -0
- package/promise/timeout.cjs +104 -0
- package/promise/timeout.d.ts +109 -0
- package/promise/timeout.js +103 -0
- package/promise/timeout.js.map +1 -0
- package/promise/try-catch.cjs +12 -0
- package/promise/try-catch.d.ts +45 -0
- package/promise/try-catch.js +11 -0
- package/promise/try-catch.js.map +1 -0
- package/promise/try-one-by-one.cjs +59 -0
- package/promise/try-one-by-one.d.ts +42 -0
- package/promise/try-one-by-one.js +58 -0
- package/promise/try-one-by-one.js.map +1 -0
- package/set/proto/difference.cjs +6 -0
- package/set/proto/difference.d.ts +26 -0
- package/set/proto/difference.js +6 -0
- package/set/proto/difference.js.map +1 -0
- package/set/proto/intersection.cjs +6 -0
- package/set/proto/intersection.d.ts +26 -0
- package/set/proto/intersection.js +6 -0
- package/set/proto/intersection.js.map +1 -0
- package/smart-map.cjs +108 -0
- package/smart-map.d.ts +81 -0
- package/smart-map.js +106 -0
- package/smart-map.js.map +1 -0
- package/string/index.cjs +4 -0
- package/string/index.d.ts +1 -0
- package/string/index.js +3 -0
- package/string/index.js.map +1 -0
- package/string/limit.cjs +35 -0
- package/string/limit.d.ts +29 -0
- package/string/limit.js +34 -0
- package/string/limit.js.map +1 -0
- package/string/proto/limit.cjs +6 -0
- package/string/proto/limit.d.ts +30 -0
- package/string/proto/limit.js +6 -0
- package/string/proto/limit.js.map +1 -0
- package/time/days.cjs +21 -0
- package/time/days.d.ts +15 -0
- package/time/days.js +20 -0
- package/time/days.js.map +1 -0
- package/time/hours.cjs +21 -0
- package/time/hours.d.ts +15 -0
- package/time/hours.js +20 -0
- package/time/hours.js.map +1 -0
- package/time/index.cjs +10 -0
- package/time/index.d.ts +7 -0
- package/time/index.js +9 -0
- package/time/index.js.map +1 -0
- package/time/minutes.cjs +21 -0
- package/time/minutes.d.ts +15 -0
- package/time/minutes.js +20 -0
- package/time/minutes.js.map +1 -0
- package/time/months.cjs +21 -0
- package/time/months.d.ts +15 -0
- package/time/months.js +20 -0
- package/time/months.js.map +1 -0
- package/time/seconds.cjs +20 -0
- package/time/seconds.d.ts +15 -0
- package/time/seconds.js +19 -0
- package/time/seconds.js.map +1 -0
- package/time/weeks.cjs +21 -0
- package/time/weeks.d.ts +15 -0
- package/time/weeks.js +20 -0
- package/time/weeks.js.map +1 -0
- package/time/years.cjs +21 -0
- package/time/years.d.ts +15 -0
- package/time/years.js +20 -0
- package/time/years.js.map +1 -0
- package/unique-id.cjs +24 -0
- package/unique-id.d.ts +21 -0
- package/unique-id.js +23 -0
- package/unique-id.js.map +1 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Get deepest object in an object chain
|
|
3
|
+
*
|
|
4
|
+
* * * * *
|
|
5
|
+
* Example usage:
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { deepest } from "@telperion/js-utils/object";
|
|
8
|
+
*
|
|
9
|
+
* const a = {
|
|
10
|
+
* x: null
|
|
11
|
+
* };
|
|
12
|
+
*
|
|
13
|
+
* const b = {
|
|
14
|
+
* x: a
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* const c = {
|
|
18
|
+
* x: b
|
|
19
|
+
* };
|
|
20
|
+
*
|
|
21
|
+
* deepest(c, 'x'); // {x: null} (a)
|
|
22
|
+
*
|
|
23
|
+
* ```
|
|
24
|
+
* Static usage example:
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import "@telperion/js-utils/object/static/deepest";
|
|
27
|
+
*
|
|
28
|
+
* const a = {
|
|
29
|
+
* x: null
|
|
30
|
+
* };
|
|
31
|
+
*
|
|
32
|
+
* const b = {
|
|
33
|
+
* x: a
|
|
34
|
+
* };
|
|
35
|
+
*
|
|
36
|
+
* const c = {
|
|
37
|
+
* x: b
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* Object.deepest(c, 'x'); // a
|
|
41
|
+
* ```
|
|
42
|
+
* * * *
|
|
43
|
+
* @param object Object to deep dive
|
|
44
|
+
* @param key key of the object which contains same type instance
|
|
45
|
+
*/
|
|
46
|
+
export function deepest(object, key) {
|
|
47
|
+
let obj = object;
|
|
48
|
+
while (obj[key]) {
|
|
49
|
+
obj = obj[key];
|
|
50
|
+
}
|
|
51
|
+
return obj;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=deepest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/object/deepest.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,UAAU,OAAO,CAAuB,MAAS,EAAE,GAAM;IAC7D,IAAI,GAAG,GAAG,MAAM,CAAC;IAEjB,OAAO,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;QAChB,GAAG,GAAG,GAAG,CAAC,GAAG,CAAM,CAAC;IACtB,CAAC;IAED,OAAO,GAA0B,CAAC;AACpC,CAAC","file":"deepest.js","sourcesContent":["import type { DeepestObject } from \"@thalesrc/extra-ts-types\";\n\n/**\n * #### Get deepest object in an object chain\n *\n * * * * *\n * Example usage:\n * ```typescript\n * import { deepest } from \"@telperion/js-utils/object\";\n *\n * const a = {\n * x: null\n * };\n *\n * const b = {\n * x: a\n * };\n *\n * const c = {\n * x: b\n * };\n *\n * deepest(c, 'x'); // {x: null} (a)\n *\n * ```\n * Static usage example:\n * ```typescript\n * import \"@telperion/js-utils/object/static/deepest\";\n *\n * const a = {\n * x: null\n * };\n *\n * const b = {\n * x: a\n * };\n *\n * const c = {\n * x: b\n * };\n *\n * Object.deepest(c, 'x'); // a\n * ```\n * * * *\n * @param object Object to deep dive\n * @param key key of the object which contains same type instance\n */\nexport function deepest<T, K extends keyof T>(object: T, key: K): DeepestObject<T, K> {\n let obj = object;\n\n while (obj[key]) {\n obj = obj[key] as T;\n }\n\n return obj as DeepestObject<T, K>;\n}\n"]}
|
package/object/index.cjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("@telperion/js-utils/object/clone"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("@telperion/js-utils/object/compact"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("@telperion/js-utils/object/deepest"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("@telperion/js-utils/object/deep-freeze"), exports);
|
package/object/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/object/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC;AACjD,cAAc,oCAAoC,CAAC;AACnD,cAAc,oCAAoC,CAAC;AACnD,cAAc,wCAAwC,CAAC","file":"index.js","sourcesContent":["export * from '@telperion/js-utils/object/clone';\nexport * from '@telperion/js-utils/object/compact';\nexport * from '@telperion/js-utils/object/deepest';\nexport * from '@telperion/js-utils/object/deep-freeze';\n"]}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { clone } from '@telperion/js-utils/object/clone';
|
|
2
|
+
declare global {
|
|
3
|
+
export interface ObjectConstructor {
|
|
4
|
+
/**
|
|
5
|
+
* #### Deep Clone
|
|
6
|
+
*
|
|
7
|
+
* A function to recursively clone objects, arrays etc. with [cloning options]{@link ICloneOptions}
|
|
8
|
+
*
|
|
9
|
+
* *References Functions & Symbols by default*
|
|
10
|
+
*
|
|
11
|
+
* * * *
|
|
12
|
+
* Example:
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { clone } from "@telperion/js-utils";
|
|
15
|
+
*
|
|
16
|
+
* const object = {a: 1, b: {c: true, d: ["x", "y"]}};
|
|
17
|
+
*
|
|
18
|
+
* // Clone all
|
|
19
|
+
* const clonedObject = clone(object);
|
|
20
|
+
* // {a: 1, b: {c: true, d: ["x", "y"]}}
|
|
21
|
+
* // object.b.d === clonedObject.b.d // false
|
|
22
|
+
*
|
|
23
|
+
* // Clone all but reference "d"
|
|
24
|
+
* const clonedObject = clone(object, {propsToRefer: ["d"]});
|
|
25
|
+
* // {a: 1, b: {c: true, d: ["x", "y"]}}
|
|
26
|
+
* // object.b.d === clonedObject.b.d // true
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* Static usage example:
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import "@telperion/js-utils/dist/as-static/clone";
|
|
32
|
+
*
|
|
33
|
+
* const object = {a: 1, b: 2};
|
|
34
|
+
* const clonedObject = Object.clone(object); // {a: 1, b: 2}
|
|
35
|
+
* ```
|
|
36
|
+
* * * *
|
|
37
|
+
* @param objectToClone Object to clone
|
|
38
|
+
* @param options {ICloneOptions} Cloning Options
|
|
39
|
+
* @see {@link ICloneOptions} for more information.
|
|
40
|
+
*/
|
|
41
|
+
clone: typeof clone;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/object/static/clone.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kCAAkC,CAAC;AA6CzD,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC","file":"clone.js","sourcesContent":["import { clone } from '@telperion/js-utils/object/clone';\n\ndeclare global {\n export interface ObjectConstructor {\n /**\n * #### Deep Clone\n *\n * A function to recursively clone objects, arrays etc. with [cloning options]{@link ICloneOptions}\n *\n * *References Functions & Symbols by default*\n *\n * * * *\n * Example:\n * ```typescript\n * import { clone } from \"@telperion/js-utils\";\n *\n * const object = {a: 1, b: {c: true, d: [\"x\", \"y\"]}};\n *\n * // Clone all\n * const clonedObject = clone(object);\n * // {a: 1, b: {c: true, d: [\"x\", \"y\"]}}\n * // object.b.d === clonedObject.b.d // false\n *\n * // Clone all but reference \"d\"\n * const clonedObject = clone(object, {propsToRefer: [\"d\"]});\n * // {a: 1, b: {c: true, d: [\"x\", \"y\"]}}\n * // object.b.d === clonedObject.b.d // true\n * ```\n *\n * Static usage example:\n * ```typescript\n * import \"@telperion/js-utils/dist/as-static/clone\";\n *\n * const object = {a: 1, b: 2};\n * const clonedObject = Object.clone(object); // {a: 1, b: 2}\n * ```\n * * * *\n * @param objectToClone Object to clone\n * @param options {ICloneOptions} Cloning Options\n * @see {@link ICloneOptions} for more information.\n */\n clone: typeof clone;\n }\n}\n\nObject.clone = clone;\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { compact } from '@telperion/js-utils/object/compact';
|
|
2
|
+
declare global {
|
|
3
|
+
export interface ObjectConstructor {
|
|
4
|
+
/**
|
|
5
|
+
* #### Removes `null` and `undefined` values and their keys from an object
|
|
6
|
+
*
|
|
7
|
+
* * * * *
|
|
8
|
+
* Example usage:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import "@telperion/js-utils/object/static/compact";
|
|
11
|
+
*
|
|
12
|
+
* const a = {
|
|
13
|
+
* x: null,
|
|
14
|
+
* y: undefined,
|
|
15
|
+
* z: 20
|
|
16
|
+
* };
|
|
17
|
+
*
|
|
18
|
+
* Object.compact(a); // {z: 20}
|
|
19
|
+
* ```
|
|
20
|
+
* * * *
|
|
21
|
+
* @param object Object delete empty keys
|
|
22
|
+
* @returns Compacted object
|
|
23
|
+
*/
|
|
24
|
+
compact: typeof compact;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/object/static/compact.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oCAAoC,CAAC;AA4B7D,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC","file":"compact.js","sourcesContent":["import { compact } from '@telperion/js-utils/object/compact';\n\ndeclare global {\n export interface ObjectConstructor {\n /**\n * #### Removes `null` and `undefined` values and their keys from an object\n *\n * * * * *\n * Example usage:\n * ```typescript\n * import \"@telperion/js-utils/object/static/compact\";\n *\n * const a = {\n * x: null,\n * y: undefined,\n * z: 20\n * };\n *\n * Object.compact(a); // {z: 20}\n * ```\n * * * *\n * @param object Object delete empty keys\n * @returns Compacted object\n */\n compact: typeof compact;\n }\n}\n\nObject.compact = compact;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/object/static/deep-freeze.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AAQpE,MAAM,CAAC,UAAU,GAAG,UAAU,CAAC","file":"deep-freeze.js","sourcesContent":["import { deepFreeze } from '@telperion/js-utils/object/deep-freeze';\n\ndeclare global {\n export interface ObjectConstructor {\n deepFreeze: typeof deepFreeze;\n }\n}\n\nObject.deepFreeze = deepFreeze;\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { deepest } from '@telperion/js-utils/object/deepest';
|
|
2
|
+
declare global {
|
|
3
|
+
export interface ObjectConstructor {
|
|
4
|
+
/**
|
|
5
|
+
* #### Get deepest value in an object chain
|
|
6
|
+
*
|
|
7
|
+
* * * * *
|
|
8
|
+
* Example usage:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import "@telperion/js-utils/object/static/deepest";
|
|
11
|
+
*
|
|
12
|
+
* const a = {
|
|
13
|
+
* x: null
|
|
14
|
+
* };
|
|
15
|
+
*
|
|
16
|
+
* const b = {
|
|
17
|
+
* x: a
|
|
18
|
+
* };
|
|
19
|
+
*
|
|
20
|
+
* const c = {
|
|
21
|
+
* x: b
|
|
22
|
+
* };
|
|
23
|
+
*
|
|
24
|
+
* Object.deepest(c, 'x'); // a
|
|
25
|
+
* ```
|
|
26
|
+
* * * *
|
|
27
|
+
* @param object Object to deep dive
|
|
28
|
+
* @param key key of the object which contains same type instance
|
|
29
|
+
*/
|
|
30
|
+
deepest: typeof deepest;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/object/static/deepest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oCAAoC,CAAC;AAkC7D,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC","file":"deepest.js","sourcesContent":["import { deepest } from '@telperion/js-utils/object/deepest';\n\ndeclare global {\n export interface ObjectConstructor {\n /**\n * #### Get deepest value in an object chain\n *\n * * * * *\n * Example usage:\n * ```typescript\n * import \"@telperion/js-utils/object/static/deepest\";\n *\n * const a = {\n * x: null\n * };\n *\n * const b = {\n * x: a\n * };\n *\n * const c = {\n * x: b\n * };\n *\n * Object.deepest(c, 'x'); // a\n * ```\n * * * *\n * @param object Object to deep dive\n * @param key key of the object which contains same type instance\n */\n deepest: typeof deepest;\n }\n}\n\nObject.deepest = deepest;\n"]}
|
package/open-promise.cjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenPromise = void 0;
|
|
4
|
+
const noop_1 = require("@telperion/js-utils/function/noop");
|
|
5
|
+
/**
|
|
6
|
+
* #### Open Promise
|
|
7
|
+
* A promise constructor to resolve or reject from outside
|
|
8
|
+
*
|
|
9
|
+
* @deprecated Use the native `Promise.withResolvers()` instead, which provides the same functionality.
|
|
10
|
+
*
|
|
11
|
+
* * * *
|
|
12
|
+
* Example with native alternative:
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Instead of:
|
|
15
|
+
* // import { OpenPromise } from "@telperion/js-utils";
|
|
16
|
+
* // const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
17
|
+
*
|
|
18
|
+
* // Use:
|
|
19
|
+
* const { promise, resolve, reject } = Promise.withResolvers();
|
|
20
|
+
*
|
|
21
|
+
* promise.then(val => console.log(val));
|
|
22
|
+
* ...
|
|
23
|
+
* ...
|
|
24
|
+
*
|
|
25
|
+
* resolve({x: 1});
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* Legacy Example:
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { OpenPromise } from "@telperion/js-utils";
|
|
31
|
+
*
|
|
32
|
+
* const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
33
|
+
*
|
|
34
|
+
* aPromiseWillBeResolvedLater.then(val => console.log(val));
|
|
35
|
+
* // aPromiseWillBeResolvedLater.finished // false
|
|
36
|
+
* ...
|
|
37
|
+
* ...
|
|
38
|
+
*
|
|
39
|
+
* aPromiseWillBeResolvedLater.resolve({x: 1});
|
|
40
|
+
* // aPromiseWillBeResolvedLater.finished // true
|
|
41
|
+
* ```
|
|
42
|
+
* * * *
|
|
43
|
+
* @template T typeof the value which is going to be resolved
|
|
44
|
+
*/
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
|
+
class OpenPromise extends Promise {
|
|
47
|
+
static #resolverHolder = noop_1.noop;
|
|
48
|
+
static #rejectorHolder = noop_1.noop;
|
|
49
|
+
#resolved = false;
|
|
50
|
+
#rejected = false;
|
|
51
|
+
#resolver = noop_1.noop;
|
|
52
|
+
#rejector = noop_1.noop;
|
|
53
|
+
/**
|
|
54
|
+
* Returns whether is the promise resolved
|
|
55
|
+
*/
|
|
56
|
+
get resolved() {
|
|
57
|
+
return this.#resolved;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Returns whether is the promise rejected
|
|
61
|
+
*/
|
|
62
|
+
get rejected() {
|
|
63
|
+
return this.#rejected;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Returns whether is the promise finished
|
|
67
|
+
*/
|
|
68
|
+
get finished() {
|
|
69
|
+
return this.#resolved || this.#rejected;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Open Promise Constructor
|
|
73
|
+
*/
|
|
74
|
+
constructor(executor = noop_1.noop) {
|
|
75
|
+
super((resolve, reject) => {
|
|
76
|
+
OpenPromise.#resolverHolder = resolve;
|
|
77
|
+
OpenPromise.#rejectorHolder = reject;
|
|
78
|
+
executor(resolve, reject);
|
|
79
|
+
});
|
|
80
|
+
this.#resolver = OpenPromise.#resolverHolder;
|
|
81
|
+
this.#rejector = OpenPromise.#rejectorHolder;
|
|
82
|
+
OpenPromise.#resolverHolder = noop_1.noop;
|
|
83
|
+
OpenPromise.#rejectorHolder = noop_1.noop;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Resolves promise
|
|
87
|
+
* @param value Value to resolve the promise
|
|
88
|
+
*/
|
|
89
|
+
resolve(...args) {
|
|
90
|
+
if (this.finished)
|
|
91
|
+
throw new Error('Promise is already finished');
|
|
92
|
+
this.#resolved = true;
|
|
93
|
+
this.#resolver(...args);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Rejects promise
|
|
97
|
+
* @param reason Error to reject promise
|
|
98
|
+
*/
|
|
99
|
+
reject(...args) {
|
|
100
|
+
if (this.finished)
|
|
101
|
+
throw new Error('Promise is already finished');
|
|
102
|
+
this.#rejected = true;
|
|
103
|
+
this.#rejector(...args);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Binds a promise to the inner promise to resolve or reject with it
|
|
107
|
+
* @param promise A promise to bind inner promise
|
|
108
|
+
*/
|
|
109
|
+
bindTo(promise) {
|
|
110
|
+
promise
|
|
111
|
+
.then(e => { this.resolve(e); })
|
|
112
|
+
.catch(e => this.reject(e));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.OpenPromise = OpenPromise;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ConstructorType } from '@thalesrc/extra-ts-types';
|
|
2
|
+
type Executor<T> = ConstructorParameters<ConstructorType<typeof Promise<T>>>[0];
|
|
3
|
+
type ExecutorParams<T> = Parameters<Executor<T>>;
|
|
4
|
+
type Resolver<T> = ExecutorParams<T>[0];
|
|
5
|
+
type Rejector = ExecutorParams<unknown>[1];
|
|
6
|
+
/**
|
|
7
|
+
* #### Open Promise
|
|
8
|
+
* A promise constructor to resolve or reject from outside
|
|
9
|
+
*
|
|
10
|
+
* @deprecated Use the native `Promise.withResolvers()` instead, which provides the same functionality.
|
|
11
|
+
*
|
|
12
|
+
* * * *
|
|
13
|
+
* Example with native alternative:
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Instead of:
|
|
16
|
+
* // import { OpenPromise } from "@telperion/js-utils";
|
|
17
|
+
* // const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
18
|
+
*
|
|
19
|
+
* // Use:
|
|
20
|
+
* const { promise, resolve, reject } = Promise.withResolvers();
|
|
21
|
+
*
|
|
22
|
+
* promise.then(val => console.log(val));
|
|
23
|
+
* ...
|
|
24
|
+
* ...
|
|
25
|
+
*
|
|
26
|
+
* resolve({x: 1});
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* Legacy Example:
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { OpenPromise } from "@telperion/js-utils";
|
|
32
|
+
*
|
|
33
|
+
* const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
34
|
+
*
|
|
35
|
+
* aPromiseWillBeResolvedLater.then(val => console.log(val));
|
|
36
|
+
* // aPromiseWillBeResolvedLater.finished // false
|
|
37
|
+
* ...
|
|
38
|
+
* ...
|
|
39
|
+
*
|
|
40
|
+
* aPromiseWillBeResolvedLater.resolve({x: 1});
|
|
41
|
+
* // aPromiseWillBeResolvedLater.finished // true
|
|
42
|
+
* ```
|
|
43
|
+
* * * *
|
|
44
|
+
* @template T typeof the value which is going to be resolved
|
|
45
|
+
*/
|
|
46
|
+
export declare class OpenPromise<T = any> extends Promise<T> {
|
|
47
|
+
#private;
|
|
48
|
+
/**
|
|
49
|
+
* Returns whether is the promise resolved
|
|
50
|
+
*/
|
|
51
|
+
get resolved(): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Returns whether is the promise rejected
|
|
54
|
+
*/
|
|
55
|
+
get rejected(): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Returns whether is the promise finished
|
|
58
|
+
*/
|
|
59
|
+
get finished(): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Open Promise Constructor
|
|
62
|
+
*/
|
|
63
|
+
constructor(executor?: Executor<T>);
|
|
64
|
+
/**
|
|
65
|
+
* Resolves promise
|
|
66
|
+
* @param value Value to resolve the promise
|
|
67
|
+
*/
|
|
68
|
+
resolve(...args: Parameters<Resolver<T>>): void;
|
|
69
|
+
/**
|
|
70
|
+
* Rejects promise
|
|
71
|
+
* @param reason Error to reject promise
|
|
72
|
+
*/
|
|
73
|
+
reject(...args: Parameters<Rejector>): void;
|
|
74
|
+
/**
|
|
75
|
+
* Binds a promise to the inner promise to resolve or reject with it
|
|
76
|
+
* @param promise A promise to bind inner promise
|
|
77
|
+
*/
|
|
78
|
+
bindTo(promise: Promise<T>): void;
|
|
79
|
+
}
|
|
80
|
+
export {};
|
package/open-promise.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { noop } from '@telperion/js-utils/function/noop';
|
|
2
|
+
/**
|
|
3
|
+
* #### Open Promise
|
|
4
|
+
* A promise constructor to resolve or reject from outside
|
|
5
|
+
*
|
|
6
|
+
* @deprecated Use the native `Promise.withResolvers()` instead, which provides the same functionality.
|
|
7
|
+
*
|
|
8
|
+
* * * *
|
|
9
|
+
* Example with native alternative:
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Instead of:
|
|
12
|
+
* // import { OpenPromise } from "@telperion/js-utils";
|
|
13
|
+
* // const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
14
|
+
*
|
|
15
|
+
* // Use:
|
|
16
|
+
* const { promise, resolve, reject } = Promise.withResolvers();
|
|
17
|
+
*
|
|
18
|
+
* promise.then(val => console.log(val));
|
|
19
|
+
* ...
|
|
20
|
+
* ...
|
|
21
|
+
*
|
|
22
|
+
* resolve({x: 1});
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Legacy Example:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { OpenPromise } from "@telperion/js-utils";
|
|
28
|
+
*
|
|
29
|
+
* const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
30
|
+
*
|
|
31
|
+
* aPromiseWillBeResolvedLater.then(val => console.log(val));
|
|
32
|
+
* // aPromiseWillBeResolvedLater.finished // false
|
|
33
|
+
* ...
|
|
34
|
+
* ...
|
|
35
|
+
*
|
|
36
|
+
* aPromiseWillBeResolvedLater.resolve({x: 1});
|
|
37
|
+
* // aPromiseWillBeResolvedLater.finished // true
|
|
38
|
+
* ```
|
|
39
|
+
* * * *
|
|
40
|
+
* @template T typeof the value which is going to be resolved
|
|
41
|
+
*/
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
export class OpenPromise extends Promise {
|
|
44
|
+
static #resolverHolder = noop;
|
|
45
|
+
static #rejectorHolder = noop;
|
|
46
|
+
#resolved = false;
|
|
47
|
+
#rejected = false;
|
|
48
|
+
#resolver = noop;
|
|
49
|
+
#rejector = noop;
|
|
50
|
+
/**
|
|
51
|
+
* Returns whether is the promise resolved
|
|
52
|
+
*/
|
|
53
|
+
get resolved() {
|
|
54
|
+
return this.#resolved;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns whether is the promise rejected
|
|
58
|
+
*/
|
|
59
|
+
get rejected() {
|
|
60
|
+
return this.#rejected;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns whether is the promise finished
|
|
64
|
+
*/
|
|
65
|
+
get finished() {
|
|
66
|
+
return this.#resolved || this.#rejected;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Open Promise Constructor
|
|
70
|
+
*/
|
|
71
|
+
constructor(executor = noop) {
|
|
72
|
+
super((resolve, reject) => {
|
|
73
|
+
OpenPromise.#resolverHolder = resolve;
|
|
74
|
+
OpenPromise.#rejectorHolder = reject;
|
|
75
|
+
executor(resolve, reject);
|
|
76
|
+
});
|
|
77
|
+
this.#resolver = OpenPromise.#resolverHolder;
|
|
78
|
+
this.#rejector = OpenPromise.#rejectorHolder;
|
|
79
|
+
OpenPromise.#resolverHolder = noop;
|
|
80
|
+
OpenPromise.#rejectorHolder = noop;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Resolves promise
|
|
84
|
+
* @param value Value to resolve the promise
|
|
85
|
+
*/
|
|
86
|
+
resolve(...args) {
|
|
87
|
+
if (this.finished)
|
|
88
|
+
throw new Error('Promise is already finished');
|
|
89
|
+
this.#resolved = true;
|
|
90
|
+
this.#resolver(...args);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Rejects promise
|
|
94
|
+
* @param reason Error to reject promise
|
|
95
|
+
*/
|
|
96
|
+
reject(...args) {
|
|
97
|
+
if (this.finished)
|
|
98
|
+
throw new Error('Promise is already finished');
|
|
99
|
+
this.#rejected = true;
|
|
100
|
+
this.#rejector(...args);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Binds a promise to the inner promise to resolve or reject with it
|
|
104
|
+
* @param promise A promise to bind inner promise
|
|
105
|
+
*/
|
|
106
|
+
bindTo(promise) {
|
|
107
|
+
promise
|
|
108
|
+
.then(e => { this.resolve(e); })
|
|
109
|
+
.catch(e => this.reject(e));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
//# sourceMappingURL=open-promise.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/open-promise.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AAOzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,8DAA8D;AAC9D,MAAM,OAAO,WAAqB,SAAQ,OAAU;IAClD,MAAM,CAAC,eAAe,GAAkB,IAAI,CAAC;IAC7C,MAAM,CAAC,eAAe,GAAa,IAAI,CAAC;IAExC,SAAS,GAAG,KAAK,CAAC;IAClB,SAAS,GAAG,KAAK,CAAC;IAElB,SAAS,GAAgB,IAAI,CAAC;IAC9B,SAAS,GAAa,IAAI,CAAC;IAG3B;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC;IAC1C,CAAC;IAED;;OAEG;IACH,YAAY,WAAwB,IAAI;QACtC,KAAK,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACxB,WAAW,CAAC,eAAe,GAAG,OAAO,CAAC;YACtC,WAAW,CAAC,eAAe,GAAG,MAAM,CAAC;YAErC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,SAAS,GAAG,WAAW,CAAC,eAAe,CAAC;QAC7C,IAAI,CAAC,SAAS,GAAG,WAAW,CAAC,eAAe,CAAC;QAE7C,WAAW,CAAC,eAAe,GAAG,IAAI,CAAC;QACnC,WAAW,CAAC,eAAe,GAAG,IAAI,CAAC;IACrC,CAAC;IAED;;;OAGG;IACH,OAAO,CAAC,GAAG,IAA6B;QACtC,IAAI,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QAElE,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,GAAG,IAA0B;QAClC,IAAI,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QAElE,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,OAAmB;QACxB,OAAO;aACJ,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;aAC/B,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAChC,CAAC","file":"open-promise.js","sourcesContent":["import type { ConstructorType } from '@thalesrc/extra-ts-types';\nimport { noop } from '@telperion/js-utils/function/noop';\n\ntype Executor<T> = ConstructorParameters<ConstructorType<typeof Promise<T>>>[0];\ntype ExecutorParams<T> = Parameters<Executor<T>>;\ntype Resolver<T> = ExecutorParams<T>[0];\ntype Rejector = ExecutorParams<unknown>[1];\n\n/**\n * #### Open Promise\n * A promise constructor to resolve or reject from outside\n *\n * @deprecated Use the native `Promise.withResolvers()` instead, which provides the same functionality.\n *\n * * * *\n * Example with native alternative:\n * ```typescript\n * // Instead of:\n * // import { OpenPromise } from \"@telperion/js-utils\";\n * // const aPromiseWillBeResolvedLater = new OpenPromise();\n *\n * // Use:\n * const { promise, resolve, reject } = Promise.withResolvers();\n *\n * promise.then(val => console.log(val));\n * ...\n * ...\n *\n * resolve({x: 1});\n * ```\n *\n * Legacy Example:\n * ```typescript\n * import { OpenPromise } from \"@telperion/js-utils\";\n *\n * const aPromiseWillBeResolvedLater = new OpenPromise();\n *\n * aPromiseWillBeResolvedLater.then(val => console.log(val));\n * // aPromiseWillBeResolvedLater.finished // false\n * ...\n * ...\n *\n * aPromiseWillBeResolvedLater.resolve({x: 1});\n * // aPromiseWillBeResolvedLater.finished // true\n * ```\n * * * *\n * @template T typeof the value which is going to be resolved\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport class OpenPromise<T = any> extends Promise<T> {\n static #resolverHolder: Resolver<any> = noop;\n static #rejectorHolder: Rejector = noop;\n\n #resolved = false;\n #rejected = false;\n\n #resolver: Resolver<T> = noop;\n #rejector: Rejector = noop;\n\n\n /**\n * Returns whether is the promise resolved\n */\n get resolved() {\n return this.#resolved;\n }\n\n /**\n * Returns whether is the promise rejected\n */\n get rejected() {\n return this.#rejected;\n }\n\n /**\n * Returns whether is the promise finished\n */\n get finished() {\n return this.#resolved || this.#rejected;\n }\n\n /**\n * Open Promise Constructor\n */\n constructor(executor: Executor<T> = noop) {\n super((resolve, reject) => {\n OpenPromise.#resolverHolder = resolve;\n OpenPromise.#rejectorHolder = reject;\n\n executor(resolve, reject);\n });\n\n this.#resolver = OpenPromise.#resolverHolder;\n this.#rejector = OpenPromise.#rejectorHolder;\n\n OpenPromise.#resolverHolder = noop;\n OpenPromise.#rejectorHolder = noop;\n }\n\n /**\n * Resolves promise\n * @param value Value to resolve the promise\n */\n resolve(...args: Parameters<Resolver<T>>): void {\n if (this.finished) throw new Error('Promise is already finished');\n\n this.#resolved = true;\n this.#resolver(...args);\n }\n\n /**\n * Rejects promise\n * @param reason Error to reject promise\n */\n reject(...args: Parameters<Rejector>): void {\n if (this.finished) throw new Error('Promise is already finished');\n\n this.#rejected = true;\n this.#rejector(...args);\n }\n\n /**\n * Binds a promise to the inner promise to resolve or reject with it\n * @param promise A promise to bind inner promise\n */\n bindTo(promise: Promise<T>): void {\n promise\n .then(e => { this.resolve(e); })\n .catch(e => this.reject(e));\n }\n}\n"]}
|