@ezez/utils 4.0.0 → 4.2.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.
Files changed (481) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +1 -0
  3. package/dist/cap.d.ts.map +1 -1
  4. package/dist/cap.js.map +1 -1
  5. package/dist/capitalize.d.ts.map +1 -1
  6. package/dist/coalesce.d.ts.map +1 -1
  7. package/dist/coalesce.js.map +1 -1
  8. package/dist/compareArrays.d.ts.map +1 -1
  9. package/dist/compareProps.d.ts.map +1 -1
  10. package/dist/compareProps.js.map +1 -1
  11. package/dist/deserialize.d.ts.map +1 -1
  12. package/dist/deserialize.js.map +1 -1
  13. package/dist/ensureArray.d.ts.map +1 -1
  14. package/dist/ensureArray.js.map +1 -1
  15. package/dist/ensureDate.d.ts.map +1 -1
  16. package/dist/ensureDate.js.map +1 -1
  17. package/dist/ensureError.d.ts.map +1 -1
  18. package/dist/ensureError.js.map +1 -1
  19. package/dist/ensurePrefix.d.ts.map +1 -1
  20. package/dist/ensurePrefix.js.map +1 -1
  21. package/dist/ensureSuffix.d.ts.map +1 -1
  22. package/dist/ensureSuffix.js.map +1 -1
  23. package/dist/ensureTimestamp.d.ts.map +1 -1
  24. package/dist/ensureTimestamp.js.map +1 -1
  25. package/dist/escapeRegExp.d.ts.map +1 -1
  26. package/dist/escapeRegExp.js +1 -1
  27. package/dist/escapeRegExp.js.map +1 -1
  28. package/dist/formatDate.d.ts.map +1 -1
  29. package/dist/formatDate.js +1 -1
  30. package/dist/formatDate.js.map +1 -1
  31. package/dist/get.d.ts.map +1 -1
  32. package/dist/get.js.map +1 -1
  33. package/dist/getMultiple.d.ts.map +1 -1
  34. package/dist/getMultiple.js.map +1 -1
  35. package/dist/index.d.ts +2 -0
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +2 -0
  38. package/dist/index.js.map +1 -1
  39. package/dist/insertSeparator.d.ts.map +1 -1
  40. package/dist/insertSeparator.js.map +1 -1
  41. package/dist/isEmpty.d.ts.map +1 -1
  42. package/dist/isEmpty.js.map +1 -1
  43. package/dist/isNumericString.d.ts.map +1 -1
  44. package/dist/isNumericString.js +1 -1
  45. package/dist/isNumericString.js.map +1 -1
  46. package/dist/isPlainObject.d.ts.map +1 -1
  47. package/dist/last.d.ts.map +1 -1
  48. package/dist/later.d.ts.map +1 -1
  49. package/dist/mapAsync.d.ts.map +1 -1
  50. package/dist/mapAsync.js.map +1 -1
  51. package/dist/mapValues.d.ts.map +1 -1
  52. package/dist/mapValues.js.map +1 -1
  53. package/dist/match.d.ts.map +1 -1
  54. package/dist/match.js.map +1 -1
  55. package/dist/memoize.d.ts +3 -0
  56. package/dist/memoize.d.ts.map +1 -0
  57. package/dist/memoize.js +19 -0
  58. package/dist/memoize.js.map +1 -0
  59. package/dist/merge.d.ts +11 -7
  60. package/dist/merge.d.ts.map +1 -1
  61. package/dist/merge.js.map +1 -1
  62. package/dist/mostFrequent.d.ts.map +1 -1
  63. package/dist/mostFrequent.js.map +1 -1
  64. package/dist/occurrences.d.ts.map +1 -1
  65. package/dist/occurrences.js.map +1 -1
  66. package/dist/omit.d.ts.map +1 -1
  67. package/dist/omit.js.map +1 -1
  68. package/dist/pick.d.ts.map +1 -1
  69. package/dist/pick.js.map +1 -1
  70. package/dist/pull.d.ts.map +1 -1
  71. package/dist/pull.js.map +1 -1
  72. package/dist/remove.d.ts.map +1 -1
  73. package/dist/remove.js +1 -1
  74. package/dist/remove.js.map +1 -1
  75. package/dist/removeCommonProperties.d.ts.map +1 -1
  76. package/dist/removeCommonProperties.js +3 -1
  77. package/dist/removeCommonProperties.js.map +1 -1
  78. package/dist/replace.d.ts.map +1 -1
  79. package/dist/replace.js +1 -1
  80. package/dist/replace.js.map +1 -1
  81. package/dist/replaceDeep.d.ts.map +1 -1
  82. package/dist/replaceDeepByFn.d.ts.map +1 -1
  83. package/dist/replaceDeepByFn.js.map +1 -1
  84. package/dist/rethrow.d.ts.map +1 -1
  85. package/dist/retry.d.ts +9 -0
  86. package/dist/retry.d.ts.map +1 -0
  87. package/dist/retry.js +26 -0
  88. package/dist/retry.js.map +1 -0
  89. package/dist/round.d.ts.map +1 -1
  90. package/dist/safe.js +1 -2
  91. package/dist/safe.js.map +1 -1
  92. package/dist/sample.d.ts +3 -1
  93. package/dist/sample.d.ts.map +1 -1
  94. package/dist/sample.js.map +1 -1
  95. package/dist/samples.d.ts.map +1 -1
  96. package/dist/samples.js.map +1 -1
  97. package/dist/scale.d.ts.map +1 -1
  98. package/dist/seq.d.ts.map +1 -1
  99. package/dist/seq.js +1 -1
  100. package/dist/seq.js.map +1 -1
  101. package/dist/serialize.d.ts.map +1 -1
  102. package/dist/serialize.js.map +1 -1
  103. package/dist/set.d.ts.map +1 -1
  104. package/dist/set.js.map +1 -1
  105. package/dist/setImmutable.d.ts.map +1 -1
  106. package/dist/setImmutable.js.map +1 -1
  107. package/dist/shuffle.d.ts.map +1 -1
  108. package/dist/sortBy.d.ts.map +1 -1
  109. package/dist/sortBy.js.map +1 -1
  110. package/dist/sortProps.d.ts.map +1 -1
  111. package/dist/sortProps.js.map +1 -1
  112. package/dist/stripPrefix.d.ts.map +1 -1
  113. package/dist/stripPrefix.js.map +1 -1
  114. package/dist/stripSuffix.d.ts.map +1 -1
  115. package/dist/stripSuffix.js.map +1 -1
  116. package/dist/throttle.d.ts.map +1 -1
  117. package/dist/throttle.js.map +1 -1
  118. package/dist/toggle.d.ts.map +1 -1
  119. package/dist/toggle.js.map +1 -1
  120. package/dist/trim.d.ts.map +1 -1
  121. package/dist/trimEnd.d.ts.map +1 -1
  122. package/dist/trimEnd.js.map +1 -1
  123. package/dist/trimStart.d.ts.map +1 -1
  124. package/dist/trimStart.js.map +1 -1
  125. package/dist/truthy.d.ts +1 -1
  126. package/dist/truthy.d.ts.map +1 -1
  127. package/dist/unique.d.ts.map +1 -1
  128. package/dist/wait.d.ts.map +1 -1
  129. package/dist/waitFor.d.ts.map +1 -1
  130. package/dist/waitFor.js +1 -1
  131. package/dist/waitFor.js.map +1 -1
  132. package/dist/waitSync.d.ts.map +1 -1
  133. package/dist/waitSync.js.map +1 -1
  134. package/docs/assets/icons.js +18 -0
  135. package/docs/assets/icons.svg +1 -0
  136. package/docs/assets/main.js +5 -3
  137. package/docs/assets/navigation.js +1 -0
  138. package/docs/assets/search.js +1 -1
  139. package/docs/assets/style.css +614 -400
  140. package/docs/documents/CHANGELOG.html +340 -0
  141. package/docs/functions/index.cap.html +5 -0
  142. package/docs/functions/index.capitalize.html +13 -0
  143. package/docs/functions/index.coalesce.html +16 -0
  144. package/docs/functions/index.compareArrays.html +8 -0
  145. package/docs/functions/index.compareProps.html +11 -0
  146. package/docs/functions/index.deserialize.html +6 -0
  147. package/docs/functions/index.ensureArray.html +3 -0
  148. package/docs/functions/index.ensureDate.html +10 -0
  149. package/docs/functions/index.ensureError.html +14 -0
  150. package/docs/functions/index.ensurePrefix.html +10 -0
  151. package/docs/functions/index.ensureSuffix.html +10 -0
  152. package/docs/functions/index.ensureTimestamp.html +10 -0
  153. package/docs/functions/index.escapeRegExp.html +7 -0
  154. package/docs/functions/index.formatDate.html +6 -0
  155. package/docs/functions/index.get.html +26 -0
  156. package/docs/functions/index.getMultiple.html +16 -0
  157. package/docs/functions/index.insertSeparator.html +7 -0
  158. package/docs/functions/index.isEmpty.html +33 -0
  159. package/docs/functions/index.isNumericString.html +5 -0
  160. package/docs/functions/index.isPlainObject.html +18 -0
  161. package/docs/functions/index.last.html +15 -0
  162. package/docs/functions/index.later-1.html +6 -0
  163. package/docs/functions/index.mapAsync.html +8 -0
  164. package/docs/functions/index.mapValues.html +14 -0
  165. package/docs/functions/index.match.html +4 -0
  166. package/docs/functions/index.memoize.html +8 -0
  167. package/docs/functions/index.merge.html +13 -0
  168. package/docs/functions/index.mostFrequent.html +2 -0
  169. package/docs/functions/index.noop.html +2 -0
  170. package/docs/functions/index.occurrences.html +14 -0
  171. package/docs/functions/index.omit.html +18 -0
  172. package/docs/functions/index.pick.html +13 -0
  173. package/docs/functions/index.pull.html +2 -0
  174. package/docs/functions/index.remove.html +2 -0
  175. package/docs/functions/index.removeCommonProperties.html +18 -0
  176. package/docs/functions/index.replace.html +10 -0
  177. package/docs/functions/index.replaceDeep.html +15 -0
  178. package/docs/functions/index.rethrow.html +2 -0
  179. package/docs/functions/index.retry.html +4 -0
  180. package/docs/functions/index.round.html +7 -0
  181. package/docs/functions/index.safe.html +7 -0
  182. package/docs/functions/index.sample.html +6 -0
  183. package/docs/functions/index.samples.html +7 -0
  184. package/docs/functions/index.scale.html +5 -0
  185. package/docs/functions/index.seq.html +17 -0
  186. package/docs/functions/index.seqEarlyBreak.html +12 -0
  187. package/docs/functions/index.serialize.html +14 -0
  188. package/docs/functions/index.set.html +26 -0
  189. package/docs/functions/index.setImmutable.html +25 -0
  190. package/docs/functions/index.shuffle.html +3 -0
  191. package/docs/functions/index.sortBy.html +6 -0
  192. package/docs/functions/index.sortProps.html +10 -0
  193. package/docs/functions/index.stripPrefix.html +4 -0
  194. package/docs/functions/index.stripSuffix.html +4 -0
  195. package/docs/functions/index.throttle.html +14 -0
  196. package/docs/functions/index.toggle.html +5 -0
  197. package/docs/functions/index.trim.html +8 -0
  198. package/docs/functions/index.trimEnd.html +8 -0
  199. package/docs/functions/index.trimStart.html +8 -0
  200. package/docs/functions/index.truthy.html +7 -0
  201. package/docs/functions/index.unique.html +6 -0
  202. package/docs/functions/index.wait.html +3 -0
  203. package/docs/functions/index.waitFor.html +10 -0
  204. package/docs/functions/index.waitSync.html +3 -0
  205. package/docs/index.html +19 -207
  206. package/docs/interfaces/index.ComparePropsOptions.html +5 -0
  207. package/docs/interfaces/index.GetMultipleSource.html +3 -0
  208. package/docs/interfaces/index.GetSource.html +3 -0
  209. package/docs/interfaces/index.IsNumericStringOptions.html +5 -0
  210. package/docs/interfaces/index.OccurencesOptions.html +3 -0
  211. package/docs/interfaces/index.SetImmutableSource.html +3 -0
  212. package/docs/interfaces/index.SetSource.html +3 -0
  213. package/docs/interfaces/index.ThrottleOptions.html +5 -0
  214. package/docs/interfaces/index.ThrottledFunctionExtras.html +5 -0
  215. package/docs/modules/index.html +87 -0
  216. package/docs/modules.html +3 -223
  217. package/docs/types/index.CustomDeserializers.html +1 -0
  218. package/docs/types/index.CustomSerializers.html +1 -0
  219. package/docs/types/index.Later.html +4 -0
  220. package/docs/types/index.MapValuesFn.html +14 -0
  221. package/docs/types/index.MatchCallback.html +1 -0
  222. package/docs/types/index.MergeTwo.html +3 -0
  223. package/docs/types/index.SeqEarlyBreaker.html +4 -0
  224. package/docs/types/index.SeqFn.html +3 -0
  225. package/docs/types/index.SeqFunctions.html +4 -0
  226. package/docs/types/index.SetImmutablePath.html +4 -0
  227. package/docs/types/index.ThrottledFunction.html +1 -0
  228. package/docs/variables/index.mapValuesUNSET.html +2 -0
  229. package/docs/variables/index.mergeUNSET.html +4 -0
  230. package/esm/cap.d.ts.map +1 -1
  231. package/esm/cap.js.map +1 -1
  232. package/esm/capitalize.d.ts.map +1 -1
  233. package/esm/coalesce.d.ts.map +1 -1
  234. package/esm/coalesce.js.map +1 -1
  235. package/esm/compareArrays.d.ts.map +1 -1
  236. package/esm/compareProps.d.ts.map +1 -1
  237. package/esm/compareProps.js.map +1 -1
  238. package/esm/deserialize.d.ts.map +1 -1
  239. package/esm/deserialize.js.map +1 -1
  240. package/esm/ensureArray.d.ts.map +1 -1
  241. package/esm/ensureArray.js.map +1 -1
  242. package/esm/ensureDate.d.ts.map +1 -1
  243. package/esm/ensureDate.js.map +1 -1
  244. package/esm/ensureError.d.ts.map +1 -1
  245. package/esm/ensureError.js.map +1 -1
  246. package/esm/ensurePrefix.d.ts.map +1 -1
  247. package/esm/ensurePrefix.js.map +1 -1
  248. package/esm/ensureSuffix.d.ts.map +1 -1
  249. package/esm/ensureSuffix.js.map +1 -1
  250. package/esm/ensureTimestamp.d.ts.map +1 -1
  251. package/esm/ensureTimestamp.js.map +1 -1
  252. package/esm/escapeRegExp.d.ts.map +1 -1
  253. package/esm/escapeRegExp.js +1 -1
  254. package/esm/escapeRegExp.js.map +1 -1
  255. package/esm/formatDate.d.ts.map +1 -1
  256. package/esm/formatDate.js +1 -1
  257. package/esm/formatDate.js.map +1 -1
  258. package/esm/get.d.ts.map +1 -1
  259. package/esm/get.js.map +1 -1
  260. package/esm/getMultiple.d.ts.map +1 -1
  261. package/esm/getMultiple.js.map +1 -1
  262. package/esm/index.d.ts +2 -0
  263. package/esm/index.d.ts.map +1 -1
  264. package/esm/index.js +2 -0
  265. package/esm/index.js.map +1 -1
  266. package/esm/insertSeparator.d.ts.map +1 -1
  267. package/esm/insertSeparator.js.map +1 -1
  268. package/esm/isEmpty.d.ts.map +1 -1
  269. package/esm/isEmpty.js.map +1 -1
  270. package/esm/isNumericString.d.ts.map +1 -1
  271. package/esm/isNumericString.js +1 -1
  272. package/esm/isNumericString.js.map +1 -1
  273. package/esm/isPlainObject.d.ts.map +1 -1
  274. package/esm/last.d.ts.map +1 -1
  275. package/esm/later.d.ts.map +1 -1
  276. package/esm/mapAsync.d.ts.map +1 -1
  277. package/esm/mapAsync.js.map +1 -1
  278. package/esm/mapValues.d.ts.map +1 -1
  279. package/esm/mapValues.js.map +1 -1
  280. package/esm/match.d.ts.map +1 -1
  281. package/esm/match.js.map +1 -1
  282. package/esm/memoize.d.ts +3 -0
  283. package/esm/memoize.d.ts.map +1 -0
  284. package/esm/memoize.js +16 -0
  285. package/esm/memoize.js.map +1 -0
  286. package/esm/merge.d.ts +11 -7
  287. package/esm/merge.d.ts.map +1 -1
  288. package/esm/merge.js.map +1 -1
  289. package/esm/mostFrequent.d.ts.map +1 -1
  290. package/esm/mostFrequent.js.map +1 -1
  291. package/esm/occurrences.d.ts.map +1 -1
  292. package/esm/occurrences.js.map +1 -1
  293. package/esm/omit.d.ts.map +1 -1
  294. package/esm/omit.js.map +1 -1
  295. package/esm/pick.d.ts.map +1 -1
  296. package/esm/pick.js.map +1 -1
  297. package/esm/pull.d.ts.map +1 -1
  298. package/esm/pull.js.map +1 -1
  299. package/esm/remove.d.ts.map +1 -1
  300. package/esm/remove.js +1 -1
  301. package/esm/remove.js.map +1 -1
  302. package/esm/removeCommonProperties.d.ts.map +1 -1
  303. package/esm/removeCommonProperties.js +3 -1
  304. package/esm/removeCommonProperties.js.map +1 -1
  305. package/esm/replace.d.ts.map +1 -1
  306. package/esm/replace.js +1 -1
  307. package/esm/replace.js.map +1 -1
  308. package/esm/replaceDeep.d.ts.map +1 -1
  309. package/esm/replaceDeepByFn.d.ts.map +1 -1
  310. package/esm/replaceDeepByFn.js.map +1 -1
  311. package/esm/rethrow.d.ts.map +1 -1
  312. package/esm/retry.d.ts +9 -0
  313. package/esm/retry.d.ts.map +1 -0
  314. package/esm/retry.js +23 -0
  315. package/esm/retry.js.map +1 -0
  316. package/esm/round.d.ts.map +1 -1
  317. package/esm/safe.js.map +1 -1
  318. package/esm/sample.d.ts +3 -1
  319. package/esm/sample.d.ts.map +1 -1
  320. package/esm/sample.js.map +1 -1
  321. package/esm/samples.d.ts.map +1 -1
  322. package/esm/samples.js.map +1 -1
  323. package/esm/scale.d.ts.map +1 -1
  324. package/esm/seq.d.ts.map +1 -1
  325. package/esm/seq.js +1 -1
  326. package/esm/seq.js.map +1 -1
  327. package/esm/serialize.d.ts.map +1 -1
  328. package/esm/serialize.js.map +1 -1
  329. package/esm/set.d.ts.map +1 -1
  330. package/esm/set.js.map +1 -1
  331. package/esm/setImmutable.d.ts.map +1 -1
  332. package/esm/setImmutable.js.map +1 -1
  333. package/esm/shuffle.d.ts.map +1 -1
  334. package/esm/sortBy.d.ts.map +1 -1
  335. package/esm/sortBy.js.map +1 -1
  336. package/esm/sortProps.d.ts.map +1 -1
  337. package/esm/sortProps.js.map +1 -1
  338. package/esm/stripPrefix.d.ts.map +1 -1
  339. package/esm/stripPrefix.js.map +1 -1
  340. package/esm/stripSuffix.d.ts.map +1 -1
  341. package/esm/stripSuffix.js.map +1 -1
  342. package/esm/throttle.d.ts.map +1 -1
  343. package/esm/throttle.js.map +1 -1
  344. package/esm/toggle.d.ts.map +1 -1
  345. package/esm/toggle.js.map +1 -1
  346. package/esm/trim.d.ts.map +1 -1
  347. package/esm/trimEnd.d.ts.map +1 -1
  348. package/esm/trimEnd.js.map +1 -1
  349. package/esm/trimStart.d.ts.map +1 -1
  350. package/esm/trimStart.js.map +1 -1
  351. package/esm/truthy.d.ts +1 -1
  352. package/esm/truthy.d.ts.map +1 -1
  353. package/esm/unique.d.ts.map +1 -1
  354. package/esm/wait.d.ts.map +1 -1
  355. package/esm/waitFor.d.ts.map +1 -1
  356. package/esm/waitFor.js +1 -1
  357. package/esm/waitFor.js.map +1 -1
  358. package/esm/waitSync.d.ts.map +1 -1
  359. package/esm/waitSync.js.map +1 -1
  360. package/package.json +10 -17
  361. package/pnpm-lock.yaml +4831 -3701
  362. package/src/compareProps.ts +9 -2
  363. package/src/deserialize.spec.ts +1 -0
  364. package/src/escapeRegExp.spec.ts +5 -3
  365. package/src/escapeRegExp.ts +3 -2
  366. package/src/formatDate.ts +2 -2
  367. package/src/get.spec.ts +2 -9
  368. package/src/getMultiple.ts +1 -0
  369. package/src/index.ts +2 -0
  370. package/src/isNumericString.ts +3 -3
  371. package/src/mapValues.spec.ts +1 -2
  372. package/src/memoize.spec.ts +90 -0
  373. package/src/memoize.ts +35 -0
  374. package/src/merge.ts +30 -8
  375. package/src/mostFrequent.ts +1 -0
  376. package/src/remove.ts +1 -1
  377. package/src/removeCommonProperties.ts +4 -2
  378. package/src/replace.ts +1 -1
  379. package/src/rethrow.ts +1 -1
  380. package/src/retry.ts +53 -0
  381. package/src/sample.spec.ts +1 -0
  382. package/src/sample.ts +18 -2
  383. package/src/samples.ts +1 -1
  384. package/src/seq.spec.ts +6 -3
  385. package/src/seq.ts +1 -1
  386. package/src/serialize.spec.ts +1 -0
  387. package/src/serialize.ts +1 -1
  388. package/src/set.ts +1 -1
  389. package/src/setImmutable.ts +1 -1
  390. package/src/sortBy.ts +1 -1
  391. package/src/throttle.ts +1 -1
  392. package/src/waitFor.spec.ts +7 -2
  393. package/src/waitFor.ts +1 -1
  394. package/typedoc.cjs +2 -4
  395. package/docs/assets/pages.css +0 -14
  396. package/docs/functions/cap.html +0 -152
  397. package/docs/functions/capitalize.html +0 -157
  398. package/docs/functions/coalesce.html +0 -162
  399. package/docs/functions/compareArrays.html +0 -165
  400. package/docs/functions/compareProps.html +0 -164
  401. package/docs/functions/deserialize.html +0 -156
  402. package/docs/functions/ensureArray.html +0 -150
  403. package/docs/functions/ensureDate.html +0 -152
  404. package/docs/functions/ensureError.html +0 -156
  405. package/docs/functions/ensurePrefix.html +0 -154
  406. package/docs/functions/ensureSuffix.html +0 -154
  407. package/docs/functions/ensureTimestamp.html +0 -152
  408. package/docs/functions/escapeRegExp.html +0 -148
  409. package/docs/functions/formatDate.html +0 -150
  410. package/docs/functions/get.html +0 -174
  411. package/docs/functions/getMultiple.html +0 -165
  412. package/docs/functions/insertSeparator.html +0 -159
  413. package/docs/functions/isEmpty.html +0 -175
  414. package/docs/functions/isNumericString.html +0 -149
  415. package/docs/functions/isPlainObject.html +0 -159
  416. package/docs/functions/last.html +0 -162
  417. package/docs/functions/later-1.html +0 -147
  418. package/docs/functions/mapAsync.html +0 -180
  419. package/docs/functions/mapValues.html +0 -166
  420. package/docs/functions/match.html +0 -158
  421. package/docs/functions/merge.html +0 -500
  422. package/docs/functions/mostFrequent.html +0 -147
  423. package/docs/functions/noop.html +0 -137
  424. package/docs/functions/occurrences.html +0 -161
  425. package/docs/functions/omit.html +0 -169
  426. package/docs/functions/pick.html +0 -165
  427. package/docs/functions/pull.html +0 -149
  428. package/docs/functions/remove.html +0 -165
  429. package/docs/functions/removeCommonProperties.html +0 -164
  430. package/docs/functions/replace.html +0 -154
  431. package/docs/functions/replaceDeep.html +0 -170
  432. package/docs/functions/rethrow.html +0 -142
  433. package/docs/functions/round.html +0 -151
  434. package/docs/functions/safe.html +0 -192
  435. package/docs/functions/sample.html +0 -149
  436. package/docs/functions/samples.html +0 -159
  437. package/docs/functions/scale.html +0 -153
  438. package/docs/functions/seq.html +0 -164
  439. package/docs/functions/seqEarlyBreak.html +0 -162
  440. package/docs/functions/serialize.html +0 -161
  441. package/docs/functions/set.html +0 -174
  442. package/docs/functions/setImmutable.html +0 -173
  443. package/docs/functions/shuffle.html +0 -149
  444. package/docs/functions/sortBy.html +0 -171
  445. package/docs/functions/sortProps.html +0 -159
  446. package/docs/functions/stripPrefix.html +0 -148
  447. package/docs/functions/stripSuffix.html +0 -148
  448. package/docs/functions/throttle.html +0 -167
  449. package/docs/functions/toggle.html +0 -154
  450. package/docs/functions/trim.html +0 -152
  451. package/docs/functions/trimEnd.html +0 -152
  452. package/docs/functions/trimStart.html +0 -152
  453. package/docs/functions/truthy.html +0 -154
  454. package/docs/functions/unique.html +0 -152
  455. package/docs/functions/wait.html +0 -145
  456. package/docs/functions/waitFor.html +0 -164
  457. package/docs/functions/waitSync.html +0 -145
  458. package/docs/interfaces/ComparePropsOptions.html +0 -70
  459. package/docs/interfaces/GetMultipleSource.html +0 -142
  460. package/docs/interfaces/GetSource.html +0 -142
  461. package/docs/interfaces/IsNumericStringOptions.html +0 -91
  462. package/docs/interfaces/OccurencesOptions.html +0 -72
  463. package/docs/interfaces/SetImmutableSource.html +0 -142
  464. package/docs/interfaces/SetSource.html +0 -142
  465. package/docs/interfaces/ThrottleOptions.html +0 -81
  466. package/docs/interfaces/ThrottledFunctionExtras.html +0 -97
  467. package/docs/pages/CHANGELOG.html +0 -973
  468. package/docs/pages/Introduction.html +0 -130
  469. package/docs/types/CustomDeserializers.html +0 -159
  470. package/docs/types/CustomSerializers.html +0 -159
  471. package/docs/types/Later.html +0 -173
  472. package/docs/types/MapValuesFn.html +0 -170
  473. package/docs/types/MatchCallback.html +0 -149
  474. package/docs/types/SeqEarlyBreaker.html +0 -150
  475. package/docs/types/SeqFn.html +0 -148
  476. package/docs/types/SeqFunctions.html +0 -141
  477. package/docs/types/SetImmutablePath.html +0 -135
  478. package/docs/types/ThrottledFunction.html +0 -149
  479. package/docs/variables/mapValuesUNSET.html +0 -137
  480. package/docs/variables/mergeUNSET.html +0 -139
  481. package/tutorials/Introduction.md +0 -1
@@ -1,13 +1,20 @@
1
1
  import { unique } from "./unique.js";
2
2
 
3
+ /**
4
+ * Options for `compareProps` function.
5
+ */
3
6
  interface ComparePropsOptions {
7
+ /**
8
+ * If `true`, missing properties in the first object will be considered as equal to explicit `undefined` properties
9
+ * in the second.
10
+ */
4
11
  missingEqualsUndefined?: boolean;
5
12
  }
6
13
 
7
14
  /**
8
15
  * Compare two objects and return an array of the properties that are different.
9
- * By default, if first object doesn't have a property at all and second object has it defined as `undefined` it will be
10
- * considered as a difference. To change this behavior set `missingEqualsUndefined` option to `true`.
16
+ * By default, if the first object doesn't have a property at all and the second object has it defined as `undefined`
17
+ * this will be considered as a difference. To change this behavior, set `missingEqualsUndefined` option to `true`.
11
18
  * @param a - first object
12
19
  * @param b - second object
13
20
  * @param options - options
@@ -5,6 +5,7 @@ import { deserialize } from "./deserialize";
5
5
  class Person {
6
6
  public name: string;
7
7
 
8
+ // eslint-disable-next-line @typescript-eslint/no-shadow
8
9
  public constructor(name: string) {
9
10
  this.name = name;
10
11
  }
@@ -3,7 +3,7 @@ import { escapeRegExp } from "./escapeRegExp.js";
3
3
  describe("escapeRegExp", () => {
4
4
  it("it should escape dot", () => {
5
5
  const userName = ".";
6
- const regex = new RegExp(`^(maciek|${escapeRegExp(userName)})$`);
6
+ const regex = new RegExp(`^(maciek|${escapeRegExp(userName)})$`, "u");
7
7
  const result = regex.test("maciek");
8
8
  result.must.be.true();
9
9
 
@@ -22,10 +22,12 @@ describe("escapeRegExp", () => {
22
22
 
23
23
  it("should escape complex string", () => {
24
24
  const userName = "([{^|";
25
- const fn = () => new RegExp(`^(maciek|${escapeRegExp(userName)})$`);
25
+ const fn = () => new RegExp(`^(maciek|${escapeRegExp(userName)})$`, "u");
26
26
  fn.must.not.throw();
27
27
 
28
- const fnNoEscape = () => new RegExp(`^(maciek|${userName})$`);
28
+ const fnNoEscape = () => new RegExp(`^(maciek|${userName})$`, "u");
29
29
  fnNoEscape.must.throw();
30
30
  });
31
+
32
+ // @TODO add tests for missing u flag, test if this flag changed anything in fn behavior
31
33
  });
@@ -2,14 +2,15 @@
2
2
  * Escapes a string to be used in a regular expression.
3
3
  * From: https://stackoverflow.com/a/3561711
4
4
  * @param string - string to escape
5
- * @example ```typescript
5
+ * @example
6
+ * ```typescript
6
7
  * const badName = "([{^|";
7
8
  * const regex = new RegExp(`^(maciek|${escapeRegExp(badName)})$`); // won't crash
8
9
  * regex.test(badName); // true
9
10
  * ```
10
11
  */
11
12
  const escapeRegExp = (string: string) => {
12
- return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&");
13
+ return string.replace(/[/\-\\^$*+?.()|[\]{}]/gu, "\\$&");
13
14
  };
14
15
 
15
16
  export {
package/src/formatDate.ts CHANGED
@@ -9,9 +9,9 @@ import { ensureDate } from "./ensureDate.js";
9
9
  * Important: this is work in progress. All locale-based values are not supported yet. Formats that heavily depend on
10
10
  * locale won't ever be supported.
11
11
  */
12
- const formatDate = (date: Date | number, format: string, locale = "en-US"): string => { // eslint-disable-line max-lines-per-function, max-len
12
+ const formatDate = (date: Date | number, format: string, locale = "en-US"): string => { // eslint-disable-line max-lines-per-function
13
13
  const d = ensureDate(date);
14
- return format.replace(/%([%a-zA-Z])/g, (_, c) => {
14
+ return format.replace(/%([%a-zA-Z])/gu, (_, c) => {
15
15
  switch (c) {
16
16
  case "%": return "%";
17
17
  // Day
package/src/get.spec.ts CHANGED
@@ -48,17 +48,16 @@ describe("get", () => {
48
48
  const value = get(testObject, "product.name");
49
49
  value.must.equal("abc");
50
50
 
51
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
52
51
  const _value = _get(testObject, "product.name");
53
52
  value.must.equal(_value);
54
53
  });
55
54
 
56
55
  it("doesn't parse string as js code, behaves NOT like lodash", () => {
57
56
  // Lodash intention-guessing example, inconsistency detected
57
+ // eslint-disable-next-line @typescript-eslint/no-shadow
58
58
  const name = get(testObject, "product[name]");
59
59
  (name === undefined).must.be.true();
60
60
 
61
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
62
61
  const _name = _get(testObject, "product[name]"); // deep value
63
62
  _name.must.equal("abc");
64
63
 
@@ -73,7 +72,7 @@ describe("get", () => {
73
72
  const value = get(testObject, "product.something");
74
73
  (value === undefined).must.be.true();
75
74
 
76
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
75
+ // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
77
76
  const _value = _get(testObject, "product.something");
78
77
  (_value === undefined).must.be.true();
79
78
  });
@@ -82,7 +81,6 @@ describe("get", () => {
82
81
  const value = get(testObject, "product.something", 3);
83
82
  value.must.equal(3);
84
83
 
85
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
86
84
  const _value = _get(testObject, "product.something", 3);
87
85
  _value.must.equal(3);
88
86
  });
@@ -91,7 +89,6 @@ describe("get", () => {
91
89
  const value = get(testObject, "product.value", 3);
92
90
  (value === undefined).must.be.true();
93
91
 
94
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
95
92
  const _value = _get(testObject, "product.value", 3);
96
93
  _value.must.equal(3);
97
94
  });
@@ -100,7 +97,6 @@ describe("get", () => {
100
97
  const value = get(testObject, "product.empty", 3);
101
98
  (value === null).must.be.true();
102
99
 
103
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
104
100
  const _value = _get(testObject, "product.empty", 3);
105
101
  (_value === null).must.be.true();
106
102
  });
@@ -109,7 +105,6 @@ describe("get", () => {
109
105
  const value = get(testObject, "product.notANumber", 3);
110
106
  value.must.be.nan();
111
107
 
112
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
113
108
  const _value = _get(testObject, "product.notANumber", 3);
114
109
  _value.must.be.nan();
115
110
  });
@@ -118,7 +113,6 @@ describe("get", () => {
118
113
  const value = get(testObject, "product.object");
119
114
  value.must.equal(otherObject);
120
115
 
121
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
122
116
  const _value = _get(testObject, "product.object");
123
117
  _value.must.equal(otherObject);
124
118
  });
@@ -143,7 +137,6 @@ describe("get", () => {
143
137
  const value = get(NaN, "product.object", 3);
144
138
  value.must.equal(3);
145
139
 
146
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
147
140
  const _value = _get(NaN, "product.object", 3);
148
141
  _value.must.equal(3);
149
142
  });
@@ -29,6 +29,7 @@ const DEFAULT = {};
29
29
  * @returns {*} - found value or default value
30
30
  */
31
31
  const getMultiple = (source: Source, defaultValue: unknown, ...paths: (string | string[])[]): unknown => {
32
+ // eslint-disable-next-line @typescript-eslint/no-shadow
32
33
  const length = paths.length;
33
34
  for (let i = 0; i < length; i++) {
34
35
  const properties = paths[i]!;
package/src/index.ts CHANGED
@@ -24,6 +24,7 @@ export * from "./later.js";
24
24
  export * from "./mapAsync.js";
25
25
  export * from "./mapValues.js";
26
26
  export * from "./match.js";
27
+ export * from "./memoize.js";
27
28
  export * from "./merge.js";
28
29
  export * from "./mostFrequent.js";
29
30
  export * from "./noop.js";
@@ -36,6 +37,7 @@ export * from "./removeCommonProperties.js";
36
37
  export * from "./replace.js";
37
38
  export * from "./replaceDeep.js";
38
39
  export * from "./rethrow.js";
40
+ export * from "./retry.js";
39
41
  export * from "./round.js";
40
42
  export * from "./safe.js";
41
43
  export * from "./sample.js";
@@ -14,10 +14,10 @@ const NOT_FOUND = -1;
14
14
  * @param {object} [options] - Options object
15
15
  * @param {boolean} [options.allowFloats=false] - Allow floats
16
16
  * @param {boolean} [options.allowExponents=false] - Allow exponents
17
- * @param {boolean} [options.allowInfinity=false] - Allow Initity and -Infinity (casing matters)
17
+ * @param {boolean} [options.allowInfinity=false] - Allow Infinity and -Infinity (casing matters)
18
18
  * @param {boolean} [options.allowNaN=false] - Allow NaN (casing matters)
19
19
  */
20
- const isNumericString = (string: string, options: Options = {}) => { // eslint-disable-line max-statements, max-lines-per-function, max-len
20
+ const isNumericString = (string: string, options: Options = {}) => { // eslint-disable-line max-statements, max-lines-per-function
21
21
  if (typeof string !== "string") {
22
22
  throw new TypeError("Expected a string");
23
23
  }
@@ -82,7 +82,7 @@ const isNumericString = (string: string, options: Options = {}) => { // eslint-d
82
82
  }
83
83
  }
84
84
 
85
- return Boolean(/^[\de.+-]+$/.exec(normalized));
85
+ return Boolean(/^[\de.+-]+$/u.exec(normalized));
86
86
  };
87
87
 
88
88
  export {
@@ -165,9 +165,8 @@ describe("mapValues", () => {
165
165
 
166
166
  const res = mapValues(array, (val) => (typeof val === "string" ? REMOVE : val));
167
167
 
168
- // eslint-disable-next-line no-sparse-arrays
169
168
  res.must.eql([
170
- 1, , 1, , 69,
169
+ 1, , 1, , 69, // eslint-disable-line no-sparse-arrays
171
170
  ]);
172
171
  ("0" in res).must.be.true();
173
172
  ("1" in res).must.be.false();
@@ -0,0 +1,90 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-shadow
2
+ import must from "must";
3
+
4
+ import createSpy from "../test/createSpy";
5
+
6
+ import { memoize } from "./memoize";
7
+
8
+ describe("memoize", () => {
9
+ it("should memoize the result if called with the same arguments", async () => {
10
+ const sum = createSpy((a: number, b: number) => a + b);
11
+ const memoizedSum = memoize(sum);
12
+
13
+ must(memoizedSum(1, 2)).equal(3);
14
+ must(sum.__spy.calls.length).equal(1);
15
+ must(memoizedSum(1, 2)).equal(3);
16
+ must(memoizedSum(1, 2)).equal(3);
17
+ must(memoizedSum(1, 2)).equal(3);
18
+ must(sum.__spy.calls.length).equal(1);
19
+ });
20
+
21
+ it("should trigger the function again if arguments are changed", async () => {
22
+ const sum = createSpy((a: number, b: number) => a + b);
23
+ const memoizedSum = memoize(sum);
24
+
25
+ must(memoizedSum(1, 2)).equal(3);
26
+ must(sum.__spy.calls.length).equal(1);
27
+ must(memoizedSum(1, 2)).equal(3);
28
+ must(sum.__spy.calls.length).equal(1);
29
+ must(memoizedSum(1, 3)).equal(4);
30
+ must(sum.__spy.calls.length).equal(2);
31
+ });
32
+
33
+ it("should remember only one value", () => {
34
+ const sum = createSpy((a: number, b: number) => a + b);
35
+ const memoizedSum = memoize(sum);
36
+
37
+ must(memoizedSum(1, 2)).equal(3);
38
+ must(sum.__spy.calls.length).equal(1);
39
+ must(memoizedSum(1, 2)).equal(3);
40
+ must(sum.__spy.calls.length).equal(1);
41
+ must(memoizedSum(1, 3)).equal(4);
42
+ must(sum.__spy.calls.length).equal(2);
43
+ must(memoizedSum(1, 2)).equal(3);
44
+ must(sum.__spy.calls.length).equal(3);
45
+ });
46
+
47
+ it("should recalc if arguments amount changes", async () => {
48
+ const sum = createSpy((a: number, b: number) => a + b);
49
+ const memoizedSum = memoize(sum);
50
+
51
+ must(memoizedSum(1, 2)).equal(3);
52
+ must(sum.__spy.calls.length).equal(1);
53
+ must(memoizedSum(1, 2, 0)).equal(3);
54
+ must(sum.__spy.calls.length).equal(2);
55
+ must(memoizedSum(1, 2)).equal(3);
56
+ must(sum.__spy.calls.length).equal(3);
57
+ });
58
+
59
+ it("works with common pitfall values", async () => {
60
+ const sum = createSpy((a: number, b: number) => a + b);
61
+ const memoizedSum = memoize(sum);
62
+
63
+ memoizedSum(1, NaN);
64
+ must(sum.__spy.calls.length).equal(1);
65
+ memoizedSum(1, NaN);
66
+ must(sum.__spy.calls.length).equal(1);
67
+
68
+ memoizedSum(1, 0);
69
+ must(sum.__spy.calls.length).equal(2);
70
+ memoizedSum(1, -0);
71
+ must(sum.__spy.calls.length).equal(3);
72
+ });
73
+
74
+ it("compares by reference", async () => {
75
+ const fn = createSpy((a: unknown) => null);
76
+ const memoizedFn = memoize(fn);
77
+
78
+ const arr: never[] = [];
79
+
80
+ memoizedFn([]);
81
+ must(fn.__spy.calls.length).equal(1);
82
+ memoizedFn([]);
83
+ must(fn.__spy.calls.length).equal(2);
84
+
85
+ memoizedFn(arr);
86
+ must(fn.__spy.calls.length).equal(3);
87
+ memoizedFn(arr);
88
+ must(fn.__spy.calls.length).equal(3);
89
+ });
90
+ });
package/src/memoize.ts ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Memoizes a function, caching the result of the last call.
3
+ * If the function is called with the same arguments (and the same context!) again, it will return the cached result.
4
+ *
5
+ * It caches only the last call, so it's not suitable for functions that are called with different arguments in a short
6
+ * time.
7
+ *
8
+ * Warning: Your function must be 100% pure, and it can't access anything dynamic outside its scope.
9
+ *
10
+ * @param fn - function to memoize
11
+ * @returns memoized function
12
+ */
13
+ const memoize = <Args extends unknown[], Ret>(fn: (...args: Args) => Ret) => {
14
+ let lastArgs: Args | undefined, lastResult: Ret, lastThis: unknown;
15
+
16
+ return function memoized(this: unknown, ...args: Args): Ret {
17
+ if (
18
+ lastThis === this
19
+ && lastArgs?.length === args.length
20
+ && args.every((arg, i) => Object.is(arg, lastArgs![i]))
21
+ ) {
22
+ return lastResult;
23
+ }
24
+
25
+ lastThis = this;
26
+ lastArgs = args;
27
+ lastResult = fn.apply(this, args);
28
+
29
+ return lastResult;
30
+ };
31
+ };
32
+
33
+ export {
34
+ memoize,
35
+ };
package/src/merge.ts CHANGED
@@ -1,19 +1,37 @@
1
1
  /**
2
2
  * Special value that can be set as a value in an object.
3
- * If this value is merged with previous value - the property in a final object will be removed;
3
+ * If this value is merged with the previous value, the property in a final object will be removed;
4
4
  * @see {@link merge}
5
5
  */
6
6
  const UNSET = typeof Symbol !== "undefined" ? Symbol("UNSET") : {};
7
7
 
8
+ /**
9
+ * Merges two objects into one. If the same property is present in both objects, the value from the second object is
10
+ * used.
11
+ */
12
+ type MergeTwo<T, U> = {
13
+ [K in keyof T | keyof U]: K extends keyof U
14
+ ? U[K]
15
+ : K extends keyof T
16
+ ? T[K]
17
+ : never;
18
+ };
19
+
8
20
  interface Merge {
9
21
  <A>(a: A): A;
10
- <A, B>(a: A, b: B): A & B;
11
- <A, B, C>(a: A, b: B, c: C): A & B & C;
12
- <A, B, C, D>(a: A, b: B, c: C, d: D): A & B & C & D;
13
- <A, B, C, D, E>(a: A, b: B, c: C, d: D, e: E): A & B & C & D & E;
14
- <A, B, C, D, E, F>(a: A, b: B, c: C, d: D, e: E, f: F): A & B & C & D & E & F;
15
- <A, B, C, D, E, F, G>(a: A, b: B, c: C, d: D, e: E, f: F, g: G): A & B & C & D & E & F & G;
16
- <A, B, C, D, E, F, G, H>(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H): A & B & C & D & E & F & G & H;
22
+ <A, B>(a: A, b: B): MergeTwo<A, B>;
23
+ <A, B, C>(a: A, b: B, c: C): MergeTwo<MergeTwo<A, B>, C>;
24
+ <A, B, C, D>(a: A, b: B, c: C, d: D): MergeTwo<MergeTwo<MergeTwo<A, B>, C>, D>;
25
+ <A, B, C, D, E>(a: A, b: B, c: C, d: D, e: E): MergeTwo<MergeTwo<MergeTwo<MergeTwo<A, B>, C>, D>, E>;
26
+ <A, B, C, D, E, F>(a: A, b: B, c: C, d: D, e: E, f: F): MergeTwo<
27
+ MergeTwo<MergeTwo<MergeTwo<MergeTwo<A, B>, C>, D>, E>, F
28
+ >;
29
+ <A, B, C, D, E, F, G>(a: A, b: B, c: C, d: D, e: E, f: F, g: G): MergeTwo<
30
+ MergeTwo<MergeTwo<MergeTwo<MergeTwo<MergeTwo<A, B>, C>, D>, E>, F>, G
31
+ >;
32
+ <A, B, C, D, E, F, G, H>(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H): MergeTwo<
33
+ MergeTwo<MergeTwo<MergeTwo<MergeTwo<MergeTwo<MergeTwo<A, B>, C>, D>, E>, F>, G>, H
34
+ >;
17
35
  <A, B extends object>(a: A, ...args: B[]): unknown;
18
36
  }
19
37
 
@@ -56,3 +74,7 @@ export {
56
74
  merge,
57
75
  UNSET as mergeUNSET,
58
76
  };
77
+
78
+ export type {
79
+ MergeTwo,
80
+ };
@@ -3,6 +3,7 @@
3
3
  * @param {Array} array
4
4
  */
5
5
  const mostFrequent = <T>(array: T[]): T => {
6
+ // eslint-disable-next-line @typescript-eslint/no-shadow
6
7
  let top = 0,
7
8
  topValue = array[0];
8
9
 
package/src/remove.ts CHANGED
@@ -18,7 +18,7 @@ const remove = <T>(array: T[], fn: (elem: T, num: number, list: T[]) => boolean)
18
18
  }
19
19
  }
20
20
 
21
- idsToRemove.forEach(id => array.splice(id, 1));
21
+ idsToRemove.forEach(id => { array.splice(id, 1); });
22
22
 
23
23
  return removedValues;
24
24
  };
@@ -28,8 +28,10 @@ const removeCommonProperties = (source: Obj, target1: Obj, ...targetN: Obj[]) =>
28
28
  for (const key of keys) {
29
29
  const allTheSame = targets.every((target) => JSON.stringify(target[key]) === JSON.stringify(source[key]));
30
30
  if (allTheSame) {
31
- // eslint-disable-next-line no-param-reassign,@typescript-eslint/no-dynamic-delete
32
- targets.forEach((target) => delete target[key]);
31
+ targets.forEach((target) => {
32
+ // eslint-disable-next-line no-param-reassign,@typescript-eslint/no-dynamic-delete
33
+ delete target[key];
34
+ });
33
35
  }
34
36
  }
35
37
  };
package/src/replace.ts CHANGED
@@ -12,7 +12,7 @@ const replace = (source: string, replaceMap: Record<string, string>) => {
12
12
  if (keys.length === 0) {
13
13
  return source;
14
14
  }
15
- const regex = new RegExp(keys.map(escapeRegExp).join("|"), "g");
15
+ const regex = new RegExp(keys.map(escapeRegExp).join("|"), "gu");
16
16
  return source.replace(regex, (matched) => replaceMap[matched]!);
17
17
  };
18
18
 
package/src/rethrow.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * @param {*} e
4
4
  */
5
5
  const rethrow = (e: unknown) => {
6
- // eslint-disable-next-line @typescript-eslint/no-throw-literal
6
+ // eslint-disable-next-line @typescript-eslint/only-throw-error
7
7
  throw e;
8
8
  };
9
9
 
package/src/retry.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { wait } from "./wait";
2
+
3
+ type EarlyBreaker = (error: unknown, count: number) => boolean;
4
+ type Options = {
5
+ /**
6
+ * Maximum number of retries. If not specified, the function will retry indefinitely.
7
+ * 0 means the function will be executed once (no retries)
8
+ * 1 means the function will be executed twice (1 retry), etc.
9
+ */
10
+ maxRetries?: number;
11
+ /**
12
+ * Function to determine if the function should be retried, based on the error that was thrown and the number of retries so far.
13
+ */
14
+ earlyBreak?: EarlyBreaker;
15
+ /**
16
+ * Number of milliseconds to wait between retries.
17
+ */
18
+ waitBetween?: number | ((retriesCount: number) => number);
19
+ };
20
+
21
+ /**
22
+ * Execute a function until it succeeds. Limit the retries, wait between retries, control when to stop trying early.
23
+ * @param fn - The function to execute.
24
+ * @param options - Options controlling the behavior of the function.
25
+ */
26
+ const retry = async <T>(fn: () => Promise<T>, options?: Options): Promise<T> => {
27
+ const maxRetries = options?.maxRetries ?? Infinity;
28
+ const earlyBreak = options?.earlyBreak ?? (() => false);
29
+ const waitBetween = options?.waitBetween ?? 0;
30
+
31
+ let retriesCount = 0;
32
+
33
+ while (true) { // eslint-disable-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
34
+ try {
35
+ return await fn();
36
+ }
37
+ catch (e: unknown) {
38
+ if (retriesCount >= maxRetries || earlyBreak(e, retriesCount)) {
39
+ throw e; // eslint-disable-line @typescript-eslint/only-throw-error
40
+ }
41
+ retriesCount++;
42
+ if (waitBetween) {
43
+ await wait(typeof waitBetween === "function" ? waitBetween(retriesCount) : waitBetween);
44
+ }
45
+ }
46
+ }
47
+ };
48
+
49
+ export {
50
+ retry,
51
+ };
52
+
53
+ // TODO unit tests
@@ -25,6 +25,7 @@ describe("sample", () => {
25
25
 
26
26
  it("should work with 0 items array", async () => {
27
27
  const array: never[] = [];
28
+ // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
28
29
  const item = sample(array);
29
30
  must(item).equal(undefined);
30
31
  });
package/src/sample.ts CHANGED
@@ -1,9 +1,25 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
+ type IsTuple<T> = T extends readonly [infer A, ...(infer B)] ? true : false;
3
+ type MaybeTupleReturn<T> = IsTuple<T> extends true
4
+ ? (T extends readonly (infer U)[] ? U : never)
5
+ : (T extends (infer U)[] ? U | undefined : never);
6
+
1
7
  /**
2
8
  * Picks a random element from an array.
3
9
  * @param array - source array
10
+ *
11
+ * @example
12
+ * sample([1, 2, 3]); // 2 (type: number | undefined)
13
+ * sample(["a", "b", "c"]); // "c" (type: string | undefined)
14
+ * sample([]); // undefined (type: undefined)
15
+ *
16
+ * const a = [1, 2, 3] as const;
17
+ * sample(a); // 2 (type: 1 | 2 | 3)
18
+ *
19
+ * sample([1, 1, 1] as [number, number, number]); // 1 (type: number)
4
20
  */
5
- const sample = <T>(array: T[]): T => {
6
- return array[Math.floor(Math.random() * array.length)]!;
21
+ const sample = <X, T extends readonly X[]>(array: T): MaybeTupleReturn<T> => {
22
+ return array[Math.floor(Math.random() * array.length)] as MaybeTupleReturn<T>;
7
23
  };
8
24
 
9
25
  export {
package/src/samples.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  * @param elementsToPick - number of elements to pick
9
9
  * @param allowShuffle - if true, it will shuffle the values if elementsToPick is greater or equal to array length instead of returning the original array
10
10
  */
11
- const samples = <T>(array: T[], elementsToPick: number, allowShuffle = false): T[] => { // eslint-disable-line max-statements,max-len
11
+ const samples = <T>(array: T[], elementsToPick: number, allowShuffle = false): T[] => { // eslint-disable-line max-statements
12
12
  if (elementsToPick < 0) {
13
13
  throw new Error("elementsToPick must be a positive number");
14
14
  }
package/src/seq.spec.ts CHANGED
@@ -119,17 +119,20 @@ describe("seq", () => {
119
119
  const list = [
120
120
  () => {
121
121
  firstCalled = true;
122
- // eslint-disable-next-line prefer-promise-reject-errors
122
+
123
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
123
124
  return Promise.reject(1);
124
125
  },
125
126
  () => {
126
127
  secondCalled = true;
127
- // eslint-disable-next-line prefer-promise-reject-errors
128
+
129
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
128
130
  return Promise.reject(2);
129
131
  },
130
132
  () => {
131
133
  thirdCalled = true;
132
- // eslint-disable-next-line prefer-promise-reject-errors
134
+
135
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
133
136
  return Promise.reject(3);
134
137
  },
135
138
  ];
package/src/seq.ts CHANGED
@@ -38,7 +38,7 @@ const run = <T>(list: Fn<T>[], earlyBreaker?: EarlyBreaker): Promise<T> => {
38
38
  const errors: Error[] = [];
39
39
 
40
40
  const doTry = function(error?: Error) {
41
- if (error !== undefined && earlyBreaker && earlyBreaker(error)) {
41
+ if (error !== undefined && earlyBreaker?.(error)) {
42
42
  reject(error);
43
43
  return;
44
44
  }
@@ -9,6 +9,7 @@ interface Test {
9
9
  class Person {
10
10
  public name: string;
11
11
 
12
+ // eslint-disable-next-line @typescript-eslint/no-shadow
12
13
  public constructor(name: string) {
13
14
  this.name = name;
14
15
  }
package/src/serialize.ts CHANGED
@@ -35,7 +35,7 @@ type Options = {
35
35
  * @param customSerializers - an object with custom serializers
36
36
  * @param options - options
37
37
  */
38
- const serialize = (data: unknown, customSerializers?: CustomSerializers, options?: Options) => { // eslint-disable-line max-lines-per-function,max-len
38
+ const serialize = (data: unknown, customSerializers?: CustomSerializers, options?: Options) => { // eslint-disable-line max-lines-per-function
39
39
  const sourceData = Object.keys(customSerializers ?? {}).length
40
40
  ? replaceDeepByFn(
41
41
  data,
package/src/set.ts CHANGED
@@ -30,7 +30,7 @@ const isObject = (value: unknown) => (typeof value === "object" || typeof value
30
30
  * { "items": { "0": value }}
31
31
  * @returns {Object} - given object or new object if source was primitive
32
32
  */
33
- const set = (source: Source, path: string | string[], value: unknown): Source | unknown => { // eslint-disable-line @typescript-eslint/no-redundant-type-constituents,max-len
33
+ const set = (source: Source, path: string | string[], value: unknown): Source | unknown => { // eslint-disable-line @typescript-eslint/no-redundant-type-constituents
34
34
  const pathParts = typeof path === "string" ? path.split(".") : path;
35
35
  const len = pathParts.length;
36
36
 
@@ -67,7 +67,7 @@ const getPathParts = (path: Path) => {
67
67
  * { "items": { "0": value }}
68
68
  * @returns {Object} - given object or new object if source was primitive
69
69
  */
70
- const setImmutable = (source: Source, path: Path, value: unknown): Source | unknown => { // eslint-disable-line max-statements, max-len, @typescript-eslint/no-redundant-type-constituents
70
+ const setImmutable = (source: Source, path: Path, value: unknown): Source | unknown => { // eslint-disable-line max-statements, @typescript-eslint/no-redundant-type-constituents
71
71
  const pathParts = getPathParts(path);
72
72
  const isValidPath = hasOnlyValidPathParts(pathParts);
73
73
  if (!isValidPath) {
package/src/sortBy.ts CHANGED
@@ -10,7 +10,7 @@ const sortBy = <T>(propertyName: keyof T, asc = true, defaultValue: unknown = nu
10
10
  return 0;
11
11
  }
12
12
  // @ts-expect-error We don't care about types here, it's for runtime pure JS too
13
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
13
+
14
14
  if ((a[propertyName] ?? defaultValue) > (b[propertyName] ?? defaultValue)) {
15
15
  // eslint-disable-next-line @typescript-eslint/no-magic-numbers
16
16
  return asc ? 1 : -1;
package/src/throttle.ts CHANGED
@@ -46,7 +46,7 @@ type CanReturnUndefined<F extends (...args: any[]) => any> = (...args: Parameter
46
46
  * @param {number | number[]} time - throttle time as number or array of numbers (min 1 element)
47
47
  * @param {Opts} options
48
48
  */
49
- const throttle = <RT, F extends (...args: any[]) => RT>( // eslint-disable-line max-lines-per-function, @typescript-eslint/no-explicit-any, max-len
49
+ const throttle = <RT, F extends (...args: any[]) => RT>( // eslint-disable-line max-lines-per-function, @typescript-eslint/no-explicit-any
50
50
  fn: F, time: number | [number, ...number[]] = 0, options?: Opts,
51
51
  ): CanReturnUndefined<F> & Extras => {
52
52
  const opts: Required<Opts> = {