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