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