@nejs/basic-extensions 2.6.0 → 2.8.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 (271) hide show
  1. package/.vscode/settings.json +5 -0
  2. package/README.md +6129 -1574
  3. package/dist/@nejs/basic-extensions.bundle.2.7.0.js +19 -0
  4. package/dist/@nejs/basic-extensions.bundle.2.7.0.js.map +7 -0
  5. package/dist/cjs/array.extensions.d.ts +39 -0
  6. package/dist/cjs/array.extensions.js +303 -0
  7. package/dist/cjs/array.extensions.js.map +1 -0
  8. package/dist/cjs/big.int.extension.d.ts +31 -0
  9. package/dist/cjs/big.int.extension.js +164 -0
  10. package/dist/cjs/big.int.extension.js.map +1 -0
  11. package/dist/cjs/{newClasses → classes}/asyncIterable.js +32 -44
  12. package/dist/cjs/classes/asyncIterable.js.map +1 -0
  13. package/dist/cjs/{newClasses → classes}/deferred.js +66 -138
  14. package/dist/cjs/classes/deferred.js.map +1 -0
  15. package/dist/cjs/{newClasses → classes}/descriptor.js +56 -90
  16. package/dist/cjs/classes/descriptor.js.map +1 -0
  17. package/dist/cjs/classes/index.d.ts +13 -0
  18. package/dist/cjs/classes/index.js +57 -0
  19. package/dist/cjs/classes/index.js.map +1 -0
  20. package/dist/cjs/classes/introspector.d.ts +20 -0
  21. package/dist/cjs/classes/introspector.js +130 -0
  22. package/dist/cjs/classes/introspector.js.map +1 -0
  23. package/dist/cjs/{newClasses → classes}/iterable.js +42 -63
  24. package/dist/cjs/classes/iterable.js.map +1 -0
  25. package/dist/cjs/classes/param.parser.d.ts +227 -0
  26. package/dist/cjs/classes/param.parser.js +242 -0
  27. package/dist/cjs/classes/param.parser.js.map +1 -0
  28. package/dist/cjs/classes/pluggable.proxy.d.ts +152 -0
  29. package/dist/cjs/classes/pluggable.proxy.js +444 -0
  30. package/dist/cjs/classes/pluggable.proxy.js.map +1 -0
  31. package/dist/cjs/{newClasses → classes}/refmap.js +18 -30
  32. package/dist/cjs/classes/refmap.js.map +1 -0
  33. package/dist/cjs/{newClasses → classes}/refset.js +28 -47
  34. package/dist/cjs/classes/refset.js.map +1 -0
  35. package/dist/cjs/classes/symkeys.d.ts +292 -0
  36. package/dist/cjs/classes/symkeys.js +424 -0
  37. package/dist/cjs/classes/symkeys.js.map +1 -0
  38. package/dist/cjs/classes/type.d.ts +56 -0
  39. package/dist/cjs/classes/type.js +405 -0
  40. package/dist/cjs/classes/type.js.map +1 -0
  41. package/dist/cjs/function.extensions.js +757 -0
  42. package/dist/cjs/function.extensions.js.map +1 -0
  43. package/dist/cjs/global.this.js +261 -0
  44. package/dist/cjs/global.this.js.map +1 -0
  45. package/dist/cjs/index.d.ts +4 -3
  46. package/dist/cjs/index.js +62 -32
  47. package/dist/cjs/index.js.map +1 -1
  48. package/dist/cjs/json.extensions.d.ts +2 -0
  49. package/dist/cjs/json.extensions.js +107 -0
  50. package/dist/cjs/json.extensions.js.map +1 -0
  51. package/dist/{mjs/mapextensions.d.ts → cjs/map.extensions.d.ts} +1 -0
  52. package/dist/cjs/map.extensions.js +142 -0
  53. package/dist/cjs/map.extensions.js.map +1 -0
  54. package/dist/cjs/number.extension.d.ts +44 -0
  55. package/dist/cjs/number.extension.js +260 -0
  56. package/dist/cjs/number.extension.js.map +1 -0
  57. package/dist/cjs/object.extensions.d.ts +62 -0
  58. package/dist/cjs/object.extensions.js +1116 -0
  59. package/dist/cjs/object.extensions.js.map +1 -0
  60. package/dist/cjs/proxy.extensions.d.ts +2 -0
  61. package/dist/cjs/proxy.extensions.js +207 -0
  62. package/dist/cjs/proxy.extensions.js.map +1 -0
  63. package/dist/cjs/reflect.extensions.js +316 -0
  64. package/dist/cjs/reflect.extensions.js.map +1 -0
  65. package/dist/cjs/regular.expression.extensions.d.ts +2 -0
  66. package/dist/cjs/regular.expression.extensions.js +423 -0
  67. package/dist/cjs/regular.expression.extensions.js.map +1 -0
  68. package/dist/cjs/set.extensions.d.ts +40 -0
  69. package/dist/cjs/{setextensions.js → set.extensions.js} +150 -2
  70. package/dist/cjs/set.extensions.js.map +1 -0
  71. package/dist/cjs/string.extensions.js +471 -0
  72. package/dist/cjs/string.extensions.js.map +1 -0
  73. package/dist/{mjs/symbolextensions.d.ts → cjs/symbol.extensions.d.ts} +1 -0
  74. package/dist/cjs/symbol.extensions.js +259 -0
  75. package/dist/cjs/symbol.extensions.js.map +1 -0
  76. package/dist/cjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
  77. package/dist/cjs/weakref.extensions.js.map +1 -0
  78. package/dist/mjs/array.extensions.d.ts +39 -0
  79. package/dist/mjs/array.extensions.js +300 -0
  80. package/dist/mjs/array.extensions.js.map +1 -0
  81. package/dist/mjs/big.int.extension.d.ts +31 -0
  82. package/dist/mjs/big.int.extension.js +161 -0
  83. package/dist/mjs/big.int.extension.js.map +1 -0
  84. package/dist/mjs/classes/asyncIterable.js.map +1 -0
  85. package/dist/mjs/classes/deferred.js.map +1 -0
  86. package/dist/mjs/{newClasses → classes}/descriptor.js +7 -4
  87. package/dist/mjs/classes/descriptor.js.map +1 -0
  88. package/dist/mjs/classes/index.d.ts +13 -0
  89. package/dist/mjs/classes/index.js +40 -0
  90. package/dist/mjs/classes/index.js.map +1 -0
  91. package/dist/mjs/classes/introspector.d.ts +20 -0
  92. package/dist/mjs/classes/introspector.js +126 -0
  93. package/dist/mjs/classes/introspector.js.map +1 -0
  94. package/dist/mjs/classes/iterable.js.map +1 -0
  95. package/dist/mjs/classes/param.parser.d.ts +227 -0
  96. package/dist/mjs/classes/param.parser.js +238 -0
  97. package/dist/mjs/classes/param.parser.js.map +1 -0
  98. package/dist/mjs/classes/pluggable.proxy.d.ts +152 -0
  99. package/dist/mjs/classes/pluggable.proxy.js +438 -0
  100. package/dist/mjs/classes/pluggable.proxy.js.map +1 -0
  101. package/dist/mjs/{newClasses → classes}/refmap.js +3 -3
  102. package/dist/mjs/classes/refmap.js.map +1 -0
  103. package/dist/mjs/classes/refset.js.map +1 -0
  104. package/dist/mjs/classes/symkeys.d.ts +292 -0
  105. package/dist/mjs/classes/symkeys.js +420 -0
  106. package/dist/mjs/classes/symkeys.js.map +1 -0
  107. package/dist/mjs/classes/type.d.ts +56 -0
  108. package/dist/mjs/classes/type.js +401 -0
  109. package/dist/mjs/classes/type.js.map +1 -0
  110. package/dist/mjs/function.extensions.js +754 -0
  111. package/dist/mjs/function.extensions.js.map +1 -0
  112. package/dist/mjs/global.this.js +258 -0
  113. package/dist/mjs/global.this.js.map +1 -0
  114. package/dist/mjs/index.d.ts +4 -3
  115. package/dist/mjs/index.js +49 -19
  116. package/dist/mjs/index.js.map +1 -1
  117. package/dist/mjs/json.extensions.d.ts +2 -0
  118. package/dist/mjs/json.extensions.js +104 -0
  119. package/dist/mjs/json.extensions.js.map +1 -0
  120. package/dist/{cjs/mapextensions.d.ts → mjs/map.extensions.d.ts} +1 -0
  121. package/dist/mjs/map.extensions.js +139 -0
  122. package/dist/mjs/map.extensions.js.map +1 -0
  123. package/dist/mjs/number.extension.d.ts +44 -0
  124. package/dist/mjs/number.extension.js +257 -0
  125. package/dist/mjs/number.extension.js.map +1 -0
  126. package/dist/mjs/object.extensions.d.ts +62 -0
  127. package/dist/mjs/object.extensions.js +1112 -0
  128. package/dist/mjs/object.extensions.js.map +1 -0
  129. package/dist/mjs/proxy.extensions.d.ts +2 -0
  130. package/dist/mjs/proxy.extensions.js +204 -0
  131. package/dist/mjs/proxy.extensions.js.map +1 -0
  132. package/dist/mjs/reflect.extensions.js +313 -0
  133. package/dist/mjs/reflect.extensions.js.map +1 -0
  134. package/dist/mjs/regular.expression.extensions.d.ts +2 -0
  135. package/dist/mjs/regular.expression.extensions.js +420 -0
  136. package/dist/mjs/regular.expression.extensions.js.map +1 -0
  137. package/dist/mjs/set.extensions.d.ts +40 -0
  138. package/dist/mjs/{setextensions.js → set.extensions.js} +149 -1
  139. package/dist/mjs/set.extensions.js.map +1 -0
  140. package/dist/mjs/string.extensions.js +468 -0
  141. package/dist/mjs/string.extensions.js.map +1 -0
  142. package/dist/{cjs/symbolextensions.d.ts → mjs/symbol.extensions.d.ts} +1 -0
  143. package/dist/mjs/symbol.extensions.js +256 -0
  144. package/dist/mjs/symbol.extensions.js.map +1 -0
  145. package/dist/mjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
  146. package/dist/mjs/weakref.extensions.js.map +1 -0
  147. package/docs/index.html +24045 -5805
  148. package/package.json +6 -4
  149. package/repl.bootstrap.js +21 -0
  150. package/src/array.extensions.js +322 -0
  151. package/src/big.int.extension.js +163 -0
  152. package/src/{newClasses → classes}/descriptor.js +16 -12
  153. package/src/classes/index.js +51 -0
  154. package/src/classes/introspector.js +167 -0
  155. package/src/classes/param.parser.js +253 -0
  156. package/src/classes/pluggable.proxy.js +485 -0
  157. package/src/{newClasses → classes}/refmap.js +5 -3
  158. package/src/classes/symkeys.js +464 -0
  159. package/src/classes/type.js +427 -0
  160. package/src/function.extensions.js +818 -0
  161. package/src/global.this.js +304 -0
  162. package/src/index.js +56 -23
  163. package/src/json.extensions.js +108 -0
  164. package/src/map.extensions.js +144 -0
  165. package/src/number.extension.js +273 -0
  166. package/src/object.extensions.js +1222 -0
  167. package/src/proxy.extensions.js +229 -0
  168. package/src/reflect.extensions.js +346 -0
  169. package/src/regular.expression.extensions.js +451 -0
  170. package/src/{setextensions.js → set.extensions.js} +151 -2
  171. package/src/string.extensions.js +515 -0
  172. package/src/symbol.extensions.js +268 -0
  173. package/tests/newClasses/refmap.test.js +3 -2
  174. package/tsconfig.base.json +5 -3
  175. package/tsconfig.cjs.json +2 -2
  176. package/tsconfig.esm.json +2 -2
  177. package/dist/@nejs/basic-extensions.bundle.2.5.0.js +0 -8
  178. package/dist/@nejs/basic-extensions.bundle.2.5.0.js.map +0 -7
  179. package/dist/cjs/arrayextensions.d.ts +0 -10
  180. package/dist/cjs/arrayextensions.js +0 -73
  181. package/dist/cjs/arrayextensions.js.map +0 -1
  182. package/dist/cjs/functionextensions.js +0 -202
  183. package/dist/cjs/functionextensions.js.map +0 -1
  184. package/dist/cjs/globals.js +0 -166
  185. package/dist/cjs/globals.js.map +0 -1
  186. package/dist/cjs/mapextensions.js +0 -32
  187. package/dist/cjs/mapextensions.js.map +0 -1
  188. package/dist/cjs/newClasses/asyncIterable.js.map +0 -1
  189. package/dist/cjs/newClasses/deferred.js.map +0 -1
  190. package/dist/cjs/newClasses/descriptor.js.map +0 -1
  191. package/dist/cjs/newClasses/iterable.js.map +0 -1
  192. package/dist/cjs/newClasses/refmap.js.map +0 -1
  193. package/dist/cjs/newClasses/refset.js.map +0 -1
  194. package/dist/cjs/objectextensions.d.ts +0 -11
  195. package/dist/cjs/objectextensions.js +0 -232
  196. package/dist/cjs/objectextensions.js.map +0 -1
  197. package/dist/cjs/reflectextensions.js +0 -111
  198. package/dist/cjs/reflectextensions.js.map +0 -1
  199. package/dist/cjs/setextensions.d.ts +0 -2
  200. package/dist/cjs/setextensions.js.map +0 -1
  201. package/dist/cjs/stringextensions.js +0 -158
  202. package/dist/cjs/stringextensions.js.map +0 -1
  203. package/dist/cjs/symbolextensions.js +0 -69
  204. package/dist/cjs/symbolextensions.js.map +0 -1
  205. package/dist/cjs/weakrefextensions.js.map +0 -1
  206. package/dist/mjs/arrayextensions.d.ts +0 -10
  207. package/dist/mjs/arrayextensions.js +0 -70
  208. package/dist/mjs/arrayextensions.js.map +0 -1
  209. package/dist/mjs/functionextensions.js +0 -199
  210. package/dist/mjs/functionextensions.js.map +0 -1
  211. package/dist/mjs/globals.js +0 -163
  212. package/dist/mjs/globals.js.map +0 -1
  213. package/dist/mjs/mapextensions.js +0 -29
  214. package/dist/mjs/mapextensions.js.map +0 -1
  215. package/dist/mjs/newClasses/asyncIterable.js.map +0 -1
  216. package/dist/mjs/newClasses/deferred.js.map +0 -1
  217. package/dist/mjs/newClasses/descriptor.js.map +0 -1
  218. package/dist/mjs/newClasses/iterable.js.map +0 -1
  219. package/dist/mjs/newClasses/refmap.js.map +0 -1
  220. package/dist/mjs/newClasses/refset.js.map +0 -1
  221. package/dist/mjs/objectextensions.d.ts +0 -11
  222. package/dist/mjs/objectextensions.js +0 -229
  223. package/dist/mjs/objectextensions.js.map +0 -1
  224. package/dist/mjs/reflectextensions.js +0 -108
  225. package/dist/mjs/reflectextensions.js.map +0 -1
  226. package/dist/mjs/setextensions.d.ts +0 -2
  227. package/dist/mjs/setextensions.js.map +0 -1
  228. package/dist/mjs/stringextensions.js +0 -155
  229. package/dist/mjs/stringextensions.js.map +0 -1
  230. package/dist/mjs/symbolextensions.js +0 -66
  231. package/dist/mjs/symbolextensions.js.map +0 -1
  232. package/dist/mjs/weakrefextensions.js.map +0 -1
  233. package/src/arrayextensions.js +0 -75
  234. package/src/functionextensions.js +0 -225
  235. package/src/globals.js +0 -196
  236. package/src/mapextensions.js +0 -32
  237. package/src/objectextensions.js +0 -256
  238. package/src/reflectextensions.js +0 -118
  239. package/src/stringextensions.js +0 -166
  240. package/src/symbolextensions.js +0 -69
  241. /package/dist/cjs/{newClasses → classes}/asyncIterable.d.ts +0 -0
  242. /package/dist/cjs/{newClasses → classes}/deferred.d.ts +0 -0
  243. /package/dist/cjs/{newClasses → classes}/descriptor.d.ts +0 -0
  244. /package/dist/cjs/{newClasses → classes}/iterable.d.ts +0 -0
  245. /package/dist/cjs/{newClasses → classes}/refmap.d.ts +0 -0
  246. /package/dist/cjs/{newClasses → classes}/refset.d.ts +0 -0
  247. /package/dist/cjs/{functionextensions.d.ts → function.extensions.d.ts} +0 -0
  248. /package/dist/cjs/{globals.d.ts → global.this.d.ts} +0 -0
  249. /package/dist/cjs/{reflectextensions.d.ts → reflect.extensions.d.ts} +0 -0
  250. /package/dist/cjs/{stringextensions.d.ts → string.extensions.d.ts} +0 -0
  251. /package/dist/cjs/{weakrefextensions.d.ts → weakref.extensions.d.ts} +0 -0
  252. /package/dist/mjs/{newClasses → classes}/asyncIterable.d.ts +0 -0
  253. /package/dist/mjs/{newClasses → classes}/asyncIterable.js +0 -0
  254. /package/dist/mjs/{newClasses → classes}/deferred.d.ts +0 -0
  255. /package/dist/mjs/{newClasses → classes}/deferred.js +0 -0
  256. /package/dist/mjs/{newClasses → classes}/descriptor.d.ts +0 -0
  257. /package/dist/mjs/{newClasses → classes}/iterable.d.ts +0 -0
  258. /package/dist/mjs/{newClasses → classes}/iterable.js +0 -0
  259. /package/dist/mjs/{newClasses → classes}/refmap.d.ts +0 -0
  260. /package/dist/mjs/{newClasses → classes}/refset.d.ts +0 -0
  261. /package/dist/mjs/{newClasses → classes}/refset.js +0 -0
  262. /package/dist/mjs/{functionextensions.d.ts → function.extensions.d.ts} +0 -0
  263. /package/dist/mjs/{globals.d.ts → global.this.d.ts} +0 -0
  264. /package/dist/mjs/{reflectextensions.d.ts → reflect.extensions.d.ts} +0 -0
  265. /package/dist/mjs/{stringextensions.d.ts → string.extensions.d.ts} +0 -0
  266. /package/dist/mjs/{weakrefextensions.d.ts → weakref.extensions.d.ts} +0 -0
  267. /package/src/{newClasses → classes}/asyncIterable.js +0 -0
  268. /package/src/{newClasses → classes}/deferred.js +0 -0
  269. /package/src/{newClasses → classes}/iterable.js +0 -0
  270. /package/src/{newClasses → classes}/refset.js +0 -0
  271. /package/src/{weakrefextensions.js → weakref.extensions.js} +0 -0
@@ -0,0 +1,1222 @@
1
+ import { Patch } from '@nejs/extension'
2
+
3
+ import { SymbolExtensions } from './symbol.extensions.js';
4
+ import { Descriptor } from './classes/descriptor.js';
5
+
6
+ const { keys: symkeys } = SymbolExtensions.patches
7
+
8
+ // Avoid circular dependencies; rewrite here for brevity
9
+ const isFn = o => typeof o === 'function' || o instanceof Function
10
+ const isStr = o => typeof o === 'string' || o instanceof String
11
+ const isBool = o => typeof o === 'boolean'
12
+ const isTrue = o => isBool(o) && o === true
13
+ const isTruthy = o => isTrue(!!o)
14
+ const isFalse = o => isBool(o) && o === false
15
+ const isFalsy = o => isFalse(!!o)
16
+
17
+ /**
18
+ * `ObjectExtensions` is a constant that applies a patch to the global
19
+ * `Object` constructor. This patch extends the `Object` with additional
20
+ * methods and properties, enhancing its functionality.
21
+ *
22
+ * The `Patch` function takes two arguments: the target object to be patched
23
+ * (in this case, `Object`), and an object containing the methods and
24
+ * properties to be added to the target object.
25
+ *
26
+ * @type {Patch}
27
+ * @memberof module:object.extensions
28
+ */
29
+ export const ObjectExtensions = new Patch(Object, {
30
+ [Patch.kMutablyHidden]: {
31
+ /**
32
+ * Creates a shallow copy of the provided object(s).
33
+ *
34
+ * This method uses the `copyObject` function with the `deep` parameter
35
+ * set to `false`, indicating a shallow copy. It takes a destination
36
+ * object and any number of source objects, and copies the properties
37
+ * from the source objects to the destination object. If a property
38
+ * already exists on the destination object, it will be overwritten.
39
+ *
40
+ * Note: This method does not copy nested objects or arrays. They are
41
+ * copied by reference, not by value. To create a deep copy, use the
42
+ * `deepCopy` method instead.
43
+ *
44
+ * @param {object} destination - The object to which properties will be
45
+ * copied.
46
+ * @param {...object} sources - The source object(s) from which
47
+ * properties will be copied.
48
+ * @returns {object} The destination object with the copied properties.
49
+ *
50
+ * @example
51
+ * const obj1 = { a: 1, b: 2 };
52
+ * const obj2 = { b: 3, c: 4 };
53
+ * const result = ObjectExtensions.copy(obj1, obj2);
54
+ * console.log(result); // Output: { a: 1, b: 3, c: 4 }
55
+ */
56
+ copy(destination, ...sources) {
57
+ return copyObject(false, destination, ...sources)
58
+ },
59
+
60
+ /**
61
+ * Creates a deep copy of the provided object(s).
62
+ *
63
+ * This method uses the `copyObject` function with the `deep` parameter
64
+ * set to `true`, indicating a deep copy. It takes a destination
65
+ * object and any number of source objects, and copies the properties
66
+ * from the source objects to the destination object. If a property
67
+ * already exists on the destination object, it will be overwritten.
68
+ *
69
+ * Note: This method copies nested objects or arrays by value, not by
70
+ * reference. To create a shallow copy, use the `copy` method instead.
71
+ *
72
+ * @param {object} destination - The object to which properties will be
73
+ * copied.
74
+ * @param {...object} sources - The source object(s) from which
75
+ * properties will be copied.
76
+ * @returns {object} The destination object with the copied properties.
77
+ *
78
+ * @example
79
+ * const obj1 = { a: 1, b: { c: 2 } };
80
+ * const obj2 = { b: { d: 3 }, e: 4 };
81
+ * const result = ObjectExtensions.deepCopy(obj1, obj2);
82
+ * console.log(result); // Output: { a: 1, b: { d: 3 }, e: 4 }
83
+ */
84
+ deepCopy(destination, ...sources) {
85
+ return copyObject(true, destination, ...sources)
86
+ },
87
+
88
+ /**
89
+ * A getter property that provides access to the definition types used
90
+ * for object property definitions. These types are used to control the
91
+ * visibility and mutability of object properties.
92
+ *
93
+ * @returns {Object} An object with getter properties for each definition
94
+ * type. The properties are:
95
+ * - `mutablyHidden`: A symbol representing a mutably hidden property,
96
+ * non-enumerable, but configurable.
97
+ * - `mutablyVisible`: A symbol representing a mutably visible property,
98
+ * enumerable, configurable
99
+ * - `immutablyHidden`: A symbol representing an immutably hidden property,
100
+ * non-enumerable, non-configurable.
101
+ * - `immutablyVisible`: A symbol representing an immutably visible
102
+ * property, enumerable, non-configurable.
103
+ *
104
+ * @example
105
+ * // Get the symbol for a mutably hidden property
106
+ * const hiddenSymbol = Object.definitionType.mutablyHidden;
107
+ *
108
+ * // Define a new mutably hidden property on an object
109
+ * Object.define(myObject, 'myProperty', myValue, hiddenSymbol);
110
+ */
111
+ get definitionType() {
112
+ return {
113
+ get mutablyHidden() { return Patch.kMutablyHidden },
114
+ get mutablyVisible() { return Patch.kMutablyVisible },
115
+ get immutablyHidden() { return Patch.kImmutablyHidden },
116
+ get immutablyVisible() { return Patch.kImmutablyVisible },
117
+ }
118
+ },
119
+
120
+ /**
121
+ * Defines a new property on an object with a specified value and
122
+ * visibility/mutability flag. The flag determines the visibility and
123
+ * mutability of the property. By default, the property is defined as
124
+ * mutably hidden.
125
+ *
126
+ * @param {object} object - The object on which to define the property.
127
+ * @param {string} key - The name of the property to be defined.
128
+ * @param {any} value - The value of the property to be defined.
129
+ * @param {symbol} [flag=Object.definitionType.mutablyHidden] - The
130
+ * visibility/mutability flag for the property. This should be one of the
131
+ * symbols available in `ObjectExtensions.definitionType`.
132
+ * @returns {object} The object with the newly defined property.
133
+ *
134
+ * @example
135
+ * // Define a new mutably hidden property on an object
136
+ * const myObject = {};
137
+ * const myValue = 'Hello, world!';
138
+ * const hiddenSymbol = Object.definitionType.mutablyHidden;
139
+ * Object.define(myObject, 'myProperty', myValue, hiddenSymbol);
140
+ * // myObject now has a mutably hidden property 'myProperty' with value
141
+ * // 'Hello, world!'
142
+ */
143
+ define(object, key, value, flag = Object.definitionType.mutablyHidden) {
144
+ const properties = Patch.getDescriptorOverridesFromSymbol(flag)
145
+ return Object.defineProperty(object, key, { ...properties, value })
146
+ },
147
+
148
+ /**
149
+ * Defines a new accessor property on an object with specified getter and
150
+ * setter functions and a visibility/mutability flag. The flag determines
151
+ * the visibility and mutability of the property. By default, the property
152
+ * is defined as mutably hidden.
153
+ *
154
+ * @param {object} object - The object on which to define the property.
155
+ * @param {string} key - The name of the property to be defined.
156
+ * @param {function} get - The getter function for the property.
157
+ * @param {function} set - The setter function for the property.
158
+ * @param {symbol} [flag=Object.definitionType.mutablyHidden] - The
159
+ * visibility/mutability flag for the property. This should be one of the
160
+ * symbols available in `ObjectExtensions.definitionType`.
161
+ * @returns {object} The object with the newly defined property.
162
+ *
163
+ * @example
164
+ * // Define a new mutably hidden accessor property on an object
165
+ * const myObject = {};
166
+ * const hiddenSymbol = ObjectExtensions.definitionType.mutablyHidden;
167
+ * ObjectExtensions.defineAccessor(
168
+ * myObject,
169
+ * 'myProperty',
170
+ * () => 'Hello, world!',
171
+ * (value) => console.log(`Setting value: ${value}`),
172
+ * hiddenSymbol
173
+ * );
174
+ * // myObject now has a mutably hidden property 'myProperty' with getter
175
+ * // and setter functions
176
+ */
177
+ defineAccessor(
178
+ object, key, get, set, flag = Object.definitionType.mutablyHidden
179
+ ) {
180
+ const properties = Patch.getDescriptorOverridesFromSymbol(flag)
181
+ return Object.defineProperty(object, key, { ...properties, get, set })
182
+ },
183
+
184
+ add(...args) {
185
+ const { isDescriptor } = Descriptor
186
+ const { isObject: isObj } = this
187
+ const { kDescriptorStore } = this
188
+
189
+ let toObject, key, value, _get, _set, storage, storageKey
190
+ let _type, _flag, _desc
191
+
192
+ // Check to see if we received multiple arguments or an object
193
+ if (args.length && isObj(args[0])) {
194
+ ({
195
+ toObject: obj,
196
+ key,
197
+ value,
198
+ get: _get,
199
+ set: _set,
200
+ storage,
201
+ storageKey,
202
+ type: _type = ['accessor', 'data'][1],
203
+ flag: _flag = undefined,
204
+ descriptorBase: _desc = undefined,
205
+ } = args[0])
206
+ }
207
+ else if (args.length > 1) {
208
+ ([
209
+ toObject,
210
+ _type,
211
+ key,
212
+ getOrValue,
213
+ _set,
214
+ storage,
215
+ storageKey,
216
+ _flag,
217
+ _desc,
218
+ ] = args)
219
+
220
+ _type = (
221
+ ['accessor', 'data'].includes(_type.toLowerCase())
222
+ ? _type.toLowerCase() : 'data'
223
+ )
224
+ _get = _type === 'accessor' ? getOrValue : undefined
225
+ _value = _type === 'data' ? getOrValue : undefined
226
+ }
227
+
228
+ const more = isDescriptor(_desc) ? _desc : {}
229
+ const flag = _flag || Object.definitionType.mutablyVisible
230
+ const props = { ...Patch.getDescriptorOverridesFromSymbol(flag), ...more }
231
+ const type = (['accessor', 'data'].includes(_type)
232
+ ? String(_type).toLowerCase() : 'data'
233
+ )
234
+
235
+ switch (type) {
236
+ case 'accessor':
237
+ let store = storage;
238
+ let storeKey = storageKey || key
239
+ let makeStore = false
240
+ let get = _get;
241
+ let set = _set;
242
+
243
+ if (!isTruthy(get) && !isFn(get)) { get = undefined }
244
+ if (!isTruthy(set) && !isFn(set)) { set = undefined }
245
+
246
+ if (isObj(store) || isTrue(store) || isFn(store)) {
247
+ makeStore = isTrue(store)
248
+ store = isFn(store) ? store() : store
249
+ store = isObj(store) ? store : (makeStore && {} || undefined)
250
+ }
251
+ // store should be defined by here: object or undefined
252
+
253
+ if (!get && !set && makeStore) {
254
+ // being lazy here, someone has defined we make an accessor but
255
+ // wants the default accessor behaviors with an associated store
256
+ // made by us.
257
+ Object.defineProperty(obj, kDescriptorStore, {
258
+ value: symkeys.add('descriptor.store', store),
259
+ configurable: true,
260
+ enumerable: false,
261
+ writable: true,
262
+ })
263
+
264
+ get = () => this[kDescriptorStore].data[storeKey]
265
+ set = (value) => { this[kDescriptorStore].data[storeKey] = value }
266
+ }
267
+
268
+ else if (get.length && set.length > 1 && store) {
269
+ // if we received a get or set that takes more arguments than
270
+ // expected, assume the last argument should be the store variable
271
+ // so we execute the supplied function with the storage and its
272
+ // results or byproducts are the result to the get/set we define
273
+ const innerGet = get
274
+ const innerSet = set
275
+ get = () => innerGet(store)
276
+ set = (value) => innerSet(value, store)
277
+ }
278
+ // get and set should be in their final state by here
279
+
280
+ Object.defineProperty(obj, key, { ...props, get, set })
281
+ break
282
+
283
+ case 'data':
284
+ Object.defineProperty(obj, key, { ...props, value })
285
+ break
286
+ }
287
+
288
+ return obj
289
+ },
290
+
291
+ /**
292
+ * Creates a new object from an array of key-value pairs (entries), with an
293
+ * optional prototype and reducer function. If no prototype is provided,
294
+ * the default Object.prototype is used. If no reducer is provided, a
295
+ * default reducer is used that assigns each value to its corresponding key.
296
+ *
297
+ * @param {Array} entries - An array of key-value pairs. Each entry should
298
+ * be an array where the first element is the key and the second element is
299
+ * the value. Non-conforming entries are ignored.
300
+ * @param {object} [prototype=Object.prototype] - The prototype to use for
301
+ * the new object. If not provided, Object.prototype is used.
302
+ * @param {Function} [reducer] - An optional reducer function to use when
303
+ * creating the new object. If not provided, a default reducer is used that
304
+ * assigns each value to its corresponding key.
305
+ * @returns {object|undefined} - The new object created from the entries, or
306
+ * undefined if the entries array is not valid or contains no valid entries.
307
+ *
308
+ * @example
309
+ * // Create an object with a custom prototype and reducer
310
+ * const myPrototype = { foo: 'bar' };
311
+ * const myReducer = (obj, [key, value]) => {
312
+ * obj[key] = value.toUpperCase();
313
+ * return obj;
314
+ * };
315
+ *
316
+ * const myEntries = [['name', 'John'], ['age', '30']];
317
+ * const myObject = Object.fromEntriesUsing(
318
+ * myEntries, myPrototype, myReducer
319
+ * );
320
+ *
321
+ * // myObject is now { name: 'JOHN', age: '30' }
322
+ * // with prototype { foo: 'bar' }
323
+ */
324
+ fromEntriesUsing(entries, prototype = Object.prototype, reducer = undefined) {
325
+ if (!Array.isArray(entries)) {
326
+ return undefined;
327
+ }
328
+
329
+ const entriesToUse = entries.filter(
330
+ entry => Array.isArray(entry) && entry.length >= 2
331
+ );
332
+
333
+ if (!entriesToUse.length) {
334
+ return undefined;
335
+ }
336
+
337
+ const useReducer = reducer instanceof Function
338
+ ? reducer
339
+ : (accumulator, [key, value]) => {
340
+ accumulator[key] = value;
341
+ return accumulator;
342
+ };
343
+
344
+ return entriesToUse.reduce(
345
+ useReducer, Object.create(prototype ?? Object.prototype)
346
+ );
347
+ },
348
+
349
+ /**
350
+ * Retrieves the prototype chain entries of a given object.
351
+ *
352
+ * This method traverses the prototype chain of the provided object and
353
+ * collects an array of entries. Each entry is a pair consisting of the
354
+ * prototype object and its property descriptors.
355
+ *
356
+ * The property descriptors are obtained using the `Reflect.ownKeys`
357
+ * method and the `Object.getOwnPropertyDescriptor` function.
358
+ *
359
+ * @param {Object} object - The object whose prototype chain entries are
360
+ * to be retrieved.
361
+ * @returns {Array} An array of entries, where each entry is a pair
362
+ * consisting of a prototype object and its property descriptors.
363
+ *
364
+ * @example
365
+ * const obj = Object.create({ foo: 'bar' });
366
+ * console.log(getPrototypeChainEntries(obj));
367
+ * // Output: [[{ foo: 'bar' }, { foo: { value: 'bar', writable: true,
368
+ * // enumerable: true, configurable: true } }], [Object.prototype, { ... }]]
369
+ */
370
+ getPrototypeChainEntries(object) {
371
+ const entries = []
372
+
373
+ let prototype = Object.getPrototypeOf(object)
374
+ while (prototype) {
375
+ const descriptors = Reflect.ownKeys(prototype).reduce((acc, key) => {
376
+ acc[key] = Object.getOwnPropertyDescriptor(prototype, key)
377
+ return acc
378
+ }, {})
379
+
380
+ entries.push([prototype, descriptors])
381
+
382
+ prototype = Object.getPrototypeOf(prototype)
383
+ }
384
+
385
+ return entries
386
+ },
387
+
388
+ /**
389
+ * Retrieves the string tag of an object. The string tag is a representation
390
+ * of the object's type, as defined by its `Object.prototype.toString`
391
+ * method. This utility method is helpful for getting a more descriptive
392
+ * type of an object than what is returned by the `typeof` operator,
393
+ * especially for custom objects.
394
+ *
395
+ * @param {*} value - The object whose string tag is to be retrieved.
396
+ * @param {boolean} strict - if this is set to true, undefined will be
397
+ * returned whenever a supplied object does not have a
398
+ * `Symbol.toStringTag` defined, period. if false, the default,
399
+ * @returns {string} - The string tag of the object, indicating its type.
400
+ */
401
+ getStringTag(value, strict = false) {
402
+ if (Object.hasStringTag(value)) {
403
+ return value[Symbol.toStringTag]
404
+ }
405
+
406
+ if (strict) {
407
+ return undefined
408
+ }
409
+
410
+ if (value && (typeof value === 'function')) {
411
+ return value.name
412
+ }
413
+
414
+ return /\s(.+)]/.exec(Object.prototype.toString.call(value))[1];
415
+ },
416
+
417
+ /**
418
+ * Determines the type of the given value based on its string tag. This method
419
+ * uses `Object.getStringTag` to obtain the string tag of the value, which
420
+ * represents its more specific type (e.g., Array, Map, Set) rather than just
421
+ * 'object'. The method then maps this string tag to the corresponding type
422
+ * present in the provided `owner` object, which defaults to `globalThis`.
423
+ * This utility method is especially useful for identifying the specific
424
+ * constructor or class of an object, beyond the basic types identified by
425
+ * the `typeof` operator.
426
+ *
427
+ * @param {any} value - The value whose type is to be determined.
428
+ * @param {object} [owner=globalThis] - The object in which to look up the
429
+ * constructor corresponding to the string tag. Defaults to `globalThis`,
430
+ * which covers global constructors like `Array`, `Object`, etc.
431
+ * @returns {Function|object|null|undefined} - Returns the constructor or
432
+ * type of the value based on its string tag. For 'Null' and 'Undefined',
433
+ * it returns `null` and `undefined`, respectively. For other types, it
434
+ * returns the corresponding constructor (e.g., `Array` for arrays) if
435
+ * available in the `owner` object.
436
+ */
437
+ getType(value, owner = globalThis) {
438
+ const stringTag = Object.getStringTag(value)
439
+
440
+ switch (stringTag) {
441
+ case 'Null': return null
442
+ case 'Undefined': return undefined
443
+ default:
444
+ return owner[stringTag]
445
+ }
446
+ },
447
+
448
+ /**
449
+ * Checks to see if the supplied `value` is both an object, and has the
450
+ * appropriate symbol defined.
451
+ *
452
+ * @param {any} value the value to determine if it contains a defined
453
+ * `Symbol.toStringTag` defined.
454
+ * @returns true if the symbol is defined, false otherwise
455
+ */
456
+ hasStringTag(value) {
457
+ return Object.isObject(value) && Reflect.has(value, Symbol.toStringTag)
458
+ },
459
+
460
+ /**
461
+ * The function checks if a value is either `undefined` or `null`.
462
+ *
463
+ * @param {any} value - The parameter "value" is a variable that can hold
464
+ * any value.
465
+ * @returns {boolean} `true` if the value is either `undefined` or `null`,
466
+ * and `false` otherwise.
467
+ */
468
+ isNullDefined(value) {
469
+ return value === undefined || value === null
470
+ },
471
+
472
+ /**
473
+ * The `ifNullDefined` function checks if a given value is either `null` or
474
+ * `undefined` and returns one of two provided values based on the result.
475
+ * This function is a convenience method for performing conditional
476
+ * operations based on the type of a value.
477
+ *
478
+ * @param {any} value - The value to be checked. If this is either `null`
479
+ * or `undefined`, `thenValue` is returned, otherwise `elseValue`
480
+ * is returned.
481
+ * @param {function | any} thenValue - The value to be returned if `value`
482
+ * is either `null` or `undefined`.
483
+ * @param {function | any} elseValue - The value to be returned if `value`
484
+ * is not either `null` or `undefined`.
485
+ * @returns {*} Returns `thenValue` if `value` is either `null` or
486
+ * `undefined`, otherwise returns `elseValue`.
487
+ *
488
+ * @example
489
+ * // Suppose we have a null value and a defined value
490
+ * let nullValue = null;
491
+ * let definedValue = "I'm defined";
492
+ *
493
+ * // Using ifNullDefined
494
+ * // Output: 'Null or Undefined'
495
+ * console.log(
496
+ * Object.ifNullDefined(nullValue, 'Null or Undefined', 'Defined')
497
+ * );
498
+ *
499
+ * // Output: 'Defined'
500
+ * console.log(
501
+ * Object.ifNullDefined(definedValue, 'Null or Undefined', 'Defined')
502
+ * );
503
+ */
504
+ ifNullDefined(value, thenValue, elseValue) {
505
+ return isThenElse(this.isNullDefined(value), thenValue, elseValue);
506
+ },
507
+
508
+ /**
509
+ * Checks if the provided value is an object.
510
+ *
511
+ * This function checks if the provided value is an instance of an Object
512
+ * or if the value is truthy and its type is 'object'. This is used to
513
+ * determine if a value can have properties and methods like an object.
514
+ *
515
+ * @param {any} value - The value to be checked.
516
+ * @returns {boolean} Returns `true` if the value is an object, `false`
517
+ * otherwise.
518
+ *
519
+ * @example
520
+ * // Using a string
521
+ * console.log(isObject('Hello, world!')); // Output: false
522
+ *
523
+ * // Using an object
524
+ * console.log(isObject({ key: 'value' })); // Output: true
525
+ *
526
+ * // Using null
527
+ * console.log(isObject(null)); // Output: false
528
+ */
529
+ isObject(value) {
530
+ return value instanceof Object || value && typeof value === 'object'
531
+ },
532
+
533
+ /**
534
+ * Determines if the provided value is an object. This method checks whether
535
+ * the value is an instance of `Object` or if its type is 'object'. It's a
536
+ * utility method for type-checking, ensuring that a value is an object
537
+ * before performing operations that are specific to objects.
538
+ *
539
+ * @param {*} value - The value to be checked.
540
+ * @returns {boolean} - Returns `true` if the value is an object,
541
+ * otherwise `false`.
542
+ },
543
+ isObject(value) {
544
+ return value && (value instanceof Object || typeof value === 'object');
545
+ },
546
+
547
+ /**
548
+ * Checks to see if the supplied value is a primitive value.
549
+ *
550
+ * @param {any} value the value to test to see if it is a primitive value type
551
+ * @returns true if the object is considered widely to be a primitive value,
552
+ * false otherwise.
553
+ */
554
+ isPrimitive(value) {
555
+ // Check for null as a special case because typeof null
556
+ // is 'object'
557
+ if (value === null) {
558
+ return true;
559
+ }
560
+
561
+ // Check for other primitives
562
+ switch (typeof value) {
563
+ case 'string':
564
+ case 'number':
565
+ case 'bigint':
566
+ case 'boolean':
567
+ case 'undefined':
568
+ case 'symbol':
569
+ return true;
570
+ default:
571
+ return false;
572
+ }
573
+ },
574
+
575
+ /**
576
+ * Executes a conditional function based on whether the provided value is
577
+ * primitive or not. This method first checks if the value is primitive
578
+ * using the `isPrimitive` method. If it is, it returns the `thenValue`,
579
+ * otherwise it returns the `elseValue`.
580
+ *
581
+ * @param {any} value - The value to be checked.
582
+ * @param {function | any} thenValue - The value to return if `value` is
583
+ * primitive.
584
+ * @param {function | any} elseValue - The value to return if `value` is
585
+ * not primitive.
586
+ * @returns {*} - Returns `thenValue` if the value is primitive, otherwise
587
+ * `elseValue`.
588
+ *
589
+ * @example
590
+ * // returns 1
591
+ * ifPrimitive('hello', 1, 2)
592
+ * // returns 2
593
+ * ifPrimitive({a: 'hello'}, 1, 2)
594
+ */
595
+ ifPrimitive(value, thenValue, elseValue) {
596
+ return isThenElse(this.isPrimitive(value), thenValue, elseValue)
597
+ },
598
+
599
+ /**
600
+ * Checks if the given value is a valid key for an object. In JavaScript, a
601
+ * valid key can be either a string or a symbol. This method is useful for
602
+ * validating object keys before using them in operations like setting or
603
+ * getting object properties.
604
+ *
605
+ * @param {*} value - The value to be checked.
606
+ * @returns {boolean} - Returns `true` if the value is a valid object key
607
+ * (string or symbol), otherwise `false`.
608
+ */
609
+ isValidKey(value) {
610
+ return (typeof value === 'string' || typeof value === 'symbol');
611
+ },
612
+
613
+ /**
614
+ * Executes a conditional function based on whether the provided
615
+ * value is a valid key for an object. This method first checks if
616
+ * the value is a valid key using the `isValidKey` method. If it is,
617
+ * it returns the `thenValue`, otherwise it returns the `elseValue`.
618
+ *
619
+ * @param {any} value - The value to be checked.
620
+ * @param {function | any} thenValue - The value to return if
621
+ * `value` is a valid key.
622
+ * @param {function | any} elseValue - The value to return if
623
+ * `value` is not a valid key.
624
+ * @returns {any} - Returns `thenValue` if the value is a valid key,
625
+ * otherwise `elseValue`.
626
+ *
627
+ * @example
628
+ * // returns 1
629
+ * ifValidKey('name', 1, 2)
630
+ * // returns 2
631
+ * ifValidKey(123, 1, 2)
632
+ */
633
+ ifValidKey(value, thenValue, elseValue) {
634
+ return isThenElse(this.isValidKey(value), thenValue, elseValue)
635
+ },
636
+
637
+ /**
638
+ * A symbol constant defined on Object that can be used to reference
639
+ * storage for an accessor descriptor created with Object.add() or
640
+ * other descriptor assigning and creation methods used by this extension.
641
+ *
642
+ * The value assigned here is actually another symbol but one generated
643
+ * by {@link Symkeys} for uniqueness and has access to data storage.
644
+ *
645
+ * @returns {Symbol} - Returns a symbol for the descriptor storage.
646
+ *
647
+ * @example
648
+ * // returns Symbol(@nejs.object.descriptor.storage)
649
+ * kDescriptorStore
650
+ *
651
+ * // add descriptor value to an object
652
+ * const object = {}
653
+ * Object.add({object, key: 'name', type: 'accessor'})
654
+ * object.name = 'Jane Doe'
655
+ *
656
+ * // Value assigned here is another symbol with its own storage generated
657
+ * // by Symkeys. Description might be '@nejs.descriptor.store #234sdafj'
658
+ * object[Object.kDescriptorStore]
659
+ *
660
+ * // But its nested data can be accessed using the '.data' getter
661
+ * object[Object.kDescriptorStore].data // { name: 'Jane Doe' }
662
+ */
663
+ get kDescriptorStore() {
664
+ return Symbol.for('@nejs.object.descriptor.storage')
665
+ },
666
+
667
+ /**
668
+ * Creates an object with predefined keys and descriptors. This method is
669
+ * useful for creating objects with specific properties and behaviors.
670
+ *
671
+ * @param {Array|Object} keys - An array of keys or an object where keys
672
+ * are the object's own properties. If an array is provided, each key will
673
+ * be assigned the `defaultValue`. If an object is provided, its own
674
+ * properties will be used as keys and their corresponding values as values.
675
+ * @param {*} [defaultValue=undefined] - The default value for each key.
676
+ * @param {string} [definedAs='data'] - Defines how the properties are
677
+ * defined. If 'data', properties are defined with a value. If 'accessor',
678
+ * properties are defined with get and set accessors.
679
+ * @param {Object} [accessorMeta={ get: undefined, set: undefined,
680
+ * thisArg: undefined }] - An object containing the get and set accessors
681
+ * and the `thisArg` to bind to the accessors.
682
+ * @param {Object} [descriptorBase={ enumerable: true, configurable: true }]
683
+ * - The base descriptor for the properties.
684
+ * @param {Object} [extraDescriptors=undefined] - Extra descriptors to be
685
+ * added to the object.
686
+ * @param {Object} [prototype=Object.prototype] - The prototype of the
687
+ * created object.
688
+ * @returns {Object} - Returns the created object.
689
+ *
690
+ * @example
691
+ * // returns { name: undefined }
692
+ * prekeyed(['name'])
693
+ * // returns { name: 'John' }
694
+ * prekeyed({ name: 'John' })
695
+ * // returns { name: 'John' }
696
+ * prekeyed(['name'], 'John')
697
+ */
698
+ prekeyed(
699
+ keys,
700
+ defaultValue = undefined,
701
+ definedAs = ['data', 'accessor'][0],
702
+ accessorMeta = { get: undefined, set: undefined, thisArg: undefined },
703
+ descriptorBase = { enumerable: true, configurable: true },
704
+ extraDescriptors = undefined,
705
+ prototype = Object.prototype
706
+ ) {
707
+ const object = Object.create(prototype, extraDescriptors)
708
+ let mapped = {}
709
+
710
+ if (Array.isArray(keys)) {
711
+ mapped = keys.reduce((a, k) => ({ ...a, [k]: defaultValue }), {})
712
+ }
713
+ else if (keys && typeof keys === 'object') {
714
+ Object.assign(mapped, keys)
715
+ }
716
+ else {
717
+ console.warn('skipping')
718
+ return object
719
+ }
720
+
721
+ for (const [key, value] of Object.entries(mapped)) {
722
+ let symKey = Symbol.for(`${key}#${Math.random().toString(36).slice(2)}`)
723
+ let suppliedValue = mapped[key] ?? defaultValue
724
+ if (definedAs === 'accessor' && accessorMeta.get === undefined) {
725
+ Object.defineProperty(
726
+ object, symKey, {
727
+ value: suppliedValue, enumerable: false, configurable: true
728
+ }
729
+ )
730
+ accessorMeta.thisArg = object
731
+ }
732
+
733
+ let descriptorRest = definedAs === 'data'
734
+ ? { value: value ?? defaultValue, writable: true }
735
+ : {
736
+ get: accessorMeta.get ?? function() { return this[symKey] },
737
+ set: accessorMeta.set ?? function(v) { this[symKey] = v }
738
+ }
739
+
740
+ if (accessorMeta.thisArg) {
741
+ descriptorRest.get = descriptorRest.get.bind(accessorMeta.thisArg)
742
+ descriptorRest.set = descriptorRest.set.bind(accessorMeta.thisArg)
743
+ }
744
+
745
+ Object.defineProperty(
746
+ object, key, { ...descriptorBase, ...descriptorRest }
747
+ )
748
+ }
749
+
750
+ return object
751
+ },
752
+
753
+ /**
754
+ * Strips an object down to only the keys specified. Optionally, any
755
+ * accessors can be made to retain their context on the source object.
756
+ *
757
+ * @param {object} object the object to pare down
758
+ * @param {Array<string|symbol>} keys the keys that should appear in the
759
+ * final reduced object
760
+ * @param {boolean} [bindAccessors = true] if this value is true then any
761
+ * accessors from the source object will continue to have their `this`
762
+ * value bound to the source. If the getter or setter on that object is
763
+ * defined using an arrow function, this will not work as intended.
764
+ * @returns {object} an object containing only the keys and symbols
765
+ * specified in the `keys` parameter.
766
+ */
767
+ stripTo(object, keys, bindAccessors = true) {
768
+ if (!object || typeof object !== 'object') {
769
+ throw new TypeError(
770
+ 'Object.stripTo requires an object to strip. Received',
771
+ object
772
+ );
773
+ }
774
+
775
+ const result = {};
776
+
777
+ if (!Array.isArray(keys)) {
778
+ return result;
779
+ }
780
+
781
+ for (let key of keys) {
782
+ if (Reflect.has(object, key)) {
783
+ const originalDescriptor = Object.getOwnPropertyDescriptor(object, key);
784
+ const descriptor = { ...originalDescriptor };
785
+
786
+ if (
787
+ typeof descriptor.get === 'function' ||
788
+ typeof descriptor.set === 'function'
789
+ ) {
790
+ if (bindAccessors) {
791
+ descriptor.get = descriptor.get?.bind(object);
792
+ descriptor.set = descriptor.set?.bind(object);
793
+ }
794
+ }
795
+
796
+ Object.defineProperty(result, key, descriptor);
797
+ }
798
+ }
799
+
800
+ return result;
801
+ },
802
+ },
803
+ });
804
+
805
+ const {
806
+ isObject: pIsObject, ifObject: pIfObject,
807
+ isNullDefined: pIsNullDefined, ifNullDefined: pIfNullDefined,
808
+ isPrimitive: pIsPrimitive, ifPrimitive: pIfPrimitive,
809
+ isValidKey: pIsValidKey, ifValidKey: pIfValidKey,
810
+ hasStringTag: pHasStringTag, getStringTag: pGetStringTag,
811
+ stripTo: pStripTo,
812
+
813
+ } = ObjectExtensions.patches;
814
+
815
+ /**
816
+ * `ObjectPrototypeExtensions` is a constant that applies a patch to the
817
+ * Object prototype. This patch extends the Object prototype with additional
818
+ * methods and properties, enhancing its functionality.
819
+ *
820
+ * The `Patch` function takes two arguments: the target object to be patched
821
+ * (in this case, `Object.prototype`), and an object containing the methods
822
+ * and properties to be added to the target object.
823
+ *
824
+ * @example
825
+ * // Using a method added by ObjectPrototypeExtensions
826
+ * const obj = {};
827
+ * console.log(obj.isObject()); // Output: true
828
+ *
829
+ * @const
830
+ * @type {Patch}
831
+ * @memberof module:object.extensions
832
+ */
833
+ export const ObjectPrototypeExtensions = new Patch(Object.prototype, {
834
+ [Patch.kMutablyHidden]: {
835
+ /**
836
+ * Retrieves the prototype chain entries of the current object.
837
+ *
838
+ * This method traverses the prototype chain of the current object
839
+ * (`this`) and collects an array of entries. Each entry is a pair
840
+ * consisting of the prototype object and its property descriptors.
841
+ *
842
+ * The property descriptors are obtained using the `Reflect.ownKeys`
843
+ * method and the `Object.getOwnPropertyDescriptor` function.
844
+ *
845
+ * @returns {Array} An array of entries, where each entry is a pair
846
+ * consisting of a prototype object and its property descriptors.
847
+ *
848
+ * @example
849
+ * const obj = Object.create({ foo: 'bar' });
850
+ * console.log(obj.getPrototypeChainEntries());
851
+ * // Output: [[{ foo: 'bar' }, { foo: { value: 'bar', writable: true, enumerable: true, configurable: true } }], [Object.prototype, { ... }]]
852
+ */
853
+ getPrototypeChainEntries() {
854
+ return ObjectExtensions.patches.getPrototypeChainEntries(this)
855
+ },
856
+
857
+ /**
858
+ * Determines if the current value is an object.
859
+ *
860
+ * This method checks whether the current value is an object,
861
+ * excluding null. It is a convenience wrapper around the
862
+ * `pIsObject` function from the `ObjectExtensions` patch.
863
+ *
864
+ * @name isObject
865
+ * @type {function}
866
+ * @memberof Object.prototype
867
+ * @returns {boolean} `true` if the current value is an object
868
+ * (excluding null), `false` otherwise.
869
+ *
870
+ * @example
871
+ * const obj = {};
872
+ * console.log(obj.isObject());
873
+ * // Output: true
874
+ *
875
+ * const str = "hello";
876
+ * console.log(str.isObject());
877
+ * // Output: false
878
+ *
879
+ * console.log(null.isObject());
880
+ * // Output: false
881
+ */
882
+ get isObject() {
883
+ return pIsObject(this)
884
+ },
885
+
886
+ /**
887
+ * Checks if the current value is an object and returns one of two
888
+ * provided values based on the result. This function is a convenience
889
+ * method for performing conditional operations based on the type of
890
+ * the current value.
891
+ *
892
+ * @name ifObject
893
+ * @type {function}
894
+ * @memberof Object.prototype
895
+ * @param {function | any} thenValue - The value to be returned if the
896
+ * current value is an object (excluding null).
897
+ * @param {function | any} elseValue - The value to be returned if the
898
+ * current value is not an object or is null.
899
+ * @returns {*} Returns `thenValue` if the current value is an object
900
+ * (excluding null), otherwise returns `elseValue`.
901
+ *
902
+ * @example
903
+ * const obj = {};
904
+ * console.log(obj.ifObject('Object', 'Not an object'));
905
+ * // Output: 'Object'
906
+ *
907
+ * const str = "hello";
908
+ * console.log(str.ifObject('Object', 'Not an object'));
909
+ * // Output: 'Not an object'
910
+ *
911
+ * console.log(null.ifObject('Object', 'Not an object'));
912
+ * // Output: 'Not an object'
913
+ */
914
+ ifObject(thenValue, elseValue) {
915
+ return pIfObject(this, thenValue, elseValue)
916
+ },
917
+
918
+ /**
919
+ * Checks if the current value is either `null` or `undefined`.
920
+ *
921
+ * @name isNullDefined
922
+ * @type {boolean}
923
+ * @memberof Object.prototype
924
+ * @returns {boolean} Returns `true` if the current value is either
925
+ * `null` or `undefined`, `false` otherwise.
926
+ *
927
+ * @example
928
+ * const obj = null;
929
+ * console.log(obj.isNullDefined);
930
+ * // Output: true
931
+ *
932
+ * const str = "hello";
933
+ * console.log(str.isNullDefined);
934
+ * // Output: false
935
+ */
936
+ get isNullDefined() {
937
+ return pIsNullDefined(this)
938
+ },
939
+
940
+ /**
941
+ * Checks if the current value is either `null` or `undefined` and
942
+ * returns one of two provided values based on the result.
943
+ *
944
+ * @name ifNullDefined
945
+ * @type {function}
946
+ * @memberof Object.prototype
947
+ * @param {function | any} thenValue - The value to be returned if the
948
+ * current value is either `null` or `undefined`.
949
+ * @param {function | any} elseValue - The value to be returned if the
950
+ * current value is not `null` or `undefined`.
951
+ * @returns {*} Returns `thenValue` if the current value is either
952
+ * `null` or `undefined`, otherwise returns `elseValue`.
953
+ *
954
+ * @example
955
+ * const obj = null
956
+ * console.log(obj.ifNullDefined('Null or Undefined', 'Defined'))
957
+ * // Output: 'Null or Undefined'
958
+ *
959
+ * const str = "hello"
960
+ * console.log(str.ifNullDefined('Null or Undefined', 'Defined'))
961
+ * // Output: 'Defined'
962
+ */
963
+ ifNullDefined(thenValue, elseValue) {
964
+ return pIfNullDefined(this, thenValue, elseValue)
965
+ },
966
+
967
+ /**
968
+ * Checks if the current value is a primitive type.
969
+ *
970
+ * Primitive types in JavaScript include `string`, `number`,
971
+ * `bigint`, `boolean`, `undefined`, `symbol`, and `null`.
972
+ *
973
+ * @name isPrimitive
974
+ * @type {boolean}
975
+ * @memberof Object.prototype
976
+ * @returns {boolean} Returns `true` if the current value is a
977
+ * primitive type, `false` otherwise.
978
+ *
979
+ * @example
980
+ * const str = "hello"
981
+ * console.log(str.isPrimitive)
982
+ * // Output: true
983
+ *
984
+ * const obj = { key: "value" }
985
+ * console.log(obj.isPrimitive)
986
+ * // Output: false
987
+ */
988
+ get isPrimitive() {
989
+ return pIsPrimitive(this)
990
+ },
991
+
992
+ /**
993
+ * Checks if the current value is a primitive type and returns one
994
+ * of two provided values based on the result.
995
+ *
996
+ * Primitive types in JavaScript include `string`, `number`,
997
+ * `bigint`, `boolean`, `undefined`, `symbol`, and `null`.
998
+ *
999
+ * @name ifPrimitive
1000
+ * @type {function}
1001
+ * @memberof Object.prototype
1002
+ * @param {function | any} thenValue - The value to be returned if
1003
+ * the current value is a primitive type.
1004
+ * @param {function | any} elseValue - The value to be returned if
1005
+ * the current value is not a primitive type.
1006
+ * @returns {*} Returns `thenValue` if the current value is a
1007
+ * primitive type, otherwise returns `elseValue`.
1008
+ *
1009
+ * @example
1010
+ * const str = "hello"
1011
+ * console.log(str.ifPrimitive('Primitive', 'Not Primitive'))
1012
+ * // Output: 'Primitive'
1013
+ *
1014
+ * const obj = { key: "value" }
1015
+ * console.log(obj.ifPrimitive('Primitive', 'Not Primitive'))
1016
+ * // Output: 'Not Primitive'
1017
+ */
1018
+ ifPrimitive(thenValue, elseValue) {
1019
+ return pIfPrimitive(this, thenValue, elseValue)
1020
+ },
1021
+
1022
+ /**
1023
+ * Determines if the current value is a valid key for an object.
1024
+ *
1025
+ * A valid key is either a string or a symbol. This method is a
1026
+ * convenience wrapper around the `pIsValidKey` function from the
1027
+ * `ObjectExtensions` patch.
1028
+ *
1029
+ * @name isValidKey
1030
+ * @type {boolean}
1031
+ * @memberof Object.prototype
1032
+ * @returns {boolean} `true` if the current value is a valid key for
1033
+ * an object (i.e., a string or symbol), `false` otherwise.
1034
+ *
1035
+ * @example
1036
+ * const str = "key"
1037
+ * console.log(str.isValidKey)
1038
+ * // Output: true
1039
+ *
1040
+ * const sym = Symbol("key")
1041
+ * console.log(sym.isValidKey)
1042
+ * // Output: true
1043
+ *
1044
+ * const num = 42
1045
+ * console.log(num.isValidKey)
1046
+ * // Output: false
1047
+ */
1048
+ get isValidKey() {
1049
+ return pIsValidKey(this)
1050
+ },
1051
+
1052
+ /**
1053
+ * Checks if the current value is a valid key for an object and returns
1054
+ * one of two provided values based on the result. This function is a
1055
+ * convenience method for performing conditional operations based on
1056
+ * the type of the current value.
1057
+ *
1058
+ * A valid key is either a string or a symbol.
1059
+ *
1060
+ * @name ifValidKey
1061
+ * @type {function}
1062
+ * @memberof Object.prototype
1063
+ * @param {function | any} thenValue - The value to be returned if the
1064
+ * current value is a valid key for an object.
1065
+ * @param {function | any} elseValue - The value to be returned if the
1066
+ * current value is not a valid key for an object.
1067
+ * @returns {*} Returns `thenValue` if the current value is a valid key
1068
+ * for an object, otherwise returns `elseValue`.
1069
+ *
1070
+ * @example
1071
+ * const str = "key"
1072
+ * console.log(str.ifValidKey('Valid Key', 'Not a Valid Key'))
1073
+ * // Output: 'Valid Key'
1074
+ *
1075
+ * const num = 42
1076
+ * console.log(num.ifValidKey('Valid Key', 'Not a Valid Key'))
1077
+ * // Output: 'Not a Valid Key'
1078
+ */
1079
+ ifValidKey(thenValue, elseValue) {
1080
+ return pIfValidKey(this, thenValue, elseValue)
1081
+ },
1082
+
1083
+ /**
1084
+ * Checks to see if the supplied `value` is both an object, and has the
1085
+ * appropriate symbol defined.
1086
+ *
1087
+ * @param {any} value the value to determine if it contains a defined
1088
+ * `Symbol.toStringTag` defined.
1089
+ * @returns true if the symbol is defined, false otherwise
1090
+ */
1091
+ get hasStringTag() {
1092
+ return pHasStringTag(this)
1093
+ },
1094
+
1095
+ /**
1096
+ * Retrieves the string tag of an object. The string tag is a representation
1097
+ * of the object's type, as defined by its `Object.prototype.toString`
1098
+ * method. This utility method is helpful for getting a more descriptive
1099
+ * type of an object than what is returned by the `typeof` operator,
1100
+ * especially for custom objects.
1101
+ *
1102
+ * @param {*} value - The object whose string tag is to be retrieved.
1103
+ * @param {boolean} strict - if this is set to true, undefined will be
1104
+ * returned whenever a supplied object does not have a
1105
+ * `Symbol.toStringTag` defined, period. if false, the default,
1106
+ * @returns {string} - The string tag of the object, indicating its type.
1107
+ */
1108
+ getStringTag(strict = false) {
1109
+ return pGetStringTag(this, strict)
1110
+ },
1111
+
1112
+ /**
1113
+ * Strips an object down to only the keys specified. Optionally, any
1114
+ * accessors can be made to retain their context on the source object.
1115
+ * This is a passthrough to the static {@link Object.stripTo} function
1116
+ *
1117
+ * @param {Array<string|symbol>} keys the keys that should appear in the
1118
+ * final reduced object
1119
+ * @param {boolean} [bindAccessors = true] if this value is true then any
1120
+ * accessors from the source object will continue to have their `this`
1121
+ * value bound to the source. If the getter or setter on that object is
1122
+ * defined using an arrow function, this will not work as intended.
1123
+ * @returns {object} an object containing only the keys and symbols
1124
+ * specified in the `keys` parameter.
1125
+ */
1126
+ stripTo(keys, bindAccessors = true) {
1127
+ return pStripTo(this, keys, bindAccessors)
1128
+ },
1129
+ },
1130
+ })
1131
+
1132
+ // NOTE to self; this is repeated here otherwise a circular reference from
1133
+ // Object<->Function<->Global occurs. See original source in global.this.js
1134
+ // {@see globalThis.isThenElse}
1135
+ function isThenElse(bv, tv, ev) {
1136
+ if (arguments.length > 1) {
1137
+ var _then = isFunction(tv) ? tv(bv) : tv; if (arguments.length > 2) {
1138
+ var _else = isFunction(ev) ? tv(bv) : ev; return bv ? _then : _else
1139
+ } return bv || _then;
1140
+ } return bv
1141
+ }
1142
+
1143
+ /**
1144
+ * Creates a deep or shallow copy of the provided source objects and merges
1145
+ * them into the destination object. The function uses a Set to keep track
1146
+ * of visited objects to avoid circular references.
1147
+ *
1148
+ * @function
1149
+ * @name copyObject
1150
+ * @param {boolean} deep - If true, performs a deep copy, otherwise performs
1151
+ * a shallow copy.
1152
+ * @param {object} destination - The object to which properties will be copied.
1153
+ * @param {...object} sources - The source object(s) from which properties
1154
+ * will be copied.
1155
+ * @returns {object} The destination object with the copied properties.
1156
+ *
1157
+ * @example
1158
+ * // Shallow copy
1159
+ * const obj1 = { a: 1, b: { c: 2 } };
1160
+ * const obj2 = { b: { d: 3 }, e: 4 };
1161
+ * const result = copyObject(false, obj1, obj2);
1162
+ * console.log(result); // Output: { a: 1, b: { d: 3 }, e: 4 }
1163
+ *
1164
+ * @example
1165
+ * // Deep copy
1166
+ * const obj1 = { a: 1, b: { c: 2 } };
1167
+ * const obj2 = { b: { d: 3 }, e: 4 };
1168
+ * const result = copyObject(true, obj1, obj2);
1169
+ * console.log(result); // Output: { a: 1, b: { c: 2, d: 3 }, e: 4 }
1170
+ */
1171
+ export function copyObject(deep, destination, ...sources) {
1172
+ const visited = new Set()
1173
+
1174
+ for (const source of sources) {
1175
+ if (source === null || typeof source !== 'object' || visited.has(source)) {
1176
+ continue
1177
+ }
1178
+
1179
+ visited.add(source)
1180
+
1181
+ const keys = Reflect.ownKeys(source)
1182
+ for (const key of keys) {
1183
+ let descriptor
1184
+ try {
1185
+ descriptor = Object.getOwnPropertyDescriptor(source, key)
1186
+ } catch (err) {
1187
+ console.warn(`Failed to get descriptor for key "${key}": ${err}`)
1188
+ continue
1189
+ }
1190
+
1191
+ const isDataDesc = Reflect.has(descriptor, 'value')
1192
+ const keyedValue = descriptor?.value
1193
+
1194
+ const conditionsMet = [
1195
+ isDataDesc,
1196
+ keyedValue,
1197
+ typeof keyedValue === 'object',
1198
+ !visited.has(keyedValue)
1199
+ ].every(condition => condition)
1200
+
1201
+ if (conditionsMet) {
1202
+ visited.add(keyedValue)
1203
+
1204
+ const prototype = Object.getPrototypeOf(keyedValue)
1205
+ const descriptors = Object.getOwnPropertyDescriptors(keyedValue)
1206
+ const replacement = Object.create(prototype, descriptors)
1207
+
1208
+ descriptor.value = deep
1209
+ ? copyObject(deep, replacement, keyedValue)
1210
+ : replacement
1211
+ }
1212
+
1213
+ try {
1214
+ Object.defineProperty(destination, key, descriptor)
1215
+ } catch (err) {
1216
+ console.error(`Failed to define property "${key}": ${err}`)
1217
+ }
1218
+ }
1219
+ }
1220
+
1221
+ return destination
1222
+ }