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