@telperion/js-utils 3.7.1 → 3.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (575) hide show
  1. package/.build-manifest.json +966 -0
  2. package/README.md +24 -0
  3. package/array/arrayize.cjs +5 -3
  4. package/array/arrayize.cjs.map +1 -0
  5. package/array/arrayize.d.cts +19 -0
  6. package/array/arrayize.d.ts +3 -1
  7. package/array/arrayize.js +2 -1
  8. package/array/arrayize.js.map +1 -1
  9. package/array/async-map.cjs +5 -3
  10. package/array/async-map.cjs.map +1 -0
  11. package/array/async-map.d.cts +36 -0
  12. package/array/async-map.d.ts +3 -1
  13. package/array/async-map.js +2 -1
  14. package/array/async-map.js.map +1 -1
  15. package/array/compact.cjs +8 -5
  16. package/array/compact.cjs.map +1 -0
  17. package/array/compact.d.cts +30 -0
  18. package/array/compact.d.ts +3 -1
  19. package/array/compact.js +4 -2
  20. package/array/compact.js.map +1 -1
  21. package/array/difference.cjs +5 -3
  22. package/array/difference.cjs.map +1 -0
  23. package/array/difference.d.cts +48 -0
  24. package/array/difference.d.ts +7 -4
  25. package/array/difference.js +2 -1
  26. package/array/difference.js.map +1 -1
  27. package/array/find-by-key.cjs +5 -3
  28. package/array/find-by-key.cjs.map +1 -0
  29. package/array/find-by-key.d.cts +32 -0
  30. package/array/find-by-key.d.ts +3 -1
  31. package/array/find-by-key.js +2 -1
  32. package/array/find-by-key.js.map +1 -1
  33. package/array/gather.cjs +5 -3
  34. package/array/gather.cjs.map +1 -0
  35. package/array/gather.d.cts +50 -0
  36. package/array/gather.d.ts +3 -1
  37. package/array/gather.js +2 -1
  38. package/array/gather.js.map +1 -1
  39. package/array/index.cjs +34 -15
  40. package/array/index.cjs.map +1 -0
  41. package/array/index.d.cts +14 -0
  42. package/array/index.d.ts +14 -12
  43. package/array/index.js +14 -13
  44. package/array/index.js.map +1 -1
  45. package/array/intersection.cjs +5 -3
  46. package/array/intersection.cjs.map +1 -0
  47. package/array/intersection.d.cts +48 -0
  48. package/array/intersection.d.ts +7 -4
  49. package/array/intersection.js +2 -1
  50. package/array/intersection.js.map +1 -1
  51. package/array/limit.cjs +5 -3
  52. package/array/limit.cjs.map +1 -0
  53. package/array/limit.d.cts +31 -0
  54. package/array/limit.d.ts +3 -1
  55. package/array/limit.js +2 -1
  56. package/array/limit.js.map +1 -1
  57. package/array/proto/async-map.cjs +6 -4
  58. package/array/proto/async-map.cjs.map +1 -0
  59. package/array/proto/async-map.d.cts +24 -0
  60. package/array/proto/async-map.d.ts +0 -1
  61. package/array/proto/async-map.js +2 -2
  62. package/array/proto/async-map.js.map +1 -1
  63. package/array/proto/compact.cjs +6 -4
  64. package/array/proto/compact.cjs.map +1 -0
  65. package/array/proto/compact.d.cts +14 -0
  66. package/array/proto/compact.d.ts +0 -1
  67. package/array/proto/compact.js +2 -2
  68. package/array/proto/compact.js.map +1 -1
  69. package/array/proto/difference.cjs +6 -4
  70. package/array/proto/difference.cjs.map +1 -0
  71. package/array/proto/difference.d.cts +28 -0
  72. package/array/proto/difference.d.ts +3 -2
  73. package/array/proto/difference.js +2 -2
  74. package/array/proto/difference.js.map +1 -1
  75. package/array/proto/find-by-key.cjs +6 -4
  76. package/array/proto/find-by-key.cjs.map +1 -0
  77. package/array/proto/find-by-key.d.cts +24 -0
  78. package/array/proto/find-by-key.d.ts +0 -1
  79. package/array/proto/find-by-key.js +2 -2
  80. package/array/proto/find-by-key.js.map +1 -1
  81. package/array/proto/gather.cjs +6 -4
  82. package/array/proto/gather.cjs.map +1 -0
  83. package/array/proto/gather.d.cts +31 -0
  84. package/array/proto/gather.d.ts +0 -1
  85. package/array/proto/gather.js +2 -2
  86. package/array/proto/gather.js.map +1 -1
  87. package/array/proto/intersection.cjs +6 -4
  88. package/array/proto/intersection.cjs.map +1 -0
  89. package/array/proto/intersection.d.cts +28 -0
  90. package/array/proto/intersection.d.ts +2 -1
  91. package/array/proto/intersection.js +2 -2
  92. package/array/proto/intersection.js.map +1 -1
  93. package/array/proto/limit.cjs +6 -4
  94. package/array/proto/limit.cjs.map +1 -0
  95. package/array/proto/limit.d.cts +23 -0
  96. package/array/proto/limit.d.ts +0 -1
  97. package/array/proto/limit.js +2 -2
  98. package/array/proto/limit.js.map +1 -1
  99. package/array/proto/remove.cjs +6 -4
  100. package/array/proto/remove.cjs.map +1 -0
  101. package/array/proto/remove.d.cts +32 -0
  102. package/array/proto/remove.d.ts +3 -3
  103. package/array/proto/remove.js +2 -2
  104. package/array/proto/remove.js.map +1 -1
  105. package/array/proto/replace.cjs +6 -4
  106. package/array/proto/replace.cjs.map +1 -0
  107. package/array/proto/replace.d.cts +73 -0
  108. package/array/proto/replace.d.ts +2 -1
  109. package/array/proto/replace.js +2 -2
  110. package/array/proto/replace.js.map +1 -1
  111. package/array/proto/slice-after.cjs +8 -0
  112. package/array/proto/slice-after.cjs.map +1 -0
  113. package/array/proto/slice-after.d.cts +33 -0
  114. package/array/proto/slice-after.d.ts +33 -0
  115. package/array/proto/slice-after.js +6 -0
  116. package/array/proto/slice-after.js.map +1 -0
  117. package/array/proto/slice-before.cjs +8 -0
  118. package/array/proto/slice-before.cjs.map +1 -0
  119. package/array/proto/slice-before.d.cts +33 -0
  120. package/array/proto/slice-before.d.ts +33 -0
  121. package/array/proto/slice-before.js +6 -0
  122. package/array/proto/slice-before.js.map +1 -0
  123. package/array/proto/uniquify-by-key.cjs +6 -4
  124. package/array/proto/uniquify-by-key.cjs.map +1 -0
  125. package/array/proto/uniquify-by-key.d.cts +23 -0
  126. package/array/proto/uniquify-by-key.d.ts +0 -1
  127. package/array/proto/uniquify-by-key.js +2 -2
  128. package/array/proto/uniquify-by-key.js.map +1 -1
  129. package/array/proto/uniquify.cjs +6 -4
  130. package/array/proto/uniquify.cjs.map +1 -0
  131. package/array/proto/uniquify.d.cts +22 -0
  132. package/array/proto/uniquify.d.ts +0 -1
  133. package/array/proto/uniquify.js +2 -2
  134. package/array/proto/uniquify.js.map +1 -1
  135. package/array/remove.cjs +5 -3
  136. package/array/remove.cjs.map +1 -0
  137. package/array/remove.d.cts +35 -0
  138. package/array/remove.d.ts +3 -1
  139. package/array/remove.js +2 -1
  140. package/array/remove.js.map +1 -1
  141. package/array/replace.cjs +5 -3
  142. package/array/replace.cjs.map +1 -0
  143. package/array/replace.d.cts +142 -0
  144. package/array/replace.d.ts +10 -7
  145. package/array/replace.js +2 -1
  146. package/array/replace.js.map +1 -1
  147. package/array/slice-after.cjs +44 -0
  148. package/array/slice-after.cjs.map +1 -0
  149. package/array/slice-after.d.cts +35 -0
  150. package/array/slice-after.d.ts +35 -0
  151. package/array/slice-after.js +42 -0
  152. package/array/slice-after.js.map +1 -0
  153. package/array/slice-before.cjs +44 -0
  154. package/array/slice-before.cjs.map +1 -0
  155. package/array/slice-before.d.cts +35 -0
  156. package/array/slice-before.d.ts +35 -0
  157. package/array/slice-before.js +42 -0
  158. package/array/slice-before.js.map +1 -0
  159. package/array/static/arrayize.cjs +6 -4
  160. package/array/static/arrayize.cjs.map +1 -0
  161. package/array/static/arrayize.d.cts +23 -0
  162. package/array/static/arrayize.d.ts +18 -1
  163. package/array/static/arrayize.js +2 -2
  164. package/array/static/arrayize.js.map +1 -1
  165. package/array/uniquify-by-key.cjs +5 -3
  166. package/array/uniquify-by-key.cjs.map +1 -0
  167. package/array/uniquify-by-key.d.cts +31 -0
  168. package/array/uniquify-by-key.d.ts +3 -1
  169. package/array/uniquify-by-key.js +2 -1
  170. package/array/uniquify-by-key.js.map +1 -1
  171. package/array/uniquify.cjs +5 -3
  172. package/array/uniquify.cjs.map +1 -0
  173. package/array/uniquify.d.cts +29 -0
  174. package/array/uniquify.d.ts +3 -1
  175. package/array/uniquify.js +2 -1
  176. package/array/uniquify.js.map +1 -1
  177. package/boolean/static/is-falsy.cjs +6 -4
  178. package/boolean/static/is-falsy.cjs.map +1 -0
  179. package/boolean/static/is-falsy.d.cts +24 -0
  180. package/boolean/static/is-falsy.d.ts +2 -1
  181. package/boolean/static/is-falsy.js +2 -2
  182. package/boolean/static/is-falsy.js.map +1 -1
  183. package/boolean/static/is-truthy.cjs +6 -4
  184. package/boolean/static/is-truthy.cjs.map +1 -0
  185. package/boolean/static/is-truthy.d.cts +24 -0
  186. package/boolean/static/is-truthy.d.ts +2 -1
  187. package/boolean/static/is-truthy.js +2 -2
  188. package/boolean/static/is-truthy.js.map +1 -1
  189. package/class/index.cjs +8 -4
  190. package/class/index.cjs.map +1 -0
  191. package/class/index.d.cts +1 -0
  192. package/class/index.d.ts +1 -1
  193. package/class/index.js +1 -2
  194. package/class/index.js.map +1 -1
  195. package/class/mixin.cjs +5 -3
  196. package/class/mixin.cjs.map +1 -0
  197. package/class/mixin.d.cts +51 -0
  198. package/class/mixin.d.ts +3 -2
  199. package/class/mixin.js +2 -1
  200. package/class/mixin.js.map +1 -1
  201. package/compact.cjs +10 -7
  202. package/compact.cjs.map +1 -0
  203. package/compact.d.cts +4 -0
  204. package/compact.d.ts +4 -2
  205. package/compact.js +7 -5
  206. package/compact.js.map +1 -1
  207. package/fesm/js-utils.mjs +1856 -0
  208. package/fesm/js-utils.mjs.map +1 -0
  209. package/function/call.cjs +25 -3
  210. package/function/call.cjs.map +1 -0
  211. package/function/call.d.cts +23 -0
  212. package/function/call.d.ts +23 -1
  213. package/function/call.js +22 -1
  214. package/function/call.js.map +1 -1
  215. package/function/debounce.cjs +20 -17
  216. package/function/debounce.cjs.map +1 -0
  217. package/function/debounce.d.cts +47 -0
  218. package/function/debounce.d.ts +8 -5
  219. package/function/debounce.js +9 -7
  220. package/function/debounce.js.map +1 -1
  221. package/function/defer.cjs +8 -5
  222. package/function/defer.cjs.map +1 -0
  223. package/function/defer.d.cts +28 -0
  224. package/function/defer.d.ts +3 -1
  225. package/function/defer.js +4 -2
  226. package/function/defer.js.map +1 -1
  227. package/function/index.cjs +18 -8
  228. package/function/index.cjs.map +1 -0
  229. package/function/index.d.cts +5 -0
  230. package/function/index.d.ts +5 -5
  231. package/function/index.js +5 -6
  232. package/function/index.js.map +1 -1
  233. package/function/noop.cjs +5 -3
  234. package/function/noop.cjs.map +1 -0
  235. package/function/noop.d.cts +22 -0
  236. package/function/noop.d.ts +3 -1
  237. package/function/noop.js +2 -1
  238. package/function/noop.js.map +1 -1
  239. package/function/of.cjs +8 -5
  240. package/function/of.cjs.map +1 -0
  241. package/function/of.d.cts +33 -0
  242. package/function/of.d.ts +3 -1
  243. package/function/of.js +4 -2
  244. package/function/of.js.map +1 -1
  245. package/function/proto/debounce.cjs +7 -5
  246. package/function/proto/debounce.cjs.map +1 -0
  247. package/function/proto/debounce.d.cts +29 -0
  248. package/function/proto/debounce.d.ts +0 -1
  249. package/function/proto/debounce.js +2 -2
  250. package/function/proto/debounce.js.map +1 -1
  251. package/function/static/call.cjs +6 -4
  252. package/function/static/call.cjs.map +1 -0
  253. package/function/static/call.d.cts +23 -0
  254. package/function/static/call.d.ts +18 -1
  255. package/function/static/call.js +2 -2
  256. package/function/static/call.js.map +1 -1
  257. package/function/static/defer.cjs +6 -4
  258. package/function/static/defer.cjs.map +1 -0
  259. package/function/static/defer.d.cts +24 -0
  260. package/function/static/defer.d.ts +2 -1
  261. package/function/static/defer.js +2 -2
  262. package/function/static/defer.js.map +1 -1
  263. package/function/static/noop.cjs +6 -4
  264. package/function/static/noop.cjs.map +1 -0
  265. package/function/static/noop.d.cts +19 -0
  266. package/function/static/noop.d.ts +2 -1
  267. package/function/static/noop.js +2 -2
  268. package/function/static/noop.js.map +1 -1
  269. package/function/static/of.cjs +6 -4
  270. package/function/static/of.cjs.map +1 -0
  271. package/function/static/of.d.cts +27 -0
  272. package/function/static/of.d.ts +2 -1
  273. package/function/static/of.js +2 -2
  274. package/function/static/of.js.map +1 -1
  275. package/index.cjs +95 -34
  276. package/index.cjs.map +1 -0
  277. package/index.d.cts +44 -0
  278. package/index.d.ts +44 -15
  279. package/index.js +43 -17
  280. package/index.js.map +1 -1
  281. package/is-falsy.cjs +6 -4
  282. package/is-falsy.cjs.map +1 -0
  283. package/is-falsy.d.cts +30 -0
  284. package/is-falsy.d.ts +4 -2
  285. package/is-falsy.js +3 -2
  286. package/is-falsy.js.map +1 -1
  287. package/is-truthy.cjs +6 -4
  288. package/is-truthy.cjs.map +1 -0
  289. package/is-truthy.d.cts +30 -0
  290. package/is-truthy.d.ts +4 -2
  291. package/is-truthy.js +3 -2
  292. package/is-truthy.js.map +1 -1
  293. package/limit.cjs +10 -7
  294. package/limit.cjs.map +1 -0
  295. package/limit.d.cts +4 -0
  296. package/limit.d.ts +4 -2
  297. package/limit.js +7 -5
  298. package/limit.js.map +1 -1
  299. package/map/index.cjs +8 -4
  300. package/map/index.cjs.map +1 -0
  301. package/map/index.d.cts +1 -0
  302. package/map/index.d.ts +1 -1
  303. package/map/index.js +1 -2
  304. package/map/index.js.map +1 -1
  305. package/map/merge.cjs +5 -3
  306. package/map/merge.cjs.map +1 -0
  307. package/map/merge.d.cts +26 -0
  308. package/map/merge.d.ts +3 -1
  309. package/map/merge.js +2 -1
  310. package/map/merge.js.map +1 -1
  311. package/math/index.cjs +8 -4
  312. package/math/index.cjs.map +1 -0
  313. package/math/index.d.cts +1 -0
  314. package/math/index.d.ts +1 -1
  315. package/math/index.js +1 -2
  316. package/math/index.js.map +1 -1
  317. package/math/min-max.cjs +5 -3
  318. package/math/min-max.cjs.map +1 -0
  319. package/math/min-max.d.cts +26 -0
  320. package/math/min-max.d.ts +3 -1
  321. package/math/min-max.js +2 -1
  322. package/math/min-max.js.map +1 -1
  323. package/math/static/min-max.cjs +6 -4
  324. package/math/static/min-max.cjs.map +1 -0
  325. package/math/static/min-max.d.cts +21 -0
  326. package/math/static/min-max.d.ts +2 -1
  327. package/math/static/min-max.js +2 -2
  328. package/math/static/min-max.js.map +1 -1
  329. package/object/clone.cjs +14 -11
  330. package/object/clone.cjs.map +1 -0
  331. package/object/clone.d.cts +164 -0
  332. package/object/clone.d.ts +9 -6
  333. package/object/clone.js +6 -4
  334. package/object/clone.js.map +1 -1
  335. package/object/compact.cjs +5 -3
  336. package/object/compact.cjs.map +1 -0
  337. package/object/compact.d.cts +39 -0
  338. package/object/compact.d.ts +3 -1
  339. package/object/compact.js +2 -1
  340. package/object/compact.js.map +1 -1
  341. package/object/deep-freeze.cjs +24 -3
  342. package/object/deep-freeze.cjs.map +1 -0
  343. package/object/deep-freeze.d.cts +22 -0
  344. package/object/deep-freeze.d.ts +22 -1
  345. package/object/deep-freeze.js +21 -1
  346. package/object/deep-freeze.js.map +1 -1
  347. package/object/deepest.cjs +5 -3
  348. package/object/deepest.cjs.map +1 -0
  349. package/object/deepest.d.cts +50 -0
  350. package/object/deepest.d.ts +5 -2
  351. package/object/deepest.js +2 -1
  352. package/object/deepest.js.map +1 -1
  353. package/object/index.cjs +16 -8
  354. package/object/index.cjs.map +1 -0
  355. package/object/index.d.cts +6 -0
  356. package/object/index.d.ts +6 -5
  357. package/object/index.js +5 -6
  358. package/object/index.js.map +1 -1
  359. package/object/map.cjs +5 -3
  360. package/object/map.cjs.map +1 -0
  361. package/object/map.d.cts +34 -0
  362. package/object/map.d.ts +3 -1
  363. package/object/map.js +2 -1
  364. package/object/map.js.map +1 -1
  365. package/object/static/clone.cjs +6 -4
  366. package/object/static/clone.cjs.map +1 -0
  367. package/object/static/clone.d.cts +44 -0
  368. package/object/static/clone.d.ts +3 -2
  369. package/object/static/clone.js +2 -2
  370. package/object/static/clone.js.map +1 -1
  371. package/object/static/compact.cjs +6 -4
  372. package/object/static/compact.cjs.map +1 -0
  373. package/object/static/compact.d.cts +27 -0
  374. package/object/static/compact.d.ts +2 -1
  375. package/object/static/compact.js +2 -2
  376. package/object/static/compact.js.map +1 -1
  377. package/object/static/deep-freeze.cjs +6 -4
  378. package/object/static/deep-freeze.cjs.map +1 -0
  379. package/object/static/deep-freeze.d.cts +23 -0
  380. package/object/static/deep-freeze.d.ts +18 -1
  381. package/object/static/deep-freeze.js +2 -2
  382. package/object/static/deep-freeze.js.map +1 -1
  383. package/object/static/deepest.cjs +6 -4
  384. package/object/static/deepest.cjs.map +1 -0
  385. package/object/static/deepest.d.cts +34 -0
  386. package/object/static/deepest.d.ts +3 -1
  387. package/object/static/deepest.js +2 -2
  388. package/object/static/deepest.js.map +1 -1
  389. package/object/static/map.cjs +6 -4
  390. package/object/static/map.cjs.map +1 -0
  391. package/object/static/map.d.cts +27 -0
  392. package/object/static/map.d.ts +2 -1
  393. package/object/static/map.js +2 -2
  394. package/object/static/map.js.map +1 -1
  395. package/open-promise.cjs +13 -11
  396. package/open-promise.cjs.map +1 -0
  397. package/open-promise.d.cts +82 -0
  398. package/open-promise.d.ts +5 -3
  399. package/open-promise.js +4 -2
  400. package/open-promise.js.map +1 -1
  401. package/package.json +820 -412
  402. package/promise/chain.cjs +5 -3
  403. package/promise/chain.cjs.map +1 -0
  404. package/promise/chain.d.cts +9 -0
  405. package/promise/chain.d.ts +3 -1
  406. package/promise/chain.js +2 -1
  407. package/promise/chain.js.map +1 -1
  408. package/promise/index.cjs +21 -10
  409. package/promise/index.cjs.map +1 -0
  410. package/promise/index.d.cts +7 -0
  411. package/promise/index.d.ts +7 -7
  412. package/promise/index.js +7 -8
  413. package/promise/index.js.map +1 -1
  414. package/promise/never.cjs +10 -7
  415. package/promise/never.cjs.map +1 -0
  416. package/promise/never.d.cts +36 -0
  417. package/promise/never.d.ts +4 -2
  418. package/promise/never.js +5 -3
  419. package/promise/never.js.map +1 -1
  420. package/promise/promisify.cjs +5 -3
  421. package/promise/promisify.cjs.map +1 -0
  422. package/promise/promisify.d.cts +9 -0
  423. package/promise/promisify.d.ts +3 -1
  424. package/promise/promisify.js +2 -1
  425. package/promise/promisify.js.map +1 -1
  426. package/promise/proto/revert.cjs +6 -4
  427. package/promise/proto/revert.cjs.map +1 -0
  428. package/promise/proto/revert.d.cts +28 -0
  429. package/promise/proto/revert.d.ts +1 -2
  430. package/promise/proto/revert.js +2 -2
  431. package/promise/proto/revert.js.map +1 -1
  432. package/promise/proto/try-catch.cjs +6 -4
  433. package/promise/proto/try-catch.cjs.map +1 -0
  434. package/promise/proto/try-catch.d.cts +29 -0
  435. package/promise/proto/try-catch.d.ts +1 -2
  436. package/promise/proto/try-catch.js +2 -2
  437. package/promise/proto/try-catch.js.map +1 -1
  438. package/promise/revert.cjs +5 -3
  439. package/promise/revert.cjs.map +1 -0
  440. package/promise/revert.d.cts +40 -0
  441. package/promise/revert.d.ts +3 -1
  442. package/promise/revert.js +2 -1
  443. package/promise/revert.js.map +1 -1
  444. package/promise/static/chain.cjs +6 -4
  445. package/promise/static/chain.cjs.map +1 -0
  446. package/promise/static/chain.d.cts +11 -0
  447. package/promise/static/chain.d.ts +2 -1
  448. package/promise/static/chain.js +2 -2
  449. package/promise/static/chain.js.map +1 -1
  450. package/promise/static/never.cjs +7 -5
  451. package/promise/static/never.cjs.map +1 -0
  452. package/promise/static/never.d.cts +40 -0
  453. package/promise/static/never.d.ts +2 -1
  454. package/promise/static/never.js +2 -2
  455. package/promise/static/never.js.map +1 -1
  456. package/promise/static/timeout.cjs +6 -4
  457. package/promise/static/timeout.cjs.map +1 -0
  458. package/promise/static/timeout.d.cts +22 -0
  459. package/promise/static/timeout.d.ts +2 -1
  460. package/promise/static/timeout.js +2 -2
  461. package/promise/static/timeout.js.map +1 -1
  462. package/promise/static/try-one-by-one.cjs +6 -4
  463. package/promise/static/try-one-by-one.cjs.map +1 -0
  464. package/promise/static/try-one-by-one.d.cts +28 -0
  465. package/promise/static/try-one-by-one.d.ts +2 -1
  466. package/promise/static/try-one-by-one.js +2 -2
  467. package/promise/static/try-one-by-one.js.map +1 -1
  468. package/promise/timeout.cjs +13 -11
  469. package/promise/timeout.cjs.map +1 -0
  470. package/promise/timeout.d.cts +112 -0
  471. package/promise/timeout.d.ts +5 -2
  472. package/promise/timeout.js +4 -3
  473. package/promise/timeout.js.map +1 -1
  474. package/promise/try-catch.cjs +5 -3
  475. package/promise/try-catch.cjs.map +1 -0
  476. package/promise/try-catch.d.cts +47 -0
  477. package/promise/try-catch.d.ts +4 -2
  478. package/promise/try-catch.js +2 -1
  479. package/promise/try-catch.js.map +1 -1
  480. package/promise/try-one-by-one.cjs +5 -3
  481. package/promise/try-one-by-one.cjs.map +1 -0
  482. package/promise/try-one-by-one.d.cts +44 -0
  483. package/promise/try-one-by-one.d.ts +3 -1
  484. package/promise/try-one-by-one.js +2 -1
  485. package/promise/try-one-by-one.js.map +1 -1
  486. package/set/proto/difference.cjs +6 -4
  487. package/set/proto/difference.cjs.map +1 -0
  488. package/set/proto/difference.d.cts +27 -0
  489. package/set/proto/difference.d.ts +2 -1
  490. package/set/proto/difference.js +2 -2
  491. package/set/proto/difference.js.map +1 -1
  492. package/set/proto/intersection.cjs +6 -4
  493. package/set/proto/intersection.cjs.map +1 -0
  494. package/set/proto/intersection.d.cts +27 -0
  495. package/set/proto/intersection.d.ts +2 -1
  496. package/set/proto/intersection.js +2 -2
  497. package/set/proto/intersection.js.map +1 -1
  498. package/smart-map.cjs +4 -3
  499. package/smart-map.cjs.map +1 -0
  500. package/smart-map.d.cts +82 -0
  501. package/smart-map.d.ts +3 -2
  502. package/smart-map.js +2 -1
  503. package/smart-map.js.map +1 -1
  504. package/string/index.cjs +8 -4
  505. package/string/index.cjs.map +1 -0
  506. package/string/index.d.cts +1 -0
  507. package/string/index.d.ts +1 -1
  508. package/string/index.js +1 -2
  509. package/string/index.js.map +1 -1
  510. package/string/limit.cjs +8 -5
  511. package/string/limit.cjs.map +1 -0
  512. package/string/limit.d.cts +31 -0
  513. package/string/limit.d.ts +3 -1
  514. package/string/limit.js +5 -3
  515. package/string/limit.js.map +1 -1
  516. package/string/proto/limit.cjs +6 -4
  517. package/string/proto/limit.cjs.map +1 -0
  518. package/string/proto/limit.d.cts +30 -0
  519. package/string/proto/limit.d.ts +3 -3
  520. package/string/proto/limit.js +2 -2
  521. package/string/proto/limit.js.map +1 -1
  522. package/time/days.cjs +8 -5
  523. package/time/days.cjs.map +1 -0
  524. package/time/days.d.cts +17 -0
  525. package/time/days.d.ts +3 -1
  526. package/time/days.js +4 -2
  527. package/time/days.js.map +1 -1
  528. package/time/hours.cjs +8 -5
  529. package/time/hours.cjs.map +1 -0
  530. package/time/hours.d.cts +17 -0
  531. package/time/hours.d.ts +3 -1
  532. package/time/hours.js +4 -2
  533. package/time/hours.js.map +1 -1
  534. package/time/index.cjs +20 -10
  535. package/time/index.cjs.map +1 -0
  536. package/time/index.d.cts +7 -0
  537. package/time/index.d.ts +7 -7
  538. package/time/index.js +7 -8
  539. package/time/index.js.map +1 -1
  540. package/time/minutes.cjs +8 -5
  541. package/time/minutes.cjs.map +1 -0
  542. package/time/minutes.d.cts +17 -0
  543. package/time/minutes.d.ts +3 -1
  544. package/time/minutes.js +4 -2
  545. package/time/minutes.js.map +1 -1
  546. package/time/months.cjs +8 -5
  547. package/time/months.cjs.map +1 -0
  548. package/time/months.d.cts +17 -0
  549. package/time/months.d.ts +3 -1
  550. package/time/months.js +4 -2
  551. package/time/months.js.map +1 -1
  552. package/time/seconds.cjs +5 -3
  553. package/time/seconds.cjs.map +1 -0
  554. package/time/seconds.d.cts +17 -0
  555. package/time/seconds.d.ts +3 -1
  556. package/time/seconds.js +2 -1
  557. package/time/seconds.js.map +1 -1
  558. package/time/weeks.cjs +8 -5
  559. package/time/weeks.cjs.map +1 -0
  560. package/time/weeks.d.cts +17 -0
  561. package/time/weeks.d.ts +3 -1
  562. package/time/weeks.js +4 -2
  563. package/time/weeks.js.map +1 -1
  564. package/time/years.cjs +8 -5
  565. package/time/years.cjs.map +1 -0
  566. package/time/years.d.cts +17 -0
  567. package/time/years.d.ts +3 -1
  568. package/time/years.js +4 -2
  569. package/time/years.js.map +1 -1
  570. package/unique-id.cjs +5 -3
  571. package/unique-id.cjs.map +1 -0
  572. package/unique-id.d.cts +23 -0
  573. package/unique-id.d.ts +4 -2
  574. package/unique-id.js +2 -1
  575. package/unique-id.js.map +1 -1
@@ -0,0 +1,1856 @@
1
+ /**
2
+ * Encapsulates a non array value with an array that contains it unless the value is already an array
3
+ *
4
+ * * * *
5
+ * Example usage:
6
+ * ```typescript
7
+ * import { arrayize } from "@telperion/js-utils";
8
+ *
9
+ * const foo = 'foo';
10
+ * const bar = ['bar'];
11
+ * const fooArr = arrayize(foo); // ['foo'];
12
+ * const barArr = arrayize(bar); // ['bar'];
13
+ * ```
14
+ * * * *
15
+ * @param value Array or single value to capsulate
16
+ */
17
+ function arrayize(value) {
18
+ return value instanceof Array ? value : [value];
19
+ }
20
+
21
+ /**
22
+ * #### Async Map
23
+ *
24
+ * Maps an array asynchronously
25
+ *
26
+ * * * *
27
+ * Example:
28
+ * ```typescript
29
+ * import { asyncMap } "@telperion/js-utils/array";
30
+ *
31
+ * const array = [1, 2, 3];
32
+ *
33
+ * asyncMap(array, async value => {
34
+ * return await addOneAfterASecond(value);
35
+ * }).then(result => {
36
+ * console.log(result); // [2, 3, 4]
37
+ * });
38
+ * ```
39
+ *
40
+ * Example as Array Prototype:
41
+ * ```typescript
42
+ * import "@telperion/js-utils/array/proto/async-map";
43
+ *
44
+ * const array = [1, 2, 3];
45
+ *
46
+ * const result = await array.asyncMap(async value => await addOneAfterASecond(value));
47
+ * // [2, 3, 4]
48
+ * ```
49
+ * * * *
50
+ * @param array Array to map
51
+ * @param mapper Callback async function to map the array
52
+ * @return A promise contains asynchronusly mapped array result
53
+ */
54
+ async function asyncMap(array, mapper) {
55
+ const promises = array.map(mapper);
56
+ return await Promise.all(promises);
57
+ }
58
+
59
+ /**
60
+ * #### Returns whether the entered value is truthy
61
+ *
62
+ * * * *
63
+ * Example usage:
64
+ * ```typescript
65
+ * import { isTruthy } from "@telperion/js-utils";
66
+ *
67
+ * isTruthy(undefined); // false
68
+ * isTruthy(true); // true
69
+ * isTruthy([]) // true
70
+ *
71
+ * const truthyValues = ["a", undefined, "b", "", "c"].filter(isTruthy); // ["a", "b", "c"]
72
+ * ```
73
+ * Static usage example:
74
+ * ```typescript
75
+ * import "@telperion/js-utils/boolean/static/is-truthy";
76
+ *
77
+ * Boolean.isTruthy(undefined); // false
78
+ * Boolean.isTruthy(true); // true
79
+ * Boolean.isTruthy([]) // true
80
+ *
81
+ * const truthyValues = ["a", undefined, "b", "", "c"].filter(Boolean.isTruthy); // ["a", "b", "c"]
82
+ * ```
83
+ * * * *
84
+ * @param value value to be checked
85
+ */
86
+ function isTruthy(value) {
87
+ return !!value;
88
+ }
89
+
90
+ /**
91
+ * #### Filters falsy values of the given array
92
+ * * does not modify the original array
93
+ * * Values to be filtered: `[undefined, null, "", 0, false, NaN]`
94
+ *
95
+ * * * *
96
+ * Example usage:
97
+ * ```typescript
98
+ * import { compact } from "@telperion/js-utils/array";
99
+ *
100
+ * const arr = [undefined, "", false, 0, 1, "1"];
101
+ * const compacted = compact(arr); // [1, "1"];
102
+ *
103
+ * ```
104
+ *
105
+ * Example as Array Prototype:
106
+ * ```typescript
107
+ * import "@telperion/js-utils/array/proto/compact";
108
+ *
109
+ * const arr = [undefined, "", false, 0, 1, "1"];
110
+ * const compacted = arr.compact(); // [1, "1"];
111
+ * ```
112
+ * * * *
113
+ * @param arrayToCompact array to compact
114
+ * @typeparam T type of array
115
+ * @returns __a new__ compacted array
116
+ */
117
+ function compact$2(arrayToCompact) {
118
+ return arrayToCompact.filter(isTruthy);
119
+ }
120
+
121
+ function difference(base, substraction, allDiff = false) {
122
+ if (base instanceof Set) {
123
+ return new Set(difference(Array.from(base), substraction));
124
+ }
125
+ const subs = Array.from(substraction);
126
+ return base.filter(value => {
127
+ const index = subs.indexOf(value);
128
+ if (index > -1 && allDiff) {
129
+ subs.splice(index, 1);
130
+ }
131
+ return index === -1;
132
+ });
133
+ }
134
+
135
+ /**
136
+ * #### Find By Key
137
+ *
138
+ * Finds an object in an array by matching the value set on the key
139
+ *
140
+ * * * *
141
+ * Example:
142
+ * ```typescript
143
+ * import { findByKey } from "@telperion/js-utils/array";
144
+ *
145
+ * const array = [{a: 1}, {a: 2}, {a: 3}];
146
+ *
147
+ * findByKey(array, "a", 2); // {a: 2}
148
+ * ```
149
+ *
150
+ * Prototype Example:
151
+ * ```typescript
152
+ * import "@telperion/js-utils/array/proto/find-by-key";
153
+ *
154
+ * const array = [{a: 1}, {a: 2}, {a: 3}];
155
+ *
156
+ * array.findByKey("a", 2); // {a: 2}
157
+ * ```
158
+ * * * *
159
+ * @param array Collection of the objects
160
+ * @param key Key to search the value on
161
+ * @param value Value to match for the key
162
+ * @return Found object or undefined
163
+ */
164
+ function findByKey(array, key, value) {
165
+ return array.find(item => item[key] === value);
166
+ }
167
+
168
+ /**
169
+ * #### Groups array items by a specified key into a Map
170
+ *
171
+ * Iterates over the array and groups items that share the same value for the given key.
172
+ * Returns a `Map` where each key is a unique value of the specified property and each value is an array of matching items.
173
+ *
174
+ * * * *
175
+ * Example usage:
176
+ * ```typescript
177
+ * import { gather } from "@telperion/js-utils/array";
178
+ *
179
+ * const users = [
180
+ * { role: "admin", name: "Alice" },
181
+ * { role: "user", name: "Bob" },
182
+ * { role: "admin", name: "Charlie" },
183
+ * ];
184
+ *
185
+ * const grouped = gather(users, "role");
186
+ * // Map {
187
+ * // "admin" => [{ role: "admin", name: "Alice" }, { role: "admin", name: "Charlie" }],
188
+ * // "user" => [{ role: "user", name: "Bob" }]
189
+ * // }
190
+ * ```
191
+ *
192
+ * Example as Array Prototype:
193
+ * ```typescript
194
+ * import "@telperion/js-utils/array/proto/gather";
195
+ *
196
+ * const users = [
197
+ * { role: "admin", name: "Alice" },
198
+ * { role: "user", name: "Bob" },
199
+ * { role: "admin", name: "Charlie" },
200
+ * ];
201
+ *
202
+ * const grouped = users.gather("role");
203
+ * // Map {
204
+ * // "admin" => [{ role: "admin", name: "Alice" }, { role: "admin", name: "Charlie" }],
205
+ * // "user" => [{ role: "user", name: "Bob" }]
206
+ * // }
207
+ * ```
208
+ * * * *
209
+ * @param array Array of objects to group
210
+ * @param key Key to group items by
211
+ * @typeparam T Type of array items
212
+ * @typeparam P Key type
213
+ * @returns A Map grouping items by the specified key's value
214
+ */
215
+ function gather(array, key) {
216
+ return array.reduce((acc, item) => {
217
+ const groupKey = item[key];
218
+ if (!acc.has(groupKey)) {
219
+ acc.set(groupKey, []);
220
+ }
221
+ acc.get(groupKey).push(item);
222
+ return acc;
223
+ }, new Map());
224
+ }
225
+
226
+ function intersection(base, inclusion, allEquals = true) {
227
+ if (base instanceof Set) {
228
+ return new Set(intersection(Array.from(base), inclusion));
229
+ }
230
+ const incs = Array.from(inclusion);
231
+ return base.filter(value => {
232
+ const index = incs.indexOf(value);
233
+ if (index > -1 && !allEquals) {
234
+ incs.splice(index, 1);
235
+ }
236
+ return index > -1;
237
+ });
238
+ }
239
+
240
+ /**
241
+ * #### Limit
242
+ *
243
+ * Returns first `n` children of an array
244
+ *
245
+ * * * *
246
+ * Example:
247
+ * ```typescript
248
+ * import { limit } from "@telperion/js-utils/array";
249
+ *
250
+ * const array = ["a", "b", "c", "d", "e", "f"];
251
+ *
252
+ * limit(array, 3); // ["a", "b", "c"]
253
+ * ```
254
+ *
255
+ * Prototype Example:
256
+ * ```typescript
257
+ * import "@telperion/js-utils/array/proto/limit";
258
+ *
259
+ * const array = ["a", "b", "c", "d", "e", "f"];
260
+ *
261
+ * array.limit(3); // ["a", "b", "c"]
262
+ * ```
263
+ * * * *
264
+ * @param array: Array to return its children
265
+ * @param count Limiter
266
+ * @return New array
267
+ */
268
+ function limit$2(array, count) {
269
+ if (count < 0) {
270
+ count = Number.MAX_SAFE_INTEGER;
271
+ }
272
+ return array.slice(0, count);
273
+ }
274
+
275
+ /**
276
+ * #### Remove
277
+ *
278
+ * Removes an item from the array
279
+ *
280
+ * Removes all item references if multi is set to `true`
281
+ *
282
+ * * * *
283
+ * Example:
284
+ * ```typescript
285
+ * import { remove } from "@telperion/js-utils/array";
286
+ *
287
+ * const array = ["a", "b", "c", "a", "b", "c"];
288
+ *
289
+ * remove(array, "b"); // ["a", "c", "a", "b", "c"]
290
+ * remove(array, "b", true); // ["a", "c", "a", "c"]
291
+ * ```
292
+ *
293
+ * Prototype Example:
294
+ * ```typescript
295
+ * import "@telperion/js-utils/array/proto/remove";
296
+ *
297
+ * const array = ["a", "b", "c", "a", "b", "c"];
298
+ *
299
+ * array.remove("b"); // ["a", "c", "a", "b", "c"]
300
+ * array.remove("b", true); // ["a", "c", "a", "c"]
301
+ * ```
302
+ * * * *
303
+ * @param array: Array to remove the item
304
+ * @param itemToRemove Item to remove
305
+ * @return New array
306
+ */
307
+ function remove(array, item, multi = false) {
308
+ let index = array.indexOf(item);
309
+ if (index < 0) {
310
+ return [...array];
311
+ }
312
+ do {
313
+ array = [...array.slice(0, index), ...array.slice(index + 1)];
314
+ index = array.indexOf(item);
315
+ } while (multi && index > -1);
316
+ return array;
317
+ }
318
+
319
+ /**
320
+ * Default Options for Replace Items Mode
321
+ */
322
+ const REPLACE_ITEMS_DEFAULT_OPTIONS = {
323
+ deleteCount: 1,
324
+ itemsToReplace: [],
325
+ startingIndex: 0
326
+ };
327
+ /**
328
+ * Default Options for Replace By Map Mode
329
+ */
330
+ const REPLACE_BY_MAP_DEFAULT_OPTIONS = {
331
+ itemsToReplace: new Map(),
332
+ multi: false
333
+ };
334
+ function replace(array, options, itemToReplace) {
335
+ if (arguments.length > 2) {
336
+ options = {
337
+ startingIndex: array.indexOf(options),
338
+ itemsToReplace: [itemToReplace]
339
+ };
340
+ }
341
+ if (options.itemsToReplace instanceof Array) {
342
+ return _replaceItems(array, options);
343
+ }
344
+ else {
345
+ return _replaceByMap(array, options);
346
+ }
347
+ }
348
+ /**
349
+ * Replace items by 'replace items' mode
350
+ *
351
+ * @param array Array to replace items
352
+ * @param options Replace options
353
+ */
354
+ function _replaceItems(array, options) {
355
+ const { startingIndex, deleteCount, itemsToReplace } = Object.assign({}, REPLACE_ITEMS_DEFAULT_OPTIONS, options);
356
+ const newArray = [...array];
357
+ // If item not found or index number is negative; do nothing
358
+ if (startingIndex < 0) {
359
+ return newArray;
360
+ }
361
+ newArray.splice(startingIndex, deleteCount, ...itemsToReplace);
362
+ return newArray;
363
+ }
364
+ /**
365
+ * Replace items by 'replace by map' mode
366
+ *
367
+ * @param array Array to replace items
368
+ * @param options Replace options
369
+ */
370
+ function _replaceByMap(array, options) {
371
+ const { itemsToReplace, multi } = Object.assign({}, REPLACE_BY_MAP_DEFAULT_OPTIONS, options);
372
+ const iterationMethod = multi ? Array.prototype.forEach : Array.prototype.some;
373
+ const newArray = [...array];
374
+ itemsToReplace.forEach((value, key) => {
375
+ iterationMethod.call(newArray, (item, index) => {
376
+ const matches = item === key;
377
+ if (matches) {
378
+ newArray.splice(index, 1, value);
379
+ }
380
+ return matches;
381
+ });
382
+ });
383
+ return newArray;
384
+ }
385
+
386
+ /**
387
+ * #### Uniquify
388
+ *
389
+ * Removes repeated items from the array
390
+ *
391
+ * * * *
392
+ * Example:
393
+ * ```typescript
394
+ * import { uniquify } "@telperion/js-utils/array";
395
+ *
396
+ * const array = ["a", "b", "c", "a", "b", "c"];
397
+ *
398
+ * uniquify(array); // ["a", "b", "c"]
399
+ * ```
400
+ *
401
+ * Prototype Example:
402
+ * ```typescript
403
+ * import "@telperion/js-utils/array/proto/uniquify";
404
+ *
405
+ * const array = ["a", "b", "c", "a", "b", "c"];
406
+ *
407
+ * array.uniquify(); // ["a", "b", "c"]
408
+ * ```
409
+ * * * *
410
+ * @return The new uniquified array
411
+ */
412
+ function uniquify(array) {
413
+ return [...new Set(array)];
414
+ }
415
+
416
+ /**
417
+ * #### Uniquify By Key
418
+ *
419
+ * Removes objects from the array which the value of its specifed key included before by another
420
+ *
421
+ * * * *
422
+ * Example:
423
+ * ```typescript
424
+ * import { uniquifyByKey } "@telperion/js-utils/array";
425
+ *
426
+ * const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
427
+ *
428
+ * uniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
429
+ * ```
430
+ *
431
+ * Prototype Example:
432
+ * ```typescript
433
+ * import "@telperion/js-utils/array/proto/uniquify-by-key";
434
+ *
435
+ * const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
436
+ *
437
+ * array.uniquifyByKey('a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
438
+ * ```
439
+ * * * *
440
+ * @param array Collection of the objects
441
+ * @param key Key to search the value on
442
+ * @return The new uniquified array
443
+ */
444
+ function uniquifyByKey(array, key) {
445
+ const cache = new Set();
446
+ return array.filter(item => {
447
+ if (cache.has(item[key])) {
448
+ return false;
449
+ }
450
+ cache.add(item[key]);
451
+ return true;
452
+ });
453
+ }
454
+
455
+ /* eslint-disable @typescript-eslint/no-explicit-any */
456
+ const PROPS_TO_FILTER = ['constructor'];
457
+ /**
458
+ * #### Mixin
459
+ *
460
+ * Creates a class that combines two classes using the mixin pattern.
461
+ * The resulting class extends the first class and includes all properties and methods from the second class.
462
+ *
463
+ * * * *
464
+ * Example:
465
+ * ```typescript
466
+ * import { mixin } from "@telperion/js-utils/class";
467
+ *
468
+ * class Timestamped {
469
+ * timestamp = Date.now();
470
+ * getAge() {
471
+ * return Date.now() - this.timestamp;
472
+ * }
473
+ * }
474
+ *
475
+ * class User {
476
+ * constructor(public name: string) {}
477
+ * greet() {
478
+ * return `Hello, ${this.name}`;
479
+ * }
480
+ * }
481
+ *
482
+ * class TimestampedUser extends mixin(User, Timestamped) {}
483
+ *
484
+ * const user = new TimestampedUser(['John'], []);
485
+ * console.log(user.greet()); // "Hello, John"
486
+ * console.log(user.getAge()); // Time since creation
487
+ * ```
488
+ *
489
+ * @param Base The base class to extend
490
+ * @param Mixin The mixin class to combine
491
+ * @returns A new class that combines both classes
492
+ */
493
+ function mixin(Base, Mixin) {
494
+ // @ts-ignore - TypeScript doesn't fully understand the mixin pattern
495
+ class Mixed extends Base {
496
+ constructor(baseArgs, mixinArgs) {
497
+ super(...baseArgs);
498
+ // Create an instance of the mixin class and copy its properties
499
+ const mixinInstance = new Mixin(...mixinArgs);
500
+ for (const key of Reflect.ownKeys(mixinInstance)) {
501
+ this[key] = mixinInstance[key];
502
+ }
503
+ }
504
+ }
505
+ // Copy prototype methods from mixin to the mixed class
506
+ const mixinKeys = Reflect.ownKeys(Mixin.prototype).filter((key) => !PROPS_TO_FILTER.includes(key));
507
+ for (const prop of mixinKeys) {
508
+ Object.defineProperty(Mixed.prototype, prop, Object.getOwnPropertyDescriptor(Mixin.prototype, prop));
509
+ }
510
+ return Mixed;
511
+ }
512
+
513
+ /**
514
+ * #### Call
515
+ *
516
+ * Calls the given function without arguments and returns its result.
517
+ * Handy for running an inline initializer where an expression is expected.
518
+ *
519
+ * * * *
520
+ * Example usage:
521
+ * ```typescript
522
+ * import { call } from "@telperion/js-utils/function/call";
523
+ *
524
+ * const config = call(() => {
525
+ * const base = loadDefaults();
526
+ * return { ...base, debug: true };
527
+ * });
528
+ * ```
529
+ * * * *
530
+ * @param func Function to call
531
+ * @returns The value returned by `func`
532
+ */
533
+ function call(func) {
534
+ return func();
535
+ }
536
+
537
+ /**
538
+ * #### Noop function
539
+ * * * *
540
+ * Example usage:
541
+ * ```typescript
542
+ * import { noop } from "@telperion/js-utils/function";
543
+ *
544
+ * noop();
545
+ * document.onload = noop;
546
+ * ```
547
+ * Static usage example:
548
+ * ```typescript
549
+ * import "@telperion/js-utils/function/static/noop";
550
+ *
551
+ * Function.noop();
552
+ * document.onload = Function.noop;
553
+ * ```
554
+ * * * *
555
+ */
556
+ function noop() { }
557
+
558
+ /**
559
+ * #### Open Promise
560
+ * A promise constructor to resolve or reject from outside
561
+ *
562
+ * @deprecated Use the native `Promise.withResolvers()` instead, which provides the same functionality.
563
+ *
564
+ * * * *
565
+ * Example with native alternative:
566
+ * ```typescript
567
+ * // Instead of:
568
+ * // import { OpenPromise } from "@telperion/js-utils";
569
+ * // const aPromiseWillBeResolvedLater = new OpenPromise();
570
+ *
571
+ * // Use:
572
+ * const { promise, resolve, reject } = Promise.withResolvers();
573
+ *
574
+ * promise.then(val => console.log(val));
575
+ * ...
576
+ * ...
577
+ *
578
+ * resolve({x: 1});
579
+ * ```
580
+ *
581
+ * Legacy Example:
582
+ * ```typescript
583
+ * import { OpenPromise } from "@telperion/js-utils";
584
+ *
585
+ * const aPromiseWillBeResolvedLater = new OpenPromise();
586
+ *
587
+ * aPromiseWillBeResolvedLater.then(val => console.log(val));
588
+ * // aPromiseWillBeResolvedLater.finished // false
589
+ * ...
590
+ * ...
591
+ *
592
+ * aPromiseWillBeResolvedLater.resolve({x: 1});
593
+ * // aPromiseWillBeResolvedLater.finished // true
594
+ * ```
595
+ * * * *
596
+ * @template T typeof the value which is going to be resolved
597
+ */
598
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
599
+ class OpenPromise extends Promise {
600
+ static #resolverHolder = noop;
601
+ static #rejectorHolder = noop;
602
+ #resolved = false;
603
+ #rejected = false;
604
+ #resolver = noop;
605
+ #rejector = noop;
606
+ /**
607
+ * Returns whether is the promise resolved
608
+ */
609
+ get resolved() {
610
+ return this.#resolved;
611
+ }
612
+ /**
613
+ * Returns whether is the promise rejected
614
+ */
615
+ get rejected() {
616
+ return this.#rejected;
617
+ }
618
+ /**
619
+ * Returns whether is the promise finished
620
+ */
621
+ get finished() {
622
+ return this.#resolved || this.#rejected;
623
+ }
624
+ /**
625
+ * Open Promise Constructor
626
+ */
627
+ constructor(executor = noop) {
628
+ super((resolve, reject) => {
629
+ OpenPromise.#resolverHolder = resolve;
630
+ OpenPromise.#rejectorHolder = reject;
631
+ executor(resolve, reject);
632
+ });
633
+ this.#resolver = OpenPromise.#resolverHolder;
634
+ this.#rejector = OpenPromise.#rejectorHolder;
635
+ OpenPromise.#resolverHolder = noop;
636
+ OpenPromise.#rejectorHolder = noop;
637
+ }
638
+ /**
639
+ * Resolves promise
640
+ * @param value Value to resolve the promise
641
+ */
642
+ resolve(...args) {
643
+ if (this.finished)
644
+ throw new Error('Promise is already finished');
645
+ this.#resolved = true;
646
+ this.#resolver(...args);
647
+ }
648
+ /**
649
+ * Rejects promise
650
+ * @param reason Error to reject promise
651
+ */
652
+ reject(...args) {
653
+ if (this.finished)
654
+ throw new Error('Promise is already finished');
655
+ this.#rejected = true;
656
+ this.#rejector(...args);
657
+ }
658
+ /**
659
+ * Binds a promise to the inner promise to resolve or reject with it
660
+ * @param promise A promise to bind inner promise
661
+ */
662
+ bindTo(promise) {
663
+ promise
664
+ .then(e => { this.resolve(e); })
665
+ .catch(e => this.reject(e));
666
+ }
667
+ }
668
+
669
+ // Defined here for documentation
670
+ function promiseTimeoutInitializer() {
671
+ const REFERANCE_CACHE = new WeakMap();
672
+ const KEY_CACHE = new Map();
673
+ const timeout = (time, value, key) => {
674
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
675
+ const openPromise = new OpenPromise();
676
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
677
+ const id = setTimeout(() => {
678
+ openPromise.resolve(value);
679
+ }, time);
680
+ const cacheObject = { openPromise, id };
681
+ REFERANCE_CACHE.set(openPromise, cacheObject);
682
+ if (key) {
683
+ KEY_CACHE.set(key, cacheObject);
684
+ }
685
+ return openPromise;
686
+ };
687
+ Object.defineProperties(timeout, {
688
+ FINISHED_ALREADY: {
689
+ writable: false,
690
+ value: Symbol('Timeout finished already')
691
+ },
692
+ IDENTIFIER_NOT_FOUND: {
693
+ writable: false,
694
+ value: Symbol('Timeout not found via identifier')
695
+ },
696
+ TIMEOUT_CANCELLED: {
697
+ writable: false,
698
+ value: Symbol('Timeout cancelled')
699
+ }
700
+ });
701
+ timeout.cancel = async (identifier, error) => {
702
+ const { FINISHED_ALREADY, IDENTIFIER_NOT_FOUND, TIMEOUT_CANCELLED } = timeout;
703
+ if (typeof error === 'undefined') {
704
+ error = TIMEOUT_CANCELLED;
705
+ }
706
+ let cache;
707
+ if (typeof identifier === 'symbol') {
708
+ cache = KEY_CACHE.get(identifier);
709
+ }
710
+ else {
711
+ cache = REFERANCE_CACHE.get(identifier);
712
+ }
713
+ if (!cache) {
714
+ throw IDENTIFIER_NOT_FOUND;
715
+ }
716
+ if (cache.openPromise.finished) {
717
+ throw FINISHED_ALREADY;
718
+ }
719
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
720
+ clearTimeout(cache.id);
721
+ cache.openPromise.reject(error);
722
+ if (typeof identifier === 'symbol') {
723
+ KEY_CACHE.delete(identifier);
724
+ }
725
+ };
726
+ return timeout;
727
+ }
728
+ /**
729
+ * #### Promise Timeout
730
+ * Returns a promise which resolves after given time
731
+ *
732
+ * * * *
733
+ * Example:
734
+ * ```typescript
735
+ * import { timeout } from "@telperion/js-utils/promise";
736
+ *
737
+ * timeout(1000);
738
+ * .then(() => console.log("will be logged after a second"));
739
+ * ```
740
+ * Example with a resolve value:
741
+ * ```typescript
742
+ * import { timeout } from "@telperion/js-utils/promise";
743
+ *
744
+ * timeout(1000, "foo");
745
+ * .then(val => console.log("will log 'foo' after a second", val));
746
+ * ```
747
+ * Can be used in promise chaining:
748
+ * ```typescript
749
+ * import { timeout } from "@telperion/js-utils/promise";
750
+ *
751
+ * fetch("http://localhost:8080/anEndpoint") // Fetch something
752
+ * .then(val => timeout(1000, val)) // Wait a second after response
753
+ * .then(val => {
754
+ * ...do something else
755
+ * });
756
+ * ```
757
+ * Static usage example:
758
+ * ```typescript
759
+ * import "@telperion/js-utils/promise/static/timeout";
760
+ *
761
+ * Promise.timeout(1000);
762
+ * .then(() => console.log("will be logged after a second"));
763
+ * ```
764
+ * * * *
765
+ * @see PromiseTimeoutFunction#cancel for cancelling
766
+ */
767
+ const timeout = promiseTimeoutInitializer();
768
+
769
+ /**
770
+ * Converts a promise or a value to a promise to ensure that the return value is a promise
771
+ *
772
+ * @param promiseOrValue The promise or value to convert to a promise
773
+ * @returns The promise of the given promise or value
774
+ */
775
+ async function promisify(promiseOrValue) {
776
+ return await promiseOrValue;
777
+ }
778
+
779
+ const DEFAULT_DEBOUNCE_TIME = 180;
780
+ const KEY_REFERENCES = new Map();
781
+ const CACHE$2 = new Map();
782
+ function debounceWithKey(key, callback, time = DEFAULT_DEBOUNCE_TIME, thisObject = null, ...args) {
783
+ let timeoutPromise;
784
+ let promise;
785
+ if (CACHE$2.has(key)) {
786
+ const stocked = CACHE$2.get(key);
787
+ timeoutPromise = stocked.timeout;
788
+ promise = stocked.promise;
789
+ timeout.cancel(timeoutPromise);
790
+ }
791
+ else {
792
+ promise = new OpenPromise();
793
+ }
794
+ timeoutPromise = timeout(time);
795
+ CACHE$2.set(key, { promise, timeout: timeoutPromise });
796
+ timeoutPromise
797
+ .then(() => {
798
+ CACHE$2.delete(key);
799
+ const result = callback.call(thisObject, ...args);
800
+ promise.bindTo(promisify(result));
801
+ return result;
802
+ })
803
+ .catch(err => {
804
+ if (err !== timeout.TIMEOUT_CANCELLED) {
805
+ promise.reject(err);
806
+ }
807
+ });
808
+ return promise;
809
+ }
810
+ /**
811
+ * #### Debounces a function that delays invoking until after given time have elapsed since the last time the debounced function was invoked
812
+ *
813
+ * * * *
814
+ * Example usage:
815
+ * ```typescript
816
+ * import { debounce } from "@telperion/js-utils/promise";
817
+ *
818
+ * function foo() {
819
+ * console.log("hello");
820
+ * }
821
+ *
822
+ * for (let i = 0; i < 5; i++) {
823
+ * debounce(foo);
824
+ * }
825
+ *
826
+ * // logs "hello" only once
827
+ * ```
828
+ *
829
+ * Static usage example:
830
+ * ```typescript
831
+ * import "@telperion/js-utils/function/proto/debounce";
832
+ *
833
+ * function foo() {
834
+ * console.log("hello");
835
+ * }
836
+ *
837
+ * for (let i = 0; i < 5; i++) {
838
+ * foo.debounce();
839
+ * }
840
+ *
841
+ * // logs "hello" only once
842
+ * ```
843
+ * * * *
844
+ * @param callback The function to execute only last of multiple execute requests by given time
845
+ * @param [time = 180] Time for debouncing
846
+ * @param [thisObject = null] This object to execute the callback function with
847
+ * @param args Function arguments
848
+ * @return A promise which resolves right after the debouncing sequence has been finished
849
+ */
850
+ function debounce(callback, time = DEFAULT_DEBOUNCE_TIME, thisObject = null, ...args) {
851
+ if (!KEY_REFERENCES.has(callback)) {
852
+ KEY_REFERENCES.set(callback, Symbol());
853
+ }
854
+ return debounceWithKey(KEY_REFERENCES.get(callback), callback, time, thisObject, ...args);
855
+ }
856
+
857
+ /**
858
+ * #### Delays the execution of the passed function to increase the render performance
859
+ *
860
+ * * * *
861
+ * Example usage:
862
+ * ```typescript
863
+ * import { defer } from "@telperion/js-utils/function";
864
+ *
865
+ * defer(() => aFunctionToDefer())
866
+ * .then(res => ...)
867
+ * .catch(err => ...);
868
+ * ```
869
+ * Example as promise static method
870
+ * ```typescript
871
+ * import "@telperion/js-utils/function/static/defer";
872
+ *
873
+ * Function.defer(() => aFunctionToDefer())
874
+ * .then(res => ...)
875
+ * .catch(err => ...);
876
+ * ```
877
+ * * * *
878
+ * @param callback Callback function to be executed
879
+ * @typeparam T type of the return value of the callback
880
+ * @returns A promise which resolves with the value of the callback right after the execution
881
+ */
882
+ function defer(callback = noop) {
883
+ return new Promise((resolve, reject) => {
884
+ setTimeout(() => {
885
+ try {
886
+ const returnValue = callback();
887
+ resolve(returnValue);
888
+ }
889
+ catch (error) {
890
+ reject(error);
891
+ }
892
+ }, 0);
893
+ });
894
+ }
895
+
896
+ /* eslint-disable @typescript-eslint/no-explicit-any */
897
+ // Symbols to protect overriding private methods & properties of SmartMap Class
898
+ const KEY__WEAKMAP = Symbol('SmartMap.weakMap');
899
+ /**
900
+ * ### SmartMap
901
+ *
902
+ * Like WeakMap but can also store values using primitive keys
903
+ *
904
+ * * * *
905
+ * Example usage:
906
+ * ```typescript
907
+ * import { SmartMap } from "@telperion/js-utils";
908
+ *
909
+ * const aMap = new SmartMap();
910
+ *
911
+ * aMap.set("foo", "foo");
912
+ * aMap.set(1, "thales rocks");
913
+ *
914
+ * console.log(aMap.size) // 2
915
+ *
916
+ * aMap.set({}, "thales rocks again");
917
+ * console.log(aMap.size) // 2
918
+ *
919
+ * const anObject = {};
920
+ * aMap.set(anObject, "thales rocks again and again");
921
+ * console.log(aMap.size) // 3
922
+ * console.log(aMap.get(anObject)) // "thales rocks again and again"
923
+ * ```
924
+ * * * *
925
+ * @template K Typeof Key
926
+ * @template V Typeof Value
927
+ */
928
+ class SmartMap extends Map {
929
+ /**
930
+ * Symbol refference of weakmap property
931
+ */
932
+ static KEY__WEAKMAP = KEY__WEAKMAP;
933
+ /**
934
+ * Allowed key types to store them in a weakmap instance
935
+ */
936
+ static TYPES_TO_STORE_IN_WEAKMAP = Object.freeze(['object', 'function']);
937
+ /**
938
+ * Returns a boolean whether if the key is storable in weakmap or not
939
+ *
940
+ * @param key Key to check if it can be stored in weakmap
941
+ * @returns A boolean whether if the key is storable in weakmap or not
942
+ */
943
+ static isStorableInWeakMap(key) {
944
+ return key !== null && SmartMap.TYPES_TO_STORE_IN_WEAKMAP.some(_key => typeof key === _key);
945
+ }
946
+ /**
947
+ * The WeakMap object to store values with associated non-primitive (object) keys
948
+ */
949
+ [KEY__WEAKMAP] = new WeakMap();
950
+ /**
951
+ * Returns a boolean asserting whether a value has been associated to the key in the SmartMap object or not.
952
+ *
953
+ * @param key The key of the element to test for presence in the SmartMap object.
954
+ * @returns A boolean asserting whether a value has been associated to the key in the SmartMap object or not.
955
+ */
956
+ has(key) {
957
+ if (SmartMap.isStorableInWeakMap(key)) {
958
+ return this[KEY__WEAKMAP].has(key);
959
+ }
960
+ return super.has(key);
961
+ }
962
+ /**
963
+ * Returns the value associated to the key, or undefined if there is none.
964
+ *
965
+ * @param key The key of the element to return from the SmartMap object.
966
+ * @returns The value associated to the key
967
+ */
968
+ get(key) {
969
+ if (SmartMap.isStorableInWeakMap(key)) {
970
+ return this[KEY__WEAKMAP].get(key);
971
+ }
972
+ return super.get(key);
973
+ }
974
+ /**
975
+ * Sets the value for the key in the SmartMap object.
976
+ *
977
+ * @param key The key of the element to add to the SmartMap object.
978
+ * @param value The value of the element to add to the SmartMap object.
979
+ * @returns Returns the SmartMap object.
980
+ */
981
+ set(key, value) {
982
+ if (SmartMap.isStorableInWeakMap(key)) {
983
+ this[KEY__WEAKMAP].set(key, value);
984
+ }
985
+ else {
986
+ super.set(key, value);
987
+ }
988
+ return this;
989
+ }
990
+ /**
991
+ * Removes the associated value from the SmartMap object,
992
+ *
993
+ * @param key The key of the element to remove from the SmartMap object.
994
+ * @returns `true` if an element in the SmartMap object existed and has been removed, or `false` if the element does not exist. `SmartMap.prototype.has(key)` will return false afterwards.
995
+ */
996
+ delete(key) {
997
+ return this[KEY__WEAKMAP].delete(key) || super.delete(key);
998
+ }
999
+ }
1000
+
1001
+ /**
1002
+ * Cache object to store previously created functions
1003
+ */
1004
+ const CACHE$1 = new SmartMap();
1005
+ /**
1006
+ * ### Function Of
1007
+ *
1008
+ * Creates a function which returns the value given
1009
+ *
1010
+ * * * *
1011
+ * Example:
1012
+ * ```typescript
1013
+ * import { of } from "@telperion/js-utils/function";
1014
+ *
1015
+ * const base = [1, 2, 5, {}, "x", "y"];
1016
+ * const mapTo = of("thales rocks");
1017
+ *
1018
+ * const mapped = base.map(mapTo);
1019
+ * // ["thales rocks", "thales rocks", "thales rocks", "thales rocks", "thales rocks", "thales rocks"]
1020
+ * ```
1021
+ * Function Static Example:
1022
+ * ```typescript
1023
+ * import "@telperion/js-utils/function/static/of";
1024
+ *
1025
+ * const base = [1, 2, 5, {}, "x", "y"];
1026
+ * const mapTo = Function.of("thales rocks");
1027
+ *
1028
+ * const mapped = base.map(mapTo);
1029
+ * // ["thales rocks", "thales rocks", "thales rocks", "thales rocks", "thales rocks", "thales rocks"]
1030
+ * ```
1031
+ * * * *
1032
+ * @param returnValue The value which created function returns
1033
+ * @returns A function which returns the `returnValue`
1034
+ */
1035
+ function of(returnValue) {
1036
+ if (!CACHE$1.has(returnValue)) {
1037
+ CACHE$1.set(returnValue, function () { return returnValue; });
1038
+ }
1039
+ return CACHE$1.get(returnValue);
1040
+ }
1041
+
1042
+ /**
1043
+ * #### Map Merge
1044
+ *
1045
+ * Merges two maps
1046
+ *
1047
+ * * * *
1048
+ * Example usage:
1049
+ * ```typescript
1050
+ * import { merge } from "@telperion/js-utils/map";
1051
+ *
1052
+ * const first = new Map();
1053
+ * first.set("a", 1);
1054
+ *
1055
+ * const second = new Map();
1056
+ * second.set("b", 2);
1057
+ *
1058
+ * merge(first, second); // [{key: "a", value: 1}, {key: "b", value: 2}]
1059
+ * ```
1060
+ * * * *
1061
+ * @param first First map
1062
+ * @param second Second map
1063
+ * @returns A new merged map
1064
+ */
1065
+ function merge(first, second) {
1066
+ const newMap = new Map(first);
1067
+ for (const key of second.keys()) {
1068
+ newMap.set(key, second.get(key));
1069
+ }
1070
+ return newMap;
1071
+ }
1072
+
1073
+ /**
1074
+ * #### Limits the value by specified range
1075
+ *
1076
+ * * * *
1077
+ * Example usage:
1078
+ * ```typescript
1079
+ * import { minMax } from "@telperion/js-utils/math";
1080
+ *
1081
+ * const limitedValue = minMax(200, 300, Math.random() * 1000);
1082
+ * ```
1083
+ *
1084
+ * Example as Math static:
1085
+ * ```typescript
1086
+ * import "@telperion/js-utils/math/static/min-max";
1087
+ *
1088
+ * const limitedValue = Math.minMax(200, 300, Math.random() * 1000);
1089
+ * ```
1090
+ * * * *
1091
+ * @param min minimum limit
1092
+ * @param max maximum limit
1093
+ * @param value value to limit
1094
+ * @return the limited value *(returns NaN if an argument is NaN)*
1095
+ */
1096
+ function minMax(min, max, value) {
1097
+ return Math.max(Math.min(max, value), min);
1098
+ }
1099
+
1100
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1101
+ /**
1102
+ * A placeholder to symbolize the cloning progress of `objectToClone` hasn't finished yet
1103
+ */
1104
+ const CLONING_IN_PROGRESS = Symbol('Cloning In Progress');
1105
+ /**
1106
+ * Function Map to clone specific objects types
1107
+ */
1108
+ // eslint-disable-next-line @typescript-eslint/ban-types
1109
+ const DEFAULT_INSTANCE_CLONERS = new Map();
1110
+ // Set cloning functions
1111
+ DEFAULT_INSTANCE_CLONERS.set(Array, arrayCloner);
1112
+ DEFAULT_INSTANCE_CLONERS.set(Map, mapCloner);
1113
+ DEFAULT_INSTANCE_CLONERS.set(Set, setCloner);
1114
+ DEFAULT_INSTANCE_CLONERS.set(Object, objectCloner);
1115
+ /**
1116
+ * Default Array Cloner
1117
+ */
1118
+ function arrayCloner(objectToClone, options, internalData) {
1119
+ return objectToClone.map((val, i, arr) => {
1120
+ if (options.propsToRefer.indexOf(i) > -1) {
1121
+ return val;
1122
+ }
1123
+ return cloner(val, options, { ...internalData, parent: arr, key: i });
1124
+ });
1125
+ }
1126
+ /**
1127
+ * Default Map Cloner
1128
+ */
1129
+ function mapCloner(objectToClone, options, internalData) {
1130
+ const newMap = new Map();
1131
+ intersection(options.propsToRefer, Array.from(objectToClone.keys())).forEach(key => {
1132
+ newMap.set(key, objectToClone.get(key));
1133
+ });
1134
+ difference(Array.from(objectToClone.keys()), options.propsToRefer).forEach(key => {
1135
+ newMap.set(key, cloner(objectToClone.get(key), options, { ...internalData, key, parent: newMap }));
1136
+ });
1137
+ return newMap;
1138
+ }
1139
+ /**
1140
+ * Default Set Cloner
1141
+ */
1142
+ function setCloner(objectToClone) {
1143
+ return new Set(objectToClone);
1144
+ }
1145
+ /**
1146
+ * Default Object Cloner
1147
+ */
1148
+ function objectCloner(objectToClone, options, internalData) {
1149
+ const { propsToRefer } = options;
1150
+ /**
1151
+ * New Object Clone
1152
+ */
1153
+ const _clone = {};
1154
+ /**
1155
+ * Object cloner
1156
+ * @param key incoming property of object to clone
1157
+ */
1158
+ function cloneObject(key) {
1159
+ _clone[key] = cloner(objectToClone[key], options, { ...internalData, parent: _clone, key });
1160
+ }
1161
+ const allProperties = [...Object.keys(objectToClone), ...Object.getOwnPropertySymbols(objectToClone)];
1162
+ // Pass unwanted props
1163
+ intersection(propsToRefer, allProperties).forEach(prop => {
1164
+ _clone[prop] = objectToClone[prop];
1165
+ });
1166
+ // Clone remaining properties
1167
+ difference(allProperties, propsToRefer).forEach(cloneObject);
1168
+ return _clone;
1169
+ }
1170
+ /**
1171
+ * #### Deep Clone
1172
+ *
1173
+ * A function to recursively clone objects, arrays etc. with [cloning options]{@link ICloneOptions}
1174
+ *
1175
+ * *References Functions & Symbols by default*
1176
+ *
1177
+ * * * *
1178
+ * Example:
1179
+ * ```typescript
1180
+ * import { clone } from "@telperion/js-utils/object";
1181
+ *
1182
+ * const object = {a: 1, b: {c: true, d: ["x", "y"]}};
1183
+ *
1184
+ * // Clone all
1185
+ * const clonedObject = clone(object);
1186
+ * // {a: 1, b: {c: true, d: ["x", "y"]}}
1187
+ * // object.b.d === clonedObject.b.d // false
1188
+ *
1189
+ * // Clone all but reference "d"
1190
+ * const clonedObject = clone(object, {propsToRefer: ["d"]});
1191
+ * // {a: 1, b: {c: true, d: ["x", "y"]}}
1192
+ * // object.b.d === clonedObject.b.d // true
1193
+ * ```
1194
+ *
1195
+ * Static usage example:
1196
+ * ```typescript
1197
+ * import "@telperion/js-utils/object/static/clone";
1198
+ *
1199
+ * const object = {a: 1, b: 2};
1200
+ * const clonedObject = Object.clone(object); // {a: 1, b: 2}
1201
+ * ```
1202
+ * * * *
1203
+ * @param objectToClone Object to clone
1204
+ * @param options {ICloneOptions} Cloning Options
1205
+ * @see {@link ICloneOptions} for more information.
1206
+ */
1207
+ function clone(objectToClone, { instancesToRefer = [], propsToRefer = [], valueFiltererToRefer = () => false, customCloners = new Map() } = {}) {
1208
+ /**
1209
+ * Whole options with defaults
1210
+ */
1211
+ const mergedCloners = merge(customCloners, DEFAULT_INSTANCE_CLONERS);
1212
+ const allOptions = { instancesToRefer, propsToRefer, valueFiltererToRefer, customCloners: mergedCloners };
1213
+ const finalizers = [];
1214
+ const cloned = cloner(objectToClone, allOptions, { clonedObjects: new Map(), finalizers });
1215
+ finalizers.forEach(callback => { callback(); });
1216
+ return cloned;
1217
+ }
1218
+ function cloner(objectToClone, allOptions, internalData) {
1219
+ const { instancesToRefer, valueFiltererToRefer, customCloners } = allOptions;
1220
+ const { clonedObjects, finalizers, parent, key } = internalData;
1221
+ // Pass primitives & functions
1222
+ if (objectToClone === null || !(objectToClone instanceof Object) || objectToClone instanceof Function) {
1223
+ return objectToClone;
1224
+ }
1225
+ // Pass unwanted references
1226
+ if (instancesToRefer.some(instance => objectToClone instanceof instance)) {
1227
+ return objectToClone;
1228
+ }
1229
+ // Pass filterer results
1230
+ if (valueFiltererToRefer(objectToClone)) {
1231
+ return objectToClone;
1232
+ }
1233
+ // If cloned before, skip circular cloning
1234
+ if (clonedObjects.has(objectToClone)) {
1235
+ if (clonedObjects.get(objectToClone) === CLONING_IN_PROGRESS) {
1236
+ finalizers.push(() => {
1237
+ parent[key] = clonedObjects.get(objectToClone);
1238
+ });
1239
+ return null;
1240
+ }
1241
+ return clonedObjects.get(objectToClone);
1242
+ }
1243
+ // Mark object as cloning
1244
+ clonedObjects.set(objectToClone, CLONING_IN_PROGRESS);
1245
+ // Clone
1246
+ const customCloner = Array.from(customCloners.keys()).find(val => objectToClone instanceof val);
1247
+ const result = customCloners.get(customCloner)(objectToClone, allOptions, { ...internalData });
1248
+ clonedObjects.set(objectToClone, result);
1249
+ return result;
1250
+ }
1251
+
1252
+ /**
1253
+ * #### Removes `null` and `undefined` values and their keys from an object
1254
+ *
1255
+ * Additional values can be removed by passing to an array as the second argument
1256
+ *
1257
+ * * * * *
1258
+ * Example usage:
1259
+ * ```typescript
1260
+ * import { compact } from "@telperion/js-utils/object";
1261
+ *
1262
+ * const a = {
1263
+ * x: null,
1264
+ * y: undefined,
1265
+ * z: 20
1266
+ * };
1267
+ *
1268
+ * compact(a); // {z: 20}
1269
+ *
1270
+ * ```
1271
+ * Static usage example:
1272
+ * ```typescript
1273
+ * import "@telperion/js-utils/object/static/compact";
1274
+ *
1275
+ * const a = {
1276
+ * x: null,
1277
+ * y: undefined,
1278
+ * z: 20
1279
+ * };
1280
+ *
1281
+ * Object.compact(a); // {z: 20}
1282
+ * ```
1283
+ * * * *
1284
+ * @param object Object delete empty keys
1285
+ * @param additionalValuesToRemove Other values to delete
1286
+ * @returns Compacted object
1287
+ */
1288
+ function compact$1(object, additionalValuesToRemove = []) {
1289
+ const newObject = { ...object };
1290
+ const valuesToRemove = [undefined, null, ...additionalValuesToRemove];
1291
+ for (const [key, value] of Object.entries(newObject)) {
1292
+ if (valuesToRemove.includes(value)) {
1293
+ delete newObject[key];
1294
+ }
1295
+ }
1296
+ return newObject;
1297
+ }
1298
+
1299
+ /**
1300
+ * #### Get deepest object in an object chain
1301
+ *
1302
+ * * * * *
1303
+ * Example usage:
1304
+ * ```typescript
1305
+ * import { deepest } from "@telperion/js-utils/object";
1306
+ *
1307
+ * const a = {
1308
+ * x: null
1309
+ * };
1310
+ *
1311
+ * const b = {
1312
+ * x: a
1313
+ * };
1314
+ *
1315
+ * const c = {
1316
+ * x: b
1317
+ * };
1318
+ *
1319
+ * deepest(c, 'x'); // {x: null} (a)
1320
+ *
1321
+ * ```
1322
+ * Static usage example:
1323
+ * ```typescript
1324
+ * import "@telperion/js-utils/object/static/deepest";
1325
+ *
1326
+ * const a = {
1327
+ * x: null
1328
+ * };
1329
+ *
1330
+ * const b = {
1331
+ * x: a
1332
+ * };
1333
+ *
1334
+ * const c = {
1335
+ * x: b
1336
+ * };
1337
+ *
1338
+ * Object.deepest(c, 'x'); // a
1339
+ * ```
1340
+ * * * *
1341
+ * @param object Object to deep dive
1342
+ * @param key key of the object which contains same type instance
1343
+ */
1344
+ function deepest(object, key) {
1345
+ let obj = object;
1346
+ while (obj[key]) {
1347
+ obj = obj[key];
1348
+ }
1349
+ return obj;
1350
+ }
1351
+
1352
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1353
+ /**
1354
+ * #### Deep Freeze
1355
+ *
1356
+ * Recursively freezes an object and every object or function it references,
1357
+ * so nested values can't be changed either.
1358
+ *
1359
+ * * * *
1360
+ * Example usage:
1361
+ * ```typescript
1362
+ * import { deepFreeze } from "@telperion/js-utils/object/deep-freeze";
1363
+ *
1364
+ * const config = deepFreeze({ api: { url: "https://example.com" } });
1365
+ *
1366
+ * config.api.url = "changed"; // ignored (throws in strict mode)
1367
+ * ```
1368
+ * * * *
1369
+ * @param object Object to freeze
1370
+ * @returns The same object, frozen
1371
+ */
1372
+ function deepFreeze(object) {
1373
+ // Retrieve the property names defined on object
1374
+ const propNames = Reflect.ownKeys(object);
1375
+ // Freeze properties before freezing self
1376
+ for (const name of propNames) {
1377
+ const value = object[name];
1378
+ if ((value && typeof value === "object") || typeof value === "function") {
1379
+ deepFreeze(value);
1380
+ }
1381
+ }
1382
+ return Object.freeze(object);
1383
+ }
1384
+
1385
+ /**
1386
+ * #### Maps the values of an object using a callback function
1387
+ *
1388
+ * Similar to `Array.prototype.map`, but for objects. Iterates over own enumerable properties
1389
+ * and returns a new object with the same keys and transformed values.
1390
+ *
1391
+ * * * *
1392
+ * Example usage:
1393
+ * ```typescript
1394
+ * import { map } from "@telperion/js-utils/object";
1395
+ *
1396
+ * const obj = { a: 1, b: 2, c: 3 };
1397
+ *
1398
+ * map(obj, value => value * 2); // { a: 2, b: 4, c: 6 }
1399
+ * ```
1400
+ *
1401
+ * Example with key usage:
1402
+ * ```typescript
1403
+ * import { map } from "@telperion/js-utils/object";
1404
+ *
1405
+ * const obj = { x: 1, y: 2 };
1406
+ *
1407
+ * map(obj, (value, key) => `${key}_${value}`); // { x: "x_1", y: "y_2" }
1408
+ * ```
1409
+ * * * *
1410
+ * @param object Object to map
1411
+ * @param fn Callback function to transform each value, receives the value, key, and the entire object as arguments
1412
+ * @returns A new object with the same keys and transformed values
1413
+ */
1414
+ function map(object, fn) {
1415
+ const result = {};
1416
+ for (const key in object) {
1417
+ if (object.hasOwnProperty(key)) {
1418
+ result[key] = fn(object[key], key, object);
1419
+ }
1420
+ }
1421
+ return result;
1422
+ }
1423
+
1424
+ /**
1425
+ * Chains promises in a sequential order
1426
+ *
1427
+ * @param promises The promise returning functions array in the order they should be executed
1428
+ * @returns A promise that resolves when all the promises in the array are resolved. The promise resolves with the value of the last promise in the array
1429
+ */
1430
+ function chain(promises) {
1431
+ return promises.reduce((promise, next) => promise.then(next), Promise.resolve(null));
1432
+ }
1433
+
1434
+ /**
1435
+ * A promise which never resolves
1436
+ *
1437
+ * Example:
1438
+ * ```typescript
1439
+ * import { NEVER } from '@telperion/js-utils/promise';
1440
+ *
1441
+ * function foo(promise = NEVER) {
1442
+ * promise.then(val => {
1443
+ * ...
1444
+ * });
1445
+ * }
1446
+ * ```
1447
+ */
1448
+ const NEVER = new Promise(noop);
1449
+ /**
1450
+ * Creates a promise which never resolves
1451
+ *
1452
+ * Example:
1453
+ * ```typescript
1454
+ * import { never } from '@telperion/js-utils/promise';
1455
+ *
1456
+ * function foo(promise) {
1457
+ * promise = promise || never();
1458
+ *
1459
+ * promise.then(val => {
1460
+ * ...
1461
+ * });
1462
+ * }
1463
+ * ```
1464
+ *
1465
+ * @returns the promise which never resolves
1466
+ */
1467
+ function never() {
1468
+ return NEVER;
1469
+ }
1470
+
1471
+ /**
1472
+ * #### Revert Promise
1473
+ * Exchanges resolve state with rejection of a promise
1474
+ *
1475
+ * * * *
1476
+ * Example usage:
1477
+ * ```typescript
1478
+ * import { revert } from "@telperion/js-utils/promise";
1479
+ *
1480
+ * const errorPromise = Promise.reject(new Error('foo'));
1481
+ *
1482
+ * revert(errorPromise)
1483
+ * .then(err => {
1484
+ * console.log("this will be logged", err);
1485
+ * })
1486
+ * .catch(res => {
1487
+ * console.log("this won't be logged", res);
1488
+ * });
1489
+ * ```
1490
+ *
1491
+ * Proto usage example:
1492
+ * ```typescript
1493
+ * import "@telperion/js-utils/promise/proto/revert";
1494
+ *
1495
+ * Promise.reject(new Error('foo'))
1496
+ * .revert()
1497
+ * .then(err => {
1498
+ * console.log("this will be logged", err);
1499
+ * })
1500
+ * .catch(res => {
1501
+ * console.log("this won't be logged", res);
1502
+ * });
1503
+ * ```
1504
+ * * * *
1505
+ * @param promise The promise to revert its statements
1506
+ * @return the reverted promise
1507
+ */
1508
+ function revert(promise) {
1509
+ return promise.then(res => { throw res; }, err => err);
1510
+ }
1511
+
1512
+ async function tryCatch(promise, defaultResult) {
1513
+ try {
1514
+ const result = await promise;
1515
+ return [null, result];
1516
+ }
1517
+ catch (error) {
1518
+ return [error, defaultResult ?? null];
1519
+ }
1520
+ }
1521
+
1522
+ /**
1523
+ * #### Try One By One
1524
+ *
1525
+ * Tries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished.
1526
+ *
1527
+ * * * *
1528
+ * Example:
1529
+ * ```typescript
1530
+ * import { tryOneByOne } from "@telperion/js-utils/promise";
1531
+ *
1532
+ * async function fooFunction() {
1533
+ * const foo = await tryOneByOne([
1534
+ * () => someCall(),
1535
+ * (err) => anotherCall(),
1536
+ * (err) => fooPromise()
1537
+ * ]);
1538
+ *
1539
+ * // do stuff
1540
+ * }
1541
+ *
1542
+ * ```
1543
+ *
1544
+ * Static Example:
1545
+ * ```typescript
1546
+ * import "@telperion/js-utils/promise/static/try-one-by-one";
1547
+ *
1548
+ * async function fooFunction() {
1549
+ * const foo = await Promise.tryOneByOne([
1550
+ * () => someCall(),
1551
+ * (err) => anotherCall(),
1552
+ * (err) => fooPromise()
1553
+ * ]);
1554
+ *
1555
+ * // do stuff
1556
+ * }
1557
+ *
1558
+ * ```
1559
+ * * * *
1560
+ * @param promises List of promises to try one by one with the order
1561
+ * @returns
1562
+ */
1563
+ async function tryOneByOne(promises) {
1564
+ return await tryPromises([...promises], null);
1565
+ }
1566
+ async function tryPromises(promises, lastError) {
1567
+ const promiseFn = promises.shift();
1568
+ if (!promiseFn)
1569
+ throw lastError;
1570
+ try {
1571
+ const promise = typeof promiseFn === "function" ? promiseFn(lastError) : promiseFn;
1572
+ return await promise;
1573
+ }
1574
+ catch (err) {
1575
+ return tryPromises(promises, err);
1576
+ }
1577
+ }
1578
+
1579
+ /**
1580
+ * Converts seconds to milliseconds
1581
+ *
1582
+ * #### Example
1583
+ * ```typescript
1584
+ * import { seconds } from "@telperion/js-utils/time";
1585
+ *
1586
+ * const delay = seconds(5); // 5000 milliseconds
1587
+ * setTimeout(() => console.log('Done!'), delay);
1588
+ * ```
1589
+ *
1590
+ * @param value - Number of seconds
1591
+ * @returns Equivalent value in milliseconds
1592
+ */
1593
+ function seconds(value) {
1594
+ return value * 1000;
1595
+ }
1596
+
1597
+ /**
1598
+ * Converts minutes to milliseconds
1599
+ *
1600
+ * #### Example
1601
+ * ```typescript
1602
+ * import { minutes } from "@telperion/js-utils/time";
1603
+ *
1604
+ * const delay = minutes(2); // 120000 milliseconds
1605
+ * setTimeout(() => console.log('Done!'), delay);
1606
+ * ```
1607
+ *
1608
+ * @param value - Number of minutes
1609
+ * @returns Equivalent value in milliseconds
1610
+ */
1611
+ function minutes(value) {
1612
+ return seconds(value) * 60;
1613
+ }
1614
+
1615
+ /**
1616
+ * Converts hours to milliseconds
1617
+ *
1618
+ * #### Example
1619
+ * ```typescript
1620
+ * import { hours } from "@telperion/js-utils/time";
1621
+ *
1622
+ * const delay = hours(1); // 3600000 milliseconds
1623
+ * setTimeout(() => console.log('Done!'), delay);
1624
+ * ```
1625
+ *
1626
+ * @param value - Number of hours
1627
+ * @returns Equivalent value in milliseconds
1628
+ */
1629
+ function hours(value) {
1630
+ return minutes(value) * 60;
1631
+ }
1632
+
1633
+ /**
1634
+ * Converts days to milliseconds
1635
+ *
1636
+ * #### Example
1637
+ * ```typescript
1638
+ * import { days } from "@telperion/js-utils/time";
1639
+ *
1640
+ * const expiryTime = Date.now() + days(7); // 7 days from now
1641
+ * console.log(new Date(expiryTime));
1642
+ * ```
1643
+ *
1644
+ * @param value - Number of days
1645
+ * @returns Equivalent value in milliseconds
1646
+ */
1647
+ function days(value) {
1648
+ return hours(value) * 24;
1649
+ }
1650
+
1651
+ /**
1652
+ * Converts weeks to milliseconds
1653
+ *
1654
+ * #### Example
1655
+ * ```typescript
1656
+ * import { weeks } from "@telperion/js-utils/time";
1657
+ *
1658
+ * const expiryTime = Date.now() + weeks(2); // 2 weeks from now
1659
+ * console.log(new Date(expiryTime));
1660
+ * ```
1661
+ *
1662
+ * @param value - Number of weeks
1663
+ * @returns Equivalent value in milliseconds
1664
+ */
1665
+ function weeks(value) {
1666
+ return days(value) * 7;
1667
+ }
1668
+
1669
+ /**
1670
+ * Converts months to milliseconds (assumes 30 days per month)
1671
+ *
1672
+ * #### Example
1673
+ * ```typescript
1674
+ * import { months } from "@telperion/js-utils/time";
1675
+ *
1676
+ * const expiryTime = Date.now() + months(3); // ~3 months from now
1677
+ * console.log(new Date(expiryTime));
1678
+ * ```
1679
+ *
1680
+ * @param value - Number of months
1681
+ * @returns Equivalent value in milliseconds (30 days per month)
1682
+ */
1683
+ function months(value) {
1684
+ return days(value) * 30;
1685
+ }
1686
+
1687
+ /**
1688
+ * Converts years to milliseconds (assumes 365 days per year)
1689
+ *
1690
+ * #### Example
1691
+ * ```typescript
1692
+ * import { years } from "@telperion/js-utils/time";
1693
+ *
1694
+ * const expiryTime = Date.now() + years(1); // ~1 year from now
1695
+ * console.log(new Date(expiryTime));
1696
+ * ```
1697
+ *
1698
+ * @param value - Number of years
1699
+ * @returns Equivalent value in milliseconds (365 days per year)
1700
+ */
1701
+ function years(value) {
1702
+ return days(value) * 365;
1703
+ }
1704
+
1705
+ /**
1706
+ * #### Compact
1707
+ *
1708
+ * Filters falsy values of the given array
1709
+ * Removes `null` and `undefined` values and their keys from an object
1710
+ *
1711
+ * * * *
1712
+ * Example usage:
1713
+ * ```typescript
1714
+ * import { compact } from "@telperion/js-utils";
1715
+ *
1716
+ * const arr = [undefined, "", false, 0, 1, "1"];
1717
+ * const compacted = compact(arr); // [1, "1"];
1718
+ *
1719
+ * const object = {
1720
+ * x: null,
1721
+ * y: undefined,
1722
+ * z: 20
1723
+ * };
1724
+ *
1725
+ * const compacted = compact(object); // {z: 20}
1726
+ * ```
1727
+ * * * *
1728
+ * @param arrayOrObject Array or Object to compact
1729
+ */
1730
+ function compact(arrayOrObject) {
1731
+ if (arrayOrObject instanceof Array) {
1732
+ return compact$2(arrayOrObject);
1733
+ }
1734
+ if (arrayOrObject instanceof Object) {
1735
+ return compact$1(arrayOrObject);
1736
+ }
1737
+ throw new TypeError('Value is not object nor array');
1738
+ }
1739
+
1740
+ /**
1741
+ * #### Returns whether the entered value is falsy
1742
+ *
1743
+ * * * *
1744
+ * Example usage:
1745
+ * ```typescript
1746
+ * import { isFalsy } from "@telperion/js-utils";
1747
+ *
1748
+ * isFalsy(undefined); // true
1749
+ * isFalsy(true); // false
1750
+ * isFalsy([]) // false
1751
+ *
1752
+ * const falsyValues = ["a", undefined, "b", "", "c"].filter(isFalsy); // [undefined, ""]
1753
+ * ```
1754
+ * Static usage example:
1755
+ * ```typescript
1756
+ * import "@telperion/js-utils/boolean/static/is-falsy";
1757
+ *
1758
+ * Boolean.isFalsy(undefined); // true
1759
+ * Boolean.isFalsy(true); // false
1760
+ * Boolean.isFalsy([]) // false
1761
+ *
1762
+ * const falsyValues = ["a", undefined, "b", "", "c"].filter(Boolean.isFalsy); // [undefined, ""]
1763
+ * ```
1764
+ * * * *
1765
+ * @param value value to be checked
1766
+ */
1767
+ function isFalsy(value) {
1768
+ return !value;
1769
+ }
1770
+
1771
+ const CACHE = {};
1772
+ /**
1773
+ * Unique Id Generator
1774
+ *
1775
+ * @param {string} [prefix]
1776
+ */
1777
+ function* uniqueIdGenerator(prefix) {
1778
+ let counter = 0;
1779
+ while (true) {
1780
+ yield prefix ? `${prefix}-${counter++}` : counter++;
1781
+ }
1782
+ }
1783
+ function uniqueId(prefix) {
1784
+ if (!prefix || typeof prefix !== 'string') {
1785
+ prefix = undefined;
1786
+ }
1787
+ if (!(prefix in CACHE)) {
1788
+ CACHE[prefix] = uniqueIdGenerator(prefix);
1789
+ }
1790
+ return CACHE[prefix].next().value;
1791
+ }
1792
+
1793
+ /**
1794
+ * #### Limit
1795
+ *
1796
+ * Limits the string to `n` character
1797
+ *
1798
+ * * * *
1799
+ * Example:
1800
+ * ```typescript
1801
+ * import { limit } from "@telperion/js-utils/string";
1802
+ *
1803
+ * const str = 'foobarbaz';
1804
+ *
1805
+ * limit(str, 3); // 'foo'
1806
+ * ```
1807
+ *
1808
+ * Prototype Example:
1809
+ * ```typescript
1810
+ * import "@telperion/js-utils/string/proto/limit";
1811
+ *
1812
+ * const str = 'foobarbaz';
1813
+ *
1814
+ * str.limit(3); // 'foo'
1815
+ * ```
1816
+ * * * *
1817
+ * @param str: String to limit
1818
+ * @param count Count to limit string character size
1819
+ * @return Limited string
1820
+ */
1821
+ function limit$1(str, count) {
1822
+ return limit$2(str.split(''), count).join('');
1823
+ }
1824
+
1825
+ /**
1826
+ * #### Limit
1827
+ *
1828
+ * Limits the string or array to `n` character
1829
+ *
1830
+ * * * *
1831
+ * Example usage:
1832
+ * ```typescript
1833
+ * import { limit } from "@telperion/js-utils";
1834
+ *
1835
+ * const str = 'foobarbaz';
1836
+ * const array = ["a", "b", "c", "d", "e", "f"];
1837
+ *
1838
+ * limit(str, 3); // 'foo'
1839
+ * limit(array, 3); // ["a", "b", "c"]
1840
+ * ```
1841
+ * * * *
1842
+ * @param arrayOrString value to limit
1843
+ * @param count Max count
1844
+ */
1845
+ function limit(arrayOrString, count) {
1846
+ if (typeof arrayOrString === 'string') {
1847
+ return limit$1(arrayOrString, count);
1848
+ }
1849
+ if (arrayOrString instanceof Array) {
1850
+ return limit$2(arrayOrString, count);
1851
+ }
1852
+ throw new Error('Value is not string nor array');
1853
+ }
1854
+
1855
+ export { DEFAULT_DEBOUNCE_TIME, NEVER, OpenPromise, SmartMap, arrayize, asyncMap, call, chain, clone, compact, days, debounce, debounceWithKey, deepFreeze, deepest, defer, difference, findByKey, gather, hours, intersection, isFalsy, isTruthy, limit, map, merge, minMax, minutes, mixin, months, never, noop, of, promisify, remove, replace, revert, seconds, timeout, tryCatch, tryOneByOne, uniqueId, uniquify, uniquifyByKey, weeks, years };
1856
+ //# sourceMappingURL=js-utils.mjs.map