@ezez/utils 1.0.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 (391) hide show
  1. package/.prettierignore +3 -0
  2. package/.prettierrc.json +1 -0
  3. package/CHANGELOG.md +244 -0
  4. package/LICENSE +21 -0
  5. package/README.md +79 -0
  6. package/babel.config.cjs +6 -0
  7. package/dist/cap.d.ts +3 -0
  8. package/dist/cap.d.ts.map +1 -0
  9. package/dist/cap.js +14 -0
  10. package/dist/cap.js.map +1 -0
  11. package/dist/capitalize.d.ts +3 -0
  12. package/dist/capitalize.d.ts.map +1 -0
  13. package/dist/capitalize.js +9 -0
  14. package/dist/capitalize.js.map +1 -0
  15. package/dist/coalesce.d.ts +3 -0
  16. package/dist/coalesce.d.ts.map +1 -0
  17. package/dist/coalesce.js +14 -0
  18. package/dist/coalesce.js.map +1 -0
  19. package/dist/ensureArray.d.ts +3 -0
  20. package/dist/ensureArray.d.ts.map +1 -0
  21. package/dist/ensureArray.js +11 -0
  22. package/dist/ensureArray.js.map +1 -0
  23. package/dist/ensureError.d.ts +3 -0
  24. package/dist/ensureError.d.ts.map +1 -0
  25. package/dist/ensureError.js +11 -0
  26. package/dist/ensureError.js.map +1 -0
  27. package/dist/get.d.ts +7 -0
  28. package/dist/get.d.ts.map +1 -0
  29. package/dist/get.js +19 -0
  30. package/dist/get.js.map +1 -0
  31. package/dist/getMultiple.d.ts +7 -0
  32. package/dist/getMultiple.d.ts.map +1 -0
  33. package/dist/getMultiple.js +18 -0
  34. package/dist/getMultiple.js.map +1 -0
  35. package/dist/index.d.ts +33 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +49 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/insertSeparator.d.ts +3 -0
  40. package/dist/insertSeparator.d.ts.map +1 -0
  41. package/dist/insertSeparator.js +18 -0
  42. package/dist/insertSeparator.js.map +1 -0
  43. package/dist/isEmpty.d.ts +3 -0
  44. package/dist/isEmpty.d.ts.map +1 -0
  45. package/dist/isEmpty.js +26 -0
  46. package/dist/isEmpty.js.map +1 -0
  47. package/dist/isPlainObject.d.ts +3 -0
  48. package/dist/isPlainObject.d.ts.map +1 -0
  49. package/dist/isPlainObject.js +10 -0
  50. package/dist/isPlainObject.js.map +1 -0
  51. package/dist/last.d.ts +3 -0
  52. package/dist/last.d.ts.map +1 -0
  53. package/dist/last.js +6 -0
  54. package/dist/last.js.map +1 -0
  55. package/dist/mapAsync.d.ts +3 -0
  56. package/dist/mapAsync.d.ts.map +1 -0
  57. package/dist/mapAsync.js +22 -0
  58. package/dist/mapAsync.js.map +1 -0
  59. package/dist/mapValues.d.ts +6 -0
  60. package/dist/mapValues.d.ts.map +1 -0
  61. package/dist/mapValues.js +18 -0
  62. package/dist/mapValues.js.map +1 -0
  63. package/dist/match.d.ts +8 -0
  64. package/dist/match.d.ts.map +1 -0
  65. package/dist/match.js +20 -0
  66. package/dist/match.js.map +1 -0
  67. package/dist/merge.d.ts +15 -0
  68. package/dist/merge.d.ts.map +1 -0
  69. package/dist/merge.js +28 -0
  70. package/dist/merge.js.map +1 -0
  71. package/dist/mostFrequent.d.ts +3 -0
  72. package/dist/mostFrequent.d.ts.map +1 -0
  73. package/dist/mostFrequent.js +21 -0
  74. package/dist/mostFrequent.js.map +1 -0
  75. package/dist/noop.d.ts +3 -0
  76. package/dist/noop.d.ts.map +1 -0
  77. package/dist/noop.js +6 -0
  78. package/dist/noop.js.map +1 -0
  79. package/dist/omit.d.ts +5 -0
  80. package/dist/omit.d.ts.map +1 -0
  81. package/dist/omit.js +21 -0
  82. package/dist/omit.js.map +1 -0
  83. package/dist/package.json +1 -0
  84. package/dist/pick.d.ts +5 -0
  85. package/dist/pick.d.ts.map +1 -0
  86. package/dist/pick.js +20 -0
  87. package/dist/pick.js.map +1 -0
  88. package/dist/pull.d.ts +3 -0
  89. package/dist/pull.d.ts.map +1 -0
  90. package/dist/pull.js +14 -0
  91. package/dist/pull.js.map +1 -0
  92. package/dist/remove.d.ts +3 -0
  93. package/dist/remove.d.ts.map +1 -0
  94. package/dist/remove.js +18 -0
  95. package/dist/remove.js.map +1 -0
  96. package/dist/rethrow.d.ts +3 -0
  97. package/dist/rethrow.d.ts.map +1 -0
  98. package/dist/rethrow.js +8 -0
  99. package/dist/rethrow.js.map +1 -0
  100. package/dist/scale.d.ts +3 -0
  101. package/dist/scale.d.ts.map +1 -0
  102. package/dist/scale.js +8 -0
  103. package/dist/scale.js.map +1 -0
  104. package/dist/seq.d.ts +8 -0
  105. package/dist/seq.d.ts.map +1 -0
  106. package/dist/seq.js +45 -0
  107. package/dist/seq.js.map +1 -0
  108. package/dist/set.d.ts +7 -0
  109. package/dist/set.d.ts.map +1 -0
  110. package/dist/set.js +25 -0
  111. package/dist/set.js.map +1 -0
  112. package/dist/setImmutable.d.ts +8 -0
  113. package/dist/setImmutable.d.ts.map +1 -0
  114. package/dist/setImmutable.js +59 -0
  115. package/dist/setImmutable.js.map +1 -0
  116. package/dist/sortBy.d.ts +3 -0
  117. package/dist/sortBy.d.ts.map +1 -0
  118. package/dist/sortBy.js +15 -0
  119. package/dist/sortBy.js.map +1 -0
  120. package/dist/throttle.d.ts +13 -0
  121. package/dist/throttle.d.ts.map +1 -0
  122. package/dist/throttle.js +82 -0
  123. package/dist/throttle.js.map +1 -0
  124. package/dist/truthy.d.ts +3 -0
  125. package/dist/truthy.d.ts.map +1 -0
  126. package/dist/truthy.js +8 -0
  127. package/dist/truthy.js.map +1 -0
  128. package/dist/wait.d.ts +3 -0
  129. package/dist/wait.d.ts.map +1 -0
  130. package/dist/wait.js +10 -0
  131. package/dist/wait.js.map +1 -0
  132. package/dist/waitFor.d.ts +3 -0
  133. package/dist/waitFor.d.ts.map +1 -0
  134. package/dist/waitFor.js +34 -0
  135. package/dist/waitFor.js.map +1 -0
  136. package/dist/waitSync.d.ts +3 -0
  137. package/dist/waitSync.d.ts.map +1 -0
  138. package/dist/waitSync.js +9 -0
  139. package/dist/waitSync.js.map +1 -0
  140. package/docs/.nojekyll +1 -0
  141. package/docs/assets/highlight.css +78 -0
  142. package/docs/assets/main.js +58 -0
  143. package/docs/assets/pages.css +14 -0
  144. package/docs/assets/search.js +1 -0
  145. package/docs/assets/style.css +1280 -0
  146. package/docs/functions/cap.html +116 -0
  147. package/docs/functions/capitalize.html +121 -0
  148. package/docs/functions/coalesce.html +127 -0
  149. package/docs/functions/ensureArray.html +114 -0
  150. package/docs/functions/ensureError.html +110 -0
  151. package/docs/functions/get.html +138 -0
  152. package/docs/functions/getMultiple.html +129 -0
  153. package/docs/functions/insertSeparator.html +123 -0
  154. package/docs/functions/isEmpty.html +139 -0
  155. package/docs/functions/isPlainObject.html +108 -0
  156. package/docs/functions/last.html +126 -0
  157. package/docs/functions/mapAsync.html +144 -0
  158. package/docs/functions/mapValues.html +130 -0
  159. package/docs/functions/match.html +122 -0
  160. package/docs/functions/merge.html +464 -0
  161. package/docs/functions/mostFrequent.html +111 -0
  162. package/docs/functions/noop.html +101 -0
  163. package/docs/functions/omit.html +129 -0
  164. package/docs/functions/pick.html +129 -0
  165. package/docs/functions/pull.html +113 -0
  166. package/docs/functions/remove.html +129 -0
  167. package/docs/functions/rethrow.html +106 -0
  168. package/docs/functions/scale.html +117 -0
  169. package/docs/functions/seq.html +128 -0
  170. package/docs/functions/seqEarlyBreak.html +126 -0
  171. package/docs/functions/set.html +138 -0
  172. package/docs/functions/setImmutable.html +137 -0
  173. package/docs/functions/sortBy.html +135 -0
  174. package/docs/functions/throttle.html +131 -0
  175. package/docs/functions/truthy.html +118 -0
  176. package/docs/functions/wait.html +109 -0
  177. package/docs/functions/waitFor.html +130 -0
  178. package/docs/functions/waitSync.html +109 -0
  179. package/docs/index.html +182 -0
  180. package/docs/interfaces/GetMultipleSource.html +106 -0
  181. package/docs/interfaces/GetSource.html +106 -0
  182. package/docs/interfaces/SetImmutableSource.html +106 -0
  183. package/docs/interfaces/SetSource.html +106 -0
  184. package/docs/interfaces/ThrottleOptions.html +80 -0
  185. package/docs/interfaces/ThrottledFunctionExtras.html +96 -0
  186. package/docs/modules.html +152 -0
  187. package/docs/pages/Introduction.html +94 -0
  188. package/docs/types/MapValuesFn.html +134 -0
  189. package/docs/types/MatchCallback.html +113 -0
  190. package/docs/types/SeqEarlyBreaker.html +114 -0
  191. package/docs/types/SeqFn.html +112 -0
  192. package/docs/types/SeqFunctions.html +105 -0
  193. package/docs/types/SetImmutablePath.html +99 -0
  194. package/docs/types/ThrottledFunction.html +113 -0
  195. package/docs/variables/mapValuesUNSET.html +101 -0
  196. package/docs/variables/mergeUNSET.html +103 -0
  197. package/esm/cap.d.ts +3 -0
  198. package/esm/cap.d.ts.map +1 -0
  199. package/esm/cap.js +11 -0
  200. package/esm/cap.js.map +1 -0
  201. package/esm/capitalize.d.ts +3 -0
  202. package/esm/capitalize.d.ts.map +1 -0
  203. package/esm/capitalize.js +6 -0
  204. package/esm/capitalize.js.map +1 -0
  205. package/esm/coalesce.d.ts +3 -0
  206. package/esm/coalesce.d.ts.map +1 -0
  207. package/esm/coalesce.js +11 -0
  208. package/esm/coalesce.js.map +1 -0
  209. package/esm/ensureArray.d.ts +3 -0
  210. package/esm/ensureArray.d.ts.map +1 -0
  211. package/esm/ensureArray.js +8 -0
  212. package/esm/ensureArray.js.map +1 -0
  213. package/esm/ensureError.d.ts +3 -0
  214. package/esm/ensureError.d.ts.map +1 -0
  215. package/esm/ensureError.js +8 -0
  216. package/esm/ensureError.js.map +1 -0
  217. package/esm/get.d.ts +7 -0
  218. package/esm/get.d.ts.map +1 -0
  219. package/esm/get.js +16 -0
  220. package/esm/get.js.map +1 -0
  221. package/esm/getMultiple.d.ts +7 -0
  222. package/esm/getMultiple.d.ts.map +1 -0
  223. package/esm/getMultiple.js +15 -0
  224. package/esm/getMultiple.js.map +1 -0
  225. package/esm/index.d.ts +33 -0
  226. package/esm/index.d.ts.map +1 -0
  227. package/esm/index.js +33 -0
  228. package/esm/index.js.map +1 -0
  229. package/esm/insertSeparator.d.ts +3 -0
  230. package/esm/insertSeparator.d.ts.map +1 -0
  231. package/esm/insertSeparator.js +15 -0
  232. package/esm/insertSeparator.js.map +1 -0
  233. package/esm/isEmpty.d.ts +3 -0
  234. package/esm/isEmpty.d.ts.map +1 -0
  235. package/esm/isEmpty.js +23 -0
  236. package/esm/isEmpty.js.map +1 -0
  237. package/esm/isPlainObject.d.ts +3 -0
  238. package/esm/isPlainObject.d.ts.map +1 -0
  239. package/esm/isPlainObject.js +7 -0
  240. package/esm/isPlainObject.js.map +1 -0
  241. package/esm/last.d.ts +3 -0
  242. package/esm/last.d.ts.map +1 -0
  243. package/esm/last.js +3 -0
  244. package/esm/last.js.map +1 -0
  245. package/esm/mapAsync.d.ts +3 -0
  246. package/esm/mapAsync.d.ts.map +1 -0
  247. package/esm/mapAsync.js +19 -0
  248. package/esm/mapAsync.js.map +1 -0
  249. package/esm/mapValues.d.ts +6 -0
  250. package/esm/mapValues.d.ts.map +1 -0
  251. package/esm/mapValues.js +14 -0
  252. package/esm/mapValues.js.map +1 -0
  253. package/esm/match.d.ts +8 -0
  254. package/esm/match.d.ts.map +1 -0
  255. package/esm/match.js +17 -0
  256. package/esm/match.js.map +1 -0
  257. package/esm/merge.d.ts +15 -0
  258. package/esm/merge.d.ts.map +1 -0
  259. package/esm/merge.js +24 -0
  260. package/esm/merge.js.map +1 -0
  261. package/esm/mostFrequent.d.ts +3 -0
  262. package/esm/mostFrequent.d.ts.map +1 -0
  263. package/esm/mostFrequent.js +18 -0
  264. package/esm/mostFrequent.js.map +1 -0
  265. package/esm/noop.d.ts +3 -0
  266. package/esm/noop.d.ts.map +1 -0
  267. package/esm/noop.js +3 -0
  268. package/esm/noop.js.map +1 -0
  269. package/esm/omit.d.ts +5 -0
  270. package/esm/omit.d.ts.map +1 -0
  271. package/esm/omit.js +20 -0
  272. package/esm/omit.js.map +1 -0
  273. package/esm/pick.d.ts +5 -0
  274. package/esm/pick.d.ts.map +1 -0
  275. package/esm/pick.js +17 -0
  276. package/esm/pick.js.map +1 -0
  277. package/esm/pull.d.ts +3 -0
  278. package/esm/pull.d.ts.map +1 -0
  279. package/esm/pull.js +11 -0
  280. package/esm/pull.js.map +1 -0
  281. package/esm/remove.d.ts +3 -0
  282. package/esm/remove.d.ts.map +1 -0
  283. package/esm/remove.js +15 -0
  284. package/esm/remove.js.map +1 -0
  285. package/esm/rethrow.d.ts +3 -0
  286. package/esm/rethrow.d.ts.map +1 -0
  287. package/esm/rethrow.js +5 -0
  288. package/esm/rethrow.js.map +1 -0
  289. package/esm/scale.d.ts +3 -0
  290. package/esm/scale.d.ts.map +1 -0
  291. package/esm/scale.js +5 -0
  292. package/esm/scale.js.map +1 -0
  293. package/esm/seq.d.ts +8 -0
  294. package/esm/seq.d.ts.map +1 -0
  295. package/esm/seq.js +41 -0
  296. package/esm/seq.js.map +1 -0
  297. package/esm/set.d.ts +7 -0
  298. package/esm/set.d.ts.map +1 -0
  299. package/esm/set.js +22 -0
  300. package/esm/set.js.map +1 -0
  301. package/esm/setImmutable.d.ts +8 -0
  302. package/esm/setImmutable.d.ts.map +1 -0
  303. package/esm/setImmutable.js +56 -0
  304. package/esm/setImmutable.js.map +1 -0
  305. package/esm/sortBy.d.ts +3 -0
  306. package/esm/sortBy.d.ts.map +1 -0
  307. package/esm/sortBy.js +12 -0
  308. package/esm/sortBy.js.map +1 -0
  309. package/esm/throttle.d.ts +13 -0
  310. package/esm/throttle.d.ts.map +1 -0
  311. package/esm/throttle.js +79 -0
  312. package/esm/throttle.js.map +1 -0
  313. package/esm/truthy.d.ts +3 -0
  314. package/esm/truthy.d.ts.map +1 -0
  315. package/esm/truthy.js +5 -0
  316. package/esm/truthy.js.map +1 -0
  317. package/esm/wait.d.ts +3 -0
  318. package/esm/wait.d.ts.map +1 -0
  319. package/esm/wait.js +7 -0
  320. package/esm/wait.js.map +1 -0
  321. package/esm/waitFor.d.ts +3 -0
  322. package/esm/waitFor.d.ts.map +1 -0
  323. package/esm/waitFor.js +31 -0
  324. package/esm/waitFor.js.map +1 -0
  325. package/esm/waitSync.d.ts +3 -0
  326. package/esm/waitSync.d.ts.map +1 -0
  327. package/esm/waitSync.js +6 -0
  328. package/esm/waitSync.js.map +1 -0
  329. package/package.json +75 -0
  330. package/src/cap.spec.ts +36 -0
  331. package/src/cap.ts +19 -0
  332. package/src/capitalize.spec.ts +18 -0
  333. package/src/capitalize.ts +16 -0
  334. package/src/coalesce.spec.ts +23 -0
  335. package/src/coalesce.ts +21 -0
  336. package/src/ensureArray.spec.ts +87 -0
  337. package/src/ensureArray.ts +13 -0
  338. package/src/ensureError.spec.ts +29 -0
  339. package/src/ensureError.ts +15 -0
  340. package/src/get.spec.ts +183 -0
  341. package/src/get.ts +53 -0
  342. package/src/getMultiple.spec.ts +25 -0
  343. package/src/getMultiple.ts +47 -0
  344. package/src/index.ts +33 -0
  345. package/src/insertSeparator.spec.ts +29 -0
  346. package/src/insertSeparator.ts +22 -0
  347. package/src/isEmpty.spec.ts +130 -0
  348. package/src/isEmpty.ts +50 -0
  349. package/src/isPlainObject.spec.ts +42 -0
  350. package/src/isPlainObject.ts +18 -0
  351. package/src/last.spec.ts +88 -0
  352. package/src/last.ts +12 -0
  353. package/src/mapAsync.spec.ts +39 -0
  354. package/src/mapAsync.ts +41 -0
  355. package/src/mapValues.spec.ts +178 -0
  356. package/src/mapValues.ts +57 -0
  357. package/src/match.spec.ts +11 -0
  358. package/src/match.ts +30 -0
  359. package/src/merge.spec.ts +69 -0
  360. package/src/merge.ts +58 -0
  361. package/src/mostFrequent.spec.ts +35 -0
  362. package/src/mostFrequent.ts +27 -0
  363. package/src/noop.ts +8 -0
  364. package/src/omit.spec.ts +181 -0
  365. package/src/omit.ts +43 -0
  366. package/src/pick.spec.ts +168 -0
  367. package/src/pick.ts +39 -0
  368. package/src/pull.spec.ts +54 -0
  369. package/src/pull.ts +18 -0
  370. package/src/remove.spec.ts +63 -0
  371. package/src/remove.ts +26 -0
  372. package/src/rethrow.ts +12 -0
  373. package/src/scale.ts +18 -0
  374. package/src/seq.spec.ts +157 -0
  375. package/src/seq.ts +104 -0
  376. package/src/set.spec.ts +348 -0
  377. package/src/set.ts +59 -0
  378. package/src/setImmutable.spec.ts +241 -0
  379. package/src/setImmutable.ts +104 -0
  380. package/src/sortBy.spec.ts +56 -0
  381. package/src/sortBy.ts +24 -0
  382. package/src/throttle.spec.ts +136 -0
  383. package/src/throttle.ts +153 -0
  384. package/src/truthy.spec.ts +21 -0
  385. package/src/truthy.ts +13 -0
  386. package/src/wait.spec.ts +21 -0
  387. package/src/wait.ts +14 -0
  388. package/src/waitFor.ts +47 -0
  389. package/src/waitSync.spec.ts +21 -0
  390. package/src/waitSync.ts +14 -0
  391. package/tutorials/Introduction.md +1 -0
@@ -0,0 +1,56 @@
1
+ const isObject = (value) => (typeof value === "object" || typeof value === "function") && value !== null;
2
+ const clone = (value) => {
3
+ if (Array.isArray(value)) {
4
+ return [...value];
5
+ }
6
+ return { ...value };
7
+ };
8
+ const hasOnlyValidPathParts = (array) => {
9
+ if (!array.length) {
10
+ return false;
11
+ }
12
+ return array.every(item => {
13
+ const type = typeof item;
14
+ return (type === "string" && item !== "") || type === "number";
15
+ });
16
+ };
17
+ const getPathParts = (path) => {
18
+ if (typeof path === "number") {
19
+ return [String(path)];
20
+ }
21
+ if (typeof path === "string") {
22
+ return path.split(".");
23
+ }
24
+ if (Array.isArray(path)) {
25
+ return path;
26
+ }
27
+ throw new TypeError("Path must be a string, a number or an array of strings and numbers");
28
+ };
29
+ const setImmutable = (source, path, value) => {
30
+ const pathParts = getPathParts(path);
31
+ const isValidPath = hasOnlyValidPathParts(pathParts);
32
+ if (!isValidPath) {
33
+ throw new TypeError("Path must not be empty or contain empty parts");
34
+ }
35
+ const len = pathParts.length;
36
+ const result = isObject(source) ? clone(source) : {};
37
+ let current = result;
38
+ for (let i = 0; i < len; i++) {
39
+ const isLast = i === len - 1;
40
+ const key = pathParts[i];
41
+ if (isLast) {
42
+ current[key] = value;
43
+ return result;
44
+ }
45
+ if (!isObject(current[key])) {
46
+ current[key] = {};
47
+ }
48
+ else {
49
+ current[key] = clone(current[key]);
50
+ }
51
+ current = current[key];
52
+ }
53
+ return result;
54
+ };
55
+ export { setImmutable };
56
+ //# sourceMappingURL=setImmutable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setImmutable.js","sourceRoot":"","sources":["../src/setImmutable.ts"],"names":[],"mappings":"AAOA,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC,IAAI,KAAK,KAAK,IAAI,CAAC;AAElH,MAAM,KAAK,GAAG,CAAC,KAAyB,EAAE,EAAE;IACxC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QACtB,OAAO,CAAC,GAAG,KAA2B,CAAC,CAAC;KAC3C;IACD,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAAG,CAAC,KAAgB,EAAE,EAAE;IAC/C,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;QACf,OAAO,KAAK,CAAC;KAChB;IACD,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;QACtB,MAAM,IAAI,GAAG,OAAO,IAAI,CAAC;QACzB,OAAO,CAAC,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,EAAE,CAAC,IAAI,IAAI,KAAK,QAAQ,CAAC;IACnE,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAUF,MAAM,YAAY,GAAG,CAAC,IAAU,EAAE,EAAE;IAChC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC1B,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;KACzB;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KAC1B;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B,CAAC,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const sortBy: <T>(propertyName: keyof T, asc?: boolean, defaultValue?: unknown) => (a: T, b: T) => 0 | 1 | -1;
2
+ export { sortBy, };
3
+ //# sourceMappingURL=sortBy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sortBy.d.ts","sourceRoot":"","sources":["../src/sortBy.ts"],"names":[],"mappings":"AAOA,QAAA,MAAM,MAAM,2DAAwD,OAAO,+BAY1E,CAAC;AAEF,OAAO,EACH,MAAM,GACT,CAAC"}
package/esm/sortBy.js ADDED
@@ -0,0 +1,12 @@
1
+ const sortBy = (propertyName, asc = true, defaultValue = null) => (a, b) => {
2
+ var _a, _b;
3
+ if (a[propertyName] === b[propertyName]) {
4
+ return 0;
5
+ }
6
+ if (((_a = a[propertyName]) !== null && _a !== void 0 ? _a : defaultValue) > ((_b = b[propertyName]) !== null && _b !== void 0 ? _b : defaultValue)) {
7
+ return asc ? 1 : -1;
8
+ }
9
+ return asc ? -1 : 1;
10
+ };
11
+ export { sortBy, };
12
+ //# sourceMappingURL=sortBy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sortBy.js","sourceRoot":"","sources":["../src/sortBy.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,GAAG,CAAI,YAAqB,EAAE,GAAG,GAAG,IAAI,EAAE,eAAwB,IAAI,EAAE,EAAE,CAAC,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE;;IAClG,IAAI,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,EAAE;QACrC,OAAO,CAAC,CAAC;KACZ;IAGD,IAAI,CAAC,MAAA,CAAC,CAAC,YAAY,CAAC,mCAAI,YAAY,CAAC,GAAG,CAAC,MAAA,CAAC,CAAC,YAAY,CAAC,mCAAI,YAAY,CAAC,EAAE;QAEvE,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KACvB;IAED,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACxB,CAAC,CAAC;AAEF,OAAO,EACH,MAAM,GACT,CAAC"}
@@ -0,0 +1,13 @@
1
+ interface Opts {
2
+ leading?: boolean;
3
+ trailing?: boolean;
4
+ }
5
+ interface Extras {
6
+ cancel: () => void;
7
+ flush: () => void;
8
+ }
9
+ type CanReturnUndefined<F extends (...args: any[]) => any> = (...args: Parameters<F>) => ReturnType<F> | undefined;
10
+ declare const throttle: <RT, F extends (...args: any[]) => RT>(fn: F, time?: number | [number, ...number[]], options?: Opts) => CanReturnUndefined<F> & Extras;
11
+ export { throttle, };
12
+ export type { Extras as ThrottledFunctionExtras, Opts as ThrottleOptions, CanReturnUndefined as ThrottledFunction, };
13
+ //# sourceMappingURL=throttle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.d.ts","sourceRoot":"","sources":["../src/throttle.ts"],"names":[],"mappings":"AAAA,UAAU,IAAI;IAIV,OAAO,CAAC,EAAE,OAAO,CAAC;IAIlB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,UAAU,MAAM;IAIZ,MAAM,EAAE,MAAM,IAAI,CAAC;IAInB,KAAK,EAAE,MAAM,IAAI,CAAC;CACrB;AAQD,KAAK,kBAAkB,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,IAAI,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;AAoBnH,QAAA,MAAM,QAAQ,2BAA4B,GAAG,EAAE,uBAC9B,MAAM,GAAG,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,YAAgB,IAAI,mCA6FlE,CAAC;AAEF,OAAO,EACH,QAAQ,GACX,CAAC;AAEF,YAAY,EACR,MAAM,IAAI,uBAAuB,EACjC,IAAI,IAAI,eAAe,EACvB,kBAAkB,IAAI,iBAAiB,GAC1C,CAAC"}
@@ -0,0 +1,79 @@
1
+ const defaultOptions = {
2
+ leading: true,
3
+ trailing: true,
4
+ };
5
+ const throttle = (fn, time = 0, options) => {
6
+ var _a, _b;
7
+ const opts = {
8
+ leading: (_a = options === null || options === void 0 ? void 0 : options.leading) !== null && _a !== void 0 ? _a : defaultOptions.leading,
9
+ trailing: (_b = options === null || options === void 0 ? void 0 : options.trailing) !== null && _b !== void 0 ? _b : defaultOptions.trailing,
10
+ };
11
+ if (!opts.trailing && !opts.leading) {
12
+ throw new TypeError("Throttle with trailing & leading options `false` won't do anything.");
13
+ }
14
+ if (typeof time !== "number" && !time.length) {
15
+ throw new TypeError("`time` must be an array with at least one number.");
16
+ }
17
+ const finalTime = typeof time === "number" ? time : time[time.length - 1];
18
+ let lastRun = 0, timeoutId = null, lastResult, lastArgs, lastTime = typeof time === "number" ? time : time[0], leadingCalled = false;
19
+ const times = typeof time === "number" ? [time] : [...time];
20
+ const throttledFn = ((...args) => {
21
+ var _a, _b, _c;
22
+ lastArgs = args;
23
+ if (timeoutId !== null) {
24
+ return lastResult;
25
+ }
26
+ if (!opts.trailing) {
27
+ if (Date.now() - lastRun < lastTime) {
28
+ return lastResult;
29
+ }
30
+ lastTime = (_a = times.shift()) !== null && _a !== void 0 ? _a : finalTime;
31
+ lastRun = Date.now();
32
+ lastResult = fn(...args);
33
+ return lastResult;
34
+ }
35
+ const diffLastRun = Date.now() - lastRun;
36
+ if (opts.leading && (!leadingCalled || typeof time === "number") && diffLastRun >= ((_b = times[0]) !== null && _b !== void 0 ? _b : finalTime)) {
37
+ lastRun = Date.now();
38
+ lastResult = fn(...args);
39
+ leadingCalled = true;
40
+ return lastResult;
41
+ }
42
+ if (lastRun || !opts.leading) {
43
+ lastTime = (_c = times.shift()) !== null && _c !== void 0 ? _c : finalTime;
44
+ }
45
+ timeoutId = setTimeout(() => {
46
+ timeoutId = null;
47
+ lastRun = Date.now();
48
+ lastResult = fn(...args);
49
+ }, lastRun ? (lastTime - diffLastRun + 1) : lastTime);
50
+ return lastResult;
51
+ });
52
+ throttledFn.cancel = () => {
53
+ timeoutId !== null && clearTimeout(timeoutId);
54
+ timeoutId = null;
55
+ lastRun = 0;
56
+ lastResult = undefined;
57
+ times.length = 0;
58
+ if (typeof time === "number") {
59
+ times.push(time);
60
+ }
61
+ else {
62
+ times.push(...time);
63
+ }
64
+ lastTime = typeof time === "number" ? time : time[0];
65
+ };
66
+ throttledFn.flush = () => {
67
+ if (timeoutId !== null) {
68
+ lastRun = Date.now();
69
+ lastResult = fn(...lastArgs);
70
+ clearTimeout(timeoutId);
71
+ timeoutId = null;
72
+ return throttledFn(...lastArgs);
73
+ }
74
+ return lastResult;
75
+ };
76
+ return throttledFn;
77
+ };
78
+ export { throttle, };
79
+ //# sourceMappingURL=throttle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.js","sourceRoot":"","sources":["../src/throttle.ts"],"names":[],"mappings":"AAsBA,MAAM,cAAc,GAAmB;IACnC,OAAO,EAAE,IAAI;IACb,QAAQ,EAAE,IAAI;CACjB,CAAC;AAuBF,MAAM,QAAQ,GAAG,CACb,EAAK,EAAE,OAAuC,CAAC,EAAE,OAAc,EACjC,EAAE;;IAChC,MAAM,IAAI,GAAmB;QACzB,OAAO,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,cAAc,CAAC,OAAO;QACnD,QAAQ,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,mCAAI,cAAc,CAAC,QAAQ;KACzD,CAAC;IAEF,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;QACjC,MAAM,IAAI,SAAS,CAAC,qEAAqE,CAAC,CAAC;KAC9F;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,IAAI,C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AC,CAAC;IACzD,CAAC,CAAC;IACF,WAAW,CAAC,KAAK,GAAG,GAAG,EAAE;QACrB,IAAI,SAAS,KAAK,IAAI,EAAE;YACpB,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACrB,UAAU,GAAG,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC;YAC7B,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,SAAS,GAAG,IAAI,CAAC;YACjB,OAAO,WAAW,CAAC,GAAG,QAAQ,CAAC,CAAC;SACnC;QACD,OAAO,UAAU,CAAC;IACtB,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACvB,CAAC,CAAC;AAEF,OAAO,EACH,QAAQ,GACX,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const truthy: <T>(val: false | "" | 0 | T | null | undefined) => val is T;
2
+ export { truthy, };
3
+ //# sourceMappingURL=truthy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truthy.d.ts","sourceRoot":"","sources":["../src/truthy.ts"],"names":[],"mappings":"AAMA,QAAA,MAAM,MAAM,6DAEX,CAAC;AAEF,OAAO,EACH,MAAM,GACT,CAAC"}
package/esm/truthy.js ADDED
@@ -0,0 +1,5 @@
1
+ const truthy = (val) => {
2
+ return Boolean(val);
3
+ };
4
+ export { truthy, };
5
+ //# sourceMappingURL=truthy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truthy.js","sourceRoot":"","sources":["../src/truthy.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,GAAG,CAAI,GAA0C,EAAY,EAAE;IACvE,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC,CAAC;AAEF,OAAO,EACH,MAAM,GACT,CAAC"}
package/esm/wait.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare const wait: (timeMs?: number) => Promise<void>;
2
+ export { wait, };
3
+ //# sourceMappingURL=wait.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wait.d.ts","sourceRoot":"","sources":["../src/wait.ts"],"names":[],"mappings":"AAKA,QAAA,MAAM,IAAI,oCAIT,CAAC;AAEF,OAAO,EACH,IAAI,GACP,CAAC"}
package/esm/wait.js ADDED
@@ -0,0 +1,7 @@
1
+ const wait = (timeMs = 0) => {
2
+ return new Promise((resolve) => {
3
+ setTimeout(() => { resolve(); }, timeMs);
4
+ });
5
+ };
6
+ export { wait, };
7
+ //# sourceMappingURL=wait.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wait.js","sourceRoot":"","sources":["../src/wait.ts"],"names":[],"mappings":"AAKA,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,EAAE;IACxB,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QACjC,UAAU,CAAC,GAAG,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IAC7C,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,OAAO,EACH,IAAI,GACP,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const waitFor: <T>(fn: () => T, interval?: number, timeout?: number) => Promise<T>;
2
+ export { waitFor };
3
+ //# sourceMappingURL=waitFor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"waitFor.d.ts","sourceRoot":"","sources":["../src/waitFor.ts"],"names":[],"mappings":"AAcA,QAAA,MAAM,OAAO,qEA8BZ,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,CAAC"}
package/esm/waitFor.js ADDED
@@ -0,0 +1,31 @@
1
+ const DEFAULT_INTERVAL = 50;
2
+ const waitFor = (fn, interval = DEFAULT_INTERVAL, timeout = Infinity) => {
3
+ return new Promise((resolve, reject) => {
4
+ let intervalTimer, failTimer;
5
+ if (isFinite(timeout)) {
6
+ failTimer = setTimeout(() => {
7
+ reject(new Error("[waitFor] Timeout"));
8
+ clearInterval(intervalTimer);
9
+ }, timeout);
10
+ }
11
+ intervalTimer = setInterval(() => {
12
+ try {
13
+ const result = fn();
14
+ if (result) {
15
+ clearTimeout(failTimer);
16
+ clearInterval(intervalTimer);
17
+ resolve(result);
18
+ }
19
+ }
20
+ catch (error) {
21
+ clearTimeout(failTimer);
22
+ clearInterval(intervalTimer);
23
+ const e = new Error("[waitFor] check function threw an error");
24
+ e.details = { error };
25
+ reject(e);
26
+ }
27
+ }, interval);
28
+ });
29
+ };
30
+ export { waitFor };
31
+ //# sourceMappingURL=waitFor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"waitFor.js","sourceRoot":"","sources":["../src/waitFor.ts"],"names":[],"mappings":"AAAA,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAc5B,MAAM,OAAO,GAAG,CAAI,EAAW,EAAE,QAAQ,GAAG,gBAAgB,EAAE,OAAO,GAAG,QAAQ,EAAE,EAAE;IAChF,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACtC,IAAI,aAAwB,EAAE,SAAmB,CAAC;QAElD,IAAI,QAAQ,CAAC,OAAO,CAAC,EAAE;YACnB,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;gBACxB,MAAM,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC;gBACvC,aAAa,CAAC,aAAa,CAAC,CAAC;YACjC,CAAC,EAAE,OAAO,CAAC,CAAC;SACf;QAED,aAAa,GAAG,WAAW,CAAC,GAAG,EAAE;YAC7B,IAAI;gBACA,MAAM,MAAM,GAAG,EAAE,EAAE,CAAC;gBACpB,IAAI,MAAM,EAAE;oBACR,YAAY,CAAC,SAAS,CAAC,CAAC;oBACxB,aAAa,CAAC,aAAa,CAAC,CAAC;oBAC7B,OAAO,CAAC,MAAM,CAAC,CAAC;iBACnB;aACJ;YACD,OAAO,KAAc,EAAE;gBACnB,YAAY,CAAC,SAAS,CAAC,CAAC;gBACxB,aAAa,CAAC,aAAa,CAAC,CAAC;gBAE7B,MAAM,CAAC,GAAkC,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;gBAC9F,CAAC,CAAC,OAAO,GAAG,EAAE,KAAK,EAAE,CAAC;gBACtB,MAAM,CAAC,CAAC,CAAC,CAAC;aACb;QACL,CAAC,EAAE,QAAQ,CAAC,CAAC;IACjB,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const waitSync: (timeMs?: number) => void;
2
+ export { waitSync, };
3
+ //# sourceMappingURL=waitSync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"waitSync.d.ts","sourceRoot":"","sources":["../src/waitSync.ts"],"names":[],"mappings":"AAKA,QAAA,MAAM,QAAQ,2BAIb,CAAC;AAEF,OAAO,EACH,QAAQ,GACX,CAAC"}
@@ -0,0 +1,6 @@
1
+ const waitSync = (timeMs = 0) => {
2
+ const s = Date.now();
3
+ while (Date.now() - s < timeMs) { }
4
+ };
5
+ export { waitSync, };
6
+ //# sourceMappingURL=waitSync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"waitSync.js","sourceRoot":"","sources":["../src/waitSync.ts"],"names":[],"mappings":"AAKA,MAAM,QAAQ,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,EAAE;IAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAErB,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,MAAM,EAAE,GAAE;AACtC,CAAC,CAAC;AAEF,OAAO,EACH,QAAQ,GACX,CAAC"}
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "@ezez/utils",
3
+ "version": "1.0.0",
4
+ "repository": "https://github.com/dzek69/bottom-line.git",
5
+ "author": "Jacek Nowacki @dzek69 <git-public@dzek.eu>",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "test": "NODE_ENV=test jest",
9
+ "test:lodashImports": "node test/lodash-tree.cjs",
10
+ "docs": "typedoc src/index.ts --skipErrorChecking --out docs --includeVersion --pluginPages ./pagesconfig.json",
11
+ "compile": "yarn compile:esm && yarn compile:cjs",
12
+ "compile:esm": "rm -rf esm && tsc --project tsconfig.esm.json && node ./build-scripts/compile.esm.after.mjs",
13
+ "compile:cjs": "rm -rf dist && tsc --project tsconfig.cjs.json && node ./build-scripts/compile.cjs.after.mjs",
14
+ "typecheck": "tsc --noEmit",
15
+ "lint": "eslint src --ext .ts,.tsx,.js,.jsx,.mjs",
16
+ "lint:fix": "yarn lint --fix",
17
+ "prepack": "yarn compile",
18
+ "prepublishOnly": "yarn audit && yarn lint && yarn test && yarn docs",
19
+ "start:dev": "nodemon",
20
+ "start:dev:compatibility": "TS_NODE_FILES=true yarn start:dev",
21
+ "prepare": "husky install",
22
+ "updates": "npx --yes npm-check-updates --dep prod",
23
+ "updates:dev": "npx --yes npm-check-updates --dep dev",
24
+ "updates:all": "npx --yes npm-check-updates"
25
+ },
26
+ "exports": {
27
+ ".": {
28
+ "require": "./dist/index.js",
29
+ "types": "./esm/index.d.ts",
30
+ "default": "./esm/index.js"
31
+ }
32
+ },
33
+ "main": "./dist/index.js",
34
+ "types": "./esm/index.d.ts",
35
+ "module": "./esm/index.js",
36
+ "type": "module",
37
+ "devDependencies": {
38
+ "@babel/core": "^7.19.3",
39
+ "@babel/preset-env": "^7.19.3",
40
+ "@babel/preset-typescript": "^7.18.6",
41
+ "@dzek69/eslint-config-base": "^2.3.0",
42
+ "@dzek69/eslint-config-typescript": "^1.0.2",
43
+ "@types/jest": "^29.0.3",
44
+ "@types/lodash": "^4.14.168",
45
+ "@typescript-eslint/eslint-plugin": "^5.38.1",
46
+ "@typescript-eslint/parser": "^5.38.1",
47
+ "babel-plugin-module-extension": "^0.1.3",
48
+ "eslint": "^8.24.0",
49
+ "fs-extra": "^10.1.0",
50
+ "husky": "^8.0.1",
51
+ "jest": "^29.1.1",
52
+ "lodash": "^4.17.21",
53
+ "must": "^0.13.4",
54
+ "nodemon": "^2.0.20",
55
+ "ts-node": "^10.9.1",
56
+ "typedoc": "^0.23.15",
57
+ "typescript": "^4.9.5",
58
+ "eslint-plugin-import": "^2.26.0",
59
+ "@dzek69/eslint-config-import": "^1.0.0",
60
+ "@dzek69/eslint-config-import-typescript": "^1.0.0",
61
+ "@knodes/typedoc-plugin-pages": "^0.23.1",
62
+ "prettier": "^2.8.3"
63
+ },
64
+ "husky": {
65
+ "hooks": {
66
+ "pre-push": "yarn prepublishOnly && yarn compile"
67
+ }
68
+ },
69
+ "libraryTemplate": {
70
+ "version": "3.8.0",
71
+ "language": "typescript",
72
+ "fixDefaultForCommonJS": false,
73
+ "jsx": false
74
+ }
75
+ }
@@ -0,0 +1,36 @@
1
+ import { cap } from "./cap.js";
2
+
3
+ describe("cap", () => {
4
+ it("caps too big values", () => {
5
+ cap(200, 100).must.equal(100);
6
+ cap(101, 100).must.equal(100);
7
+ cap(100.0001, 100).must.equal(100);
8
+ });
9
+
10
+ it("leaves not big values", () => {
11
+ cap(100, 100).must.equal(100);
12
+ cap(99.999, 100).must.equal(99.999);
13
+ });
14
+
15
+ it("caps too small values", () => {
16
+ cap(-100, 100, 5).must.equal(5);
17
+ cap(4, 100, 5).must.equal(5);
18
+ });
19
+
20
+ it("leaves not too small values", () => {
21
+ cap(6, 100, 5).must.equal(6);
22
+ cap(5, 100, 5).must.equal(5);
23
+ cap(23, 100, 5).must.equal(23);
24
+ });
25
+
26
+ it("throws when max expected value is lower than min expected value", () => {
27
+ (() => cap(6.4, 5, 7)).must.throw();
28
+ });
29
+
30
+ it("allows the same min and max value", function() {
31
+ (() => cap(5, 5, 5)).must.not.throw();
32
+ cap(5, 5, 5).must.equal(5);
33
+ cap(4, 5, 5).must.equal(5);
34
+ cap(6, 5, 5).must.equal(5);
35
+ });
36
+ });
package/src/cap.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Returns a number capped between an upper and lower limit.
3
+ * @param {number} value - value to cap
4
+ * @param {number} upperLimit - upper limit, inclusive
5
+ * @param {number} [lowerLimit] - lower limit, inclusive
6
+ */
7
+ const cap = (value: number, upperLimit: number, lowerLimit?: number) => {
8
+ if (lowerLimit == null) {
9
+ return Math.min(value, upperLimit);
10
+ }
11
+ if (upperLimit < lowerLimit) {
12
+ throw new TypeError("upperLimit must be greater than lowerLimit");
13
+ }
14
+ return Math.min(upperLimit, Math.max(lowerLimit, value));
15
+ };
16
+
17
+ export {
18
+ cap,
19
+ };
@@ -0,0 +1,18 @@
1
+ import { capitalize } from "./capitalize.js";
2
+
3
+ describe("capitalize", () => {
4
+ it("capitalizes first letter", () => {
5
+ const string = "abcd";
6
+ capitalize(string).must.equal("Abcd");
7
+ });
8
+
9
+ it("doesn't touch other characters by default", function() {
10
+ const string = "aBcD";
11
+ capitalize(string).must.equal("ABcD");
12
+ });
13
+
14
+ it("converts other to small caps if reqested", function() {
15
+ const string = "aBcD";
16
+ capitalize(string, true).must.equal("Abcd");
17
+ });
18
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Capitalize the first letter of a string.
3
+ * @param {string} text - source string
4
+ * @param {boolean} [restLowercase=false] - should the rest of the string be forced to lowercase?
5
+ * @example capitalize("hello") // "Hello"
6
+ * @example capitalize("hello my Friend", true) // "Hello my friend"
7
+ * @example capitalize("hello my Friend", false) // "Hello my Friend"
8
+ */
9
+ const capitalize = (text: string, restLowercase = false) => {
10
+ const rest = restLowercase ? text.slice(1).toLowerCase() : text.slice(1);
11
+ return text.charAt(0).toUpperCase() + rest;
12
+ };
13
+
14
+ export {
15
+ capitalize,
16
+ };
@@ -0,0 +1,23 @@
1
+ import { coalesce } from "./coalesce.js";
2
+
3
+ describe("coalesce", () => {
4
+ it("works with basic values", () => {
5
+ coalesce(undefined, null, 69).must.equal(69);
6
+ coalesce(null, null, undefined, 69, null).must.equal(69);
7
+ });
8
+
9
+ it("returns falsy values", () => {
10
+ coalesce(undefined, null, false, 5).must.equal(false);
11
+ coalesce(undefined, null, 0, 5).must.equal(0);
12
+ coalesce(undefined, null, "", 5).must.equal("");
13
+ coalesce(undefined, null, NaN, 5).must.be.nan();
14
+ });
15
+
16
+ it("returns null if no non-nil value is found", () => {
17
+ (coalesce(null, undefined) === null).must.be.true();
18
+ });
19
+
20
+ it("returns null if no arguments are given", () => {
21
+ (coalesce() === null).must.be.true();
22
+ });
23
+ });
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Returns first non-nil (not undefined, not null) value from given arguments.
3
+ *
4
+ * @param {...*} args - values
5
+ * @example coalesce(null, undefined, void 0, 5); // returns 5
6
+ * @example coalesce(4, null, 6, undefined); // returns 4
7
+ * @example coalesce(undefined); // returns null
8
+ * @example coalesce(); // returns null
9
+ * @returns {*|null} first non-nil value or null
10
+ */
11
+ const coalesce = <T>(...args: T[]): NonNullable<T> | null => {
12
+ for (let i = 0; i < args.length; i++) {
13
+ const item = args[i];
14
+ if (item != null) {
15
+ return item;
16
+ }
17
+ }
18
+ return null;
19
+ };
20
+
21
+ export { coalesce };
@@ -0,0 +1,87 @@
1
+ import { ensureArray } from "./ensureArray.js";
2
+
3
+ describe("ensureArray", () => {
4
+ it("must return given value untouched if the value is an array", () => {
5
+ singleValue: {
6
+ const x = [5];
7
+ const result = ensureArray(x);
8
+ result.must.be.an.array();
9
+ result.must.have.length(1);
10
+ result.must.equal(x);
11
+ result.must.eql([5]);
12
+ }
13
+
14
+ noValue: {
15
+ const x = [];
16
+ const result = ensureArray(x);
17
+ result.must.be.an.array();
18
+ result.must.have.length(0);
19
+ result.must.equal(x);
20
+ }
21
+
22
+ manyValues: {
23
+ const x = [1, "5"];
24
+ const result = ensureArray(x);
25
+ result.must.be.an.array();
26
+ result.must.have.length(2);
27
+ result.must.equal(x);
28
+ result.must.eql([1, "5"]);
29
+ }
30
+
31
+ arrayOfArrays: {
32
+ const x = [[1], [2]];
33
+ const result = ensureArray(x);
34
+ result.must.be.an.array();
35
+ result.must.have.length(2);
36
+ result.must.equal(x);
37
+ result.must.eql([[1], [2]]);
38
+ }
39
+ });
40
+
41
+ it("must arrarize object", () => {
42
+ const y = { title: "dzek" };
43
+ const result = ensureArray(y);
44
+ result.must.be.an.array();
45
+ result.must.have.length(1);
46
+ result[0].must.equal(y);
47
+ result.must.eql([{ title: "dzek" }]);
48
+ });
49
+
50
+ it("must arrarize function", () => {
51
+ const y = () => 5;
52
+
53
+ const result = ensureArray(y);
54
+ result.must.be.an.array();
55
+ result.must.have.length(1);
56
+ result[0].must.equal(y);
57
+ result.must.eql([y]);
58
+ });
59
+
60
+ it("must arrarize null", () => {
61
+ const result = ensureArray(null);
62
+ result.must.be.an.array();
63
+ result.must.have.length(1);
64
+ result.must.eql([null]);
65
+ });
66
+
67
+ it("must arrarize string", () => {
68
+ const result = ensureArray("hey joe");
69
+ result.must.be.an.array();
70
+ result.must.have.length(1);
71
+ result.must.eql(["hey joe"]);
72
+ });
73
+
74
+ it("must arrarize number", () => {
75
+ const result = ensureArray(123);
76
+ result.must.be.an.array();
77
+ result.must.have.length(1);
78
+ result.must.eql([123]);
79
+ });
80
+
81
+ it("must arrarize NaN", () => {
82
+ const result = ensureArray(NaN);
83
+ result.must.be.an.array();
84
+ result.must.have.length(1);
85
+ result.must.eql([NaN]);
86
+ });
87
+ });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Wraps value in an array until the value is already an array
3
+ * @param {*} value - value to be wrapped
4
+ * @returns {Array}
5
+ */
6
+ const ensureArray = <T>(value: T | T[]): T[] => {
7
+ if (Array.isArray(value)) {
8
+ return value;
9
+ }
10
+ return [value];
11
+ };
12
+
13
+ export { ensureArray };
@@ -0,0 +1,29 @@
1
+ import { ensureError } from "./ensureError.js";
2
+
3
+ describe("ensureError", () => {
4
+ it("returns unmodified error if error given", () => {
5
+ const e = new Error("Test");
6
+
7
+ const result = ensureError(e);
8
+ (e === result).must.be.true();
9
+ result.message.must.equal("Test");
10
+ });
11
+
12
+ it("returns error on anything else than error", () => {
13
+ const eNull = ensureError(null);
14
+ eNull.must.be.instanceof(Error);
15
+ eNull.message.must.equal("Expected error instance, got something else: null");
16
+
17
+ const eUndefined = ensureError(undefined);
18
+ eUndefined.must.be.instanceof(Error);
19
+ eUndefined.message.must.equal("Expected error instance, got something else: undefined");
20
+
21
+ const eNumber = ensureError(6);
22
+ eNumber.must.be.instanceof(Error);
23
+ eNumber.message.must.equal("Expected error instance, got something else: 6");
24
+
25
+ const eFunction = ensureError(() => {});
26
+ eFunction.must.be.instanceof(Error);
27
+ eFunction.message.must.equal("Expected error instance, got something else: () => {}");
28
+ });
29
+ });