@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,181 @@
1
+ import { omit as _omit } from "lodash";
2
+
3
+ import { omit } from "./omit.js";
4
+
5
+ describe("omit", () => {
6
+ it("returns empty object when source is not given, behaves like lodash", () => {
7
+ caseundefined: {
8
+ const value = omit(undefined, ["name", "count"]);
9
+ value.must.eql({});
10
+
11
+ const _value = _omit(undefined, ["name", "count"]);
12
+ _value.must.eql({});
13
+ }
14
+
15
+ casenull: {
16
+ const value = omit(null, ["name", "count"]);
17
+ value.must.eql({});
18
+
19
+ const _value = _omit(null, ["name", "count"]);
20
+ _value.must.eql({});
21
+ }
22
+ });
23
+
24
+ it("returns cloned object when properties are not given, behaves like lodash", () => {
25
+ const source = {
26
+ title: "Master od disaster",
27
+ count: 1,
28
+ };
29
+
30
+ caseundefined: {
31
+ const value = omit(source);
32
+ value.must.eql({
33
+ title: "Master od disaster",
34
+ count: 1,
35
+ });
36
+
37
+ const _value = _omit(source);
38
+ _value.must.eql({
39
+ title: "Master od disaster",
40
+ count: 1,
41
+ });
42
+ }
43
+
44
+ caseempty: {
45
+ const value = omit(source, []);
46
+ value.must.eql({
47
+ title: "Master od disaster",
48
+ count: 1,
49
+ });
50
+
51
+ const _value = _omit(source, []);
52
+ _value.must.eql({
53
+ title: "Master od disaster",
54
+ count: 1,
55
+ });
56
+ }
57
+ });
58
+
59
+ it("returns empty object when source is number, behaves like lodash", () => {
60
+ const value = omit(2, ["title"]);
61
+ value.must.eql({});
62
+
63
+ const _value = _omit(2, ["title"]);
64
+ _value.must.eql({});
65
+ });
66
+
67
+ it("returns empty object when source is string, behaves NOT like lodash", () => {
68
+ const value = omit("two", ["title"]);
69
+ value.must.eql({});
70
+
71
+ const _value = _omit("two", ["title"]);
72
+ _value.must.eql({ 0: "t", 1: "w", 2: "o" });
73
+ });
74
+
75
+ it("returns new object when source is function, behaves like lodash", () => {
76
+ const source = () => {};
77
+ source.title = "ABCD";
78
+ source.weight = "XXX";
79
+
80
+ const value = omit(source, ["title"]);
81
+ value.must.eql({
82
+ weight: "XXX",
83
+ });
84
+
85
+ const _value = _omit(source, ["title"]);
86
+ _value.must.eql({
87
+ weight: "XXX",
88
+ });
89
+ });
90
+
91
+ it("returns new object without given properties, behaves like lodash", () => {
92
+ const source = {
93
+ a: 5,
94
+ b: "string",
95
+ c: undefined,
96
+ d: 0,
97
+ e: null,
98
+ f: false,
99
+ };
100
+
101
+ const value = omit(source, ["a", "d", "g"]);
102
+ value.must.eql({
103
+ b: "string",
104
+ c: undefined,
105
+ e: null,
106
+ f: false,
107
+ });
108
+ value.must.not.equal(source);
109
+
110
+ const _value = _omit(source, ["a", "d", "g"]);
111
+ _value.must.eql({
112
+ b: "string",
113
+ c: undefined,
114
+ e: null,
115
+ f: false,
116
+ });
117
+ _value.must.not.equal(source);
118
+ });
119
+
120
+ it("returns inherited properties (as own), behaves like lodash", () => {
121
+ const Test = function() {};
122
+ Test.prototype.name = "xxx";
123
+
124
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
125
+ const testInstance = new Test();
126
+ testInstance.length = 100;
127
+
128
+ const value = omit(testInstance, ["title"]);
129
+ value.must.eql({
130
+ name: "xxx",
131
+ length: 100,
132
+ });
133
+ value.hasOwnProperty("name").must.be.true();
134
+ value.hasOwnProperty("length").must.be.true();
135
+
136
+ const _value = _omit(testInstance, ["title"]);
137
+ _value.must.eql({
138
+ name: "xxx",
139
+ length: 100,
140
+ });
141
+ _value.hasOwnProperty("name").must.be.true();
142
+ _value.hasOwnProperty("length").must.be.true();
143
+ });
144
+
145
+ it("skips non-enumerable properties, behaves like lodash", () => {
146
+ const source = {
147
+ aaa: 123,
148
+ title: 666,
149
+ };
150
+ Object.defineProperty(source, "name", {
151
+ enumerable: false,
152
+ value: "test",
153
+ });
154
+
155
+ const value = omit(source, ["title"]);
156
+ value.must.eql({
157
+ aaa: 123,
158
+ });
159
+ ("name" in value).must.be.false();
160
+
161
+ const _value = _omit(source, ["title"]);
162
+ _value.must.eql({
163
+ aaa: 123,
164
+ });
165
+ ("name" in _value).must.be.false();
166
+ });
167
+
168
+ it("returns object when array is given as source, behaves like lodash", () => {
169
+ const source = ["a", "b", "c"];
170
+
171
+ const value = omit(source, [0, "2", 15, "xxx"]);
172
+ value.must.eql({
173
+ 1: "b",
174
+ });
175
+
176
+ const _value = _omit(source, [0, "2", 15, "xxx"]);
177
+ _value.must.eql({
178
+ 1: "b",
179
+ });
180
+ });
181
+ });
package/src/omit.ts ADDED
@@ -0,0 +1,43 @@
1
+ // TODO verify & maybe fix typings when object is an Array
2
+
3
+ /**
4
+ * Returns new object with copied all properties without these specified.
5
+ *
6
+ * @param {Object} object - source object
7
+ * @param {Array.<string>} props - properties to skip
8
+ * @example
9
+ * omit({ name: "Jack", age: 69 }, ["age", "title"]);
10
+ * // { name: "Jack" }
11
+ * @example
12
+ * omit(["hello", "world"], [0]);
13
+ * // { 1: "world" }
14
+ * @returns {Object} - new object without given properties
15
+ */
16
+ const omit = <T extends object, K extends keyof T>(
17
+ object: T | null, props: K[],
18
+ ): T extends null ? { [key: string]: never } : Omit<T, K> => {
19
+ if (!object || (typeof object !== "object" && typeof object !== "function")) {
20
+ // @ts-expect-error TS can't handle implementation of dynamic return types yet
21
+ return {};
22
+ }
23
+
24
+ if (!Array.isArray(props) || !props.length) {
25
+ // @ts-expect-error TS can't handle implementation of dynamic return types yet
26
+ return {
27
+ ...object,
28
+ };
29
+ }
30
+
31
+ const useProps = props.map(String);
32
+
33
+ const result = {};
34
+ for (const key in object) {
35
+ if (!useProps.includes(key)) {
36
+ (result as T)[key] = object[key];
37
+ }
38
+ }
39
+ // @ts-expect-error TS can't handle implementation of dynamic return types yet
40
+ return result;
41
+ };
42
+
43
+ export { omit };
@@ -0,0 +1,168 @@
1
+ import { pick as _pick } from "lodash";
2
+
3
+ import { pick } from "./pick.js";
4
+
5
+ describe("pick", () => {
6
+ it("returns empty object when source is not given, behaves like lodash", () => {
7
+ caseundefined: {
8
+ const value = pick(undefined, ["name", "count"]);
9
+ value.must.eql({});
10
+
11
+ // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
12
+ const _value = _pick(undefined, ["name", "count"]);
13
+ _value.must.eql({});
14
+ }
15
+
16
+ casenull: {
17
+ const value = pick(null, ["name", "count"]);
18
+ value.must.eql({});
19
+
20
+ const _value = _pick(null, ["name", "count"]);
21
+ _value.must.eql({});
22
+ }
23
+ });
24
+
25
+ it("returns empty object when properties to copy are not given, behaves like lodash", () => {
26
+ const source = {
27
+ title: "Master od disaster",
28
+ count: 1,
29
+ };
30
+
31
+ caseundefined: {
32
+ const value = pick(source);
33
+ value.must.eql({});
34
+
35
+ const _value = _pick(source);
36
+ _value.must.eql({});
37
+ }
38
+
39
+ caseempty: {
40
+ const value = pick(source, []);
41
+ value.must.eql({});
42
+
43
+ const _value = _pick(source, []);
44
+ _value.must.eql({});
45
+ }
46
+ });
47
+
48
+ it("returns empty object when source is primitive, behaves like lodash", () => {
49
+ number: {
50
+ const value = pick(2, ["title"]);
51
+ value.must.eql({});
52
+
53
+ const _value = _pick(2, ["title"]);
54
+ _value.must.eql({});
55
+ }
56
+
57
+ string: {
58
+ const value = pick("two", ["title"]);
59
+ value.must.eql({});
60
+
61
+ const _value = _pick("two", ["title"]);
62
+ _value.must.eql({});
63
+ }
64
+ });
65
+
66
+ it("returns new object with expected properties, ignores not defined properties, behaves like lodash", () => {
67
+ const source = {
68
+ a: 5,
69
+ b: "string",
70
+ c: undefined,
71
+ d: 0,
72
+ e: null,
73
+ f: false,
74
+ };
75
+
76
+ const value = pick(source, ["a", "b", "c", "d", "e", "f", "g"]);
77
+ value.must.eql({
78
+ a: 5,
79
+ b: "string",
80
+ c: undefined,
81
+ d: 0,
82
+ e: null,
83
+ f: false,
84
+ });
85
+ value.must.not.equal(source);
86
+
87
+ const _value = _pick(source, ["a", "b", "c", "d", "e", "f", "g"]);
88
+ _value.must.eql({
89
+ a: 5,
90
+ b: "string",
91
+ c: undefined,
92
+ d: 0,
93
+ e: null,
94
+ f: false,
95
+ });
96
+ _value.must.not.equal(source);
97
+ });
98
+
99
+ it("returns new object when source is function, behaves like lodash", () => {
100
+ const source = () => {};
101
+ source.title = "ABCD";
102
+
103
+ const value = pick(source, ["title"]);
104
+ value.must.eql({
105
+ title: "ABCD",
106
+ });
107
+
108
+ const _value = _pick(source, ["title"]);
109
+ _value.must.eql({
110
+ title: "ABCD",
111
+ });
112
+ });
113
+
114
+ it("returns inherited properties, behaves like lodash", () => {
115
+ const Test = function() {};
116
+ Test.prototype.count = 1;
117
+
118
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
119
+ const testInstance = new Test();
120
+ testInstance.length = 2;
121
+
122
+ const value = pick(testInstance, ["count", "length"]);
123
+ value.must.eql({
124
+ count: 1,
125
+ length: 2,
126
+ });
127
+
128
+ const _value = _pick(testInstance, ["count", "length"]);
129
+ _value.must.eql({
130
+ count: 1,
131
+ length: 2,
132
+ });
133
+ });
134
+
135
+ it("returns non-enumerable properties, behaves like lodash", () => {
136
+ const source = {};
137
+ Object.defineProperty(source, "count", {
138
+ enumerable: false,
139
+ value: 23,
140
+ });
141
+
142
+ const value = pick(source, ["count"]);
143
+ value.must.eql({
144
+ count: 23,
145
+ });
146
+
147
+ const _value = _pick(source, ["count"]);
148
+ _value.must.eql({
149
+ count: 23,
150
+ });
151
+ });
152
+
153
+ it("returns object when array is given as source, behaves like lodash", () => {
154
+ const source = ["a", "b", "c"];
155
+
156
+ const value = pick(source, [0, "2", 15, "xxx"]);
157
+ value.must.eql({
158
+ 0: "a",
159
+ 2: "c",
160
+ });
161
+
162
+ const _value = _pick(source, [0, "2", 15, "xxx"]);
163
+ _value.must.eql({
164
+ 0: "a",
165
+ 2: "c",
166
+ });
167
+ });
168
+ });
package/src/pick.ts ADDED
@@ -0,0 +1,39 @@
1
+ // TODO verify & maybe fix typings when object is an Array
2
+
3
+ /**
4
+ * Returns new object with copied given properties from source object.
5
+ *
6
+ * @param {Object} object - source object
7
+ * @param {Array.<string>} props - properties to copy
8
+ * @example
9
+ * pick({ name: "Jack", age: 69 }, ["age", "title"]);
10
+ * // { age: 69 }
11
+ * @example
12
+ * pick(["hello", "world"], [0]);
13
+ * // { 0: hello }
14
+ * @returns {Object} - new object with given properties
15
+ */
16
+ const pick = <T extends object, K extends keyof T>(
17
+ object: T | null, props: K[],
18
+ ): T extends null ? { [ key: string]: never } : Pick<T, K> => {
19
+ if (
20
+ !object
21
+ || (typeof object !== "object" && typeof object !== "function")
22
+ || !Array.isArray(props)
23
+ || !props.length
24
+ ) {
25
+ // @ts-expect-error TS can't handle implementation of dynamic return types yet
26
+ return {};
27
+ }
28
+ const result = {};
29
+ props.forEach(property => {
30
+ if (property in object) {
31
+ (result as T)[property] = object[property];
32
+ }
33
+ });
34
+
35
+ // @ts-expect-error TS can't handle implementation of dynamic return types yet
36
+ return result;
37
+ };
38
+
39
+ export { pick };
@@ -0,0 +1,54 @@
1
+ import { pull } from "./pull.js";
2
+
3
+ describe("pull", function() {
4
+ it("should remove all given values", function() {
5
+ const source = [1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 2, 2];
6
+ const result = pull(source, 1);
7
+ (source === result).must.be.true();
8
+
9
+ result.includes(1).must.be.false();
10
+ result.must.eql([2, 2, 2, 2, 2, 2, 2, 2]);
11
+ });
12
+
13
+ it("should be able to clear the array", function() {
14
+ const source = [1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 2, 2];
15
+ const result = pull(source, 1, 2);
16
+ (source === result).must.be.true();
17
+ result.must.have.length(0);
18
+ });
19
+
20
+ it("should remove NaNs", function() {
21
+ const source = [1, NaN, 3];
22
+ pull(source, NaN);
23
+ source.must.eql([1, 3]);
24
+ });
25
+
26
+ it("should remove nothing", function() {
27
+ const source = [1, NaN, 3, undefined];
28
+ pull(source);
29
+ source.must.eql([1, NaN, 3, undefined]);
30
+ });
31
+
32
+ it("should remove null or undefined or referenced objects", function() {
33
+ const obj = {};
34
+ const source = [1, undefined, 2, null, 3, obj, 4, {}];
35
+ pull(source, obj, null, undefined);
36
+ source.must.eql([1, 2, 3, 4, {}]);
37
+ });
38
+
39
+ it("should work with sparse arrays, removing holes when requested to remove undefined", function() {
40
+ // eslint-disable-next-line no-sparse-arrays
41
+ const source = [1, , 3, , 5];
42
+ pull(source, undefined);
43
+ source.must.eql([1, 3, 5]);
44
+ });
45
+
46
+ it("should work with sparse arrays, keeping holes when requested", function() {
47
+ // eslint-disable-next-line no-sparse-arrays
48
+ const source = [1, , 3, , 5];
49
+ pull(source, null);
50
+ // eslint-disable-next-line no-sparse-arrays
51
+ source.must.eql([1, ,3, , 5]);
52
+ ("1" in source).must.be.false();
53
+ });
54
+ });
package/src/pull.ts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Modifies the array, removing all occurrences of specified values
3
+ * @param {Array} array
4
+ * @param {...*}valuesToPull
5
+ */
6
+ const pull = <T>(array: T[], ...valuesToPull: T[]): T[] => {
7
+ const len = array.length;
8
+ for (let i = len - 1; i >= 0; i--) {
9
+ if (valuesToPull.includes(array[i])) {
10
+ array.splice(i, 1);
11
+ }
12
+ }
13
+ return array;
14
+ };
15
+
16
+ export {
17
+ pull,
18
+ };
@@ -0,0 +1,63 @@
1
+ import { remove as _remove } from "lodash";
2
+
3
+ import createSpy from "../test/createSpy.js";
4
+
5
+ import { remove } from "./remove.js";
6
+
7
+ describe("remove", () => {
8
+ it("removes values", () => {
9
+ const fn = x => x % 2 === 0;
10
+
11
+ const src = [1, 2, 3, 4, 5, 6, 7];
12
+ const _src = [...src];
13
+
14
+ remove(src, fn);
15
+ src.must.eql([1, 3, 5, 7]);
16
+
17
+ _remove(_src, fn);
18
+ _src.must.eql([1, 3, 5, 7]);
19
+ });
20
+
21
+ it("returns removed values", () => {
22
+ const fn = x => x % 2 === 0;
23
+
24
+ const src = [1, 2, 3, 4, 5, 6, 7];
25
+ const _src = [...src];
26
+
27
+ const removed = remove(src, fn);
28
+ removed.must.eql([2, 4, 6]);
29
+
30
+ const _removed = _remove(_src, fn);
31
+ _removed.must.eql([2, 4, 6]);
32
+ });
33
+
34
+ it("calls test function with correct arguments", () => {
35
+ const fn = x => x % 2 === 0;
36
+ const spy = createSpy(fn);
37
+ const _spy = createSpy(fn);
38
+
39
+ const src = [1, 2, 3, 4, 5, 6, 7];
40
+ const _src = [...src];
41
+ const srcCopy = [...src];
42
+
43
+ remove(src, spy);
44
+ spy.__spy.calls.must.have.length(7);
45
+ spy.__spy.calls[0].must.eql([1, 0, srcCopy]);
46
+ spy.__spy.calls[1].must.eql([2, 1, srcCopy]);
47
+ spy.__spy.calls[2].must.eql([3, 2, srcCopy]);
48
+ spy.__spy.calls[3].must.eql([4, 3, srcCopy]);
49
+ spy.__spy.calls[4].must.eql([5, 4, srcCopy]);
50
+ spy.__spy.calls[5].must.eql([6, 5, srcCopy]);
51
+ spy.__spy.calls[6].must.eql([7, 6, srcCopy]);
52
+
53
+ remove(_src, _spy);
54
+ _spy.__spy.calls.must.have.length(7);
55
+ _spy.__spy.calls[0].must.eql([1, 0, srcCopy]);
56
+ _spy.__spy.calls[1].must.eql([2, 1, srcCopy]);
57
+ _spy.__spy.calls[2].must.eql([3, 2, srcCopy]);
58
+ _spy.__spy.calls[3].must.eql([4, 3, srcCopy]);
59
+ _spy.__spy.calls[4].must.eql([5, 4, srcCopy]);
60
+ _spy.__spy.calls[5].must.eql([6, 5, srcCopy]);
61
+ _spy.__spy.calls[6].must.eql([7, 6, srcCopy]);
62
+ });
63
+ });
package/src/remove.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Removes values from array using function as predicate. Returns removed values.
3
+ *
4
+ * @param {Array} array
5
+ * @param {function} fn
6
+ * @returns {Array}
7
+ */
8
+ const remove = <T>(array: T[], fn: (elem: T, num: number, list: T[]) => boolean): T[] => {
9
+ const len = array.length;
10
+
11
+ const idsToRemove = []; // not removing instantly to feed `fn` untouched array for each iteration, lodash does that
12
+ const removedValues = [];
13
+
14
+ for (let counter = 0; counter < len; counter++) {
15
+ if (fn(array[counter], counter, array)) {
16
+ idsToRemove.push(counter - idsToRemove.length);
17
+ removedValues.push(array[counter]);
18
+ }
19
+ }
20
+
21
+ idsToRemove.forEach(id => array.splice(id, 1));
22
+
23
+ return removedValues;
24
+ };
25
+
26
+ export { remove };
package/src/rethrow.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Throws given value.
3
+ * @param {*} e
4
+ */
5
+ const rethrow = (e: unknown) => {
6
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
7
+ throw e;
8
+ };
9
+
10
+ export {
11
+ rethrow,
12
+ };
package/src/scale.ts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Changes the scale of given value, i.e.: replaces values from 2.9 to 3.3 into values from 0 to 100. Useful for i.e.:
3
+ * displaying measured battery voltage as a percent value.
4
+ * Pro-tip: use `Function.prototype.bind` to create yourself a function with bound 4 first arguments to re-use without
5
+ * repeating the from-to scale.
6
+ * @param {number} fromMin
7
+ * @param {number} fromMax
8
+ * @param {number} toMin
9
+ * @param {number} toMax
10
+ * @param {number} number
11
+ */
12
+ const scale = (fromMin: number, fromMax: number, toMin: number, toMax: number, number: number) => {
13
+ return toMin + ((number - fromMin) / (fromMax - fromMin) * (toMax - toMin));
14
+ };
15
+
16
+ export {
17
+ scale,
18
+ };