@telperion/js-utils 3.5.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 (351) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +621 -0
  3. package/array/arrayize.cjs +22 -0
  4. package/array/arrayize.d.ts +17 -0
  5. package/array/arrayize.js +21 -0
  6. package/array/arrayize.js.map +1 -0
  7. package/array/async-map.cjs +40 -0
  8. package/array/async-map.d.ts +34 -0
  9. package/array/async-map.js +39 -0
  10. package/array/async-map.js.map +1 -0
  11. package/array/compact.cjs +34 -0
  12. package/array/compact.d.ts +28 -0
  13. package/array/compact.js +33 -0
  14. package/array/compact.js.map +1 -0
  15. package/array/difference.cjs +16 -0
  16. package/array/difference.d.ts +45 -0
  17. package/array/difference.js +15 -0
  18. package/array/difference.js.map +1 -0
  19. package/array/find-by-key.cjs +35 -0
  20. package/array/find-by-key.d.ts +30 -0
  21. package/array/find-by-key.js +34 -0
  22. package/array/find-by-key.js.map +1 -0
  23. package/array/index.cjs +14 -0
  24. package/array/index.d.ts +11 -0
  25. package/array/index.js +13 -0
  26. package/array/index.js.map +1 -0
  27. package/array/intersection.cjs +16 -0
  28. package/array/intersection.d.ts +45 -0
  29. package/array/intersection.js +15 -0
  30. package/array/intersection.js.map +1 -0
  31. package/array/limit.cjs +37 -0
  32. package/array/limit.d.ts +29 -0
  33. package/array/limit.js +36 -0
  34. package/array/limit.js.map +1 -0
  35. package/array/proto/async-map.cjs +6 -0
  36. package/array/proto/async-map.d.ts +25 -0
  37. package/array/proto/async-map.js +6 -0
  38. package/array/proto/async-map.js.map +1 -0
  39. package/array/proto/compact.cjs +6 -0
  40. package/array/proto/compact.d.ts +15 -0
  41. package/array/proto/compact.js +6 -0
  42. package/array/proto/compact.js.map +1 -0
  43. package/array/proto/difference.cjs +6 -0
  44. package/array/proto/difference.d.ts +27 -0
  45. package/array/proto/difference.js +6 -0
  46. package/array/proto/difference.js.map +1 -0
  47. package/array/proto/find-by-key.cjs +6 -0
  48. package/array/proto/find-by-key.d.ts +25 -0
  49. package/array/proto/find-by-key.js +6 -0
  50. package/array/proto/find-by-key.js.map +1 -0
  51. package/array/proto/intersection.cjs +6 -0
  52. package/array/proto/intersection.d.ts +27 -0
  53. package/array/proto/intersection.js +6 -0
  54. package/array/proto/intersection.js.map +1 -0
  55. package/array/proto/limit.cjs +6 -0
  56. package/array/proto/limit.d.ts +24 -0
  57. package/array/proto/limit.js +6 -0
  58. package/array/proto/limit.js.map +1 -0
  59. package/array/proto/remove.cjs +6 -0
  60. package/array/proto/remove.d.ts +32 -0
  61. package/array/proto/remove.js +6 -0
  62. package/array/proto/remove.js.map +1 -0
  63. package/array/proto/replace.cjs +6 -0
  64. package/array/proto/replace.d.ts +72 -0
  65. package/array/proto/replace.js +6 -0
  66. package/array/proto/replace.js.map +1 -0
  67. package/array/proto/uniquify-by-key.cjs +6 -0
  68. package/array/proto/uniquify-by-key.d.ts +24 -0
  69. package/array/proto/uniquify-by-key.js +6 -0
  70. package/array/proto/uniquify-by-key.js.map +1 -0
  71. package/array/proto/uniquify.cjs +6 -0
  72. package/array/proto/uniquify.d.ts +23 -0
  73. package/array/proto/uniquify.js +6 -0
  74. package/array/proto/uniquify.js.map +1 -0
  75. package/array/remove.cjs +46 -0
  76. package/array/remove.d.ts +33 -0
  77. package/array/remove.js +45 -0
  78. package/array/remove.js.map +1 -0
  79. package/array/replace.cjs +69 -0
  80. package/array/replace.d.ts +139 -0
  81. package/array/replace.js +68 -0
  82. package/array/replace.js.map +1 -0
  83. package/array/static/arrayize.cjs +4 -0
  84. package/array/static/arrayize.d.ts +6 -0
  85. package/array/static/arrayize.js +4 -0
  86. package/array/static/arrayize.js.map +1 -0
  87. package/array/uniquify-by-key.cjs +41 -0
  88. package/array/uniquify-by-key.d.ts +29 -0
  89. package/array/uniquify-by-key.js +40 -0
  90. package/array/uniquify-by-key.js.map +1 -0
  91. package/array/uniquify.cjs +32 -0
  92. package/array/uniquify.d.ts +27 -0
  93. package/array/uniquify.js +31 -0
  94. package/array/uniquify.js.map +1 -0
  95. package/boolean/static/is-falsy.cjs +4 -0
  96. package/boolean/static/is-falsy.d.ts +23 -0
  97. package/boolean/static/is-falsy.js +4 -0
  98. package/boolean/static/is-falsy.js.map +1 -0
  99. package/boolean/static/is-truthy.cjs +4 -0
  100. package/boolean/static/is-truthy.d.ts +23 -0
  101. package/boolean/static/is-truthy.js +4 -0
  102. package/boolean/static/is-truthy.js.map +1 -0
  103. package/class/index.cjs +4 -0
  104. package/class/index.d.ts +1 -0
  105. package/class/index.js +3 -0
  106. package/class/index.js.map +1 -0
  107. package/class/mixin.cjs +60 -0
  108. package/class/mixin.d.ts +50 -0
  109. package/class/mixin.js +59 -0
  110. package/class/mixin.js.map +1 -0
  111. package/compact.cjs +39 -0
  112. package/compact.d.ts +2 -0
  113. package/compact.js +38 -0
  114. package/compact.js.map +1 -0
  115. package/function/call.cjs +6 -0
  116. package/function/call.d.ts +1 -0
  117. package/function/call.js +5 -0
  118. package/function/call.js.map +1 -0
  119. package/function/debounce.cjs +85 -0
  120. package/function/debounce.d.ts +44 -0
  121. package/function/debounce.js +82 -0
  122. package/function/debounce.js.map +1 -0
  123. package/function/defer.cjs +42 -0
  124. package/function/defer.d.ts +26 -0
  125. package/function/defer.js +41 -0
  126. package/function/defer.js.map +1 -0
  127. package/function/index.cjs +8 -0
  128. package/function/index.d.ts +5 -0
  129. package/function/index.js +7 -0
  130. package/function/index.js.map +1 -0
  131. package/function/noop.cjs +23 -0
  132. package/function/noop.d.ts +20 -0
  133. package/function/noop.js +22 -0
  134. package/function/noop.js.map +1 -0
  135. package/function/of.cjs +44 -0
  136. package/function/of.d.ts +31 -0
  137. package/function/of.js +43 -0
  138. package/function/of.js.map +1 -0
  139. package/function/proto/debounce.cjs +6 -0
  140. package/function/proto/debounce.d.ts +30 -0
  141. package/function/proto/debounce.js +6 -0
  142. package/function/proto/debounce.js.map +1 -0
  143. package/function/static/call.cjs +4 -0
  144. package/function/static/call.d.ts +6 -0
  145. package/function/static/call.js +4 -0
  146. package/function/static/call.js.map +1 -0
  147. package/function/static/defer.cjs +4 -0
  148. package/function/static/defer.d.ts +23 -0
  149. package/function/static/defer.js +4 -0
  150. package/function/static/defer.js.map +1 -0
  151. package/function/static/noop.cjs +4 -0
  152. package/function/static/noop.d.ts +18 -0
  153. package/function/static/noop.js +4 -0
  154. package/function/static/noop.js.map +1 -0
  155. package/function/static/of.cjs +4 -0
  156. package/function/static/of.d.ts +26 -0
  157. package/function/static/of.js +4 -0
  158. package/function/static/of.js.map +1 -0
  159. package/index.cjs +31 -0
  160. package/index.d.ts +14 -0
  161. package/index.js +17 -0
  162. package/index.js.map +1 -0
  163. package/is-falsy.cjs +33 -0
  164. package/is-falsy.d.ts +28 -0
  165. package/is-falsy.js +32 -0
  166. package/is-falsy.js.map +1 -0
  167. package/is-truthy.cjs +33 -0
  168. package/is-truthy.d.ts +28 -0
  169. package/is-truthy.js +32 -0
  170. package/is-truthy.js.map +1 -0
  171. package/limit.cjs +34 -0
  172. package/limit.d.ts +2 -0
  173. package/limit.js +33 -0
  174. package/limit.js.map +1 -0
  175. package/map/index.cjs +4 -0
  176. package/map/index.d.ts +1 -0
  177. package/map/index.js +3 -0
  178. package/map/index.js.map +1 -0
  179. package/map/merge.cjs +33 -0
  180. package/map/merge.d.ts +24 -0
  181. package/map/merge.js +32 -0
  182. package/map/merge.js.map +1 -0
  183. package/math/index.cjs +4 -0
  184. package/math/index.d.ts +1 -0
  185. package/math/index.js +3 -0
  186. package/math/index.js.map +1 -0
  187. package/math/min-max.cjs +29 -0
  188. package/math/min-max.d.ts +24 -0
  189. package/math/min-max.js +28 -0
  190. package/math/min-max.js.map +1 -0
  191. package/math/static/min-max.cjs +4 -0
  192. package/math/static/min-max.d.ts +20 -0
  193. package/math/static/min-max.js +4 -0
  194. package/math/static/min-max.js.map +1 -0
  195. package/object/clone.cjs +157 -0
  196. package/object/clone.d.ts +161 -0
  197. package/object/clone.js +156 -0
  198. package/object/clone.js.map +1 -0
  199. package/object/compact.cjs +49 -0
  200. package/object/compact.d.ts +37 -0
  201. package/object/compact.js +48 -0
  202. package/object/compact.js.map +1 -0
  203. package/object/deep-freeze.cjs +16 -0
  204. package/object/deep-freeze.d.ts +1 -0
  205. package/object/deep-freeze.js +15 -0
  206. package/object/deep-freeze.js.map +1 -0
  207. package/object/deepest.cjs +55 -0
  208. package/object/deepest.d.ts +47 -0
  209. package/object/deepest.js +54 -0
  210. package/object/deepest.js.map +1 -0
  211. package/object/index.cjs +7 -0
  212. package/object/index.d.ts +4 -0
  213. package/object/index.js +6 -0
  214. package/object/index.js.map +1 -0
  215. package/object/static/clone.cjs +4 -0
  216. package/object/static/clone.d.ts +43 -0
  217. package/object/static/clone.js +4 -0
  218. package/object/static/clone.js.map +1 -0
  219. package/object/static/compact.cjs +4 -0
  220. package/object/static/compact.d.ts +26 -0
  221. package/object/static/compact.js +4 -0
  222. package/object/static/compact.js.map +1 -0
  223. package/object/static/deep-freeze.cjs +4 -0
  224. package/object/static/deep-freeze.d.ts +6 -0
  225. package/object/static/deep-freeze.js +4 -0
  226. package/object/static/deep-freeze.js.map +1 -0
  227. package/object/static/deepest.cjs +4 -0
  228. package/object/static/deepest.d.ts +32 -0
  229. package/object/static/deepest.js +4 -0
  230. package/object/static/deepest.js.map +1 -0
  231. package/open-promise.cjs +115 -0
  232. package/open-promise.d.ts +80 -0
  233. package/open-promise.js +113 -0
  234. package/open-promise.js.map +1 -0
  235. package/package.json +667 -0
  236. package/promise/chain.cjs +12 -0
  237. package/promise/chain.d.ts +7 -0
  238. package/promise/chain.js +11 -0
  239. package/promise/chain.js.map +1 -0
  240. package/promise/index.cjs +10 -0
  241. package/promise/index.d.ts +7 -0
  242. package/promise/index.js +9 -0
  243. package/promise/index.js.map +1 -0
  244. package/promise/never.cjs +41 -0
  245. package/promise/never.d.ts +34 -0
  246. package/promise/never.js +39 -0
  247. package/promise/never.js.map +1 -0
  248. package/promise/promisify.cjs +12 -0
  249. package/promise/promisify.d.ts +7 -0
  250. package/promise/promisify.js +11 -0
  251. package/promise/promisify.js.map +1 -0
  252. package/promise/proto/revert.cjs +6 -0
  253. package/promise/proto/revert.d.ts +29 -0
  254. package/promise/proto/revert.js +6 -0
  255. package/promise/proto/revert.js.map +1 -0
  256. package/promise/proto/try-catch.cjs +7 -0
  257. package/promise/proto/try-catch.d.ts +30 -0
  258. package/promise/proto/try-catch.js +7 -0
  259. package/promise/proto/try-catch.js.map +1 -0
  260. package/promise/revert.cjs +43 -0
  261. package/promise/revert.d.ts +38 -0
  262. package/promise/revert.js +42 -0
  263. package/promise/revert.js.map +1 -0
  264. package/promise/static/chain.cjs +4 -0
  265. package/promise/static/chain.d.ts +10 -0
  266. package/promise/static/chain.js +4 -0
  267. package/promise/static/chain.js.map +1 -0
  268. package/promise/static/never.cjs +5 -0
  269. package/promise/static/never.d.ts +39 -0
  270. package/promise/static/never.js +5 -0
  271. package/promise/static/never.js.map +1 -0
  272. package/promise/static/timeout.cjs +4 -0
  273. package/promise/static/timeout.d.ts +21 -0
  274. package/promise/static/timeout.js +4 -0
  275. package/promise/static/timeout.js.map +1 -0
  276. package/promise/static/try-one-by-one.cjs +4 -0
  277. package/promise/static/try-one-by-one.d.ts +27 -0
  278. package/promise/static/try-one-by-one.js +4 -0
  279. package/promise/static/try-one-by-one.js.map +1 -0
  280. package/promise/timeout.cjs +104 -0
  281. package/promise/timeout.d.ts +109 -0
  282. package/promise/timeout.js +103 -0
  283. package/promise/timeout.js.map +1 -0
  284. package/promise/try-catch.cjs +12 -0
  285. package/promise/try-catch.d.ts +45 -0
  286. package/promise/try-catch.js +11 -0
  287. package/promise/try-catch.js.map +1 -0
  288. package/promise/try-one-by-one.cjs +59 -0
  289. package/promise/try-one-by-one.d.ts +42 -0
  290. package/promise/try-one-by-one.js +58 -0
  291. package/promise/try-one-by-one.js.map +1 -0
  292. package/set/proto/difference.cjs +6 -0
  293. package/set/proto/difference.d.ts +26 -0
  294. package/set/proto/difference.js +6 -0
  295. package/set/proto/difference.js.map +1 -0
  296. package/set/proto/intersection.cjs +6 -0
  297. package/set/proto/intersection.d.ts +26 -0
  298. package/set/proto/intersection.js +6 -0
  299. package/set/proto/intersection.js.map +1 -0
  300. package/smart-map.cjs +108 -0
  301. package/smart-map.d.ts +81 -0
  302. package/smart-map.js +106 -0
  303. package/smart-map.js.map +1 -0
  304. package/string/index.cjs +4 -0
  305. package/string/index.d.ts +1 -0
  306. package/string/index.js +3 -0
  307. package/string/index.js.map +1 -0
  308. package/string/limit.cjs +35 -0
  309. package/string/limit.d.ts +29 -0
  310. package/string/limit.js +34 -0
  311. package/string/limit.js.map +1 -0
  312. package/string/proto/limit.cjs +6 -0
  313. package/string/proto/limit.d.ts +30 -0
  314. package/string/proto/limit.js +6 -0
  315. package/string/proto/limit.js.map +1 -0
  316. package/time/days.cjs +21 -0
  317. package/time/days.d.ts +15 -0
  318. package/time/days.js +20 -0
  319. package/time/days.js.map +1 -0
  320. package/time/hours.cjs +21 -0
  321. package/time/hours.d.ts +15 -0
  322. package/time/hours.js +20 -0
  323. package/time/hours.js.map +1 -0
  324. package/time/index.cjs +10 -0
  325. package/time/index.d.ts +7 -0
  326. package/time/index.js +9 -0
  327. package/time/index.js.map +1 -0
  328. package/time/minutes.cjs +21 -0
  329. package/time/minutes.d.ts +15 -0
  330. package/time/minutes.js +20 -0
  331. package/time/minutes.js.map +1 -0
  332. package/time/months.cjs +21 -0
  333. package/time/months.d.ts +15 -0
  334. package/time/months.js +20 -0
  335. package/time/months.js.map +1 -0
  336. package/time/seconds.cjs +20 -0
  337. package/time/seconds.d.ts +15 -0
  338. package/time/seconds.js +19 -0
  339. package/time/seconds.js.map +1 -0
  340. package/time/weeks.cjs +21 -0
  341. package/time/weeks.d.ts +15 -0
  342. package/time/weeks.js +20 -0
  343. package/time/weeks.js.map +1 -0
  344. package/time/years.cjs +21 -0
  345. package/time/years.d.ts +15 -0
  346. package/time/years.js +20 -0
  347. package/time/years.js.map +1 -0
  348. package/unique-id.cjs +24 -0
  349. package/unique-id.d.ts +21 -0
  350. package/unique-id.js +23 -0
  351. package/unique-id.js.map +1 -0
@@ -0,0 +1,161 @@
1
+ /**
2
+ * #### Cloning Options
3
+ * Cloning options of the [clone]{@link clone} function
4
+ */
5
+ export interface ICloneOptions {
6
+ /**
7
+ * #### Instance constructors which cloning unwanted
8
+ * Give the instance constructors to prevent them being cloned
9
+ *
10
+ * * * *
11
+ * Example:
12
+ * ```typescript
13
+ * import { clone } from "@telperion/js-utils/object";
14
+ *
15
+ * const object = {
16
+ * width: 250,
17
+ * element: document.querySelector("div")
18
+ * };
19
+ *
20
+ * const clonedObject = clone(object, {instancesToRefer: [ HTMLElement ]});
21
+ * // clonedObject.element === object.element
22
+ * ```
23
+ * * * *
24
+ * @default []
25
+ */
26
+ instancesToRefer?: any[];
27
+ /**
28
+ * #### Value filterer to pass same references
29
+ * Being called for each value while cloning. Return true to prevent cloning for the incoming value and pass the same reference instead.
30
+ *
31
+ * * * *
32
+ * Example:
33
+ * ```typescript
34
+ * import { clone } from "@telperion/js-utils/object";
35
+ *
36
+ * const john = { age: 20, willBeReferenced: true };
37
+ * const jane = { age: 25, willBeReferenced: false };
38
+ *
39
+ * const array = [john, jane];
40
+ *
41
+ * const clonedArray = clone(array, {valueFiltererToRefer: person => person.willBeReferenced });
42
+ *
43
+ * // clonedArray[0] === array[0]
44
+ * // clonedArray[1] !== array[1]
45
+ * ```
46
+ * * * *
47
+ * @param value incoming value to be cloned
48
+ * @return Return true to pass same reference, false to clone
49
+ * @default () => false
50
+ */
51
+ valueFiltererToRefer?(value: any): boolean;
52
+ /**
53
+ * #### Properties which cloning unwanted
54
+ * Give the unwanted properties to prevent them being cloned
55
+ *
56
+ * * * *
57
+ * Example:
58
+ * ```typescript
59
+ * import { clone } from "@telperion/js-utils/object";
60
+ *
61
+ * const object = {
62
+ * width: 250,
63
+ * element: document.querySelector("div")
64
+ * };
65
+ *
66
+ * const clonedObject = clone(object, {propsToRefer: [ "element" ]});
67
+ * // clonedObject.element === object.element
68
+ * ```
69
+ * * * *
70
+ * @default []
71
+ */
72
+ propsToRefer?: Array<any>;
73
+ /**
74
+ * #### Custom Cloner Map
75
+ *
76
+ * * * *
77
+ * Example:
78
+ * ```typescript
79
+ * import { clone } from "@telperion/js-utils/object";
80
+ *
81
+ * const object = {
82
+ * width: 250,
83
+ * element: document.querySelector("div")
84
+ * };
85
+ *
86
+ * function divCloner(objectToClone, options, internalData) {
87
+ * const newElement = document.createElement("div");
88
+ * newElement.classList.add("cloned");
89
+ * return newElement;
90
+ * }
91
+ *
92
+ * const customCloners = new Map();
93
+ * customCloners.set(HTMLDivElement, divCloner);
94
+ *
95
+ * const clonedObject = clone(object);
96
+ * ```
97
+ * * * *
98
+ */
99
+ customCloners?: Map<Function, TInstanceCloner>;
100
+ }
101
+ /**
102
+ * #### Internal Data For Cloning Process
103
+ */
104
+ export interface IClonerInternalData {
105
+ clonedObjects: Map<object, object | symbol>;
106
+ parent?: Record<any, any>;
107
+ key?: number | string | symbol;
108
+ finalizers: Array<() => void>;
109
+ }
110
+ export type PolyfillRequired<T, TNames extends string> = {
111
+ [P in TNames]: (T & {
112
+ [name: string]: never;
113
+ })[P];
114
+ };
115
+ /**
116
+ * Cloning options with all properties required
117
+ * @see ICloneOptions
118
+ */
119
+ export type TNonOptionalCloneOptions = PolyfillRequired<ICloneOptions, keyof ICloneOptions>;
120
+ /**
121
+ * Custom Cloner Function
122
+ */
123
+ export type TInstanceCloner = <T>(objectToClone: T, options: TNonOptionalCloneOptions, internalData: IClonerInternalData) => T;
124
+ /**
125
+ * #### Deep Clone
126
+ *
127
+ * A function to recursively clone objects, arrays etc. with [cloning options]{@link ICloneOptions}
128
+ *
129
+ * *References Functions & Symbols by default*
130
+ *
131
+ * * * *
132
+ * Example:
133
+ * ```typescript
134
+ * import { clone } from "@telperion/js-utils/object";
135
+ *
136
+ * const object = {a: 1, b: {c: true, d: ["x", "y"]}};
137
+ *
138
+ * // Clone all
139
+ * const clonedObject = clone(object);
140
+ * // {a: 1, b: {c: true, d: ["x", "y"]}}
141
+ * // object.b.d === clonedObject.b.d // false
142
+ *
143
+ * // Clone all but reference "d"
144
+ * const clonedObject = clone(object, {propsToRefer: ["d"]});
145
+ * // {a: 1, b: {c: true, d: ["x", "y"]}}
146
+ * // object.b.d === clonedObject.b.d // true
147
+ * ```
148
+ *
149
+ * Static usage example:
150
+ * ```typescript
151
+ * import "@telperion/js-utils/object/static/clone";
152
+ *
153
+ * const object = {a: 1, b: 2};
154
+ * const clonedObject = Object.clone(object); // {a: 1, b: 2}
155
+ * ```
156
+ * * * *
157
+ * @param objectToClone Object to clone
158
+ * @param options {ICloneOptions} Cloning Options
159
+ * @see {@link ICloneOptions} for more information.
160
+ */
161
+ export declare function clone<T>(objectToClone: T, { instancesToRefer, propsToRefer, valueFiltererToRefer, customCloners }?: ICloneOptions): T;
@@ -0,0 +1,156 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { difference } from '@telperion/js-utils/array/difference';
3
+ import { intersection } from '@telperion/js-utils/array/intersection';
4
+ import { merge } from '@telperion/js-utils/map/merge';
5
+ /**
6
+ * A placeholder to symbolize the cloning progress of `objectToClone` hasn't finished yet
7
+ */
8
+ const CLONING_IN_PROGRESS = Symbol('Cloning In Progress');
9
+ /**
10
+ * Function Map to clone specific objects types
11
+ */
12
+ // eslint-disable-next-line @typescript-eslint/ban-types
13
+ const DEFAULT_INSTANCE_CLONERS = new Map();
14
+ // Set cloning functions
15
+ DEFAULT_INSTANCE_CLONERS.set(Array, arrayCloner);
16
+ DEFAULT_INSTANCE_CLONERS.set(Map, mapCloner);
17
+ DEFAULT_INSTANCE_CLONERS.set(Set, setCloner);
18
+ DEFAULT_INSTANCE_CLONERS.set(Object, objectCloner);
19
+ /**
20
+ * Default Array Cloner
21
+ */
22
+ function arrayCloner(objectToClone, options, internalData) {
23
+ return objectToClone.map((val, i, arr) => {
24
+ if (options.propsToRefer.indexOf(i) > -1) {
25
+ return val;
26
+ }
27
+ return cloner(val, options, { ...internalData, parent: arr, key: i });
28
+ });
29
+ }
30
+ /**
31
+ * Default Map Cloner
32
+ */
33
+ function mapCloner(objectToClone, options, internalData) {
34
+ const newMap = new Map();
35
+ intersection(options.propsToRefer, Array.from(objectToClone.keys())).forEach(key => {
36
+ newMap.set(key, objectToClone.get(key));
37
+ });
38
+ difference(Array.from(objectToClone.keys()), options.propsToRefer).forEach(key => {
39
+ newMap.set(key, cloner(objectToClone.get(key), options, { ...internalData, key, parent: newMap }));
40
+ });
41
+ return newMap;
42
+ }
43
+ /**
44
+ * Default Set Cloner
45
+ */
46
+ function setCloner(objectToClone) {
47
+ return new Set(objectToClone);
48
+ }
49
+ /**
50
+ * Default Object Cloner
51
+ */
52
+ function objectCloner(objectToClone, options, internalData) {
53
+ const { propsToRefer } = options;
54
+ /**
55
+ * New Object Clone
56
+ */
57
+ const _clone = {};
58
+ /**
59
+ * Object cloner
60
+ * @param key incoming property of object to clone
61
+ */
62
+ function cloneObject(key) {
63
+ _clone[key] = cloner(objectToClone[key], options, { ...internalData, parent: _clone, key });
64
+ }
65
+ const allProperties = [...Object.keys(objectToClone), ...Object.getOwnPropertySymbols(objectToClone)];
66
+ // Pass unwanted props
67
+ intersection(propsToRefer, allProperties).forEach(prop => {
68
+ _clone[prop] = objectToClone[prop];
69
+ });
70
+ // Clone remaining properties
71
+ difference(allProperties, propsToRefer).forEach(cloneObject);
72
+ return _clone;
73
+ }
74
+ /**
75
+ * #### Deep Clone
76
+ *
77
+ * A function to recursively clone objects, arrays etc. with [cloning options]{@link ICloneOptions}
78
+ *
79
+ * *References Functions & Symbols by default*
80
+ *
81
+ * * * *
82
+ * Example:
83
+ * ```typescript
84
+ * import { clone } from "@telperion/js-utils/object";
85
+ *
86
+ * const object = {a: 1, b: {c: true, d: ["x", "y"]}};
87
+ *
88
+ * // Clone all
89
+ * const clonedObject = clone(object);
90
+ * // {a: 1, b: {c: true, d: ["x", "y"]}}
91
+ * // object.b.d === clonedObject.b.d // false
92
+ *
93
+ * // Clone all but reference "d"
94
+ * const clonedObject = clone(object, {propsToRefer: ["d"]});
95
+ * // {a: 1, b: {c: true, d: ["x", "y"]}}
96
+ * // object.b.d === clonedObject.b.d // true
97
+ * ```
98
+ *
99
+ * Static usage example:
100
+ * ```typescript
101
+ * import "@telperion/js-utils/object/static/clone";
102
+ *
103
+ * const object = {a: 1, b: 2};
104
+ * const clonedObject = Object.clone(object); // {a: 1, b: 2}
105
+ * ```
106
+ * * * *
107
+ * @param objectToClone Object to clone
108
+ * @param options {ICloneOptions} Cloning Options
109
+ * @see {@link ICloneOptions} for more information.
110
+ */
111
+ export function clone(objectToClone, { instancesToRefer = [], propsToRefer = [], valueFiltererToRefer = () => false, customCloners = new Map() } = {}) {
112
+ /**
113
+ * Whole options with defaults
114
+ */
115
+ const mergedCloners = merge(customCloners, DEFAULT_INSTANCE_CLONERS);
116
+ const allOptions = { instancesToRefer, propsToRefer, valueFiltererToRefer, customCloners: mergedCloners };
117
+ const finalizers = [];
118
+ const cloned = cloner(objectToClone, allOptions, { clonedObjects: new Map(), finalizers });
119
+ finalizers.forEach(callback => { callback(); });
120
+ return cloned;
121
+ }
122
+ function cloner(objectToClone, allOptions, internalData) {
123
+ const { instancesToRefer, valueFiltererToRefer, customCloners } = allOptions;
124
+ const { clonedObjects, finalizers, parent, key } = internalData;
125
+ // Pass primitives & functions
126
+ if (objectToClone === null || !(objectToClone instanceof Object) || objectToClone instanceof Function) {
127
+ return objectToClone;
128
+ }
129
+ // Pass unwanted references
130
+ if (instancesToRefer.some(instance => objectToClone instanceof instance)) {
131
+ return objectToClone;
132
+ }
133
+ // Pass filterer results
134
+ if (valueFiltererToRefer(objectToClone)) {
135
+ return objectToClone;
136
+ }
137
+ // If cloned before, skip circular cloning
138
+ if (clonedObjects.has(objectToClone)) {
139
+ if (clonedObjects.get(objectToClone) === CLONING_IN_PROGRESS) {
140
+ finalizers.push(() => {
141
+ parent[key] = clonedObjects.get(objectToClone);
142
+ });
143
+ return null;
144
+ }
145
+ return clonedObjects.get(objectToClone);
146
+ }
147
+ // Mark object as cloning
148
+ clonedObjects.set(objectToClone, CLONING_IN_PROGRESS);
149
+ // Clone
150
+ const customCloner = Array.from(customCloners.keys()).find(val => objectToClone instanceof val);
151
+ const result = customCloners.get(customCloner)(objectToClone, allOptions, { ...internalData });
152
+ clonedObjects.set(objectToClone, result);
153
+ return result;
154
+ }
155
+
156
+ //# sourceMappingURL=clone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../libs/js-utils/src/object/clone.ts"],"names":[],"mappings":"AAAA,uDAAuD;AAEvD,OAAO,EAAE,UAAU,EAAE,MAAM,sCAAsC,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAC;AAoItD;;GAEG;AACH,MAAM,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC;AAE1D;;GAEG;AACH,wDAAwD;AACxD,MAAM,wBAAwB,GAAG,IAAI,GAAG,EAA6B,CAAC;AAEtE,wBAAwB;AACxB,wBAAwB,CAAC,GAAG,CAAC,KAAK,EAAE,WAAkB,CAAC,CAAC;AACxD,wBAAwB,CAAC,GAAG,CAAC,GAAG,EAAE,SAAgB,CAAC,CAAC;AACpD,wBAAwB,CAAC,GAAG,CAAC,GAAG,EAAE,SAAgB,CAAC,CAAC;AACpD,wBAAwB,CAAC,GAAG,CAAC,MAAM,EAAE,YAAmB,CAAC,CAAC;AAE1D;;GAEG;AACH,SAAS,WAAW,CAAC,aAAoB,EAAE,OAAiC,EAAE,YAAiC;IAC7G,OAAO,aAAa,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,EAAE;QACvC,IAAI,OAAO,CAAC,YAAa,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;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CAAC,aAAa,EAAE,mBAAmB,CAAC,CAAC;IAEtD,QAAQ;IACR,MAAM,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,aAAc,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,aAAa,YAAY,GAAG,CAAC,CAAC;IACjG,MAAM,MAAM,GAAG,aAAc,CAAC,GAAG,CAAC,YAAa,CAAE,CAAC,aAAa,EAAE,UAAU,EAAE,EAAE,GAAG,YAAY,EAAE,CAAC,CAAC;IAClG,aAAa,CAAC,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC;AAChB,CAAC","file":"clone.js","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { difference } from '@telperion/js-utils/array/difference';\nimport { intersection } from '@telperion/js-utils/array/intersection';\nimport { merge } from '@telperion/js-utils/map/merge';\n\n/**\n * #### Cloning Options\n * Cloning options of the [clone]{@link clone} function\n */\nexport interface ICloneOptions {\n /**\n * #### Instance constructors which cloning unwanted\n * Give the instance constructors to prevent them being cloned\n *\n * * * *\n * Example:\n * ```typescript\n * import { clone } from \"@telperion/js-utils/object\";\n *\n * const object = {\n * width: 250,\n * element: document.querySelector(\"div\")\n * };\n *\n * const clonedObject = clone(object, {instancesToRefer: [ HTMLElement ]});\n * // clonedObject.element === object.element\n * ```\n * * * *\n * @default []\n */\n instancesToRefer?: any[];\n\n /**\n * #### Value filterer to pass same references\n * Being called for each value while cloning. Return true to prevent cloning for the incoming value and pass the same reference instead.\n *\n * * * *\n * Example:\n * ```typescript\n * import { clone } from \"@telperion/js-utils/object\";\n *\n * const john = { age: 20, willBeReferenced: true };\n * const jane = { age: 25, willBeReferenced: false };\n *\n * const array = [john, jane];\n *\n * const clonedArray = clone(array, {valueFiltererToRefer: person => person.willBeReferenced });\n *\n * // clonedArray[0] === array[0]\n * // clonedArray[1] !== array[1]\n * ```\n * * * *\n * @param value incoming value to be cloned\n * @return Return true to pass same reference, false to clone\n * @default () => false\n */\n valueFiltererToRefer?(value: any): boolean;\n\n /**\n * #### Properties which cloning unwanted\n * Give the unwanted properties to prevent them being cloned\n *\n * * * *\n * Example:\n * ```typescript\n * import { clone } from \"@telperion/js-utils/object\";\n *\n * const object = {\n * width: 250,\n * element: document.querySelector(\"div\")\n * };\n *\n * const clonedObject = clone(object, {propsToRefer: [ \"element\" ]});\n * // clonedObject.element === object.element\n * ```\n * * * *\n * @default []\n */\n propsToRefer?: Array<any>;\n\n /**\n * #### Custom Cloner Map\n *\n * * * *\n * Example:\n * ```typescript\n * import { clone } from \"@telperion/js-utils/object\";\n *\n * const object = {\n * width: 250,\n * element: document.querySelector(\"div\")\n * };\n *\n * function divCloner(objectToClone, options, internalData) {\n * const newElement = document.createElement(\"div\");\n * newElement.classList.add(\"cloned\");\n * return newElement;\n * }\n *\n * const customCloners = new Map();\n * customCloners.set(HTMLDivElement, divCloner);\n *\n * const clonedObject = clone(object);\n * ```\n * * * *\n */\n // eslint-disable-next-line @typescript-eslint/ban-types\n customCloners?: Map<Function, TInstanceCloner>;\n}\n\n/**\n * #### Internal Data For Cloning Process\n */\nexport interface IClonerInternalData {\n clonedObjects: Map<object, object | symbol>;\n parent?: Record<any, any>;\n key?: number | string | symbol;\n finalizers: Array<() => void>;\n}\n\n// TODO: uncomment the code below and delete polyfill when typedoc supports ts@2.8.x\n// type TNonOptionalCloneOptions = Required<ICloneOptions>;\nexport type PolyfillRequired<T, TNames extends string> = { [P in TNames]: (T & { [name: string]: never })[P] };\n\n/**\n * Cloning options with all properties required\n * @see ICloneOptions\n */\nexport type TNonOptionalCloneOptions = PolyfillRequired<ICloneOptions, keyof ICloneOptions>;\n\n/**\n * Custom Cloner Function\n */\nexport type TInstanceCloner = <T>(objectToClone: T, options: TNonOptionalCloneOptions, internalData: IClonerInternalData) => T;\n\n/**\n * A placeholder to symbolize the cloning progress of `objectToClone` hasn't finished yet\n */\nconst CLONING_IN_PROGRESS = Symbol('Cloning In Progress');\n\n/**\n * Function Map to clone specific objects types\n */\n// eslint-disable-next-line @typescript-eslint/ban-types\nconst DEFAULT_INSTANCE_CLONERS = new Map<Function, TInstanceCloner>();\n\n// Set cloning functions\nDEFAULT_INSTANCE_CLONERS.set(Array, arrayCloner as any);\nDEFAULT_INSTANCE_CLONERS.set(Map, mapCloner as any);\nDEFAULT_INSTANCE_CLONERS.set(Set, setCloner as any);\nDEFAULT_INSTANCE_CLONERS.set(Object, objectCloner as any);\n\n/**\n * Default Array Cloner\n */\nfunction arrayCloner(objectToClone: any[], options: TNonOptionalCloneOptions, internalData: IClonerInternalData) {\n return objectToClone.map((val, i, arr) => {\n if (options.propsToRefer!.indexOf(i) > -1) {\n return val;\n }\n\n return cloner(val, options, { ...internalData, parent: arr, key: i });\n }) as any;\n}\n\n/**\n * Default Map Cloner\n */\nfunction mapCloner(objectToClone: Map<any, any>, options: TNonOptionalCloneOptions, internalData: IClonerInternalData) {\n const newMap = new Map();\n\n intersection(options.propsToRefer!, Array.from(objectToClone.keys())).forEach(key => {\n newMap.set(key, objectToClone.get(key));\n });\n\n difference(Array.from(objectToClone.keys()), options.propsToRefer!).forEach(key => {\n newMap.set(key, cloner(objectToClone.get(key), options, { ...internalData, key, parent: newMap }));\n });\n\n return newMap;\n}\n\n/**\n * Default Set Cloner\n */\nfunction setCloner(objectToClone: Set<any>) {\n return new Set(objectToClone);\n}\n\n/**\n * Default Object Cloner\n */\nfunction objectCloner(objectToClone: Record<any, any>, options: TNonOptionalCloneOptions, internalData: IClonerInternalData) {\n const { propsToRefer } = options;\n\n /**\n * New Object Clone\n */\n const _clone: Record<any, any> = {};\n\n /**\n * Object cloner\n * @param key incoming property of object to clone\n */\n function cloneObject(key: any) {\n _clone[key] = cloner(objectToClone[key], options, { ...internalData, parent: _clone, key });\n }\n\n const allProperties = [...Object.keys(objectToClone), ...Object.getOwnPropertySymbols(objectToClone)];\n\n // Pass unwanted props\n intersection(<any[]>propsToRefer, allProperties).forEach(prop => {\n _clone[prop] = objectToClone[prop];\n });\n\n // Clone remaining properties\n difference(allProperties, propsToRefer!).forEach(cloneObject);\n\n return _clone;\n}\n\n/**\n * #### Deep Clone\n *\n * A function to recursively clone objects, arrays etc. with [cloning options]{@link ICloneOptions}\n *\n * *References Functions & Symbols by default*\n *\n * * * *\n * Example:\n * ```typescript\n * import { clone } from \"@telperion/js-utils/object\";\n *\n * const object = {a: 1, b: {c: true, d: [\"x\", \"y\"]}};\n *\n * // Clone all\n * const clonedObject = clone(object);\n * // {a: 1, b: {c: true, d: [\"x\", \"y\"]}}\n * // object.b.d === clonedObject.b.d // false\n *\n * // Clone all but reference \"d\"\n * const clonedObject = clone(object, {propsToRefer: [\"d\"]});\n * // {a: 1, b: {c: true, d: [\"x\", \"y\"]}}\n * // object.b.d === clonedObject.b.d // true\n * ```\n *\n * Static usage example:\n * ```typescript\n * import \"@telperion/js-utils/object/static/clone\";\n *\n * const object = {a: 1, b: 2};\n * const clonedObject = Object.clone(object); // {a: 1, b: 2}\n * ```\n * * * *\n * @param objectToClone Object to clone\n * @param options {ICloneOptions} Cloning Options\n * @see {@link ICloneOptions} for more information.\n */\nexport function clone<T>(\n objectToClone: T,\n {\n instancesToRefer = [],\n propsToRefer = [],\n valueFiltererToRefer = () => false,\n customCloners = new Map()\n }: ICloneOptions = <ICloneOptions>{}\n): T {\n /**\n * Whole options with defaults\n */\n const mergedCloners = merge(customCloners, DEFAULT_INSTANCE_CLONERS);\n const allOptions: TNonOptionalCloneOptions = { instancesToRefer, propsToRefer, valueFiltererToRefer, customCloners: mergedCloners };\n\n const finalizers: (() => void)[] = [];\n const cloned = cloner(objectToClone, allOptions, { clonedObjects: new Map(), finalizers });\n\n finalizers.forEach(callback => { callback(); });\n\n return cloned;\n}\n\nfunction cloner<T>(\n objectToClone: T,\n allOptions: TNonOptionalCloneOptions,\n internalData: IClonerInternalData\n): T {\n const { instancesToRefer, valueFiltererToRefer, customCloners } = allOptions;\n const { clonedObjects, finalizers, parent, key } = internalData;\n\n // Pass primitives & functions\n if (objectToClone === null || !(objectToClone instanceof Object) || objectToClone instanceof Function) {\n return objectToClone;\n }\n\n // Pass unwanted references\n if (instancesToRefer!.some(instance => objectToClone instanceof instance)) {\n return objectToClone;\n }\n\n // Pass filterer results\n if (valueFiltererToRefer!(objectToClone)) {\n return objectToClone;\n }\n\n // If cloned before, skip circular cloning\n if (clonedObjects.has(objectToClone)) {\n if (clonedObjects.get(objectToClone) === CLONING_IN_PROGRESS) {\n finalizers.push(() => {\n parent![key as any] = clonedObjects.get(objectToClone);\n });\n\n return null!;\n }\n return clonedObjects.get(objectToClone) as T;\n }\n\n // Mark object as cloning\n clonedObjects.set(objectToClone, CLONING_IN_PROGRESS);\n\n // Clone\n const customCloner = Array.from(customCloners!.keys()).find(val => objectToClone instanceof val);\n const result = customCloners!.get(customCloner!)!(objectToClone, allOptions, { ...internalData });\n clonedObjects.set(objectToClone, result);\n return result;\n}\n"]}
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compact = compact;
4
+ /**
5
+ * #### Removes `null` and `undefined` values and their keys from an object
6
+ *
7
+ * Additional values can be removed by passing to an array as the second argument
8
+ *
9
+ * * * * *
10
+ * Example usage:
11
+ * ```typescript
12
+ * import { compact } from "@telperion/js-utils/object";
13
+ *
14
+ * const a = {
15
+ * x: null,
16
+ * y: undefined,
17
+ * z: 20
18
+ * };
19
+ *
20
+ * compact(a); // {z: 20}
21
+ *
22
+ * ```
23
+ * Static usage example:
24
+ * ```typescript
25
+ * import "@telperion/js-utils/object/static/compact";
26
+ *
27
+ * const a = {
28
+ * x: null,
29
+ * y: undefined,
30
+ * z: 20
31
+ * };
32
+ *
33
+ * Object.compact(a); // {z: 20}
34
+ * ```
35
+ * * * *
36
+ * @param object Object delete empty keys
37
+ * @param additionalValuesToRemove Other values to delete
38
+ * @returns Compacted object
39
+ */
40
+ function compact(object, additionalValuesToRemove = []) {
41
+ const newObject = { ...object };
42
+ const valuesToRemove = [undefined, null, ...additionalValuesToRemove];
43
+ for (const [key, value] of Object.entries(newObject)) {
44
+ if (valuesToRemove.includes(value)) {
45
+ delete newObject[key];
46
+ }
47
+ }
48
+ return newObject;
49
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * #### Removes `null` and `undefined` values and their keys from an object
3
+ *
4
+ * Additional values can be removed by passing to an array as the second argument
5
+ *
6
+ * * * * *
7
+ * Example usage:
8
+ * ```typescript
9
+ * import { compact } from "@telperion/js-utils/object";
10
+ *
11
+ * const a = {
12
+ * x: null,
13
+ * y: undefined,
14
+ * z: 20
15
+ * };
16
+ *
17
+ * compact(a); // {z: 20}
18
+ *
19
+ * ```
20
+ * Static usage example:
21
+ * ```typescript
22
+ * import "@telperion/js-utils/object/static/compact";
23
+ *
24
+ * const a = {
25
+ * x: null,
26
+ * y: undefined,
27
+ * z: 20
28
+ * };
29
+ *
30
+ * Object.compact(a); // {z: 20}
31
+ * ```
32
+ * * * *
33
+ * @param object Object delete empty keys
34
+ * @param additionalValuesToRemove Other values to delete
35
+ * @returns Compacted object
36
+ */
37
+ export declare function compact<T extends Record<string | number | symbol, any>>(object: T, additionalValuesToRemove?: any[]): Partial<T>;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * #### Removes `null` and `undefined` values and their keys from an object
3
+ *
4
+ * Additional values can be removed by passing to an array as the second argument
5
+ *
6
+ * * * * *
7
+ * Example usage:
8
+ * ```typescript
9
+ * import { compact } from "@telperion/js-utils/object";
10
+ *
11
+ * const a = {
12
+ * x: null,
13
+ * y: undefined,
14
+ * z: 20
15
+ * };
16
+ *
17
+ * compact(a); // {z: 20}
18
+ *
19
+ * ```
20
+ * Static usage example:
21
+ * ```typescript
22
+ * import "@telperion/js-utils/object/static/compact";
23
+ *
24
+ * const a = {
25
+ * x: null,
26
+ * y: undefined,
27
+ * z: 20
28
+ * };
29
+ *
30
+ * Object.compact(a); // {z: 20}
31
+ * ```
32
+ * * * *
33
+ * @param object Object delete empty keys
34
+ * @param additionalValuesToRemove Other values to delete
35
+ * @returns Compacted object
36
+ */
37
+ export function compact(object, additionalValuesToRemove = []) {
38
+ const newObject = { ...object };
39
+ const valuesToRemove = [undefined, null, ...additionalValuesToRemove];
40
+ for (const [key, value] of Object.entries(newObject)) {
41
+ if (valuesToRemove.includes(value)) {
42
+ delete newObject[key];
43
+ }
44
+ }
45
+ return newObject;
46
+ }
47
+
48
+ //# sourceMappingURL=compact.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../libs/js-utils/src/object/compact.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,OAAO,CACrB,MAAS,EACT,2BAAkC,EAAE;IAEpC,MAAM,SAAS,GAAG,EAAC,GAAG,MAAM,EAAC,CAAC;IAC9B,MAAM,cAAc,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,GAAG,wBAAwB,CAAC,CAAC;IAEtE,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;QACrD,IAAI,cAAc,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","file":"compact.js","sourcesContent":["/**\n * #### Removes `null` and `undefined` values and their keys from an object\n *\n * Additional values can be removed by passing to an array as the second argument\n *\n * * * * *\n * Example usage:\n * ```typescript\n * import { compact } from \"@telperion/js-utils/object\";\n *\n * const a = {\n * x: null,\n * y: undefined,\n * z: 20\n * };\n *\n * compact(a); // {z: 20}\n *\n * ```\n * Static usage example:\n * ```typescript\n * import \"@telperion/js-utils/object/static/compact\";\n *\n * const a = {\n * x: null,\n * y: undefined,\n * z: 20\n * };\n *\n * Object.compact(a); // {z: 20}\n * ```\n * * * *\n * @param object Object delete empty keys\n * @param additionalValuesToRemove Other values to delete\n * @returns Compacted object\n */\nexport function compact<T extends Record<string|number|symbol, any>>(\n object: T,\n additionalValuesToRemove: any[] = []\n): Partial<T> {\n const newObject = {...object};\n const valuesToRemove = [undefined, null, ...additionalValuesToRemove];\n\n for (const [key, value] of Object.entries(newObject)) {\n if (valuesToRemove.includes(value)) {\n delete newObject[key];\n }\n }\n\n return newObject;\n}\n"]}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.deepFreeze = deepFreeze;
5
+ function deepFreeze(object) {
6
+ // Retrieve the property names defined on object
7
+ const propNames = Reflect.ownKeys(object);
8
+ // Freeze properties before freezing self
9
+ for (const name of propNames) {
10
+ const value = object[name];
11
+ if ((value && typeof value === "object") || typeof value === "function") {
12
+ deepFreeze(value);
13
+ }
14
+ }
15
+ return Object.freeze(object);
16
+ }
@@ -0,0 +1 @@
1
+ export declare function deepFreeze<T extends string | number | symbol = string, U = any>(object: Record<T, U>): Readonly<Record<T, U>>;
@@ -0,0 +1,15 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ export function deepFreeze(object) {
3
+ // Retrieve the property names defined on object
4
+ const propNames = Reflect.ownKeys(object);
5
+ // Freeze properties before freezing self
6
+ for (const name of propNames) {
7
+ const value = object[name];
8
+ if ((value && typeof value === "object") || typeof value === "function") {
9
+ deepFreeze(value);
10
+ }
11
+ }
12
+ return Object.freeze(object);
13
+ }
14
+
15
+ //# sourceMappingURL=deep-freeze.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../libs/js-utils/src/object/deep-freeze.ts"],"names":[],"mappings":"AAAA,uDAAuD;AAEvD,MAAM,UAAU,UAAU,CAAuD,MAAoB;IACnG,gDAAgD;IAChD,MAAM,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAE1C,yCAAyC;IACzC,KAAK,MAAM,IAAI,IAAI,SAAS,EAAE,CAAC;QAC7B,MAAM,KAAK,GAAI,MAAc,CAAC,IAAI,CAAC,CAAC;QAEpC,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;YACxE,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC","file":"deep-freeze.js","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nexport function deepFreeze<T extends string | number | symbol = string, U = any>(object: Record<T, U>) {\n // Retrieve the property names defined on object\n const propNames = Reflect.ownKeys(object);\n\n // Freeze properties before freezing self\n for (const name of propNames) {\n const value = (object as any)[name];\n\n if ((value && typeof value === \"object\") || typeof value === \"function\") {\n deepFreeze(value);\n }\n }\n\n return Object.freeze(object);\n}\n"]}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deepest = deepest;
4
+ /**
5
+ * #### Get deepest object in an object chain
6
+ *
7
+ * * * * *
8
+ * Example usage:
9
+ * ```typescript
10
+ * import { deepest } from "@telperion/js-utils/object";
11
+ *
12
+ * const a = {
13
+ * x: null
14
+ * };
15
+ *
16
+ * const b = {
17
+ * x: a
18
+ * };
19
+ *
20
+ * const c = {
21
+ * x: b
22
+ * };
23
+ *
24
+ * deepest(c, 'x'); // {x: null} (a)
25
+ *
26
+ * ```
27
+ * Static usage example:
28
+ * ```typescript
29
+ * import "@telperion/js-utils/object/static/deepest";
30
+ *
31
+ * const a = {
32
+ * x: null
33
+ * };
34
+ *
35
+ * const b = {
36
+ * x: a
37
+ * };
38
+ *
39
+ * const c = {
40
+ * x: b
41
+ * };
42
+ *
43
+ * Object.deepest(c, 'x'); // a
44
+ * ```
45
+ * * * *
46
+ * @param object Object to deep dive
47
+ * @param key key of the object which contains same type instance
48
+ */
49
+ function deepest(object, key) {
50
+ let obj = object;
51
+ while (obj[key]) {
52
+ obj = obj[key];
53
+ }
54
+ return obj;
55
+ }
@@ -0,0 +1,47 @@
1
+ import type { DeepestObject } from "@thalesrc/extra-ts-types";
2
+ /**
3
+ * #### Get deepest object in an object chain
4
+ *
5
+ * * * * *
6
+ * Example usage:
7
+ * ```typescript
8
+ * import { deepest } from "@telperion/js-utils/object";
9
+ *
10
+ * const a = {
11
+ * x: null
12
+ * };
13
+ *
14
+ * const b = {
15
+ * x: a
16
+ * };
17
+ *
18
+ * const c = {
19
+ * x: b
20
+ * };
21
+ *
22
+ * deepest(c, 'x'); // {x: null} (a)
23
+ *
24
+ * ```
25
+ * Static usage example:
26
+ * ```typescript
27
+ * import "@telperion/js-utils/object/static/deepest";
28
+ *
29
+ * const a = {
30
+ * x: null
31
+ * };
32
+ *
33
+ * const b = {
34
+ * x: a
35
+ * };
36
+ *
37
+ * const c = {
38
+ * x: b
39
+ * };
40
+ *
41
+ * Object.deepest(c, 'x'); // a
42
+ * ```
43
+ * * * *
44
+ * @param object Object to deep dive
45
+ * @param key key of the object which contains same type instance
46
+ */
47
+ export declare function deepest<T, K extends keyof T>(object: T, key: K): DeepestObject<T, K>;