@ezez/utils 4.4.0 → 4.6.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 (381) hide show
  1. package/CHANGELOG.md +21 -1
  2. package/dist/cap.d.ts.map +1 -1
  3. package/dist/cap.js.map +1 -1
  4. package/dist/capitalize.d.ts.map +1 -1
  5. package/dist/capitalize.js.map +1 -1
  6. package/dist/compareArrays.d.ts +4 -4
  7. package/dist/compareArrays.d.ts.map +1 -1
  8. package/dist/compareArrays.js.map +1 -1
  9. package/dist/deserialize.d.ts +1 -1
  10. package/dist/deserialize.d.ts.map +1 -1
  11. package/dist/deserialize.js +2 -1
  12. package/dist/deserialize.js.map +1 -1
  13. package/dist/ensureError.d.ts.map +1 -1
  14. package/dist/ensureError.js +3 -1
  15. package/dist/ensureError.js.map +1 -1
  16. package/dist/ensurePrefix.d.ts.map +1 -1
  17. package/dist/ensurePrefix.js.map +1 -1
  18. package/dist/ensureSuffix.d.ts.map +1 -1
  19. package/dist/ensureSuffix.js.map +1 -1
  20. package/dist/escapeRegExp.d.ts.map +1 -1
  21. package/dist/escapeRegExp.js.map +1 -1
  22. package/dist/formatDate.d.ts.map +1 -1
  23. package/dist/formatDate.js.map +1 -1
  24. package/dist/getMultiple.d.ts.map +1 -1
  25. package/dist/getMultiple.js.map +1 -1
  26. package/dist/ignore.d.ts +3 -0
  27. package/dist/ignore.d.ts.map +1 -0
  28. package/dist/ignore.js +16 -0
  29. package/dist/ignore.js.map +1 -0
  30. package/dist/index.d.ts +3 -0
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +3 -0
  33. package/dist/index.js.map +1 -1
  34. package/dist/isEmpty.d.ts.map +1 -1
  35. package/dist/isEmpty.js +4 -4
  36. package/dist/isEmpty.js.map +1 -1
  37. package/dist/isNumericString.d.ts.map +1 -1
  38. package/dist/isNumericString.js.map +1 -1
  39. package/dist/isPlainObject.d.ts.map +1 -1
  40. package/dist/isPlainObject.js.map +1 -1
  41. package/dist/match.d.ts.map +1 -1
  42. package/dist/match.js.map +1 -1
  43. package/dist/memoize.d.ts.map +1 -1
  44. package/dist/memoize.js.map +1 -1
  45. package/dist/mostFrequent.d.ts.map +1 -1
  46. package/dist/mostFrequent.js.map +1 -1
  47. package/dist/noop.d.ts.map +1 -1
  48. package/dist/noop.js.map +1 -1
  49. package/dist/occurrences.d.ts.map +1 -1
  50. package/dist/occurrences.js.map +1 -1
  51. package/dist/removeCommonProperties.d.ts.map +1 -1
  52. package/dist/removeCommonProperties.js.map +1 -1
  53. package/dist/replace.d.ts.map +1 -1
  54. package/dist/replace.js.map +1 -1
  55. package/dist/rethrow.d.ts +1 -1
  56. package/dist/rethrow.d.ts.map +1 -1
  57. package/dist/rethrow.js.map +1 -1
  58. package/dist/round.d.ts.map +1 -1
  59. package/dist/round.js.map +1 -1
  60. package/dist/safe.d.ts.map +1 -1
  61. package/dist/safe.js.map +1 -1
  62. package/dist/scale.d.ts.map +1 -1
  63. package/dist/scale.js.map +1 -1
  64. package/dist/seq.d.ts.map +1 -1
  65. package/dist/seq.js +5 -1
  66. package/dist/seq.js.map +1 -1
  67. package/dist/serialize.d.ts.map +1 -1
  68. package/dist/serialize.js +2 -2
  69. package/dist/serialize.js.map +1 -1
  70. package/dist/serializeToBuffer/const.d.ts +10 -0
  71. package/dist/serializeToBuffer/const.d.ts.map +1 -0
  72. package/dist/serializeToBuffer/const.js +16 -0
  73. package/dist/serializeToBuffer/const.js.map +1 -0
  74. package/dist/serializeToBuffer/serializeToBuffer.d.ts +5 -0
  75. package/dist/serializeToBuffer/serializeToBuffer.d.ts.map +1 -0
  76. package/dist/serializeToBuffer/serializeToBuffer.js +39 -0
  77. package/dist/serializeToBuffer/serializeToBuffer.js.map +1 -0
  78. package/dist/serializeToBuffer/unserializeFromBuffer.d.ts +5 -0
  79. package/dist/serializeToBuffer/unserializeFromBuffer.d.ts.map +1 -0
  80. package/dist/serializeToBuffer/unserializeFromBuffer.js +52 -0
  81. package/dist/serializeToBuffer/unserializeFromBuffer.js.map +1 -0
  82. package/dist/setImmutable.js.map +1 -1
  83. package/dist/sortBy.d.ts +1 -1
  84. package/dist/sortBy.d.ts.map +1 -1
  85. package/dist/sortBy.js.map +1 -1
  86. package/dist/sortByMultiple.d.ts +1 -1
  87. package/dist/sortByMultiple.d.ts.map +1 -1
  88. package/dist/sortByMultiple.js.map +1 -1
  89. package/dist/trim.d.ts.map +1 -1
  90. package/dist/trim.js +1 -1
  91. package/dist/trim.js.map +1 -1
  92. package/dist/trimEnd.d.ts.map +1 -1
  93. package/dist/trimEnd.js.map +1 -1
  94. package/dist/trimStart.d.ts.map +1 -1
  95. package/dist/trimStart.js.map +1 -1
  96. package/dist/unique.d.ts +1 -1
  97. package/dist/unique.d.ts.map +1 -1
  98. package/dist/unique.js.map +1 -1
  99. package/dist/utils/utils.js +1 -0
  100. package/dist/utils/utils.js.map +1 -1
  101. package/dist/wait.d.ts.map +1 -1
  102. package/dist/wait.js.map +1 -1
  103. package/dist/waitFor.d.ts.map +1 -1
  104. package/dist/waitFor.js.map +1 -1
  105. package/dist/waitSync.d.ts.map +1 -1
  106. package/dist/waitSync.js.map +1 -1
  107. package/docs/assets/hierarchy.js +1 -0
  108. package/docs/assets/icons.js +1 -1
  109. package/docs/assets/icons.svg +1 -1
  110. package/docs/assets/main.js +5 -5
  111. package/docs/assets/navigation.js +1 -1
  112. package/docs/assets/search.js +1 -1
  113. package/docs/assets/style.css +1405 -1288
  114. package/docs/documents/Changelog.html +370 -0
  115. package/docs/functions/index.cap.html +3 -3
  116. package/docs/functions/index.capitalize.html +7 -7
  117. package/docs/functions/index.coalesce.html +8 -8
  118. package/docs/functions/index.compareArrays.html +5 -5
  119. package/docs/functions/index.compareProps.html +5 -5
  120. package/docs/functions/index.deserialize.html +4 -4
  121. package/docs/functions/index.ensureArray.html +3 -3
  122. package/docs/functions/index.ensureDate.html +6 -6
  123. package/docs/functions/index.ensureError.html +6 -6
  124. package/docs/functions/index.ensurePrefix.html +5 -5
  125. package/docs/functions/index.ensureSuffix.html +5 -5
  126. package/docs/functions/index.ensureTimestamp.html +6 -6
  127. package/docs/functions/index.escapeRegExp.html +5 -5
  128. package/docs/functions/index.formatDate.html +5 -2
  129. package/docs/functions/index.get.html +8 -8
  130. package/docs/functions/index.getMultiple.html +6 -6
  131. package/docs/functions/index.ignore.html +8 -0
  132. package/docs/functions/index.insertSeparator.html +5 -5
  133. package/docs/functions/index.isEmpty.html +12 -13
  134. package/docs/functions/index.isNumericString.html +3 -3
  135. package/docs/functions/index.isPlainObject.html +8 -8
  136. package/docs/functions/index.last.html +7 -7
  137. package/docs/functions/index.later-1.html +3 -3
  138. package/docs/functions/index.mapAsync.html +5 -5
  139. package/docs/functions/index.mapValues.html +7 -7
  140. package/docs/functions/index.match.html +4 -4
  141. package/docs/functions/index.memoize.html +4 -4
  142. package/docs/functions/index.merge.html +6 -6
  143. package/docs/functions/index.mostFrequent.html +2 -2
  144. package/docs/functions/index.noop.html +2 -2
  145. package/docs/functions/index.occurrences.html +7 -6
  146. package/docs/functions/index.omit.html +7 -7
  147. package/docs/functions/index.pick.html +7 -7
  148. package/docs/functions/index.pull.html +2 -2
  149. package/docs/functions/index.race.html +5 -5
  150. package/docs/functions/index.remove.html +2 -2
  151. package/docs/functions/index.removeCommonProperties.html +5 -5
  152. package/docs/functions/index.replace.html +6 -6
  153. package/docs/functions/index.replaceDeep.html +3 -3
  154. package/docs/functions/index.rethrow.html +2 -2
  155. package/docs/functions/index.retry.html +4 -4
  156. package/docs/functions/index.round.html +4 -4
  157. package/docs/functions/index.safe.html +5 -5
  158. package/docs/functions/index.sample.html +4 -4
  159. package/docs/functions/index.samples.html +3 -3
  160. package/docs/functions/index.scale.html +2 -2
  161. package/docs/functions/index.seq.html +7 -7
  162. package/docs/functions/index.seqEarlyBreak.html +6 -6
  163. package/docs/functions/index.serialize.html +4 -5
  164. package/docs/functions/index.serializeToBuffer.html +19 -0
  165. package/docs/functions/index.set.html +8 -8
  166. package/docs/functions/index.setImmutable.html +7 -7
  167. package/docs/functions/index.shuffle.html +3 -3
  168. package/docs/functions/index.sortBy.html +3 -3
  169. package/docs/functions/index.sortByMultiple.html +5 -5
  170. package/docs/functions/index.sortProps.html +4 -4
  171. package/docs/functions/index.stripPrefix.html +3 -3
  172. package/docs/functions/index.stripSuffix.html +3 -3
  173. package/docs/functions/index.throttle.html +4 -4
  174. package/docs/functions/index.toggle.html +4 -4
  175. package/docs/functions/index.trim.html +4 -4
  176. package/docs/functions/index.trimEnd.html +4 -4
  177. package/docs/functions/index.trimStart.html +4 -4
  178. package/docs/functions/index.truthy.html +4 -4
  179. package/docs/functions/index.unique.html +4 -4
  180. package/docs/functions/index.unserializeFromBuffer.html +8 -0
  181. package/docs/functions/index.wait.html +3 -3
  182. package/docs/functions/index.waitFor.html +4 -4
  183. package/docs/functions/index.waitSync.html +3 -3
  184. package/docs/index.html +16 -16
  185. package/docs/interfaces/index.ComparePropsOptions.html +4 -4
  186. package/docs/interfaces/index.GetMultipleSource.html +3 -3
  187. package/docs/interfaces/index.GetSource.html +3 -3
  188. package/docs/interfaces/index.IsNumericStringOptions.html +5 -5
  189. package/docs/interfaces/index.OccurencesOptions.html +2 -2
  190. package/docs/interfaces/index.SetImmutableSource.html +3 -3
  191. package/docs/interfaces/index.SetSource.html +3 -3
  192. package/docs/interfaces/index.ThrottleOptions.html +3 -3
  193. package/docs/interfaces/index.ThrottledFunctionExtras.html +4 -4
  194. package/docs/modules/index.html +1 -89
  195. package/docs/modules.html +1 -3
  196. package/docs/types/index.CustomDeserializers.html +1 -1
  197. package/docs/types/index.CustomSerializers.html +1 -1
  198. package/docs/types/index.Later.html +4 -4
  199. package/docs/types/index.MapValuesFn.html +7 -7
  200. package/docs/types/index.MatchCallback.html +1 -1
  201. package/docs/types/index.MergeTwo.html +2 -2
  202. package/docs/types/index.SeqEarlyBreaker.html +4 -4
  203. package/docs/types/index.SeqFn.html +3 -3
  204. package/docs/types/index.SeqFunctions.html +3 -3
  205. package/docs/types/index.SetImmutablePath.html +2 -2
  206. package/docs/types/index.ThrottledFunction.html +1 -1
  207. package/docs/variables/index.mapValuesUNSET.html +2 -2
  208. package/docs/variables/index.mergeUNSET.html +3 -3
  209. package/esm/cap.d.ts.map +1 -1
  210. package/esm/cap.js.map +1 -1
  211. package/esm/capitalize.d.ts.map +1 -1
  212. package/esm/capitalize.js.map +1 -1
  213. package/esm/compareArrays.d.ts +4 -4
  214. package/esm/compareArrays.d.ts.map +1 -1
  215. package/esm/compareArrays.js.map +1 -1
  216. package/esm/deserialize.d.ts +1 -1
  217. package/esm/deserialize.d.ts.map +1 -1
  218. package/esm/deserialize.js +1 -1
  219. package/esm/deserialize.js.map +1 -1
  220. package/esm/ensureError.d.ts.map +1 -1
  221. package/esm/ensureError.js +3 -1
  222. package/esm/ensureError.js.map +1 -1
  223. package/esm/ensurePrefix.d.ts.map +1 -1
  224. package/esm/ensurePrefix.js.map +1 -1
  225. package/esm/ensureSuffix.d.ts.map +1 -1
  226. package/esm/ensureSuffix.js.map +1 -1
  227. package/esm/escapeRegExp.d.ts.map +1 -1
  228. package/esm/escapeRegExp.js.map +1 -1
  229. package/esm/formatDate.d.ts.map +1 -1
  230. package/esm/formatDate.js.map +1 -1
  231. package/esm/getMultiple.d.ts.map +1 -1
  232. package/esm/getMultiple.js.map +1 -1
  233. package/esm/ignore.d.ts +3 -0
  234. package/esm/ignore.d.ts.map +1 -0
  235. package/esm/ignore.js +13 -0
  236. package/esm/ignore.js.map +1 -0
  237. package/esm/index.d.ts +3 -0
  238. package/esm/index.d.ts.map +1 -1
  239. package/esm/index.js +3 -0
  240. package/esm/index.js.map +1 -1
  241. package/esm/isEmpty.d.ts.map +1 -1
  242. package/esm/isEmpty.js +4 -4
  243. package/esm/isEmpty.js.map +1 -1
  244. package/esm/isNumericString.d.ts.map +1 -1
  245. package/esm/isNumericString.js.map +1 -1
  246. package/esm/isPlainObject.d.ts.map +1 -1
  247. package/esm/isPlainObject.js.map +1 -1
  248. package/esm/match.d.ts.map +1 -1
  249. package/esm/match.js.map +1 -1
  250. package/esm/memoize.d.ts.map +1 -1
  251. package/esm/memoize.js.map +1 -1
  252. package/esm/mostFrequent.d.ts.map +1 -1
  253. package/esm/mostFrequent.js.map +1 -1
  254. package/esm/noop.d.ts.map +1 -1
  255. package/esm/noop.js.map +1 -1
  256. package/esm/occurrences.d.ts.map +1 -1
  257. package/esm/occurrences.js.map +1 -1
  258. package/esm/removeCommonProperties.d.ts.map +1 -1
  259. package/esm/removeCommonProperties.js.map +1 -1
  260. package/esm/replace.d.ts.map +1 -1
  261. package/esm/replace.js.map +1 -1
  262. package/esm/rethrow.d.ts +1 -1
  263. package/esm/rethrow.d.ts.map +1 -1
  264. package/esm/rethrow.js.map +1 -1
  265. package/esm/round.d.ts.map +1 -1
  266. package/esm/round.js.map +1 -1
  267. package/esm/safe.d.ts.map +1 -1
  268. package/esm/safe.js.map +1 -1
  269. package/esm/scale.d.ts.map +1 -1
  270. package/esm/scale.js.map +1 -1
  271. package/esm/seq.d.ts.map +1 -1
  272. package/esm/seq.js +5 -1
  273. package/esm/seq.js.map +1 -1
  274. package/esm/serialize.d.ts.map +1 -1
  275. package/esm/serialize.js +2 -2
  276. package/esm/serialize.js.map +1 -1
  277. package/esm/serializeToBuffer/const.d.ts +10 -0
  278. package/esm/serializeToBuffer/const.d.ts.map +1 -0
  279. package/esm/serializeToBuffer/const.js +10 -0
  280. package/esm/serializeToBuffer/const.js.map +1 -0
  281. package/esm/serializeToBuffer/serializeToBuffer.d.ts +5 -0
  282. package/esm/serializeToBuffer/serializeToBuffer.d.ts.map +1 -0
  283. package/esm/serializeToBuffer/serializeToBuffer.js +36 -0
  284. package/esm/serializeToBuffer/serializeToBuffer.js.map +1 -0
  285. package/esm/serializeToBuffer/unserializeFromBuffer.d.ts +5 -0
  286. package/esm/serializeToBuffer/unserializeFromBuffer.d.ts.map +1 -0
  287. package/esm/serializeToBuffer/unserializeFromBuffer.js +48 -0
  288. package/esm/serializeToBuffer/unserializeFromBuffer.js.map +1 -0
  289. package/esm/setImmutable.js.map +1 -1
  290. package/esm/sortBy.d.ts +1 -1
  291. package/esm/sortBy.d.ts.map +1 -1
  292. package/esm/sortBy.js.map +1 -1
  293. package/esm/sortByMultiple.d.ts +1 -1
  294. package/esm/sortByMultiple.d.ts.map +1 -1
  295. package/esm/sortByMultiple.js.map +1 -1
  296. package/esm/trim.d.ts.map +1 -1
  297. package/esm/trim.js +1 -1
  298. package/esm/trim.js.map +1 -1
  299. package/esm/trimEnd.d.ts.map +1 -1
  300. package/esm/trimEnd.js.map +1 -1
  301. package/esm/trimStart.d.ts.map +1 -1
  302. package/esm/trimStart.js.map +1 -1
  303. package/esm/unique.d.ts +1 -1
  304. package/esm/unique.d.ts.map +1 -1
  305. package/esm/unique.js.map +1 -1
  306. package/esm/utils/utils.js +1 -0
  307. package/esm/utils/utils.js.map +1 -1
  308. package/esm/wait.d.ts.map +1 -1
  309. package/esm/wait.js.map +1 -1
  310. package/esm/waitFor.d.ts.map +1 -1
  311. package/esm/waitFor.js.map +1 -1
  312. package/esm/waitSync.d.ts.map +1 -1
  313. package/esm/waitSync.js.map +1 -1
  314. package/package.json +31 -34
  315. package/src/cap.ts +1 -1
  316. package/src/capitalize.ts +3 -3
  317. package/src/compareArrays.ts +2 -3
  318. package/src/deserialize.spec.ts +2 -1
  319. package/src/deserialize.ts +1 -1
  320. package/src/ensureError.ts +6 -2
  321. package/src/ensurePrefix.ts +1 -1
  322. package/src/ensureSuffix.ts +1 -1
  323. package/src/escapeRegExp.ts +1 -1
  324. package/src/formatDate.ts +7 -2
  325. package/src/get.spec.ts +0 -1
  326. package/src/getMultiple.ts +0 -1
  327. package/src/ignore.spec.ts +80 -0
  328. package/src/ignore.ts +38 -0
  329. package/src/index.ts +3 -0
  330. package/src/isEmpty.ts +9 -15
  331. package/src/isNumericString.ts +1 -1
  332. package/src/isPlainObject.ts +1 -1
  333. package/src/later.spec.ts +1 -0
  334. package/src/mapAsync.ts +4 -4
  335. package/src/match.ts +1 -1
  336. package/src/memoize.spec.ts +1 -1
  337. package/src/memoize.ts +1 -1
  338. package/src/mostFrequent.spec.ts +2 -0
  339. package/src/mostFrequent.ts +0 -1
  340. package/src/noop.ts +1 -1
  341. package/src/occurrences.ts +1 -1
  342. package/src/remove.spec.ts +1 -1
  343. package/src/removeCommonProperties.spec.ts +2 -0
  344. package/src/removeCommonProperties.ts +1 -1
  345. package/src/replace.ts +1 -1
  346. package/src/replaceDeep.spec.ts +2 -0
  347. package/src/replaceDeepByFn.spec.ts +2 -0
  348. package/src/rethrow.ts +1 -1
  349. package/src/round.ts +1 -1
  350. package/src/safe.ts +2 -1
  351. package/src/scale.ts +1 -1
  352. package/src/seq.ts +7 -3
  353. package/src/serialize.spec.ts +2 -1
  354. package/src/serialize.ts +4 -5
  355. package/src/serializeToBuffer/const.ts +16 -0
  356. package/src/serializeToBuffer/serialization.spec.ts +61 -0
  357. package/src/serializeToBuffer/serializeToBuffer.spec.ts +43 -0
  358. package/src/serializeToBuffer/serializeToBuffer.ts +89 -0
  359. package/src/serializeToBuffer/unserializeFromBuffer.spec.ts +63 -0
  360. package/src/serializeToBuffer/unserializeFromBuffer.ts +74 -0
  361. package/src/setImmutable.ts +1 -1
  362. package/src/sortBy.ts +3 -1
  363. package/src/sortByMultiple.ts +5 -3
  364. package/src/toggle.spec.ts +2 -0
  365. package/src/trim.spec.ts +2 -0
  366. package/src/trim.ts +3 -2
  367. package/src/trimEnd.spec.ts +2 -0
  368. package/src/trimEnd.ts +1 -1
  369. package/src/trimStart.spec.ts +2 -0
  370. package/src/trimStart.ts +1 -1
  371. package/src/unique.ts +2 -2
  372. package/src/wait.ts +1 -1
  373. package/src/waitFor.spec.ts +9 -8
  374. package/src/waitFor.ts +1 -1
  375. package/src/waitSync.spec.ts +2 -0
  376. package/src/waitSync.ts +1 -1
  377. package/.prettierignore +0 -3
  378. package/.prettierrc.json +0 -1
  379. package/docs/documents/CHANGELOG.html +0 -349
  380. package/pnpm-lock.yaml +0 -6529
  381. package/typedoc.cjs +0 -3
package/src/scale.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  * @param {number} toMax
10
10
  * @param {number} number
11
11
  */
12
- const scale = (fromMin: number, fromMax: number, toMin: number, toMax: number, number: number) => {
12
+ const scale = (fromMin: number, fromMax: number, toMin: number, toMax: number, number: number): number => {
13
13
  return toMin + ((number - fromMin) / (fromMax - fromMin) * (toMax - toMin));
14
14
  };
15
15
 
package/src/seq.ts CHANGED
@@ -48,7 +48,11 @@ const run = <T>(list: Fn<T>[], earlyBreaker?: EarlyBreaker): Promise<T> => {
48
48
  }
49
49
  const fn = promises.shift();
50
50
  if (typeof fn !== "function") {
51
- const finalError = new Error("Every function had thrown.");
51
+ const finalError = new Error("Every function had thrown.", {
52
+ cause: {
53
+ errors,
54
+ },
55
+ });
52
56
  // @ts-expect-error More details on error object are wanted
53
57
  finalError.details = { errors };
54
58
  reject(finalError);
@@ -73,7 +77,7 @@ const run = <T>(list: Fn<T>[], earlyBreaker?: EarlyBreaker): Promise<T> => {
73
77
  * arguments with array
74
78
  * @returns {*} - whatever gets returned from given functions
75
79
  */
76
- const seqEarlyBreak = <T>(earlyBreaker: EarlyBreaker | undefined, ...args: Functions<T>) => {
80
+ const seqEarlyBreak = <T>(earlyBreaker: EarlyBreaker | undefined, ...args: Functions<T>): Promise<T> => {
77
81
  if (args.length === 1) {
78
82
  return run(Array.isArray(args[0]) ? args[0] : [args[0]], earlyBreaker);
79
83
  }
@@ -91,7 +95,7 @@ const seqEarlyBreak = <T>(earlyBreaker: EarlyBreaker | undefined, ...args: Funct
91
95
  * arguments with array
92
96
  * @returns {*} - whatever gets returned from given functions
93
97
  */
94
- const seq = <T>(...fns: Functions<T>) => {
98
+ const seq = <T>(...fns: Functions<T>): Promise<T> => {
95
99
  return seqEarlyBreak(undefined, ...fns);
96
100
  };
97
101
 
@@ -1,3 +1,5 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
1
3
  import type { CustomSerializers } from "./serialize";
2
4
 
3
5
  import { serialize } from "./serialize";
@@ -9,7 +11,6 @@ interface Test {
9
11
  class Person {
10
12
  public name: string;
11
13
 
12
- // eslint-disable-next-line @typescript-eslint/no-shadow
13
14
  public constructor(name: string) {
14
15
  this.name = name;
15
16
  }
package/src/serialize.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { sortProps } from "./sortProps.js";
2
- import { replaceDeepByFn } from "./replaceDeepByFn.js";
3
1
  import { isPlainObject } from "./isPlainObject.js";
2
+ import { replaceDeepByFn } from "./replaceDeepByFn.js";
3
+ import { sortProps } from "./sortProps.js";
4
4
  import { DataWrapper } from "./utils/utils.js";
5
5
 
6
6
  type CustomSerializers = {
@@ -23,8 +23,7 @@ type Options = {
23
23
  *
24
24
  * It also supports custom serializers, which can be used to serialize custom data types. Each value has a prefix which
25
25
  * specifies the type of the value, custom serializers is a map of these prefixes to functions which can serialize the
26
- * value into a string. IMPORTANT: Because this is using JSON.serialize under the hood if a value to serialize includes
27
- * `toJSON` it won't trigger custom serializer but will be serialized as string. `Date` class defines `toJSON` method!
26
+ * value into a string.
28
27
  *
29
28
  * The extra aim of this function is to produce the same output for "the same" data, regardless of the order of the keys
30
29
  * (which is not guaranteed by JS spec, but in practice it is guaranteed by current implementations of all JS engines),
@@ -35,7 +34,7 @@ type Options = {
35
34
  * @param customSerializers - an object with custom serializers
36
35
  * @param options - options
37
36
  */
38
- const serialize = (data: unknown, customSerializers?: CustomSerializers, options?: Options) => { // eslint-disable-line max-lines-per-function
37
+ const serialize = (data: unknown, customSerializers?: CustomSerializers, options?: Options): string => { // eslint-disable-line max-lines-per-function
39
38
  const sourceData = Object.keys(customSerializers ?? {}).length
40
39
  ? replaceDeepByFn(
41
40
  data,
@@ -0,0 +1,16 @@
1
+ const BINARY_MARK_BIN = "t";
2
+ const BINARY_MARK_STRING = "f";
3
+ const BINARY_MARK_JSON = "j";
4
+
5
+ const BINARY_MARK_MAP = {
6
+ [BINARY_MARK_BIN]: "binary",
7
+ [BINARY_MARK_STRING]: "string",
8
+ [BINARY_MARK_JSON]: "json",
9
+ };
10
+
11
+ export {
12
+ BINARY_MARK_BIN,
13
+ BINARY_MARK_STRING,
14
+ BINARY_MARK_JSON,
15
+ BINARY_MARK_MAP,
16
+ };
@@ -0,0 +1,61 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
3
+ import { serializeToBuffer } from "./serializeToBuffer";
4
+ import { unserializeFromBuffer } from "./unserializeFromBuffer";
5
+
6
+ const serialize = serializeToBuffer.bind(null, Buffer, []);
7
+ const unserialize = unserializeFromBuffer.bind(null, Buffer, []);
8
+
9
+ describe("serialization", () => {
10
+ it("serializes basic string", async () => {
11
+ const data = ["TEST"];
12
+ must(unserialize(serialize(...data))).eql(data);
13
+ });
14
+
15
+ it("serializes json", async () => {
16
+ const data = [
17
+ { a: 1, b: 2 },
18
+ ];
19
+ must(unserialize(serialize(...data))).eql(data);
20
+ });
21
+
22
+ it("serializes multiple data", async () => {
23
+ const data = [
24
+ "hey",
25
+ 3,
26
+ { a: 1 },
27
+ Buffer.from("test"),
28
+ ];
29
+ const result = unserialize(serialize(...data));
30
+ const buffer = result.pop() as Buffer;
31
+ must(result).eql(data.slice(0, 3));
32
+ must(buffer).be.instanceof(Buffer);
33
+ must(buffer.toString()).equal("test");
34
+ });
35
+
36
+ it("serializes undefined", async () => {
37
+ const data = [undefined];
38
+ const result = unserialize(serialize(...data));
39
+ must(result).have.length(1);
40
+ must(result[0]).be.undefined();
41
+ });
42
+
43
+ it("serializes JSON-looking string", async () => {
44
+ const data = ["\"TEST\""];
45
+ must(unserialize(serialize(...data))).eql(data);
46
+ });
47
+
48
+ it("serializes huge object", async () => {
49
+ const data = [{
50
+ a: 1,
51
+ b: false,
52
+ c: true,
53
+ d: "test",
54
+ e: null,
55
+ f: undefined,
56
+ g: [1, 2, 3],
57
+ h: { a: 1, b: [2] },
58
+ }];
59
+ must(unserialize(serialize(...data))).eql(data);
60
+ });
61
+ });
@@ -0,0 +1,43 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
3
+ import { serializeToBuffer } from "./serializeToBuffer";
4
+
5
+ const serialize = serializeToBuffer.bind(null, Buffer, []);
6
+
7
+ describe("serialize", () => {
8
+ it("serializes basic string", async () => {
9
+ // len 4, type string, data "test", separator
10
+ must(serialize("test").toString("hex")).equal("3466007465737400");
11
+ });
12
+
13
+ it("serializes multiple strings", async () => {
14
+ // len 4, type string, data "test", separator
15
+ must(serialize("test", "test").toString("hex")).equal("34660074657374003466007465737400");
16
+ });
17
+
18
+ it("serializes basic buffer", async () => {
19
+ // len 4, type bin, data "test", separator
20
+ must(serialize(Buffer.from("test")).toString("hex")).equal("3474007465737400");
21
+ });
22
+
23
+ it("serializes both buffer and string", async () => {
24
+ // len 4, type bin, data "test", separator, len 4, bin false, data "test", separator
25
+ must(serialize(Buffer.from("test"), "test").toString("hex")).equal("34740074657374003466007465737400");
26
+ });
27
+
28
+ it("serializes JSON-looking string", async () => {
29
+ // len 6, type string, data "test", separator
30
+ must(serialize(`"test"`).toString("hex")).equal("36660022746573742200");
31
+ });
32
+
33
+ it("serializes JSON-serialized string", async () => {
34
+ // len 8, type string, data "test", separator
35
+ must(serialize(`"s:test"`).toString("hex")).equal("38660022733a746573742200");
36
+ });
37
+
38
+ it("serializes number", async () => {
39
+ // len 5, type json, data "n:1", separator
40
+ must(serialize(1).toString("hex")).equal("356a00226e3a312200");
41
+ });
42
+ // TODO more tests
43
+ });
@@ -0,0 +1,89 @@
1
+ import { serialize } from "../serialize.js";
2
+ import { BINARY_MARK_BIN, BINARY_MARK_JSON, BINARY_MARK_STRING } from "./const.js";
3
+
4
+ type SerializeArgs = Parameters<typeof serialize> extends [any, ...infer Rest] ? Rest : never; // eslint-disable-line @typescript-eslint/no-explicit-any
5
+
6
+ /**
7
+ * Serialize any data to a Buffer, useful when working with web sockets or other binary protocols.
8
+ * Make sure to understand how {@link serialize} works before using this function.
9
+ *
10
+ * To make this function work in browsers it accepts a BufferImplementation parameter.
11
+ * To make the serialization customizable it accepts a serializeArgs parameter.
12
+ * It's therefore recommended to first prepare the function with bind, like:
13
+ * @example
14
+ * ```ts
15
+ * import { serializeToBuffer as serializeRaw } from "@ezez/utils";
16
+ * import { Buffer } from "buffer/"; // skip import in Node.js
17
+ *
18
+ * // prepare the function:
19
+ * const serializeToBuffer = serializeRaw.bind(null, Buffer, []); // array is customization params for serialize
20
+ *
21
+ * const serialized = serializeToBuffer("test", 1, { test: "test" });
22
+ * ```
23
+ *
24
+ * Otherwise pass the params each time:
25
+ * ```typescript
26
+ * import { serializeToBuffer as serializeRaw } from "@ezez/utils";
27
+ * const serialized = serializeToBuffer(Buffer, [], "test", 1, { test: "test" });
28
+ * const otherSerialized = serializeToBuffer(Buffer, [], any, other, data);
29
+ * ```
30
+ * @param BufferImplementation - Buffer implementation, in browsers use `buffer` npm package
31
+ * and import it from `buffer/`, in Node.js simply pass `Buffer`.
32
+ * @param serializeArgs - [customSerializers, options] - optional arguments if you need to serialize custom data types,
33
+ * see: {@link serialize}
34
+ * @param args - any data to serialize, can be a Buffer, string or any other data type
35
+ * @returns Buffer - serialized data
36
+ */
37
+ const serializeToBuffer = (
38
+ BufferImplementation: typeof Buffer, serializeArgs: SerializeArgs, ...args: unknown[]
39
+ ): Buffer => {
40
+ const separator = BufferImplementation.from([0x0]);
41
+ type DataType = {
42
+ mark: typeof BINARY_MARK_BIN;
43
+ data: Buffer;
44
+ } | {
45
+ mark: typeof BINARY_MARK_STRING;
46
+ data: string;
47
+ } | {
48
+ mark: typeof BINARY_MARK_JSON;
49
+ data: unknown;
50
+ };
51
+
52
+ const convertedArgs = args.map((arg) => {
53
+ if (arg instanceof BufferImplementation) {
54
+ return {
55
+ mark: BINARY_MARK_BIN,
56
+ data: arg,
57
+ } satisfies DataType;
58
+ }
59
+ if (typeof arg === "string") {
60
+ return {
61
+ mark: BINARY_MARK_STRING,
62
+ data: arg,
63
+ } satisfies DataType;
64
+ }
65
+ return {
66
+ mark: BINARY_MARK_JSON,
67
+ data: serialize(arg, ...serializeArgs),
68
+ } satisfies DataType;
69
+ });
70
+ const dataToSend: Buffer[] = [];
71
+ let totalLength = 0;
72
+ convertedArgs.forEach((arg) => {
73
+ const len = String(arg.data.length) + arg.mark;
74
+
75
+ dataToSend.push(BufferImplementation.from(len, "utf-8"));
76
+ dataToSend.push(separator);
77
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
78
+ dataToSend.push(arg.mark === BINARY_MARK_BIN ? arg.data : BufferImplementation.from(arg.data, "utf-8"));
79
+ dataToSend.push(separator);
80
+
81
+ totalLength += len.length + separator.length + arg.data.length + separator.length;
82
+ });
83
+
84
+ return BufferImplementation.concat(dataToSend, totalLength);
85
+ };
86
+
87
+ export {
88
+ serializeToBuffer,
89
+ };
@@ -0,0 +1,63 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
3
+ import { unserializeFromBuffer } from "./unserializeFromBuffer";
4
+
5
+ const unserialize = unserializeFromBuffer.bind(null, Buffer, []);
6
+
7
+ describe("unserialize", () => {
8
+ it("unserializes basic string", async () => {
9
+ must(unserialize(Buffer.from([0x34, 0x66, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00]))).eql(["test"]);
10
+ });
11
+
12
+ it("unserializes multiple strings", async () => {
13
+ must(unserialize(Buffer.from([
14
+ 0x34, 0x66, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00,
15
+ 0x34, 0x66, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00,
16
+ ]))).eql(["test", "test"]);
17
+ });
18
+
19
+ it("unserializes buffer", async () => {
20
+ const result = unserialize(Buffer.from([0x34, 0x74, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00]));
21
+ must(result.length).equal(1);
22
+ must(result[0]).instanceof(Buffer);
23
+ must((result[0] as Buffer).toString()).equal("test");
24
+ });
25
+
26
+ it("unserializes both buffer and string", async () => {
27
+ const result = unserialize(Buffer.from([
28
+ 0x34, 0x74, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00,
29
+ 0x34, 0x66, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00,
30
+ ]));
31
+
32
+ must(result.length).equal(2);
33
+
34
+ must(result[0]).instanceof(Buffer);
35
+ must((result[0] as Buffer).toString()).equal("test");
36
+
37
+ must(result[1]).equal("test");
38
+ });
39
+
40
+ it("unserializes JSON-looking string as string", async () => {
41
+ must(unserialize(
42
+ Buffer.from([0x36, 0x66, 0x00, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x00]),
43
+ )).eql([`"test"`]);
44
+ });
45
+
46
+ it("unserializes JSON-serialized string as string", async () => {
47
+ must(unserialize(Buffer.from([
48
+ 0x38, 0x66, 0x00, 0x22, 0x73, 0x3a, 0x74, 0x65, 0x73, 0x74, 0x22, 0x00,
49
+ ]))).eql([`"s:test"`]);
50
+ });
51
+
52
+ it("unserializes JSON-serialized json as string", async () => {
53
+ must(unserialize(Buffer.from([
54
+ 0x38, 0x6a, 0x00, 0x22, 0x73, 0x3a, 0x74, 0x65, 0x73, 0x74, 0x22, 0x00,
55
+ ]))).eql(["test"]);
56
+ });
57
+
58
+ it("unserializes number as number", async () => {
59
+ must(unserialize(Buffer.from([
60
+ 0x35, 0x6a, 0x00, 0x22, 0x6e, 0x3a, 0x31, 0x22, 0x00,
61
+ ]))).eql([1]);
62
+ });
63
+ });
@@ -0,0 +1,74 @@
1
+ import { deserialize } from "../deserialize.js";
2
+ import { BINARY_MARK_BIN, BINARY_MARK_MAP, BINARY_MARK_STRING } from "./const.js";
3
+
4
+ const MAX_DATA_PARTS = 4;
5
+
6
+ const NOT_FOUND = -1;
7
+ const LAST_CHAR = -1;
8
+
9
+ type DeserializeArgs = Parameters<typeof deserialize> extends [any, ...infer Rest] ? Rest : never; // eslint-disable-line @typescript-eslint/no-explicit-any
10
+
11
+ /**
12
+ * Unserialize data from a Buffer serialized with {@link serializeToBuffer}, useful when working with web sockets or
13
+ * other binary protocols. Make sure to understand how {@link deserialize} works before using this function.
14
+ * @param BufferImplementation - Buffer implementation, in browsers use `buffer` npm package
15
+ * and import it from `buffer/`, in Node.js simply pass `Buffer`.
16
+ * @param deserializeArgs - [customDeserializers] - optional arguments if you need to unserialize custom data types,
17
+ * @param rawData - Buffer to unserialize
18
+ * @returns unserialized data
19
+ */
20
+ const unserializeFromBuffer = <RT extends any[] = unknown[]>( // eslint-disable-line @typescript-eslint/no-explicit-any,max-statements
21
+ BufferImplementation: typeof Buffer, deserializeArgs: DeserializeArgs, rawData: Buffer | Uint8Array,
22
+ ): RT => {
23
+ const intData: Uint8Array = rawData instanceof Uint8Array ? rawData : new Uint8Array(rawData);
24
+
25
+ if (intData.length === 0) {
26
+ throw new Error("No data to unserialize");
27
+ }
28
+
29
+ let startPoint = 0;
30
+ const result = [];
31
+
32
+ let i = 0;
33
+ while (i++ < MAX_DATA_PARTS) {
34
+ const dataSplitPoint = intData.indexOf(0, startPoint); // find null
35
+ if (dataSplitPoint === NOT_FOUND) { // no null found = no data
36
+ break;
37
+ }
38
+
39
+ const lengthBytes = BufferImplementation.from(intData.slice(startPoint, dataSplitPoint)).toString("utf8");
40
+ const binaryMark = lengthBytes.slice(LAST_CHAR) as keyof typeof BINARY_MARK_MAP;
41
+ if (!Object.keys(BINARY_MARK_MAP).includes(binaryMark)) {
42
+ throw new Error(`Invalid binary mark: ${binaryMark}`);
43
+ }
44
+
45
+ const len = Number(lengthBytes.slice(0, lengthBytes.length - 1));
46
+
47
+ const dataStringFrom = dataSplitPoint + 1;
48
+ const dataStringTo = dataStringFrom + len;
49
+ const dataBytes = intData.slice(dataStringFrom, dataStringTo);
50
+
51
+ if (binaryMark === BINARY_MARK_BIN) {
52
+ result.push(dataBytes);
53
+ }
54
+ else if (binaryMark === BINARY_MARK_STRING) {
55
+ const stringData = BufferImplementation.from(dataBytes).toString("utf8");
56
+ result.push(stringData);
57
+ }
58
+ else {
59
+ const stringData = BufferImplementation.from(dataBytes).toString("utf8");
60
+ const jsonData = deserialize(stringData, ...deserializeArgs);
61
+ result.push(jsonData);
62
+ }
63
+
64
+ startPoint = dataStringTo + 1; // skip separating null
65
+ }
66
+
67
+ if (result.length === 0) {
68
+ throw new Error("No data found (no split points)");
69
+ }
70
+
71
+ return result as RT;
72
+ };
73
+
74
+ export { unserializeFromBuffer, unserializeFromBuffer as deserializeFromBuffer };
@@ -9,7 +9,7 @@ const isObject = (value: unknown) => (typeof value === "object" || typeof value
9
9
 
10
10
  const clone = (value: unknown[] | Source) => {
11
11
  if (Array.isArray(value)) {
12
- return [...value as never as unknown[]]; // typecasts to satisfy eslint
12
+ return [...value];
13
13
  }
14
14
  return { ...value };
15
15
  };
package/src/sortBy.ts CHANGED
@@ -1,3 +1,5 @@
1
+ /* eslint-disable @typescript-eslint/no-magic-numbers */
2
+
1
3
  import { sortByMultiple } from "./sortByMultiple.js";
2
4
 
3
5
  /**
@@ -10,7 +12,7 @@ import { sortByMultiple } from "./sortByMultiple.js";
10
12
  */
11
13
  const sortBy = <T extends Record<string | number | symbol, any>>( // eslint-disable-line @typescript-eslint/no-explicit-any
12
14
  propertyName: keyof T, asc = true, defaultValue: unknown = null,
13
- ) => (a: T, b: T) => {
15
+ ) => (a: T, b: T): -1 | 0 | 1 => {
14
16
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
15
17
  return sortByMultiple([propertyName], asc, { [propertyName]: defaultValue } as Partial<Record<keyof T, any>>)(a, b);
16
18
  };
@@ -1,3 +1,5 @@
1
+ /* eslint-disable @typescript-eslint/no-magic-numbers */
2
+
1
3
  /**
2
4
  * Returns a function that can be used as a callback to `.sort()` method. Returned function will sort array by given
3
5
  * properties.
@@ -34,12 +36,12 @@ const sortByMultiple = <
34
36
  ascending: boolean | Record<K[number], boolean> = true,
35
37
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
38
  defaultValues?: Partial<Record<K[number], any>>, // Force defaultValues to use only keys from K
37
- ): ((a: T, b: T) => number) => (a: T, b: T) => {
39
+ ): ((a: T, b: T) => 1 | -1 | 0) => (a: T, b: T): 1 | -1 | 0 => {
38
40
  for (const propertyName of propertyNames) {
39
41
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
40
42
  const asc = typeof ascending === "boolean" ? ascending : (ascending[propertyName] ?? true);
41
- const aBiggerValue = asc ? 1 : -1; // eslint-disable-line @typescript-eslint/no-magic-numbers
42
- const bBiggerValue = asc ? -1 : 1; // eslint-disable-line @typescript-eslint/no-magic-numbers
43
+ const aBiggerValue = asc ? 1 : -1;
44
+ const bBiggerValue = asc ? -1 : 1;
43
45
 
44
46
  const aValue = a[propertyName] ?? defaultValues?.[propertyName];
45
47
  const bValue = b[propertyName] ?? defaultValues?.[propertyName];
@@ -1,3 +1,5 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
1
3
  import { toggle } from "./toggle.js";
2
4
 
3
5
  describe("toggle", () => {
package/src/trim.spec.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
1
3
  import { trim } from "./trim";
2
4
 
3
5
  describe("trim", () => {
package/src/trim.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { trimStart } from "./trimStart.js";
2
1
  import { trimEnd } from "./trimEnd.js";
2
+ import { trimStart } from "./trimStart.js";
3
3
 
4
4
  /**
5
5
  * Removes given characters from both sides of the string.
@@ -14,10 +14,11 @@ import { trimEnd } from "./trimEnd.js";
14
14
  * trim("!aaa!!!", "!"); // "aaa"
15
15
  * trim("!aaa!!!", "!!!"); // "!aaa"
16
16
  */
17
- const trim = (source: string, characters: string) => {
17
+ const trim = (source: string, characters: string): string => {
18
18
  return trimStart(trimEnd(source, characters), characters);
19
19
  };
20
20
 
21
21
  export {
22
22
  trim,
23
23
  };
24
+
@@ -1,3 +1,5 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
1
3
  import { trimEnd } from "./trimEnd";
2
4
 
3
5
  describe("trimEnd", () => {
package/src/trimEnd.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  * trimEnd("abcxzyz", "yz"); // "abcxz"
10
10
  * trimEnd("!aaa!!", "!"); // "!aaa"
11
11
  */
12
- const trimEnd = (source: string, characters: string) => {
12
+ const trimEnd = (source: string, characters: string): string => {
13
13
  let s = source;
14
14
  while (s.endsWith(characters)) {
15
15
  s = s.slice(0, -characters.length);
@@ -1,3 +1,5 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
1
3
  import { trimStart } from "./trimStart";
2
4
 
3
5
  describe("trimStart", () => {
package/src/trimStart.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  * trimStart("abbcb", "ab"); // "bcb"
10
10
  * trimStart("!!aaa!", "!"); // "aaa!"
11
11
  */
12
- const trimStart = (source: string, characters: string) => {
12
+ const trimStart = (source: string, characters: string): string => {
13
13
  let s = source;
14
14
  while (s.startsWith(characters)) {
15
15
  s = s.slice(characters.length);
package/src/unique.ts CHANGED
@@ -3,8 +3,8 @@
3
3
  * @param arr - source array
4
4
  * @example unique([1, 2, 3, 2, 1]) // [1, 2, 3]
5
5
  */
6
- const unique = <T extends unknown[]>(arr: T): T => {
7
- return [...new Set(arr)] as T;
6
+ const unique = <T>(arr: T[]): T[] => {
7
+ return [...new Set(arr)];
8
8
  };
9
9
 
10
10
  export {
package/src/wait.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * @param {number} timeMs - time to wait
4
4
  * @returns {Promise<void>}
5
5
  */
6
- const wait = (timeMs = 0) => {
6
+ const wait = (timeMs = 0): Promise<void> => {
7
7
  return new Promise<void>((resolve) => {
8
8
  setTimeout(() => { resolve(); }, timeMs);
9
9
  });
@@ -1,10 +1,10 @@
1
1
  // eslint-disable-next-line @typescript-eslint/no-shadow
2
2
  import must from "must";
3
3
 
4
+ // @ts-ignore
4
5
  import createSpy from "../test/createSpy";
5
-
6
- import { waitFor } from "./waitFor";
7
6
  import { wait } from "./wait";
7
+ import { waitFor } from "./waitFor";
8
8
 
9
9
  describe("waitFor", () => {
10
10
  it("calls function multiple times until it returns truthy value", async () => {
@@ -91,12 +91,13 @@ describe("waitFor", () => {
91
91
 
92
92
  it("crashes if check function crashes", async () => {
93
93
  await waitFor(() => {
94
+ // @ts-ignore
94
95
  throw new Error(5);
95
- }, 40).then(() => {
96
+ }, { interval: 40 }).then(() => {
96
97
  throw new Error("Should not resolve");
97
- }, (e) => {
98
+ }, (e: unknown) => {
98
99
  must(e).instanceOf(Error);
99
- must(e.message).equal("[waitFor] check function threw an error");
100
+ must((e as Error).message).equal("[waitFor] check function threw an error");
100
101
  });
101
102
  });
102
103
 
@@ -134,11 +135,11 @@ describe("waitFor", () => {
134
135
  it("by crashing if check function returns rejected promise", async () => {
135
136
  await waitFor(() => {
136
137
  return Promise.reject(new Error("oops"));
137
- }, 40).then(() => {
138
+ }, { interval: 40 }).then(() => {
138
139
  throw new Error("Should not resolve");
139
- }, (e) => {
140
+ }, (e: unknown) => {
140
141
  must(e).instanceOf(Error);
141
- must(e.message).equal("[waitFor] check function threw an error");
142
+ must((e as Error).message).equal("[waitFor] check function threw an error");
142
143
  });
143
144
  });
144
145
  });
package/src/waitFor.ts CHANGED
@@ -39,7 +39,7 @@ const defaultOptions: Required<Options> = {
39
39
  * @param fn - callback function
40
40
  * @param options - options object
41
41
  */
42
- const waitFor = <T>(fn: () => MaybePromise<T>, options: Options = defaultOptions) => {
42
+ const waitFor = <T>(fn: () => MaybePromise<T>, options: Options = defaultOptions): Promise<T> => {
43
43
  return new Promise<T>((resolve, reject) => {
44
44
  let intervalTimer: TTimeout, failTimer: TTimeout;
45
45
 
@@ -1,3 +1,5 @@
1
+ import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
2
+
1
3
  import { waitSync } from "./waitSync.js";
2
4
 
3
5
  describe("wait", () => {
package/src/waitSync.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * @param {number} timeMs - time to wait
4
4
  * @returns {Promise<void>}
5
5
  */
6
- const waitSync = (timeMs = 0) => {
6
+ const waitSync = (timeMs = 0): void => {
7
7
  const s = Date.now();
8
8
  // eslint-disable-next-line no-empty
9
9
  while (Date.now() - s < timeMs) {}
package/.prettierignore DELETED
@@ -1,3 +0,0 @@
1
- *.js
2
- *.ts
3
- *.tsx
package/.prettierrc.json DELETED
@@ -1 +0,0 @@
1
- {}