@rzl-zone/utils-js 3.13.1 → 3.14.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/README.md +7 -6
- package/dist/.references/index.d.cts +2 -1
- package/dist/.references/index.d.ts +2 -1
- package/dist/AbortError-CgE6QNOR.js +20 -0
- package/dist/AbortError-CgE6QNOR.js.map +1 -0
- package/dist/AbortError-D54txsmn.cjs +26 -0
- package/dist/AbortError-D54txsmn.cjs.map +1 -0
- package/dist/{assertIsArray-HgzPQIAM.js → assertIsArray-DmlqnTiq.js} +3 -3
- package/dist/assertIsArray-DmlqnTiq.js.map +1 -0
- package/dist/{assertIsArray-CeXL4xv5.cjs → assertIsArray-Xx-zlo6O.cjs} +5 -5
- package/dist/assertIsArray-Xx-zlo6O.cjs.map +1 -0
- package/dist/assertIsPlainObject-CIzDchLs.js +20 -0
- package/dist/assertIsPlainObject-CIzDchLs.js.map +1 -0
- package/dist/assertIsPlainObject-DYoxHcW5.cjs +26 -0
- package/dist/assertIsPlainObject-DYoxHcW5.cjs.map +1 -0
- package/dist/{assertIsString-DUrVt82d.cjs → assertIsString-CuPbh3FB.cjs} +6 -6
- package/dist/assertIsString-CuPbh3FB.cjs.map +1 -0
- package/dist/{assertIsString-CY_WkCLr.js → assertIsString-sgY3ml5f.js} +4 -4
- package/dist/assertIsString-sgY3ml5f.js.map +1 -0
- package/dist/assertions/index.cjs +14 -12
- package/dist/assertions/index.cjs.map +1 -1
- package/dist/assertions/index.d.cts +2 -2
- package/dist/assertions/index.d.ts +2 -2
- package/dist/assertions/index.js +10 -8
- package/dist/assertions/index.js.map +1 -1
- package/dist/conversions/index.cjs +6 -6
- package/dist/conversions/index.d.cts +2 -2
- package/dist/conversions/index.d.ts +2 -2
- package/dist/conversions/index.js +6 -6
- package/dist/{conversions-1s7xMBe_.js → conversions-BkxqYXxy.js} +43 -40
- package/dist/conversions-BkxqYXxy.js.map +1 -0
- package/dist/conversions-UdMB90xR.cjs +449 -0
- package/dist/conversions-UdMB90xR.cjs.map +1 -0
- package/dist/errors/index.cjs +13 -0
- package/dist/errors/index.d.cts +180 -0
- package/dist/errors/index.d.ts +180 -0
- package/dist/errors/index.js +11 -0
- package/dist/events/index.cjs +10 -9
- package/dist/events/index.cjs.map +1 -1
- package/dist/events/index.d.cts +185 -100
- package/dist/events/index.d.ts +185 -100
- package/dist/events/index.js +9 -8
- package/dist/events/index.js.map +1 -1
- package/dist/formatter-BFgV1HhV-BtIJs2H2.js +55 -0
- package/dist/formatter-BFgV1HhV-BtIJs2H2.js.map +1 -0
- package/dist/formatter-BFgV1HhV-DyybWSuj.cjs +67 -0
- package/dist/formatter-BFgV1HhV-DyybWSuj.cjs.map +1 -0
- package/dist/formatters/index.cjs +2 -2
- package/dist/formatters/index.d.cts +2 -2
- package/dist/formatters/index.d.ts +2 -2
- package/dist/formatters/index.js +2 -2
- package/dist/formatters-Cjng28nY.cjs +436 -0
- package/dist/formatters-Cjng28nY.cjs.map +1 -0
- package/dist/{formatters-C6cVZBbG.js → formatters-Dqccnt-p.js} +78 -71
- package/dist/formatters-Dqccnt-p.js.map +1 -0
- package/dist/generators/index.cjs +37 -31
- package/dist/generators/index.cjs.map +1 -1
- package/dist/generators/index.d.cts +2 -2
- package/dist/generators/index.d.ts +2 -2
- package/dist/generators/index.js +29 -23
- package/dist/generators/index.js.map +1 -1
- package/dist/hasOwnProp-DTWYqy7S.cjs +53 -0
- package/dist/hasOwnProp-DTWYqy7S.cjs.map +1 -0
- package/dist/hasOwnProp-yVVsxPin.js +47 -0
- package/dist/hasOwnProp-yVVsxPin.js.map +1 -0
- package/dist/{index-BG8wdusZ.d.cts → index-9Ql1W3gt.d.cts} +229 -200
- package/dist/{index-RNQBTK8A.d.cts → index-BD0J8wkE.d.cts} +1374 -936
- package/dist/index-BcoUy2oV.d.ts +2424 -0
- package/dist/{index-B5EBAgmC.d.cts → index-Bpk3YwGW.d.ts} +1592 -790
- package/dist/index-CTMG8cZV.d.ts +1041 -0
- package/dist/index-DUBCXPy4.d.cts +1041 -0
- package/dist/{index-CRz9rSZL.d.ts → index-DhUTzQ_o.d.ts} +1374 -936
- package/dist/{index-B1VQ6USF.d.ts → index-DqYMKSmd.d.ts} +229 -200
- package/dist/index-JfwSupYB.d.ts +1103 -0
- package/dist/{index-ORbo-7zA.d.cts → index-O2q2f-pu.d.cts} +242 -133
- package/dist/{index-DPJ-e2JZ.d.ts → index-VgNZ6ndx.d.cts} +1592 -790
- package/dist/{index-ORbo-7zA.d.ts → index-k_rjVRjU.d.ts} +242 -133
- package/dist/index-or0oP9i2.d.cts +2179 -0
- package/dist/index-or0oP9i2.d.ts +2179 -0
- package/dist/index-u4-Euidh.d.cts +1103 -0
- package/dist/index-zAqCZxGr.d.cts +2424 -0
- package/dist/{isBigInt-y7s3rsKc.js → isBigInt-DdFRCpeE.js} +2 -2
- package/dist/isBigInt-DdFRCpeE.js.map +1 -0
- package/dist/{isBigInt-BVj0M5pq.cjs → isBigInt-LkCpVJ3E.cjs} +2 -2
- package/dist/isBigInt-LkCpVJ3E.cjs.map +1 -0
- package/dist/{isEmptyString-BYMz_wt9.js → isEmptyString-B6ubItgT.js} +4 -4
- package/dist/isEmptyString-B6ubItgT.js.map +1 -0
- package/dist/{isEmptyString-C8PYNPgw.cjs → isEmptyString-Csc5Sf_W.cjs} +5 -5
- package/dist/isEmptyString-Csc5Sf_W.cjs.map +1 -0
- package/dist/isEmptyValue-h97JT4pD.js +36 -0
- package/dist/isEmptyValue-h97JT4pD.js.map +1 -0
- package/dist/isEmptyValue-qNOhUBFE.cjs +54 -0
- package/dist/isEmptyValue-qNOhUBFE.cjs.map +1 -0
- package/dist/{isFinite-DeDaBDr2.cjs → isFinite-CKeuVXbD.cjs} +6 -6
- package/dist/isFinite-CKeuVXbD.cjs.map +1 -0
- package/dist/{isFinite-BEJFOv6B.js → isFinite-CzYYv-mQ.js} +5 -5
- package/dist/isFinite-CzYYv-mQ.js.map +1 -0
- package/dist/{isInteger-DEDbBGez.js → isInteger-BtA3flbk.js} +4 -4
- package/dist/isInteger-BtA3flbk.js.map +1 -0
- package/dist/{isInteger-CQLThJOR.cjs → isInteger-iTok9NaH.cjs} +4 -4
- package/dist/isInteger-iTok9NaH.cjs.map +1 -0
- package/dist/isNumber-C1mOuBuA.js +20 -0
- package/dist/isNumber-C1mOuBuA.js.map +1 -0
- package/dist/isNumber-CM1vdNcW.cjs +26 -0
- package/dist/isNumber-CM1vdNcW.cjs.map +1 -0
- package/dist/isPlainObject-DQoM4DGQ.d.ts +331 -0
- package/dist/isPlainObject-Y__iykWN.d.cts +331 -0
- package/dist/{isServer-DmycqWsF.cjs → isServer-CWlp2R2N.cjs} +2 -2
- package/dist/isServer-CWlp2R2N.cjs.map +1 -0
- package/dist/{isServer-q3o6cSwD.js → isServer-Drgyboev.js} +2 -2
- package/dist/isServer-Drgyboev.js.map +1 -0
- package/dist/{isEqual-DM4S_kWh.js → isTypedArray-BY8CIj3k.js} +29 -9
- package/dist/isTypedArray-BY8CIj3k.js.map +1 -0
- package/dist/{isEqual-DaPDxMyw.cjs → isTypedArray-DspIjxdk.cjs} +45 -13
- package/dist/isTypedArray-DspIjxdk.cjs.map +1 -0
- package/dist/{isValidDomain-B8Nukgw0.js → isValidDomain-BPfJEkFk.js} +13 -54
- package/dist/isValidDomain-BPfJEkFk.js.map +1 -0
- package/dist/{isValidDomain-ZbyeGOO9.cjs → isValidDomain-DUUCoKtD.cjs} +16 -69
- package/dist/isValidDomain-DUUCoKtD.cjs.map +1 -0
- package/dist/{assertIsBoolean-D41HrTB_.cjs → logger-BXg2xufj.cjs} +658 -612
- package/dist/logger-BXg2xufj.cjs.map +1 -0
- package/dist/{assertIsBoolean-CWUs-I5l.js → logger-Wxl_Krg6.js} +657 -587
- package/dist/logger-Wxl_Krg6.js.map +1 -0
- package/dist/{noop-BY43WaoU.cjs → noop-ByxeOveJ.cjs} +2 -2
- package/dist/noop-ByxeOveJ.cjs.map +1 -0
- package/dist/{noop-CjY8xVHv.js → noop-D8aGbjbP.js} +2 -2
- package/dist/noop-D8aGbjbP.js.map +1 -0
- package/dist/{normalizeSpaces-BhZp0U_6.cjs → normalizeSpaces-CQ7tZLNG.cjs} +6 -9
- package/dist/normalizeSpaces-CQ7tZLNG.cjs.map +1 -0
- package/dist/{normalizeSpaces-BoUDtq5P.js → normalizeSpaces-DNUOxp_8.js} +4 -7
- package/dist/normalizeSpaces-DNUOxp_8.js.map +1 -0
- package/dist/operations/index.cjs +152 -22
- package/dist/operations/index.cjs.map +1 -1
- package/dist/operations/index.d.cts +257 -35
- package/dist/operations/index.d.ts +257 -35
- package/dist/operations/index.js +142 -13
- package/dist/operations/index.js.map +1 -1
- package/dist/parsers/index.cjs +2 -2
- package/dist/parsers/index.d.cts +238 -180
- package/dist/parsers/index.d.ts +238 -180
- package/dist/parsers/index.js +2 -2
- package/dist/{parsers-B_FPX35n.cjs → parsers-CJ5tHtqF.cjs} +10 -9
- package/dist/parsers-CJ5tHtqF.cjs.map +1 -0
- package/dist/{parsers-B8KYXIjE.js → parsers-DMlqliPw.js} +9 -8
- package/dist/parsers-DMlqliPw.js.map +1 -0
- package/dist/{parsing-BCZGco7n.cjs → parsing-DB_S0WFz.cjs} +4 -4
- package/dist/parsing-DB_S0WFz.cjs.map +1 -0
- package/dist/{parsing-Bfaw85Hd.js → parsing-DhIQhWpX.js} +3 -3
- package/dist/parsing-DhIQhWpX.js.map +1 -0
- package/dist/predicates/index.cjs +43 -43
- package/dist/predicates/index.d.cts +3 -3
- package/dist/predicates/index.d.ts +3 -3
- package/dist/predicates/index.js +14 -14
- package/dist/{predicates-KlyOvBSC.js → predicates-BzYMbNrZ.js} +45 -40
- package/dist/predicates-BzYMbNrZ.js.map +1 -0
- package/dist/{predicates-8y28VrbT.cjs → predicates-DDW3etqF.cjs} +89 -84
- package/dist/predicates-DDW3etqF.cjs.map +1 -0
- package/dist/promises/index.cjs +27 -26
- package/dist/promises/index.cjs.map +1 -1
- package/dist/promises/index.d.cts +227 -75
- package/dist/promises/index.d.ts +227 -75
- package/dist/promises/index.js +27 -26
- package/dist/promises/index.js.map +1 -1
- package/dist/{punyCode-DnKi0qjS.cjs → punyCode-B_VPjG0d.cjs} +12 -12
- package/dist/punyCode-B_VPjG0d.cjs.map +1 -0
- package/dist/{punyCode-Dwto1RpA.js → punyCode-DsuJbWCl.js} +12 -12
- package/dist/punyCode-DsuJbWCl.js.map +1 -0
- package/dist/{removeSpaces--8NW-HSx.js → removeSpaces-BF-T_1Yt.js} +4 -4
- package/dist/removeSpaces-BF-T_1Yt.js.map +1 -0
- package/dist/{removeSpaces-CmrrkeXz.cjs → removeSpaces-BJu-gKps.cjs} +7 -7
- package/dist/removeSpaces-BJu-gKps.cjs.map +1 -0
- package/dist/rzl-utils.global.js +13 -13
- package/dist/safeJsonParse-B5F8XtrZ.cjs +209 -0
- package/dist/safeJsonParse-B5F8XtrZ.cjs.map +1 -0
- package/dist/{safeJsonParse-OnUwG2El.js → safeJsonParse-DYSkUnhM.js} +29 -28
- package/dist/safeJsonParse-DYSkUnhM.js.map +1 -0
- package/dist/{safeStableStringify-B2KTTS3r.js → safeStableStringify-CDSYnLUz.js} +20 -13
- package/dist/safeStableStringify-CDSYnLUz.js.map +1 -0
- package/dist/safeStableStringify-MuBiT-4B.cjs +125 -0
- package/dist/safeStableStringify-MuBiT-4B.cjs.map +1 -0
- package/dist/strings/index.cjs +26 -30
- package/dist/strings/index.cjs.map +1 -1
- package/dist/strings/index.d.cts +2 -2
- package/dist/strings/index.d.ts +2 -2
- package/dist/strings/index.js +13 -17
- package/dist/strings/index.js.map +1 -1
- package/dist/tailwind/index.cjs +3 -3
- package/dist/tailwind/index.d.cts +3 -3
- package/dist/tailwind/index.d.ts +3 -3
- package/dist/tailwind/index.js +3 -3
- package/dist/{tailwind-CUdmv-oO.js → tailwind-BhHULklj.js} +38 -32
- package/dist/tailwind-BhHULklj.js.map +1 -0
- package/dist/{tailwind-DUuboq52.cjs → tailwind-hx6TIFIL.cjs} +59 -53
- package/dist/tailwind-hx6TIFIL.cjs.map +1 -0
- package/dist/{toStringArrayUnRecursive-OOHGazks.js → toStringArrayUnRecursive-U-nkEIDs.js} +9 -9
- package/dist/toStringArrayUnRecursive-U-nkEIDs.js.map +1 -0
- package/dist/toStringArrayUnRecursive-_lJEmBlf.cjs +51 -0
- package/dist/toStringArrayUnRecursive-_lJEmBlf.cjs.map +1 -0
- package/dist/urls/index.cjs +3 -3
- package/dist/urls/index.d.cts +635 -443
- package/dist/urls/index.d.ts +635 -443
- package/dist/urls/index.js +3 -3
- package/dist/{urls-BXT4Krsq.js → urls-BcGIFgfR.js} +50 -41
- package/dist/urls-BcGIFgfR.js.map +1 -0
- package/dist/urls-DGvZsMSN.cjs +308 -0
- package/dist/urls-DGvZsMSN.cjs.map +1 -0
- package/package.json +6 -6
- package/dist/assertIsArray-CeXL4xv5.cjs.map +0 -1
- package/dist/assertIsArray-HgzPQIAM.js.map +0 -1
- package/dist/assertIsBoolean-CWUs-I5l.js.map +0 -1
- package/dist/assertIsBoolean-D41HrTB_.cjs.map +0 -1
- package/dist/assertIsString-CY_WkCLr.js.map +0 -1
- package/dist/assertIsString-DUrVt82d.cjs.map +0 -1
- package/dist/conversions-1s7xMBe_.js.map +0 -1
- package/dist/conversions-B3Rf8uId.cjs +0 -446
- package/dist/conversions-B3Rf8uId.cjs.map +0 -1
- package/dist/formatters-C6cVZBbG.js.map +0 -1
- package/dist/formatters-aEAAuzjx.cjs +0 -429
- package/dist/formatters-aEAAuzjx.cjs.map +0 -1
- package/dist/index-CPCt6_Wq.d.cts +0 -765
- package/dist/index-CPCt6_Wq.d.ts +0 -765
- package/dist/index-DBwNa1iO.d.ts +0 -947
- package/dist/index-DPTYOQkn.d.cts +0 -947
- package/dist/index-Dig1fIhZ.d.cts +0 -1716
- package/dist/index-Dig1fIhZ.d.ts +0 -1716
- package/dist/isBigInt-BVj0M5pq.cjs.map +0 -1
- package/dist/isBigInt-y7s3rsKc.js.map +0 -1
- package/dist/isEmptyObject-BsHngDz4.cjs +0 -37
- package/dist/isEmptyObject-BsHngDz4.cjs.map +0 -1
- package/dist/isEmptyObject-DPGR9iPj.js +0 -25
- package/dist/isEmptyObject-DPGR9iPj.js.map +0 -1
- package/dist/isEmptyString-BYMz_wt9.js.map +0 -1
- package/dist/isEmptyString-C8PYNPgw.cjs.map +0 -1
- package/dist/isEmptyValue-BXf7f1jz.cjs +0 -30
- package/dist/isEmptyValue-BXf7f1jz.cjs.map +0 -1
- package/dist/isEmptyValue-D4OHTB3u.js +0 -24
- package/dist/isEmptyValue-D4OHTB3u.js.map +0 -1
- package/dist/isEqual-DM4S_kWh.js.map +0 -1
- package/dist/isEqual-DaPDxMyw.cjs.map +0 -1
- package/dist/isFinite-BEJFOv6B.js.map +0 -1
- package/dist/isFinite-DeDaBDr2.cjs.map +0 -1
- package/dist/isInteger-CQLThJOR.cjs.map +0 -1
- package/dist/isInteger-DEDbBGez.js.map +0 -1
- package/dist/isPlainObject-BwTkj3G0.d.cts +0 -339
- package/dist/isPlainObject-BwTkj3G0.d.ts +0 -339
- package/dist/isServer-DmycqWsF.cjs.map +0 -1
- package/dist/isServer-q3o6cSwD.js.map +0 -1
- package/dist/isTypedArray-BQLbLXsU.cjs +0 -43
- package/dist/isTypedArray-BQLbLXsU.cjs.map +0 -1
- package/dist/isTypedArray-XiBnifLN.js +0 -31
- package/dist/isTypedArray-XiBnifLN.js.map +0 -1
- package/dist/isValidDomain-B8Nukgw0.js.map +0 -1
- package/dist/isValidDomain-ZbyeGOO9.cjs.map +0 -1
- package/dist/noop-BY43WaoU.cjs.map +0 -1
- package/dist/noop-CjY8xVHv.js.map +0 -1
- package/dist/normalizeSpaces-BhZp0U_6.cjs.map +0 -1
- package/dist/normalizeSpaces-BoUDtq5P.js.map +0 -1
- package/dist/parsers-B8KYXIjE.js.map +0 -1
- package/dist/parsers-B_FPX35n.cjs.map +0 -1
- package/dist/parsing-BCZGco7n.cjs.map +0 -1
- package/dist/parsing-Bfaw85Hd.js.map +0 -1
- package/dist/predicates-8y28VrbT.cjs.map +0 -1
- package/dist/predicates-KlyOvBSC.js.map +0 -1
- package/dist/punyCode-DnKi0qjS.cjs.map +0 -1
- package/dist/punyCode-Dwto1RpA.js.map +0 -1
- package/dist/removeSpaces--8NW-HSx.js.map +0 -1
- package/dist/removeSpaces-CmrrkeXz.cjs.map +0 -1
- package/dist/safeJsonParse-OnUwG2El.js.map +0 -1
- package/dist/safeJsonParse-sFqVXf-e.cjs +0 -208
- package/dist/safeJsonParse-sFqVXf-e.cjs.map +0 -1
- package/dist/safeStableStringify-B2KTTS3r.js.map +0 -1
- package/dist/safeStableStringify-Cmi6GyWj.cjs +0 -106
- package/dist/safeStableStringify-Cmi6GyWj.cjs.map +0 -1
- package/dist/tailwind-CUdmv-oO.js.map +0 -1
- package/dist/tailwind-DUuboq52.cjs.map +0 -1
- package/dist/toStringArrayUnRecursive-OOHGazks.js.map +0 -1
- package/dist/toStringArrayUnRecursive-pfk2oP8C.cjs +0 -51
- package/dist/toStringArrayUnRecursive-pfk2oP8C.cjs.map +0 -1
- package/dist/urls-BL9C_yU3.cjs +0 -299
- package/dist/urls-BL9C_yU3.cjs.map +0 -1
- package/dist/urls-BXT4Krsq.js.map +0 -1
package/dist/promises/index.d.ts
CHANGED
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
* ========================================================================
|
|
3
3
|
* @rzl-zone/utils-js
|
|
4
4
|
* ------------------------------------------------------------------------
|
|
5
|
-
* Version: `3.
|
|
5
|
+
* Version: `3.14.0`
|
|
6
6
|
* Author: `Rizalvin Dwiky <rizalvindwiky@gmail.com>`
|
|
7
7
|
* Repository: `https://github.com/rzl-zone/rzl-zone/tree/main/packages/utils-js`
|
|
8
8
|
* ========================================================================
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import { CustomPromiseType } from "
|
|
11
|
+
import { o as CustomPromiseType } from "../index-or0oP9i2.js";
|
|
12
12
|
/** -------------------------------------------------------------
|
|
13
13
|
* * ***Utility Class: `CustomPromise`.***
|
|
14
|
-
*
|
|
14
|
+
* --------------------------------------------------------------
|
|
15
15
|
* **A strongly typed extension of the native {@link Promise | **`Promise`**}.**
|
|
16
|
+
*
|
|
17
|
+
* ---
|
|
16
18
|
* 1. **Behaves exactly like a normal Promise** for `then`/`catch`
|
|
17
19
|
* and `await` semantics.
|
|
18
20
|
* 2. **Stores the final resolution or rejection internally** so
|
|
@@ -20,104 +22,254 @@ import { CustomPromiseType } from "@rzl-zone/ts-types-plus";
|
|
|
20
22
|
* 3. **Adds a `finish` method** which runs once after settlement
|
|
21
23
|
* with access to both the fulfilled value *and* the rejection
|
|
22
24
|
* reason (only one will be defined).
|
|
25
|
+
*
|
|
26
|
+
* ---
|
|
23
27
|
* - **Key Differences from a Native `Promise`:**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
28
|
+
* - Every call to `then`/`catch` returns **`CustomPromise`**
|
|
29
|
+
* again, so the `finish` method remains available on the
|
|
30
|
+
* entire chain.
|
|
31
|
+
* - `finish` provides a tuple-like callback:
|
|
32
|
+
* - `val` ➔ defined when fulfilled.
|
|
33
|
+
* - `err` ➔ defined when rejected.
|
|
34
|
+
*
|
|
35
|
+
* ---
|
|
30
36
|
* @template Success Type of the resolved value.
|
|
31
37
|
* @template Error Type of the rejection reason (default `unknown`).
|
|
38
|
+
*
|
|
39
|
+
* ---
|
|
32
40
|
* @example
|
|
33
|
-
*
|
|
34
|
-
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { CustomPromise, type CustomPromiseType } from "@rzl-zone/utils-js/promises";
|
|
35
43
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
44
|
+
* type User = { id: number; name: string };
|
|
45
|
+
* type ApiError = { message: string };
|
|
38
46
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* function fetchUser(): CustomPromiseType<User, ApiError> {
|
|
48
|
+
* return new CustomPromise<User, ApiError>((resolve, reject) => {
|
|
49
|
+
* setTimeout(
|
|
50
|
+
* () =>
|
|
51
|
+
* void (Math.random() > 0.5
|
|
52
|
+
* ? resolve({ id: 1, name: "Alice" })
|
|
53
|
+
* : reject({ message: "Random failure" })),
|
|
54
|
+
* 500
|
|
55
|
+
* );
|
|
56
|
+
* });
|
|
57
|
+
* }
|
|
50
58
|
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
59
|
+
* fetchUser()
|
|
60
|
+
* .then(user => {
|
|
61
|
+
* console.log("SUCCESS:", user);
|
|
62
|
+
* return user;
|
|
63
|
+
* })
|
|
64
|
+
* .catch(err => {
|
|
65
|
+
* console.error("ERROR:", err);
|
|
66
|
+
* throw err;
|
|
67
|
+
* })
|
|
68
|
+
* .finish((val, err) => {
|
|
69
|
+
* // Runs once after settle, regardless of outcome
|
|
70
|
+
* console.log("FINISH:", { val, err });
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
65
73
|
* ---
|
|
66
74
|
* - **Implementation Notes:**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
75
|
+
* - Uses `Object.setPrototypeOf` to preserve the prototype chain
|
|
76
|
+
* for environments targeting ES5 or when subclassing Promise.
|
|
77
|
+
* - Internal settlement state is tracked automatically,
|
|
78
|
+
* allowing `finish` to access the final result even
|
|
79
|
+
* when registered after settlement.
|
|
72
80
|
*/
|
|
73
|
-
declare class CustomPromise<Success,
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
81
|
+
declare class CustomPromise<Success, Failure = unknown> extends Promise<Success> implements CustomPromiseType<Success, Failure> {
|
|
82
|
+
/** ------------------------------------------------------------
|
|
83
|
+
* * ***Constructor: `CustomPromise`.***
|
|
84
|
+
* -------------------------------------------------------------
|
|
85
|
+
* **Creates a new `CustomPromise` instance using a custom executor.**
|
|
86
|
+
*
|
|
87
|
+
* ---
|
|
88
|
+
* - **Behavior:**
|
|
89
|
+
* - Behaves similarly to the native
|
|
90
|
+
* ***`Promise`*** constructor.
|
|
91
|
+
* - Receives custom `resolve` and `reject`
|
|
92
|
+
* functions through the executor callback.
|
|
93
|
+
* - Stores the resolved value or rejection reason
|
|
94
|
+
* internally for use by `finish`.
|
|
95
|
+
* - Preserves the `CustomPromise` prototype chain
|
|
96
|
+
* for subclassed promise behavior.
|
|
97
|
+
* - Ensures chained `then` and `catch`
|
|
98
|
+
* operations continue returning
|
|
99
|
+
* ***`CustomPromise`*** instances.
|
|
100
|
+
*
|
|
101
|
+
* ---
|
|
102
|
+
* @param {(resolve, reject) => void} executor
|
|
103
|
+
* ***Executor function responsible for controlling the asynchronous operation lifecycle.***
|
|
104
|
+
*
|
|
105
|
+
* @param {(value: Success) => void} executor.resolve
|
|
106
|
+
* ***Function used to resolve the promise with a value.***
|
|
107
|
+
*
|
|
108
|
+
* @param {(error: Error) => void} executor.reject
|
|
109
|
+
* ***Function used to reject the promise with an error.***
|
|
110
|
+
*
|
|
111
|
+
* ---
|
|
112
|
+
* @throws **{@link Error | `Error`}**
|
|
113
|
+
* Any uncaught exception thrown inside the executor
|
|
114
|
+
* automatically rejects the promise.
|
|
115
|
+
*
|
|
116
|
+
* ---
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* const promise = new CustomPromise<string>(
|
|
120
|
+
* (resolve) => {
|
|
121
|
+
* setTimeout(
|
|
122
|
+
* () => resolve("Done"),
|
|
123
|
+
* 1000
|
|
124
|
+
* );
|
|
125
|
+
* }
|
|
126
|
+
* );
|
|
127
|
+
* ```
|
|
128
|
+
* ---
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const promise = new CustomPromise<
|
|
132
|
+
* string,
|
|
133
|
+
* Error
|
|
134
|
+
* >((resolve, reject) => {
|
|
135
|
+
* Math.random() > 0.5
|
|
136
|
+
* ? resolve("Success")
|
|
137
|
+
* : reject(
|
|
138
|
+
* new Error("Failed")
|
|
139
|
+
* );
|
|
140
|
+
* });
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
constructor(executor: (resolve: (value: Success) => void, reject: (error: Failure) => void) => void);
|
|
144
|
+
/** ---------------------------------------------------------------
|
|
145
|
+
* * ***Overrides `Promise.then()` while preserving the
|
|
146
|
+
* `CustomPromise` prototype chain.***
|
|
147
|
+
* ----------------------------------------------------------------
|
|
148
|
+
* **Registers callbacks for both fulfillment and rejection.**
|
|
149
|
+
*
|
|
150
|
+
* ---
|
|
151
|
+
* Unlike the native `Promise`, this override ensures the returned
|
|
152
|
+
* value remains a ***`CustomPromise`*** so additional methods
|
|
153
|
+
* such as `finish()` continue to be available throughout the chain.
|
|
154
|
+
*
|
|
155
|
+
* ---
|
|
156
|
+
* @template TResult1
|
|
157
|
+
* ***Result type returned by the fulfillment handler.***
|
|
158
|
+
*
|
|
159
|
+
* @template TResult2
|
|
160
|
+
* ***Result type returned by the rejection handler.***
|
|
161
|
+
*
|
|
162
|
+
* ---
|
|
163
|
+
* @param onfulfilled
|
|
164
|
+
* ***Callback executed when the promise resolves successfully.***
|
|
165
|
+
*
|
|
166
|
+
* @param onfulfilled.value
|
|
167
|
+
* ***Resolved value from the current promise.***
|
|
168
|
+
*
|
|
169
|
+
* @param onrejected
|
|
170
|
+
* ***Callback executed when the promise is rejected.***
|
|
171
|
+
*
|
|
172
|
+
* @param onrejected.reason
|
|
173
|
+
* ***Rejection reason from the current promise.***
|
|
174
|
+
*
|
|
175
|
+
* ---
|
|
176
|
+
* @returns A new ***`CustomPromise`*** containing the transformed result.
|
|
177
|
+
*/
|
|
178
|
+
override then<TResult1 = Success, TResult2 = never>(onfulfilled?: ((value: Success) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: Failure) => TResult2 | PromiseLike<TResult2>) | null): CustomPromise<TResult1 | TResult2, Failure>;
|
|
179
|
+
/** ---------------------------------------------------------------
|
|
180
|
+
* * ***Overrides `Promise.catch()` while preserving the
|
|
181
|
+
* `CustomPromise` prototype chain.***
|
|
182
|
+
* ----------------------------------------------------------------
|
|
183
|
+
* **Registers a callback for promise rejection handling.**
|
|
184
|
+
*
|
|
185
|
+
* ---
|
|
186
|
+
* Unlike the native `Promise`, this override ensures the returned
|
|
187
|
+
* value remains a ***`CustomPromise`*** so additional methods
|
|
188
|
+
* such as `finish()` continue to be available throughout the chain.
|
|
189
|
+
*
|
|
190
|
+
* ---
|
|
191
|
+
* @template TResult
|
|
192
|
+
* ***Result type returned by the rejection handler.***
|
|
193
|
+
*
|
|
194
|
+
* ---
|
|
195
|
+
* @param onrejected
|
|
196
|
+
* ***Callback executed when the promise is rejected.***
|
|
197
|
+
*
|
|
198
|
+
* @param onrejected.reason
|
|
199
|
+
* ***Rejection reason from the current promise.***
|
|
200
|
+
*
|
|
201
|
+
* ---
|
|
202
|
+
* @returns A new ***`CustomPromise`*** containing either the
|
|
203
|
+
* original success value or the transformed rejection result.
|
|
204
|
+
*/
|
|
205
|
+
override catch<TResult = never>(onrejected?: ((reason: Failure) => TResult | PromiseLike<TResult>) | null): CustomPromise<Success | TResult, Failure>;
|
|
206
|
+
/** ---------------------------------------------------------------
|
|
207
|
+
* * ***Registers a callback to be invoked exactly once when the
|
|
208
|
+
* promise settles.***
|
|
209
|
+
* ----------------------------------------------------------------
|
|
210
|
+
* *If the promise is already settled when `finish()` is called,
|
|
211
|
+
* the callback executes immediately on the same tick.*
|
|
212
|
+
*
|
|
213
|
+
* ---
|
|
214
|
+
* @param callback
|
|
215
|
+
* ***Callback receiving the final settlement result.***
|
|
216
|
+
*
|
|
217
|
+
* @param callback.value
|
|
218
|
+
* ***Resolved value when the promise is fulfilled.***
|
|
219
|
+
*
|
|
220
|
+
* @param callback.error
|
|
221
|
+
* ***Rejection reason when the promise is rejected.***
|
|
222
|
+
*
|
|
223
|
+
* ---
|
|
89
224
|
* @returns `this` for fluent chaining.
|
|
90
225
|
*/
|
|
91
|
-
finish(
|
|
226
|
+
finish(callback: (value?: Success, error?: Failure) => void): this;
|
|
92
227
|
}
|
|
93
228
|
/** ------------------------------------------------------------
|
|
94
229
|
* * ***Utility: `delay`.***
|
|
95
|
-
*
|
|
230
|
+
* -------------------------------------------------------------
|
|
96
231
|
* **Creates a Promise-based delay that resolves after a given number
|
|
97
232
|
* of milliseconds, optionally supports cancellation with `AbortSignal`.**
|
|
233
|
+
*
|
|
234
|
+
* ---
|
|
98
235
|
* - **Behavior:**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
236
|
+
* - Validates `milliSeconds` is a non-zero, non-negative integer.
|
|
237
|
+
* - Validates `signal` is an `AbortSignal` instance when provided.
|
|
238
|
+
* - Cleans up event listeners and timers properly.
|
|
239
|
+
*
|
|
240
|
+
* ---
|
|
102
241
|
* @param {number} [milliSeconds=1000]
|
|
103
242
|
* The duration of the delay in milliseconds, default is `1000`.
|
|
104
243
|
* @param {AbortSignal} [signal]
|
|
105
244
|
* An optional `AbortSignal` that can cancel the delay.
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* rejects with an `AbortError` if aborted.
|
|
245
|
+
*
|
|
246
|
+
* ---
|
|
109
247
|
* @throws **{@link TypeError | `TypeError`}** while validates `milliSeconds` and `signal`:
|
|
110
248
|
* - If `milliSeconds` **is not a valid** an `integer-number`, `NaN`, `negative-number`, or `≤ 0`.
|
|
111
249
|
* - If `signal` **is not a valid** an`AbortSignal`.
|
|
112
|
-
* @throws **{@link
|
|
113
|
-
* @example
|
|
114
|
-
* // Waits for 2 seconds
|
|
115
|
-
* await delay(2000);
|
|
250
|
+
* @throws **{@link AbortError | `AbortError`}** if the delay is aborted using the provided signal.
|
|
116
251
|
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
252
|
+
* ---
|
|
253
|
+
* @returns {Promise<void>}
|
|
254
|
+
* A promise that resolves after the specified delay or
|
|
255
|
+
* rejects with an `AbortError` if aborted.
|
|
256
|
+
*
|
|
257
|
+
* ---
|
|
258
|
+
* @example
|
|
259
|
+
* 1. #### Waits for 2 seconds:
|
|
260
|
+
* ```ts
|
|
261
|
+
* await delay(2000);
|
|
262
|
+
* ```
|
|
263
|
+
* ---
|
|
264
|
+
* 2. #### Delay with AbortSignal:
|
|
265
|
+
* ```ts
|
|
266
|
+
* const controller = new AbortController();
|
|
267
|
+
* delay(5000, controller.signal).catch(err => {
|
|
268
|
+
* console.log(err.name)
|
|
269
|
+
* // ➔ "AbortError:['@rzl-zone/utils-js/promises:delay']"
|
|
270
|
+
* });
|
|
271
|
+
* controller.abort();
|
|
272
|
+
* ```
|
|
121
273
|
*/
|
|
122
274
|
declare const delay: (milliSeconds?: number, signal?: AbortSignal) => Promise<void>;
|
|
123
275
|
export { CustomPromise, type CustomPromiseType, delay };
|
package/dist/promises/index.js
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
* ========================================================================
|
|
3
3
|
* @rzl-zone/utils-js
|
|
4
4
|
* ------------------------------------------------------------------------
|
|
5
|
-
* Version: `3.
|
|
5
|
+
* Version: `3.14.0`
|
|
6
6
|
* Author: `Rizalvin Dwiky <rizalvindwiky@gmail.com>`
|
|
7
7
|
* Repository: `https://github.com/rzl-zone/rzl-zone/tree/main/packages/utils-js`
|
|
8
8
|
* ========================================================================
|
|
9
9
|
*/
|
|
10
|
-
import {
|
|
11
|
-
import { t as safeStableStringify } from "../safeStableStringify-
|
|
12
|
-
import { t as isInteger } from "../isInteger-
|
|
10
|
+
import { i as getPreciseType, t as createMessage, x as isNull } from "../logger-Wxl_Krg6.js";
|
|
11
|
+
import { t as safeStableStringify } from "../safeStableStringify-CDSYnLUz.js";
|
|
12
|
+
import { t as isInteger } from "../isInteger-BtA3flbk.js";
|
|
13
|
+
import { t as AbortError } from "../AbortError-CgE6QNOR.js";
|
|
13
14
|
var CustomPromise = class extends Promise {
|
|
14
15
|
_value;
|
|
15
16
|
_error;
|
|
@@ -21,14 +22,14 @@ var CustomPromise = class extends Promise {
|
|
|
21
22
|
resolveOuter = resolve;
|
|
22
23
|
rejectOuter = reject;
|
|
23
24
|
});
|
|
24
|
-
executor((
|
|
25
|
-
this._value =
|
|
26
|
-
resolveOuter(
|
|
27
|
-
this._finish.forEach((
|
|
28
|
-
}, (
|
|
29
|
-
this._error =
|
|
30
|
-
rejectOuter(
|
|
31
|
-
this._finish.forEach((
|
|
25
|
+
executor(async (val) => {
|
|
26
|
+
this._value = await val;
|
|
27
|
+
resolveOuter(val);
|
|
28
|
+
this._finish.forEach(async (fn) => fn(await val, void 0));
|
|
29
|
+
}, async (err) => {
|
|
30
|
+
this._error = await err;
|
|
31
|
+
rejectOuter(err);
|
|
32
|
+
this._finish.forEach(async (fn) => fn(void 0, await err));
|
|
32
33
|
});
|
|
33
34
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
34
35
|
}
|
|
@@ -38,21 +39,15 @@ var CustomPromise = class extends Promise {
|
|
|
38
39
|
catch(onrejected) {
|
|
39
40
|
return super.catch(onrejected);
|
|
40
41
|
}
|
|
41
|
-
finish(
|
|
42
|
-
if (this._value !== void 0 || this._error !== void 0)
|
|
43
|
-
else this._finish.push(
|
|
42
|
+
finish(callback) {
|
|
43
|
+
if (this._value !== void 0 || this._error !== void 0) callback(this._value, this._error);
|
|
44
|
+
else this._finish.push(callback);
|
|
44
45
|
return this;
|
|
45
46
|
}
|
|
46
47
|
};
|
|
47
|
-
var AbortError = class extends Error {
|
|
48
|
-
constructor(message = "The operation was aborted", name = "AbortError") {
|
|
49
|
-
super(message);
|
|
50
|
-
this.name = name;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
48
|
const delay = (milliSeconds = 1e3, signal) => {
|
|
54
|
-
if (!isInteger(milliSeconds) || milliSeconds <= 0) throw new TypeError(`First parameter (\`milliSeconds\`) must be of type \`number\` and value must be a \`non-zero\`, \`non-NaN\`, \`non-negative\`, and \`integer-number\`, but received: \`${getPreciseType(milliSeconds)}\`, with value: \`${safeStableStringify(milliSeconds, { keepUndefined: true })}\`.`);
|
|
55
|
-
if (isNull(signal) || signal && !(signal instanceof AbortSignal)) throw new TypeError("Second parameter (`signal`) must be an `instance of AbortSignal` if provided.");
|
|
49
|
+
if (!isInteger(milliSeconds) || milliSeconds <= 0) throw new TypeError(errorMsg(`First parameter (\`milliSeconds\`) must be of type \`number\` and value must be a \`non-zero\`, \`non-NaN\`, \`non-negative\`, and \`integer-number\`, but received: \`${getPreciseType(milliSeconds)}\`, with value: \`${safeStableStringify(milliSeconds, { keepUndefined: true })}\`.`));
|
|
50
|
+
if (isNull(signal) || signal && !(signal instanceof AbortSignal)) throw new TypeError(errorMsg("Second parameter (`signal`) must be an `instance of AbortSignal` if provided."));
|
|
56
51
|
return new Promise((resolve, reject) => {
|
|
57
52
|
const timer = setTimeout(() => {
|
|
58
53
|
cleanup();
|
|
@@ -64,11 +59,17 @@ const delay = (milliSeconds = 1e3, signal) => {
|
|
|
64
59
|
};
|
|
65
60
|
const onAbort = () => {
|
|
66
61
|
cleanup();
|
|
67
|
-
reject(new AbortError("Function `delay` from `@rzl-zone/utils-js` was aborted.", "
|
|
62
|
+
reject(new AbortError("Function `delay` from `@rzl-zone/utils-js` was aborted.", { suffix: ":['@rzl-zone/utils-js/promises:delay']" }));
|
|
68
63
|
};
|
|
69
|
-
if (signal)
|
|
70
|
-
|
|
64
|
+
if (signal) {
|
|
65
|
+
if (signal.aborted) {
|
|
66
|
+
onAbort();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
70
|
+
}
|
|
71
71
|
});
|
|
72
72
|
};
|
|
73
|
+
const errorMsg = (msg) => createMessage("delay", msg);
|
|
73
74
|
export { CustomPromise, delay };
|
|
74
75
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/promises/CustomPromise.ts","../../src/promises/delay.ts"],"sourcesContent":["import type { CustomPromiseType } from \"@rzl-zone/ts-types-plus\";\n\nexport type { CustomPromiseType };\n\n/** -------------------------------------------------------------\n * * ***Utility Class: `CustomPromise`.***\n * -------------------------------------------------------------\n * **A strongly typed extension of the native {@link Promise | **`Promise`**}.**\n * 1. **Behaves exactly like a normal Promise** for `then`/`catch`\n * and `await` semantics.\n * 2. **Stores the final resolution or rejection internally** so\n * it can be retrieved by a custom `finish` handler.\n * 3. **Adds a `finish` method** which runs once after settlement\n * with access to both the fulfilled value *and* the rejection\n * reason (only one will be defined).\n * - **Key Differences from a Native `Promise`:**\n * - Every call to `then`/`catch` returns **`CustomPromise`**\n * again, so the `finish` method remains available on the\n * entire chain.\n * - `finish` provides a tuple-like callback:\n * - `val` → defined when fulfilled.\n * - `err` → defined when rejected.\n * @template Success Type of the resolved value.\n * @template Error Type of the rejection reason (default `unknown`).\n * @example\n * ```ts\n * import { CustomPromise, type CustomPromiseType } from \"@rzl-zone/utils-js/promises\";\n *\n * type User = { id: number; name: string };\n * type ApiError = { message: string };\n *\n * function fetchUser(): CustomPromiseType<User, ApiError> {\n * return new CustomPromise<User, ApiError>((resolve, reject) => {\n * setTimeout(\n * () =>\n * void (Math.random() > 0.5\n * ? resolve({ id: 1, name: \"Alice\" })\n * : reject({ message: \"Random failure\" })),\n * 500\n * );\n * });\n * }\n *\n * fetchUser()\n * .then(user => {\n * console.log(\"SUCCESS:\", user);\n * return user;\n * })\n * .catch(err => {\n * console.error(\"ERROR:\", err);\n * throw err;\n * })\n * .finish((val, err) => {\n * // Runs once after settle, regardless of outcome\n * console.log(\"FINISH:\", { val, err });\n * });\n * ```\n * ---\n * - **Implementation Notes:**\n * - Uses `Object.setPrototypeOf` to preserve the prototype chain\n * for environments targeting ES5 or when subclassing Promise.\n * - Internal `_value` and `_error` are updated as soon as the\n * executor resolves or rejects, guaranteeing `finish` receives\n * the final state even when added after settlement.\n */\nexport class CustomPromise<Success, Error = unknown>\n extends Promise<Success>\n implements CustomPromiseType<Success, Error>\n{\n private _value?: Success;\n private _error?: Error;\n private _finish: Array<(v?: Success, e?: Error) => void> = [];\n\n constructor(\n executor: (\n resolve: (v: Success) => void,\n reject: (e: Error) => void\n ) => void\n ) {\n let resolveOuter!: (v: Success) => void;\n let rejectOuter!: (e: Error) => void;\n\n super((resolve, reject) => {\n resolveOuter = resolve;\n rejectOuter = reject;\n });\n\n executor(\n (v) => {\n this._value = v;\n resolveOuter(v);\n this._finish.forEach((f) => f(v, undefined));\n },\n (e) => {\n this._error = e;\n rejectOuter(e);\n this._finish.forEach((f) => f(undefined, e));\n }\n );\n\n Object.setPrototypeOf(this, new.target.prototype);\n }\n\n override then<TResult1 = Success, TResult2 = never>(\n onfulfilled?: ((value: Success) => TResult1 | PromiseLike<TResult1>) | null,\n onrejected?: ((reason: Error) => TResult2 | PromiseLike<TResult2>) | null\n ): CustomPromise<TResult1 | TResult2, Error> {\n return super.then(onfulfilled, onrejected) as unknown as CustomPromise<\n TResult1 | TResult2,\n Error\n >;\n }\n\n override catch<TResult = never>(\n onrejected?: ((reason: Error) => TResult | PromiseLike<TResult>) | null\n ): CustomPromise<Success | TResult, Error> {\n return super.catch(onrejected) as unknown as CustomPromise<\n Success | TResult,\n Error\n >;\n }\n\n /**\n * Registers a callback to be invoked **exactly once** when the\n * promise settles, with access to both the resolved value and\n * the rejection reason.\n *\n * If the promise is already settled when `finish` is called,\n * the callback executes immediately on the same tick.\n *\n * @param cb Callback receiving the final `(value, error)`.\n * @returns `this` for fluent chaining.\n */\n finish(cb: (val?: Success, err?: Error) => void): this {\n if (this._value !== undefined || this._error !== undefined) {\n cb(this._value, this._error);\n } else {\n this._finish.push(cb);\n }\n return this;\n }\n}\n","import { isNull } from \"@/predicates/is/isNull\";\nimport { isInteger } from \"@/predicates/is/isInteger\";\nimport { getPreciseType } from \"@/predicates/type/getPreciseType\";\nimport { safeStableStringify } from \"@/conversions/stringify/safeStableStringify\";\n\n/** -------------------------------------------------------------------\n * * ***Custom `AbortError` for cross-runtime delay cancellation.***\n * -------------------------------------------------------------------\n */\nclass AbortError extends Error {\n constructor(\n message: string = \"The operation was aborted\",\n name: string = \"AbortError\"\n ) {\n super(message);\n this.name = name;\n }\n}\n\n/** ------------------------------------------------------------\n * * ***Utility: `delay`.***\n * ------------------------------------------------------------\n * **Creates a Promise-based delay that resolves after a given number\n * of milliseconds, optionally supports cancellation with `AbortSignal`.**\n * - **Behavior:**\n * - Validates `milliSeconds` is a non-zero, non-negative integer.\n * - Validates `signal` is an `AbortSignal` instance when provided.\n * - Cleans up event listeners and timers properly.\n * @param {number} [milliSeconds=1000]\n * The duration of the delay in milliseconds, default is `1000`.\n * @param {AbortSignal} [signal]\n * An optional `AbortSignal` that can cancel the delay.\n * @returns {Promise<void>}\n * A promise that resolves after the specified delay or\n * rejects with an `AbortError` if aborted.\n * @throws **{@link TypeError | `TypeError`}** while validates `milliSeconds` and `signal`:\n * - If `milliSeconds` **is not a valid** an `integer-number`, `NaN`, `negative-number`, or `≤ 0`.\n * - If `signal` **is not a valid** an`AbortSignal`.\n * @throws **{@link DOMException | `DOMException`}** if the delay is aborted using the signal, rejects with `AbortError`.\n * @example\n * // Waits for 2 seconds\n * await delay(2000);\n *\n * // Delay with AbortSignal\n * const controller = new AbortController();\n * delay(5000, controller.signal).catch(err => console.log(err.name)); // \"AbortError\"\n * controller.abort();\n */\nexport const delay = (\n milliSeconds: number = 1000,\n signal?: AbortSignal\n): Promise<void> => {\n if (!isInteger(milliSeconds) || milliSeconds <= 0) {\n throw new TypeError(\n `First parameter (\\`milliSeconds\\`) must be of type \\`number\\` and value must be a \\`non-zero\\`, \\`non-NaN\\`, \\`non-negative\\`, and \\`integer-number\\`, but received: \\`${getPreciseType(\n milliSeconds\n )}\\`, with value: \\`${safeStableStringify(milliSeconds, {\n keepUndefined: true\n })}\\`.`\n );\n }\n\n if (isNull(signal) || (signal && !(signal instanceof AbortSignal))) {\n throw new TypeError(\n \"Second parameter (`signal`) must be an `instance of AbortSignal` if provided.\"\n );\n }\n\n return new Promise<void>((resolve, reject) => {\n const timer = setTimeout(() => {\n cleanup();\n resolve();\n }, milliSeconds);\n\n const cleanup = () => {\n clearTimeout(timer);\n if (signal) signal.removeEventListener(\"abort\", onAbort);\n };\n\n const onAbort = () => {\n cleanup();\n reject(\n new AbortError(\n \"Function `delay` from `@rzl-zone/utils-js` was aborted.\",\n \"AbortError\"\n )\n );\n };\n\n if (signal) {\n if (signal.aborted) {\n onAbort();\n } else {\n signal.addEventListener(\"abort\", onAbort, { once: true });\n }\n }\n });\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,IAAa,gBAAb,cACU,QAEV;CACE,AAAQ;CACR,AAAQ;CACR,AAAQ,UAAmD,CAAC;CAE5D,YACE,UAIA;EACA,IAAI;EACJ,IAAI;EAEJ,OAAO,SAAS,WAAW;GACzB,eAAe;GACf,cAAc;EAChB,CAAC;EAED,UACG,MAAM;GACL,KAAK,SAAS;GACd,aAAa,CAAC;GACd,KAAK,QAAQ,SAAS,MAAM,EAAE,GAAG,MAAS,CAAC;EAC7C,IACC,MAAM;GACL,KAAK,SAAS;GACd,YAAY,CAAC;GACb,KAAK,QAAQ,SAAS,MAAM,EAAE,QAAW,CAAC,CAAC;EAC7C,CACF;EAEA,OAAO,eAAe,MAAM,IAAI,OAAO,SAAS;CAClD;CAEA,AAAS,KACP,aACA,YAC2C;EAC3C,OAAO,MAAM,KAAK,aAAa,UAAU;CAI3C;CAEA,AAAS,MACP,YACyC;EACzC,OAAO,MAAM,MAAM,UAAU;CAI/B;;;;;;;;;;;;CAaA,OAAO,IAAgD;EACrD,IAAI,KAAK,WAAW,UAAa,KAAK,WAAW,QAC/C,GAAG,KAAK,QAAQ,KAAK,MAAM;OAE3B,KAAK,QAAQ,KAAK,EAAE;EAEtB,OAAO;CACT;AACF;;;;;;;;ACpIA,IAAM,aAAN,cAAyB,MAAM;CAC7B,YACE,UAAkB,6BAClB,OAAe,cACf;EACA,MAAM,OAAO;EACb,KAAK,OAAO;CACd;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAa,SACX,eAAuB,KACvB,WACkB;CAClB,IAAI,CAAC,UAAU,YAAY,KAAK,gBAAgB,GAC9C,MAAM,IAAI,UACR,0KAA0K,eACxK,YACF,EAAE,oBAAoB,oBAAoB,cAAc,EACtD,eAAe,KACjB,CAAC,EAAE,IACL;CAGF,IAAI,OAAO,MAAM,KAAM,UAAU,EAAE,kBAAkB,cACnD,MAAM,IAAI,UACR,+EACF;CAGF,OAAO,IAAI,SAAe,SAAS,WAAW;EAC5C,MAAM,QAAQ,iBAAiB;GAC7B,QAAQ;GACR,QAAQ;EACV,GAAG,YAAY;EAEf,MAAM,gBAAgB;GACpB,aAAa,KAAK;GAClB,IAAI,QAAQ,OAAO,oBAAoB,SAAS,OAAO;EACzD;EAEA,MAAM,gBAAgB;GACpB,QAAQ;GACR,OACE,IAAI,WACF,2DACA,YACF,CACF;EACF;EAEA,IAAI,QACF,IAAI,OAAO,SACT,QAAQ;OAER,OAAO,iBAAiB,SAAS,SAAS,EAAE,MAAM,KAAK,CAAC;CAG9D,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/promises/CustomPromise.ts","../../src/promises/delay.ts"],"sourcesContent":["import type { CustomPromiseType } from \"@rzl-zone/ts-types-plus\";\n\nexport type { CustomPromiseType };\n\n/** -------------------------------------------------------------\n * * ***Utility Class: `CustomPromise`.***\n * --------------------------------------------------------------\n * **A strongly typed extension of the native {@link Promise | **`Promise`**}.**\n *\n * ---\n * 1. **Behaves exactly like a normal Promise** for `then`/`catch`\n * and `await` semantics.\n * 2. **Stores the final resolution or rejection internally** so\n * it can be retrieved by a custom `finish` handler.\n * 3. **Adds a `finish` method** which runs once after settlement\n * with access to both the fulfilled value *and* the rejection\n * reason (only one will be defined).\n *\n * ---\n * - **Key Differences from a Native `Promise`:**\n * - Every call to `then`/`catch` returns **`CustomPromise`**\n * again, so the `finish` method remains available on the\n * entire chain.\n * - `finish` provides a tuple-like callback:\n * - `val` ➔ defined when fulfilled.\n * - `err` ➔ defined when rejected.\n *\n * ---\n * @template Success Type of the resolved value.\n * @template Error Type of the rejection reason (default `unknown`).\n *\n * ---\n * @example\n * ```ts\n * import { CustomPromise, type CustomPromiseType } from \"@rzl-zone/utils-js/promises\";\n *\n * type User = { id: number; name: string };\n * type ApiError = { message: string };\n *\n * function fetchUser(): CustomPromiseType<User, ApiError> {\n * return new CustomPromise<User, ApiError>((resolve, reject) => {\n * setTimeout(\n * () =>\n * void (Math.random() > 0.5\n * ? resolve({ id: 1, name: \"Alice\" })\n * : reject({ message: \"Random failure\" })),\n * 500\n * );\n * });\n * }\n *\n * fetchUser()\n * .then(user => {\n * console.log(\"SUCCESS:\", user);\n * return user;\n * })\n * .catch(err => {\n * console.error(\"ERROR:\", err);\n * throw err;\n * })\n * .finish((val, err) => {\n * // Runs once after settle, regardless of outcome\n * console.log(\"FINISH:\", { val, err });\n * });\n * ```\n * ---\n * - **Implementation Notes:**\n * - Uses `Object.setPrototypeOf` to preserve the prototype chain\n * for environments targeting ES5 or when subclassing Promise.\n * - Internal settlement state is tracked automatically,\n * allowing `finish` to access the final result even\n * when registered after settlement.\n */\nexport class CustomPromise<Success, Failure = unknown>\n extends Promise<Success>\n implements CustomPromiseType<Success, Failure>\n{\n /** ------------------------------------------------------------\n *\n * @internal\n * Stores the resolved value after promise settlement.\n *\n * -------------------------------------------------------------\n */\n private _value?: Success;\n /** ------------------------------------------------------------\n *\n * @internal\n * Stores the rejection reason after promise settlement.\n *\n * -------------------------------------------------------------\n */\n private _error?: Failure;\n /** ------------------------------------------------------------\n *\n * @internal\n * Stores registered `finish` callbacks until the\n * promise settles.\n *\n * -------------------------------------------------------------\n */\n private _finish: Array<(v?: Success, e?: Failure) => void> = [];\n\n /** ------------------------------------------------------------\n * * ***Constructor: `CustomPromise`.***\n * -------------------------------------------------------------\n * **Creates a new `CustomPromise` instance using a custom executor.**\n *\n * ---\n * - **Behavior:**\n * - Behaves similarly to the native\n * ***`Promise`*** constructor.\n * - Receives custom `resolve` and `reject`\n * functions through the executor callback.\n * - Stores the resolved value or rejection reason\n * internally for use by `finish`.\n * - Preserves the `CustomPromise` prototype chain\n * for subclassed promise behavior.\n * - Ensures chained `then` and `catch`\n * operations continue returning\n * ***`CustomPromise`*** instances.\n *\n * ---\n * @param {(resolve, reject) => void} executor\n * ***Executor function responsible for controlling the asynchronous operation lifecycle.***\n *\n * @param {(value: Success) => void} executor.resolve\n * ***Function used to resolve the promise with a value.***\n *\n * @param {(error: Error) => void} executor.reject\n * ***Function used to reject the promise with an error.***\n *\n * ---\n * @throws **{@link Error | `Error`}**\n * Any uncaught exception thrown inside the executor\n * automatically rejects the promise.\n *\n * ---\n * @example\n * ```ts\n * const promise = new CustomPromise<string>(\n * (resolve) => {\n * setTimeout(\n * () => resolve(\"Done\"),\n * 1000\n * );\n * }\n * );\n * ```\n * ---\n * @example\n * ```ts\n * const promise = new CustomPromise<\n * string,\n * Error\n * >((resolve, reject) => {\n * Math.random() > 0.5\n * ? resolve(\"Success\")\n * : reject(\n * new Error(\"Failed\")\n * );\n * });\n * ```\n */\n constructor(\n executor: (\n /**\n * * ***Function used to resolve the promise with a value.***\n */\n resolve: (value: Success) => void,\n\n /**\n * * ***Function used to reject the promise with an error.***\n */\n reject: (error: Failure) => void\n ) => void\n ) {\n let resolveOuter: (v: Success) => void;\n let rejectOuter: (e: Failure) => void;\n\n super((resolve, reject) => {\n resolveOuter = resolve;\n rejectOuter = reject;\n });\n\n executor(\n async (val) => {\n this._value = await val;\n resolveOuter(val);\n this._finish.forEach(async (fn) => fn(await val, undefined));\n },\n async (err) => {\n this._error = await err;\n rejectOuter(err);\n this._finish.forEach(async (fn) => fn(undefined, await err));\n }\n );\n\n Object.setPrototypeOf(this, new.target.prototype);\n }\n\n /** ---------------------------------------------------------------\n * * ***Overrides `Promise.then()` while preserving the\n * `CustomPromise` prototype chain.***\n * ----------------------------------------------------------------\n * **Registers callbacks for both fulfillment and rejection.**\n *\n * ---\n * Unlike the native `Promise`, this override ensures the returned\n * value remains a ***`CustomPromise`*** so additional methods\n * such as `finish()` continue to be available throughout the chain.\n *\n * ---\n * @template TResult1\n * ***Result type returned by the fulfillment handler.***\n *\n * @template TResult2\n * ***Result type returned by the rejection handler.***\n *\n * ---\n * @param onfulfilled\n * ***Callback executed when the promise resolves successfully.***\n *\n * @param onfulfilled.value\n * ***Resolved value from the current promise.***\n *\n * @param onrejected\n * ***Callback executed when the promise is rejected.***\n *\n * @param onrejected.reason\n * ***Rejection reason from the current promise.***\n *\n * ---\n * @returns A new ***`CustomPromise`*** containing the transformed result.\n */\n override then<TResult1 = Success, TResult2 = never>(\n onfulfilled?:\n | ((\n /**\n * * ***Resolved value from the current promise.***\n */\n value: Success\n ) => TResult1 | PromiseLike<TResult1>)\n | null,\n onrejected?:\n | ((\n /**\n * * ***Rejection reason from the current promise.***\n */\n reason: Failure\n ) => TResult2 | PromiseLike<TResult2>)\n | null\n ): CustomPromise<TResult1 | TResult2, Failure> {\n return super.then(onfulfilled, onrejected) as unknown as CustomPromise<\n TResult1 | TResult2,\n Failure\n >;\n }\n\n /** ---------------------------------------------------------------\n * * ***Overrides `Promise.catch()` while preserving the\n * `CustomPromise` prototype chain.***\n * ----------------------------------------------------------------\n * **Registers a callback for promise rejection handling.**\n *\n * ---\n * Unlike the native `Promise`, this override ensures the returned\n * value remains a ***`CustomPromise`*** so additional methods\n * such as `finish()` continue to be available throughout the chain.\n *\n * ---\n * @template TResult\n * ***Result type returned by the rejection handler.***\n *\n * ---\n * @param onrejected\n * ***Callback executed when the promise is rejected.***\n *\n * @param onrejected.reason\n * ***Rejection reason from the current promise.***\n *\n * ---\n * @returns A new ***`CustomPromise`*** containing either the\n * original success value or the transformed rejection result.\n */\n override catch<TResult = never>(\n onrejected?:\n | ((\n /**\n * * ***Rejection reason from the current promise.***\n */\n reason: Failure\n ) => TResult | PromiseLike<TResult>)\n | null\n ): CustomPromise<Success | TResult, Failure> {\n return super.catch(onrejected) as unknown as CustomPromise<\n Success | TResult,\n Failure\n >;\n }\n\n /** ---------------------------------------------------------------\n * * ***Registers a callback to be invoked exactly once when the\n * promise settles.***\n * ----------------------------------------------------------------\n * *If the promise is already settled when `finish()` is called,\n * the callback executes immediately on the same tick.*\n *\n * ---\n * @param callback\n * ***Callback receiving the final settlement result.***\n *\n * @param callback.value\n * ***Resolved value when the promise is fulfilled.***\n *\n * @param callback.error\n * ***Rejection reason when the promise is rejected.***\n *\n * ---\n * @returns `this` for fluent chaining.\n */\n finish(\n callback: (\n /**\n * * ***Resolved value when the promise is fulfilled.***\n */\n value?: Success,\n\n /**\n * * ***Rejection reason when the promise is rejected.***\n */\n error?: Failure\n ) => void\n ): this {\n if (this._value !== undefined || this._error !== undefined) {\n callback(this._value, this._error);\n } else {\n this._finish.push(callback);\n }\n return this;\n }\n}\n","import { createMessage } from \"@/_private/logger\";\n\nimport { AbortError } from \"@/errors/AbortError\";\nimport { isNull } from \"@/predicates/is/isNull\";\nimport { isInteger } from \"@/predicates/is/isInteger\";\nimport { getPreciseType } from \"@/predicates/type/getPreciseType\";\nimport { safeStableStringify } from \"@/conversions/stringify/safeStableStringify\";\n\n/** ------------------------------------------------------------\n * * ***Utility: `delay`.***\n * -------------------------------------------------------------\n * **Creates a Promise-based delay that resolves after a given number\n * of milliseconds, optionally supports cancellation with `AbortSignal`.**\n *\n * ---\n * - **Behavior:**\n * - Validates `milliSeconds` is a non-zero, non-negative integer.\n * - Validates `signal` is an `AbortSignal` instance when provided.\n * - Cleans up event listeners and timers properly.\n *\n * ---\n * @param {number} [milliSeconds=1000]\n * The duration of the delay in milliseconds, default is `1000`.\n * @param {AbortSignal} [signal]\n * An optional `AbortSignal` that can cancel the delay.\n *\n * ---\n * @throws **{@link TypeError | `TypeError`}** while validates `milliSeconds` and `signal`:\n * - If `milliSeconds` **is not a valid** an `integer-number`, `NaN`, `negative-number`, or `≤ 0`.\n * - If `signal` **is not a valid** an`AbortSignal`.\n * @throws **{@link AbortError | `AbortError`}** if the delay is aborted using the provided signal.\n *\n * ---\n * @returns {Promise<void>}\n * A promise that resolves after the specified delay or\n * rejects with an `AbortError` if aborted.\n *\n * ---\n * @example\n * 1. #### Waits for 2 seconds:\n * ```ts\n * await delay(2000);\n * ```\n * ---\n * 2. #### Delay with AbortSignal:\n * ```ts\n * const controller = new AbortController();\n * delay(5000, controller.signal).catch(err => {\n * console.log(err.name)\n * // ➔ \"AbortError:['@rzl-zone/utils-js/promises:delay']\"\n * });\n * controller.abort();\n * ```\n */\nexport const delay = (\n milliSeconds: number = 1000,\n signal?: AbortSignal\n): Promise<void> => {\n if (!isInteger(milliSeconds) || milliSeconds <= 0) {\n throw new TypeError(\n errorMsg(\n `First parameter (\\`milliSeconds\\`) must be of type \\`number\\` and value must be a \\`non-zero\\`, \\`non-NaN\\`, \\`non-negative\\`, and \\`integer-number\\`, but received: \\`${getPreciseType(\n milliSeconds\n )}\\`, with value: \\`${safeStableStringify(milliSeconds, {\n keepUndefined: true\n })}\\`.`\n )\n );\n }\n\n if (isNull(signal) || (signal && !(signal instanceof AbortSignal))) {\n throw new TypeError(\n errorMsg(\n \"Second parameter (`signal`) must be an `instance of AbortSignal` if provided.\"\n )\n );\n }\n\n return new Promise<void>((resolve, reject) => {\n const timer = setTimeout(() => {\n cleanup();\n resolve();\n }, milliSeconds);\n\n const cleanup = () => {\n clearTimeout(timer);\n if (signal) signal.removeEventListener(\"abort\", onAbort);\n };\n\n const onAbort = () => {\n cleanup();\n reject(\n new AbortError(\n \"Function `delay` from `@rzl-zone/utils-js` was aborted.\",\n {\n suffix: \":['@rzl-zone/utils-js/promises:delay']\"\n }\n )\n );\n };\n\n if (signal) {\n if (signal.aborted) {\n onAbort();\n return;\n }\n\n signal.addEventListener(\"abort\", onAbort, { once: true });\n }\n });\n};\n\n/**\n * @internal ***`Not part of the public API.`***\n */\nconst errorMsg = (msg: string) => createMessage(\"delay\", msg);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyEA,IAAa,gBAAb,cACU,QAEV;;;;;;;;CAQE,AAAQ;;;;;;;;CAQR,AAAQ;;;;;;;;;CASR,AAAQ,UAAqD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+D9D,YACE,UAWA;EACA,IAAI;EACJ,IAAI;EAEJ,OAAO,SAAS,WAAW;GACzB,eAAe;GACf,cAAc;EAChB,CAAC;EAED,SACE,OAAO,QAAQ;GACb,KAAK,SAAS,MAAM;GACpB,aAAa,GAAG;GAChB,KAAK,QAAQ,QAAQ,OAAO,OAAO,GAAG,MAAM,KAAK,MAAS,CAAC;EAC7D,GACA,OAAO,QAAQ;GACb,KAAK,SAAS,MAAM;GACpB,YAAY,GAAG;GACf,KAAK,QAAQ,QAAQ,OAAO,OAAO,GAAG,QAAW,MAAM,GAAG,CAAC;EAC7D,CACF;EAEA,OAAO,eAAe,MAAM,IAAI,OAAO,SAAS;CAClD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoCA,AAAS,KACP,aAQA,YAQ6C;EAC7C,OAAO,MAAM,KAAK,aAAa,UAAU;CAI3C;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4BA,AAAS,MACP,YAQ2C;EAC3C,OAAO,MAAM,MAAM,UAAU;CAI/B;;;;;;;;;;;;;;;;;;;;;CAsBA,OACE,UAWM;EACN,IAAI,KAAK,WAAW,UAAa,KAAK,WAAW,QAC/C,SAAS,KAAK,QAAQ,KAAK,MAAM;OAEjC,KAAK,QAAQ,KAAK,QAAQ;EAE5B,OAAO;CACT;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC/RA,MAAa,SACX,eAAuB,KACvB,WACkB;CAClB,IAAI,CAAC,UAAU,YAAY,KAAK,gBAAgB,GAC9C,MAAM,IAAI,UACR,SACE,0KAA0K,eACxK,YACF,EAAE,oBAAoB,oBAAoB,cAAc,EACtD,eAAe,KACjB,CAAC,EAAE,IACL,CACF;CAGF,IAAI,OAAO,MAAM,KAAM,UAAU,EAAE,kBAAkB,cACnD,MAAM,IAAI,UACR,SACE,+EACF,CACF;CAGF,OAAO,IAAI,SAAe,SAAS,WAAW;EAC5C,MAAM,QAAQ,iBAAiB;GAC7B,QAAQ;GACR,QAAQ;EACV,GAAG,YAAY;EAEf,MAAM,gBAAgB;GACpB,aAAa,KAAK;GAClB,IAAI,QAAQ,OAAO,oBAAoB,SAAS,OAAO;EACzD;EAEA,MAAM,gBAAgB;GACpB,QAAQ;GACR,OACE,IAAI,WACF,2DACA,EACE,QAAQ,yCACV,CACF,CACF;EACF;EAEA,IAAI,QAAQ;GACV,IAAI,OAAO,SAAS;IAClB,QAAQ;IACR;GACF;GAEA,OAAO,iBAAiB,SAAS,SAAS,EAAE,MAAM,KAAK,CAAC;EAC1D;CACF,CAAC;AACH;;;;AAKA,MAAM,YAAY,QAAgB,cAAc,SAAS,GAAG"}
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
* ========================================================================
|
|
3
3
|
* @rzl-zone/utils-js
|
|
4
4
|
* ------------------------------------------------------------------------
|
|
5
|
-
* Version: `3.
|
|
5
|
+
* Version: `3.14.0`
|
|
6
6
|
* Author: `Rizalvin Dwiky <rizalvindwiky@gmail.com>`
|
|
7
7
|
* Repository: `https://github.com/rzl-zone/rzl-zone/tree/main/packages/utils-js`
|
|
8
8
|
* ========================================================================
|
|
9
9
|
*/
|
|
10
10
|
"use strict";
|
|
11
|
-
const
|
|
12
|
-
const require_assertIsString = require('./assertIsString-
|
|
11
|
+
const require_logger = require('./logger-BXg2xufj.cjs');
|
|
12
|
+
const require_assertIsString = require('./assertIsString-CuPbh3FB.cjs');
|
|
13
13
|
const maxInt = 2147483647;
|
|
14
14
|
const base = 36, tMin = 1, tMax = 26, skew = 38, damp = 700;
|
|
15
15
|
const initialBias = 72, initialN = 128, delimiter = "-";
|
|
@@ -44,7 +44,7 @@ function mapDomain(domain, fn) {
|
|
|
44
44
|
return result + map(labels, fn).join(".");
|
|
45
45
|
}
|
|
46
46
|
function ucs2decode(input) {
|
|
47
|
-
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) =>
|
|
47
|
+
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) => require_logger.createMessage("punycodeUtilsJS.ucs2.decode", `Parameter \`input\` must be of type \`${validType}\`, but received: \`${currentType}\`.`) });
|
|
48
48
|
const output = [];
|
|
49
49
|
let counter = 0;
|
|
50
50
|
const length = input.length;
|
|
@@ -62,7 +62,7 @@ function ucs2decode(input) {
|
|
|
62
62
|
return output;
|
|
63
63
|
}
|
|
64
64
|
const ucs2encode = (points) => {
|
|
65
|
-
if (!Array.isArray(points) || !points.every((p) => typeof p === "number" && Number.isFinite(p))) throw new TypeError(
|
|
65
|
+
if (!Array.isArray(points) || !points.every((p) => typeof p === "number" && Number.isFinite(p))) throw new TypeError(require_logger.createMessage("punycodeUtilsJS.ucs2.encode", `Parameter \`points\` must be an array of numbers, but received: \`${require_logger.getPreciseType(points)}\`.`));
|
|
66
66
|
return String.fromCodePoint(...points);
|
|
67
67
|
};
|
|
68
68
|
function basicToDigit(codePoint) {
|
|
@@ -85,7 +85,7 @@ function adapt(delta, numPoints, firstTime) {
|
|
|
85
85
|
return floor(k + 36 * delta / (delta + skew));
|
|
86
86
|
}
|
|
87
87
|
function decode(input) {
|
|
88
|
-
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) =>
|
|
88
|
+
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) => require_logger.createMessage("punycodeUtilsJS.decode", `Parameter \`input\` must be of type \`${validType}\`, but received: \`${currentType}\`.`) });
|
|
89
89
|
const output = [];
|
|
90
90
|
const inputLength = input.length;
|
|
91
91
|
let i = 0, n = initialN, bias = initialBias;
|
|
@@ -96,7 +96,7 @@ function decode(input) {
|
|
|
96
96
|
output.push(input.charCodeAt(j));
|
|
97
97
|
}
|
|
98
98
|
for (let index = basic > 0 ? basic + 1 : 0; index < inputLength;) {
|
|
99
|
-
const
|
|
99
|
+
const oldIdx = i;
|
|
100
100
|
let w = 1;
|
|
101
101
|
for (let k = base;; k += base) {
|
|
102
102
|
if (index >= inputLength) error("invalid-input");
|
|
@@ -111,7 +111,7 @@ function decode(input) {
|
|
|
111
111
|
w *= baseMinusT;
|
|
112
112
|
}
|
|
113
113
|
const out = output.length + 1;
|
|
114
|
-
bias = adapt(i -
|
|
114
|
+
bias = adapt(i - oldIdx, out, oldIdx === 0);
|
|
115
115
|
if (floor(i / out) > maxInt - n) error("overflow");
|
|
116
116
|
n += floor(i / out);
|
|
117
117
|
i %= out;
|
|
@@ -120,7 +120,7 @@ function decode(input) {
|
|
|
120
120
|
return String.fromCodePoint(...output);
|
|
121
121
|
}
|
|
122
122
|
function encode(input) {
|
|
123
|
-
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) =>
|
|
123
|
+
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) => require_logger.createMessage("punycodeUtilsJS.encode", `Parameter \`input\` must be of type \`${validType}\`, but received: \`${currentType}\`.`) });
|
|
124
124
|
const output = [];
|
|
125
125
|
const points = ucs2decode(input);
|
|
126
126
|
const inputLength = points.length;
|
|
@@ -158,11 +158,11 @@ function encode(input) {
|
|
|
158
158
|
return output.join("");
|
|
159
159
|
}
|
|
160
160
|
function toUnicode(input) {
|
|
161
|
-
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) =>
|
|
161
|
+
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) => require_logger.createMessage("punycodeUtilsJS.toUnicode", `Parameter \`input\` must be of type \`${validType}\`, but received: \`${currentType}\`.`) });
|
|
162
162
|
return mapDomain(input, (str) => regexPunycode.test(str) ? decode(str.slice(4).toLowerCase()) : str);
|
|
163
163
|
}
|
|
164
164
|
function toASCII(input) {
|
|
165
|
-
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) =>
|
|
165
|
+
require_assertIsString.assertIsString(input, { message: ({ currentType, validType }) => require_logger.createMessage("punycodeUtilsJS.toASCII", `Parameter \`input\` must be of type \`${validType}\`, but received: \`${currentType}\`.`) });
|
|
166
166
|
return mapDomain(input, (str) => regexNonASCII.test(str) ? "xn--" + encode(str) : str);
|
|
167
167
|
}
|
|
168
168
|
const punycodeUtilsJS = {
|
|
@@ -182,4 +182,4 @@ Object.defineProperty(exports, 'punycodeUtilsJS', {
|
|
|
182
182
|
return punycodeUtilsJS;
|
|
183
183
|
}
|
|
184
184
|
});
|
|
185
|
-
//# sourceMappingURL=punyCode-
|
|
185
|
+
//# sourceMappingURL=punyCode-B_VPjG0d.cjs.map
|