@nejs/basic-extensions 2.21.5 → 2.22.6

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 (266) hide show
  1. package/README.md +78 -4
  2. package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
  3. package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
  4. package/dist/cjs/classes/index.cjs +11129 -0
  5. package/dist/cjs/classes/index.cjs.map +7 -0
  6. package/dist/cjs/index.cjs +15191 -0
  7. package/dist/cjs/index.cjs.map +7 -0
  8. package/dist/cjs/utils/index.cjs +3954 -0
  9. package/dist/cjs/utils/index.cjs.map +7 -0
  10. package/dist/esm/basic-extensions.mjs +25 -0
  11. package/dist/esm/basic-extensions.mjs.map +7 -0
  12. package/package.json +16 -22
  13. package/repl.bootstrap.js +4 -7
  14. package/repl.history +26 -26
  15. package/src/classes/enumeration.js +466 -0
  16. package/src/classes/index.js +5 -1
  17. package/src/index.js +3 -1
  18. package/src/regular.expression.extensions.js +0 -35
  19. package/src/utils/toolkit.js +699 -516
  20. package/tests/arrayextensions.test.js +3 -3
  21. package/tests/index.test.js +3 -1
  22. package/tests/newClasses/asyncIterable.test.js +3 -3
  23. package/tests/newClasses/deferred.test.js +3 -3
  24. package/tests/newClasses/descriptor.test.js +3 -3
  25. package/tests/newClasses/iterable.test.js +3 -3
  26. package/tests/newClasses/refmap.test.js +3 -3
  27. package/tests/newClasses/refset.test.js +3 -3
  28. package/tests/objectextensions.test.js +3 -3
  29. package/tests/setextensions.test.js +3 -3
  30. package/tests/stringextensions.test.js +3 -2
  31. package/tests/utils/descriptor.utils.test.js +1 -1
  32. package/tests/utils/toolkit.test.js +429 -163
  33. package/.esdoc.json +0 -9
  34. package/.vscode/settings.json +0 -5
  35. package/bin/build +0 -27
  36. package/bin/clean +0 -14
  37. package/bin/esbuild +0 -91
  38. package/bin/fixup +0 -13
  39. package/bin/repl.basics.js +0 -584
  40. package/bin/repl.signature.js +0 -63
  41. package/bin/version +0 -100
  42. package/dist/@nejs/basic-extensions.bundle.2.21.5.js +0 -25
  43. package/dist/@nejs/basic-extensions.bundle.2.21.5.js.map +0 -7
  44. package/dist/cjs/array.extensions.d.ts +0 -39
  45. package/dist/cjs/array.extensions.js +0 -477
  46. package/dist/cjs/array.extensions.js.map +0 -1
  47. package/dist/cjs/big.int.extension.d.ts +0 -31
  48. package/dist/cjs/big.int.extension.js +0 -273
  49. package/dist/cjs/big.int.extension.js.map +0 -1
  50. package/dist/cjs/classes/asyncIterable.d.ts +0 -126
  51. package/dist/cjs/classes/asyncIterable.js +0 -209
  52. package/dist/cjs/classes/asyncIterable.js.map +0 -1
  53. package/dist/cjs/classes/deferred.d.ts +0 -146
  54. package/dist/cjs/classes/deferred.js +0 -291
  55. package/dist/cjs/classes/deferred.js.map +0 -1
  56. package/dist/cjs/classes/descriptor.d.ts +0 -334
  57. package/dist/cjs/classes/descriptor.js +0 -537
  58. package/dist/cjs/classes/descriptor.js.map +0 -1
  59. package/dist/cjs/classes/enum.d.ts +0 -50
  60. package/dist/cjs/classes/enum.js +0 -405
  61. package/dist/cjs/classes/enum.js.map +0 -1
  62. package/dist/cjs/classes/index.d.ts +0 -15
  63. package/dist/cjs/classes/index.js +0 -63
  64. package/dist/cjs/classes/index.js.map +0 -1
  65. package/dist/cjs/classes/introspector.d.ts +0 -20
  66. package/dist/cjs/classes/introspector.js +0 -130
  67. package/dist/cjs/classes/introspector.js.map +0 -1
  68. package/dist/cjs/classes/iterable.d.ts +0 -169
  69. package/dist/cjs/classes/iterable.js +0 -268
  70. package/dist/cjs/classes/iterable.js.map +0 -1
  71. package/dist/cjs/classes/param.parser.d.ts +0 -221
  72. package/dist/cjs/classes/param.parser.js +0 -242
  73. package/dist/cjs/classes/param.parser.js.map +0 -1
  74. package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
  75. package/dist/cjs/classes/pluggable.proxy.js +0 -444
  76. package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
  77. package/dist/cjs/classes/property.d.ts +0 -79
  78. package/dist/cjs/classes/property.js +0 -284
  79. package/dist/cjs/classes/property.js.map +0 -1
  80. package/dist/cjs/classes/refmap.d.ts +0 -238
  81. package/dist/cjs/classes/refmap.js +0 -421
  82. package/dist/cjs/classes/refmap.js.map +0 -1
  83. package/dist/cjs/classes/refset.d.ts +0 -186
  84. package/dist/cjs/classes/refset.js +0 -370
  85. package/dist/cjs/classes/refset.js.map +0 -1
  86. package/dist/cjs/classes/symkeys.d.ts +0 -349
  87. package/dist/cjs/classes/symkeys.js +0 -510
  88. package/dist/cjs/classes/symkeys.js.map +0 -1
  89. package/dist/cjs/classes/type.d.ts +0 -56
  90. package/dist/cjs/classes/type.js +0 -405
  91. package/dist/cjs/classes/type.js.map +0 -1
  92. package/dist/cjs/function.extensions.d.ts +0 -12
  93. package/dist/cjs/function.extensions.js +0 -758
  94. package/dist/cjs/function.extensions.js.map +0 -1
  95. package/dist/cjs/global.this.d.ts +0 -2
  96. package/dist/cjs/global.this.js +0 -300
  97. package/dist/cjs/global.this.js.map +0 -1
  98. package/dist/cjs/index.d.ts +0 -31
  99. package/dist/cjs/index.js +0 -228
  100. package/dist/cjs/index.js.map +0 -1
  101. package/dist/cjs/json.extensions.d.ts +0 -2
  102. package/dist/cjs/json.extensions.js +0 -109
  103. package/dist/cjs/json.extensions.js.map +0 -1
  104. package/dist/cjs/map.extensions.d.ts +0 -3
  105. package/dist/cjs/map.extensions.js +0 -143
  106. package/dist/cjs/map.extensions.js.map +0 -1
  107. package/dist/cjs/math.extension.d.ts +0 -14
  108. package/dist/cjs/math.extension.js +0 -71
  109. package/dist/cjs/math.extension.js.map +0 -1
  110. package/dist/cjs/number.extension.d.ts +0 -44
  111. package/dist/cjs/number.extension.js +0 -278
  112. package/dist/cjs/number.extension.js.map +0 -1
  113. package/dist/cjs/object.extensions.d.ts +0 -33
  114. package/dist/cjs/object.extensions.js +0 -1091
  115. package/dist/cjs/object.extensions.js.map +0 -1
  116. package/dist/cjs/package.json +0 -3
  117. package/dist/cjs/proxy.extensions.d.ts +0 -2
  118. package/dist/cjs/proxy.extensions.js +0 -207
  119. package/dist/cjs/proxy.extensions.js.map +0 -1
  120. package/dist/cjs/reflect.extensions.d.ts +0 -14
  121. package/dist/cjs/reflect.extensions.js +0 -316
  122. package/dist/cjs/reflect.extensions.js.map +0 -1
  123. package/dist/cjs/regular.expression.extensions.d.ts +0 -2
  124. package/dist/cjs/regular.expression.extensions.js +0 -423
  125. package/dist/cjs/regular.expression.extensions.js.map +0 -1
  126. package/dist/cjs/set.extensions.d.ts +0 -40
  127. package/dist/cjs/set.extensions.js +0 -355
  128. package/dist/cjs/set.extensions.js.map +0 -1
  129. package/dist/cjs/string.extensions.d.ts +0 -23
  130. package/dist/cjs/string.extensions.js +0 -704
  131. package/dist/cjs/string.extensions.js.map +0 -1
  132. package/dist/cjs/symbol.extensions.d.ts +0 -11
  133. package/dist/cjs/symbol.extensions.js +0 -735
  134. package/dist/cjs/symbol.extensions.js.map +0 -1
  135. package/dist/cjs/utils/copy.object.d.ts +0 -408
  136. package/dist/cjs/utils/copy.object.js +0 -720
  137. package/dist/cjs/utils/copy.object.js.map +0 -1
  138. package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
  139. package/dist/cjs/utils/descriptor.utils.js +0 -889
  140. package/dist/cjs/utils/descriptor.utils.js.map +0 -1
  141. package/dist/cjs/utils/index.d.ts +0 -75
  142. package/dist/cjs/utils/index.js +0 -61
  143. package/dist/cjs/utils/index.js.map +0 -1
  144. package/dist/cjs/utils/stdout.d.ts +0 -742
  145. package/dist/cjs/utils/stdout.js +0 -1042
  146. package/dist/cjs/utils/stdout.js.map +0 -1
  147. package/dist/cjs/utils/toolkit.d.ts +0 -1898
  148. package/dist/cjs/utils/toolkit.js +0 -1378
  149. package/dist/cjs/utils/toolkit.js.map +0 -1
  150. package/dist/cjs/weakref.extensions.d.ts +0 -2
  151. package/dist/cjs/weakref.extensions.js +0 -19
  152. package/dist/cjs/weakref.extensions.js.map +0 -1
  153. package/dist/mjs/array.extensions.d.ts +0 -39
  154. package/dist/mjs/array.extensions.js +0 -474
  155. package/dist/mjs/array.extensions.js.map +0 -1
  156. package/dist/mjs/big.int.extension.d.ts +0 -31
  157. package/dist/mjs/big.int.extension.js +0 -270
  158. package/dist/mjs/big.int.extension.js.map +0 -1
  159. package/dist/mjs/classes/asyncIterable.d.ts +0 -126
  160. package/dist/mjs/classes/asyncIterable.js +0 -204
  161. package/dist/mjs/classes/asyncIterable.js.map +0 -1
  162. package/dist/mjs/classes/deferred.d.ts +0 -146
  163. package/dist/mjs/classes/deferred.js +0 -287
  164. package/dist/mjs/classes/deferred.js.map +0 -1
  165. package/dist/mjs/classes/descriptor.d.ts +0 -334
  166. package/dist/mjs/classes/descriptor.js +0 -533
  167. package/dist/mjs/classes/descriptor.js.map +0 -1
  168. package/dist/mjs/classes/enum.d.ts +0 -50
  169. package/dist/mjs/classes/enum.js +0 -400
  170. package/dist/mjs/classes/enum.js.map +0 -1
  171. package/dist/mjs/classes/index.d.ts +0 -15
  172. package/dist/mjs/classes/index.js +0 -46
  173. package/dist/mjs/classes/index.js.map +0 -1
  174. package/dist/mjs/classes/introspector.d.ts +0 -20
  175. package/dist/mjs/classes/introspector.js +0 -126
  176. package/dist/mjs/classes/introspector.js.map +0 -1
  177. package/dist/mjs/classes/iterable.d.ts +0 -169
  178. package/dist/mjs/classes/iterable.js +0 -263
  179. package/dist/mjs/classes/iterable.js.map +0 -1
  180. package/dist/mjs/classes/param.parser.d.ts +0 -221
  181. package/dist/mjs/classes/param.parser.js +0 -238
  182. package/dist/mjs/classes/param.parser.js.map +0 -1
  183. package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
  184. package/dist/mjs/classes/pluggable.proxy.js +0 -438
  185. package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
  186. package/dist/mjs/classes/property.d.ts +0 -79
  187. package/dist/mjs/classes/property.js +0 -280
  188. package/dist/mjs/classes/property.js.map +0 -1
  189. package/dist/mjs/classes/refmap.d.ts +0 -238
  190. package/dist/mjs/classes/refmap.js +0 -417
  191. package/dist/mjs/classes/refmap.js.map +0 -1
  192. package/dist/mjs/classes/refset.d.ts +0 -186
  193. package/dist/mjs/classes/refset.js +0 -366
  194. package/dist/mjs/classes/refset.js.map +0 -1
  195. package/dist/mjs/classes/symkeys.d.ts +0 -349
  196. package/dist/mjs/classes/symkeys.js +0 -506
  197. package/dist/mjs/classes/symkeys.js.map +0 -1
  198. package/dist/mjs/classes/type.d.ts +0 -56
  199. package/dist/mjs/classes/type.js +0 -401
  200. package/dist/mjs/classes/type.js.map +0 -1
  201. package/dist/mjs/function.extensions.d.ts +0 -12
  202. package/dist/mjs/function.extensions.js +0 -755
  203. package/dist/mjs/function.extensions.js.map +0 -1
  204. package/dist/mjs/global.this.d.ts +0 -2
  205. package/dist/mjs/global.this.js +0 -264
  206. package/dist/mjs/global.this.js.map +0 -1
  207. package/dist/mjs/index.d.ts +0 -31
  208. package/dist/mjs/index.js +0 -206
  209. package/dist/mjs/index.js.map +0 -1
  210. package/dist/mjs/json.extensions.d.ts +0 -2
  211. package/dist/mjs/json.extensions.js +0 -106
  212. package/dist/mjs/json.extensions.js.map +0 -1
  213. package/dist/mjs/map.extensions.d.ts +0 -3
  214. package/dist/mjs/map.extensions.js +0 -140
  215. package/dist/mjs/map.extensions.js.map +0 -1
  216. package/dist/mjs/math.extension.d.ts +0 -14
  217. package/dist/mjs/math.extension.js +0 -68
  218. package/dist/mjs/math.extension.js.map +0 -1
  219. package/dist/mjs/number.extension.d.ts +0 -44
  220. package/dist/mjs/number.extension.js +0 -275
  221. package/dist/mjs/number.extension.js.map +0 -1
  222. package/dist/mjs/object.extensions.d.ts +0 -33
  223. package/dist/mjs/object.extensions.js +0 -1088
  224. package/dist/mjs/object.extensions.js.map +0 -1
  225. package/dist/mjs/package.json +0 -3
  226. package/dist/mjs/proxy.extensions.d.ts +0 -2
  227. package/dist/mjs/proxy.extensions.js +0 -204
  228. package/dist/mjs/proxy.extensions.js.map +0 -1
  229. package/dist/mjs/reflect.extensions.d.ts +0 -14
  230. package/dist/mjs/reflect.extensions.js +0 -313
  231. package/dist/mjs/reflect.extensions.js.map +0 -1
  232. package/dist/mjs/regular.expression.extensions.d.ts +0 -2
  233. package/dist/mjs/regular.expression.extensions.js +0 -420
  234. package/dist/mjs/regular.expression.extensions.js.map +0 -1
  235. package/dist/mjs/set.extensions.d.ts +0 -40
  236. package/dist/mjs/set.extensions.js +0 -352
  237. package/dist/mjs/set.extensions.js.map +0 -1
  238. package/dist/mjs/string.extensions.d.ts +0 -23
  239. package/dist/mjs/string.extensions.js +0 -701
  240. package/dist/mjs/string.extensions.js.map +0 -1
  241. package/dist/mjs/symbol.extensions.d.ts +0 -11
  242. package/dist/mjs/symbol.extensions.js +0 -732
  243. package/dist/mjs/symbol.extensions.js.map +0 -1
  244. package/dist/mjs/utils/copy.object.d.ts +0 -408
  245. package/dist/mjs/utils/copy.object.js +0 -702
  246. package/dist/mjs/utils/copy.object.js.map +0 -1
  247. package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
  248. package/dist/mjs/utils/descriptor.utils.js +0 -875
  249. package/dist/mjs/utils/descriptor.utils.js.map +0 -1
  250. package/dist/mjs/utils/index.d.ts +0 -75
  251. package/dist/mjs/utils/index.js +0 -45
  252. package/dist/mjs/utils/index.js.map +0 -1
  253. package/dist/mjs/utils/stdout.d.ts +0 -742
  254. package/dist/mjs/utils/stdout.js +0 -1037
  255. package/dist/mjs/utils/stdout.js.map +0 -1
  256. package/dist/mjs/utils/toolkit.d.ts +0 -1898
  257. package/dist/mjs/utils/toolkit.js +0 -1373
  258. package/dist/mjs/utils/toolkit.js.map +0 -1
  259. package/dist/mjs/weakref.extensions.d.ts +0 -2
  260. package/dist/mjs/weakref.extensions.js +0 -16
  261. package/dist/mjs/weakref.extensions.js.map +0 -1
  262. package/jsdoc-config.json +0 -31
  263. package/tsconfig.base.json +0 -28
  264. package/tsconfig.cjs.json +0 -8
  265. package/tsconfig.esm.json +0 -8
  266. package/vitest.config.js +0 -7
@@ -1,1091 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- 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 property_js_1 = require("./classes/property.js");
8
- const toolkit_js_1 = require("./utils/toolkit.js");
9
- const copy_object_js_1 = require("./utils/copy.object.js");
10
- const { keys: symkeys } = symbol_extensions_js_1.SymbolExtensions.patches;
11
- /**
12
- * `ObjectExtensions` is a constant that applies a patch to the global
13
- * `Object` constructor. This patch extends the `Object` with additional
14
- * methods and properties, enhancing its functionality.
15
- *
16
- * The `Patch` function takes two arguments: the target object to be patched
17
- * (in this case, `Object`), and an object containing the methods and
18
- * properties to be added to the target object.
19
- *
20
- * @type {Patch}
21
- * @memberof module:object.extensions
22
- */
23
- exports.ObjectExtensions = new extension_1.Patch(Object, {
24
- [extension_1.Patch.kMutablyHidden]: {
25
- add(...args) {
26
- const { isDescriptor } = descriptor_js_1.Descriptor;
27
- const { isObject: isObj } = this;
28
- const { kDescriptorStore } = this;
29
- let obj, key, value, _get, _set, storage, storageKey;
30
- let _type, _flag, _desc;
31
- // Check to see if we received multiple arguments or an object
32
- if (args.length && isObj(args[0])) {
33
- ({
34
- to: obj,
35
- key,
36
- value,
37
- get: _get,
38
- set: _set,
39
- storage,
40
- storageKey,
41
- type: _type = ['accessor', 'data'][1],
42
- flag: _flag = undefined,
43
- descriptorBase: _desc = undefined,
44
- } = args[0]);
45
- }
46
- else if (args.length > 1) {
47
- ([
48
- to,
49
- _type,
50
- key,
51
- getOrValue,
52
- _set,
53
- storage,
54
- storageKey,
55
- _flag,
56
- _desc,
57
- ] = args);
58
- obj = to;
59
- _type = (['accessor', 'data'].includes(_type.toLowerCase())
60
- ? _type.toLowerCase() : 'data');
61
- _get = _type === 'accessor' ? getOrValue : undefined;
62
- _value = _type === 'data' ? getOrValue : undefined;
63
- }
64
- if (!this.isObject(obj)) {
65
- console.warn('Object.add() must receive an object for `toObject`');
66
- return obj;
67
- }
68
- const more = isDescriptor(_desc) ? _desc : {};
69
- const flag = _flag || Object.definitionType.mutablyVisible;
70
- const props = { ...extension_1.Patch.getDescriptorOverridesFromSymbol(flag), ...more };
71
- const type = (['accessor', 'data'].includes(_type)
72
- ? String(_type).toLowerCase() : 'data');
73
- switch (type) {
74
- case 'accessor':
75
- let store = storage;
76
- let storeKey = storageKey || key;
77
- let makeStore = false;
78
- let get = _get;
79
- let set = _set;
80
- if (!toolkit_js_1.is.truthy(get) && !toolkit_js_1.is.function(get)) {
81
- get = undefined;
82
- }
83
- if (!toolkit_js_1.is.truthy(set) && !toolkit_js_1.is.function(set)) {
84
- set = undefined;
85
- }
86
- if (isObj(store) || toolkit_js_1.is.true(store) || toolkit_js_1.is.function(store)) {
87
- makeStore = toolkit_js_1.is.true(store);
88
- store = toolkit_js_1.is.fn(store) ? store() : store;
89
- store = toolkit_js_1.is.object(store) ? store : (makeStore && {} || undefined);
90
- }
91
- // store should be defined by here: object or undefined
92
- if ((!get && !set) && makeStore) {
93
- // being lazy here, someone has defined we make an accessor but
94
- // wants the default accessor behaviors with an associated store
95
- // made by us.
96
- Object.defineProperty(obj, kDescriptorStore, {
97
- value: symkeys.add('descriptor.store', store),
98
- configurable: true,
99
- enumerable: false,
100
- writable: true,
101
- });
102
- get = () => this[kDescriptorStore]?.data?.[storeKey];
103
- set = (value) => { this[kDescriptorStore].data[storeKey] = value; };
104
- }
105
- else if (get?.length && set?.length > 1 && store) {
106
- // if we received a get or set that takes more arguments than
107
- // expected, assume the last argument should be the store variable
108
- // so we execute the supplied function with the storage and its
109
- // results or byproducts are the result to the get/set we define
110
- const innerGet = get;
111
- const innerSet = set;
112
- get = () => innerGet(store);
113
- set = (value) => innerSet(value, store);
114
- }
115
- // get and set should be in their final state by here
116
- Object.defineProperty(obj, key, { ...props, get, set });
117
- break;
118
- case 'data':
119
- Object.defineProperty(obj, key, { ...props, value });
120
- break;
121
- }
122
- return obj;
123
- },
124
- addAccessor(to, key, getter, setter, storage) {
125
- const store = storage ?? (!getter && !setter) ? true : undefined;
126
- return this.add({ to, key, get: getter, set: setter, storage: store });
127
- },
128
- addData(to, key, value) {
129
- return this.add({ to, key, value });
130
- },
131
- /**
132
- * Creates a shallow copy of the provided object(s).
133
- *
134
- * This method uses the `copyObject` function with the `deep` parameter
135
- * set to `false`, indicating a shallow copy. It takes a destination
136
- * object and any number of source objects, and copies the properties
137
- * from the source objects to the destination object. If a property
138
- * already exists on the destination object, it will be overwritten.
139
- *
140
- * Note: This method does not copy nested objects or arrays. They are
141
- * copied by reference, not by value. To create a deep copy, use the
142
- * `deepCopy` method instead.
143
- *
144
- * @param {object} destination - The object to which properties will be
145
- * copied.
146
- * @param {...object} sources - The source object(s) from which
147
- * properties will be copied.
148
- * @returns {object} The destination object with the copied properties.
149
- *
150
- * @example
151
- * const obj1 = { a: 1, b: 2 };
152
- * const obj2 = { b: 3, c: 4 };
153
- * const result = ObjectExtensions.copy(obj1, obj2);
154
- * console.log(result); // Output: { a: 1, b: 3, c: 4 }
155
- */
156
- copy(destination, ...sources) {
157
- return (0, copy_object_js_1.copyObject)(false, destination, ...sources);
158
- },
159
- /**
160
- * Creates a deep copy of the provided object(s).
161
- *
162
- * This method uses the `copyObject` function with the `deep` parameter
163
- * set to `true`, indicating a deep copy. It takes a destination
164
- * object and any number of source objects, and copies the properties
165
- * from the source objects to the destination object. If a property
166
- * already exists on the destination object, it will be overwritten.
167
- *
168
- * Note: This method copies nested objects or arrays by value, not by
169
- * reference. To create a shallow copy, use the `copy` method instead.
170
- *
171
- * @param {object} destination - The object to which properties will be
172
- * copied.
173
- * @param {...object} sources - The source object(s) from which
174
- * properties will be copied.
175
- * @returns {object} The destination object with the copied properties.
176
- *
177
- * @example
178
- * const obj1 = { a: 1, b: { c: 2 } };
179
- * const obj2 = { b: { d: 3 }, e: 4 };
180
- * const result = ObjectExtensions.deepCopy(obj1, obj2);
181
- * console.log(result); // Output: { a: 1, b: { d: 3 }, e: 4 }
182
- */
183
- deepCopy(destination, ...sources) {
184
- return (0, copy_object_js_1.copyObject)(true, destination, ...sources);
185
- },
186
- /**
187
- * Defines a new property on an object with a specified value and
188
- * visibility/mutability flag. The flag determines the visibility and
189
- * mutability of the property. By default, the property is defined as
190
- * mutably hidden.
191
- *
192
- * @param {object} object - The object on which to define the property.
193
- * @param {string} key - The name of the property to be defined.
194
- * @param {any} value - The value of the property to be defined.
195
- * @param {symbol} [flag=Object.definitionType.mutablyHidden] - The
196
- * visibility/mutability flag for the property. This should be one of the
197
- * symbols available in `ObjectExtensions.definitionType`.
198
- * @returns {object} The object with the newly defined property.
199
- *
200
- * @example
201
- * // Define a new mutably hidden property on an object
202
- * const myObject = {};
203
- * const myValue = 'Hello, world!';
204
- * const hiddenSymbol = Object.definitionType.mutablyHidden;
205
- * Object.define(myObject, 'myProperty', myValue, hiddenSymbol);
206
- * // myObject now has a mutably hidden property 'myProperty' with value
207
- * // 'Hello, world!'
208
- */
209
- define(object, key, value, flag = Object.definitionType.mutablyHidden) {
210
- const properties = extension_1.Patch.getDescriptorOverridesFromSymbol(flag);
211
- return Object.defineProperty(object, key, { ...properties, value });
212
- },
213
- /**
214
- * A getter property that provides access to the definition types used
215
- * for object property definitions. These types are used to control the
216
- * visibility and mutability of object properties.
217
- *
218
- * @returns {Object} An object with getter properties for each definition
219
- * type. The properties are:
220
- * - `mutablyHidden`: A symbol representing a mutably hidden property,
221
- * non-enumerable, but configurable.
222
- * - `mutablyVisible`: A symbol representing a mutably visible property,
223
- * enumerable, configurable
224
- * - `immutablyHidden`: A symbol representing an immutably hidden property,
225
- * non-enumerable, non-configurable.
226
- * - `immutablyVisible`: A symbol representing an immutably visible
227
- * property, enumerable, non-configurable.
228
- *
229
- * @example
230
- * // Get the symbol for a mutably hidden property
231
- * const hiddenSymbol = Object.definitionType.mutablyHidden;
232
- *
233
- * // Define a new mutably hidden property on an object
234
- * Object.define(myObject, 'myProperty', myValue, hiddenSymbol);
235
- */
236
- get definitionType() {
237
- return {
238
- get mutablyHidden() { return extension_1.Patch.kMutablyHidden; },
239
- get mutablyVisible() { return extension_1.Patch.kMutablyVisible; },
240
- get immutablyHidden() { return extension_1.Patch.kImmutablyHidden; },
241
- get immutablyVisible() { return extension_1.Patch.kImmutablyVisible; },
242
- };
243
- },
244
- /**
245
- * Defines a new accessor property on an object with specified getter and
246
- * setter functions and a visibility/mutability flag. The flag determines
247
- * the visibility and mutability of the property. By default, the property
248
- * is defined as mutably hidden.
249
- *
250
- * @param {object} object - The object on which to define the property.
251
- * @param {string} key - The name of the property to be defined.
252
- * @param {function} get - The getter function for the property.
253
- * @param {function} set - The setter function for the property.
254
- * @param {symbol} [flag=Object.definitionType.mutablyHidden] - The
255
- * visibility/mutability flag for the property. This should be one of the
256
- * symbols available in `ObjectExtensions.definitionType`.
257
- * @returns {object} The object with the newly defined property.
258
- *
259
- * @example
260
- * // Define a new mutably hidden accessor property on an object
261
- * const myObject = {};
262
- * const hiddenSymbol = ObjectExtensions.definitionType.mutablyHidden;
263
- * ObjectExtensions.defineAccessor(
264
- * myObject,
265
- * 'myProperty',
266
- * () => 'Hello, world!',
267
- * (value) => console.log(`Setting value: ${value}`),
268
- * hiddenSymbol
269
- * );
270
- * // myObject now has a mutably hidden property 'myProperty' with getter
271
- * // and setter functions
272
- */
273
- defineAccessor(object, key, get, set, flag = Object.definitionType.mutablyHidden) {
274
- const properties = extension_1.Patch.getDescriptorOverridesFromSymbol(flag);
275
- return Object.defineProperty(object, key, { ...properties, get, set });
276
- },
277
- /**
278
- * Creates a new object from an array of key-value pairs (entries), with an
279
- * optional prototype and reducer function. If no prototype is provided,
280
- * the default Object.prototype is used. If no reducer is provided, a
281
- * default reducer is used that assigns each value to its corresponding key.
282
- *
283
- * @param {Array} entries - An array of key-value pairs. Each entry should
284
- * be an array where the first element is the key and the second element is
285
- * the value. Non-conforming entries are ignored.
286
- * @param {object} [prototype=Object.prototype] - The prototype to use for
287
- * the new object. If not provided, Object.prototype is used.
288
- * @param {Function} [reducer] - An optional reducer function to use when
289
- * creating the new object. If not provided, a default reducer is used that
290
- * assigns each value to its corresponding key.
291
- * @returns {object|undefined} - The new object created from the entries, or
292
- * undefined if the entries array is not valid or contains no valid entries.
293
- *
294
- * @example
295
- * // Create an object with a custom prototype and reducer
296
- * const myPrototype = { foo: 'bar' };
297
- * const myReducer = (obj, [key, value]) => {
298
- * obj[key] = value.toUpperCase();
299
- * return obj;
300
- * };
301
- *
302
- * const myEntries = [['name', 'John'], ['age', '30']];
303
- * const myObject = Object.fromEntriesUsing(
304
- * myEntries, myPrototype, myReducer
305
- * );
306
- *
307
- * // myObject is now { name: 'JOHN', age: '30' }
308
- * // with prototype { foo: 'bar' }
309
- */
310
- fromEntriesUsing(entries, prototype = Object.prototype, reducer = undefined) {
311
- if (!toolkit_js_1.is.array(entries)) {
312
- return undefined;
313
- }
314
- const entriesToUse = entries.filter(entry => toolkit_js_1.is.array(entry) && entry.length >= 2);
315
- if (!entriesToUse.length) {
316
- return undefined;
317
- }
318
- const useReducer = reducer instanceof Function
319
- ? reducer
320
- : (accumulator, [key, value]) => {
321
- accumulator[key] = value;
322
- return accumulator;
323
- };
324
- return entriesToUse.reduce(useReducer, Object.create(prototype ?? Object.prototype));
325
- },
326
- /**
327
- * Retrieves the prototype chain entries of a given object.
328
- *
329
- * This method traverses the prototype chain of the provided object and
330
- * collects an array of entries. Each entry is a pair consisting of the
331
- * prototype object and its property descriptors.
332
- *
333
- * The property descriptors are obtained using the `Reflect.ownKeys`
334
- * method and the `Object.getOwnPropertyDescriptor` function.
335
- *
336
- * @param {Object} object - The object whose prototype chain entries are
337
- * to be retrieved.
338
- * @returns {Array} An array of entries, where each entry is a pair
339
- * consisting of a prototype object and its property descriptors.
340
- *
341
- * @example
342
- * const obj = Object.create({ foo: 'bar' });
343
- * console.log(getPrototypeChainEntries(obj));
344
- * // Output: [[{ foo: 'bar' }, { foo: { value: 'bar', writable: true,
345
- * // enumerable: true, configurable: true } }], [Object.prototype, { ... }]]
346
- */
347
- getPrototypeChainEntries(object) {
348
- const entries = [];
349
- let prototype = Object.getPrototypeOf(object);
350
- while (prototype) {
351
- const descriptors = Reflect.ownKeys(prototype).reduce((acc, key) => {
352
- acc[key] = Object.getOwnPropertyDescriptor(prototype, key);
353
- return acc;
354
- }, {});
355
- entries.push([prototype, descriptors]);
356
- prototype = Object.getPrototypeOf(prototype);
357
- }
358
- return entries;
359
- },
360
- /**
361
- * Retrieves the string tag of an object. The string tag is a representation
362
- * of the object's type, as defined by its `Object.prototype.toString`
363
- * method. This utility method is helpful for getting a more descriptive
364
- * type of an object than what is returned by the `typeof` operator,
365
- * especially for custom objects.
366
- *
367
- * @param {*} value - The object whose string tag is to be retrieved.
368
- * @param {boolean} strict - if this is set to true, undefined will be
369
- * returned whenever a supplied object does not have a
370
- * `Symbol.toStringTag` defined, period. if false, the default,
371
- * @returns {string} - The string tag of the object, indicating its type.
372
- */
373
- getStringTag(value, strict = false) {
374
- if (toolkit_js_1.has.stringTag(value)) {
375
- return value[Symbol.toStringTag];
376
- }
377
- if (strict) {
378
- return undefined;
379
- }
380
- if (value && (typeof value === 'function')) {
381
- return value.name;
382
- }
383
- return /\s(.+)]/.exec(Object.prototype.toString.call(value))[1];
384
- },
385
- /**
386
- * Determines the type of the given value based on its string tag. This method
387
- * uses `Object.getStringTag` to obtain the string tag of the value, which
388
- * represents its more specific type (e.g., Array, Map, Set) rather than just
389
- * 'object'. The method then maps this string tag to the corresponding type
390
- * present in the provided `owner` object, which defaults to `globalThis`.
391
- * This utility method is especially useful for identifying the specific
392
- * constructor or class of an object, beyond the basic types identified by
393
- * the `typeof` operator.
394
- *
395
- * @param {any} value - The value whose type is to be determined.
396
- * @param {object} [owner=globalThis] - The object in which to look up the
397
- * constructor corresponding to the string tag. Defaults to `globalThis`,
398
- * which covers global constructors like `Array`, `Object`, etc.
399
- * @returns {Function|object|null|undefined} - Returns the constructor or
400
- * type of the value based on its string tag. For 'Null' and 'Undefined',
401
- * it returns `null` and `undefined`, respectively. For other types, it
402
- * returns the corresponding constructor (e.g., `Array` for arrays) if
403
- * available in the `owner` object.
404
- */
405
- getType(value, owner = globalThis) {
406
- const stringTag = Object.getStringTag(value);
407
- switch (stringTag) {
408
- case 'Null': return null;
409
- case 'Undefined': return undefined;
410
- default:
411
- return owner[stringTag];
412
- }
413
- },
414
- /**
415
- * Checks to see if the supplied `value` is both an object, and has the
416
- * appropriate symbol defined.
417
- *
418
- * @param {any} value the value to determine if it contains a defined
419
- * `Symbol.toStringTag` defined.
420
- * @returns true if the symbol is defined, false otherwise
421
- */
422
- hasStringTag(value) {
423
- return toolkit_js_1.has.stringTag(value);
424
- },
425
- /**
426
- * The function checks if a value is either `undefined` or `null`.
427
- *
428
- * @param {any} value - The parameter "value" is a variable that can hold
429
- * any value.
430
- * @returns {boolean} `true` if the value is either `undefined` or `null`,
431
- * and `false` otherwise.
432
- */
433
- isNullDefined(value) {
434
- return toolkit_js_1.is.nullish(value);
435
- },
436
- /**
437
- * The `ifNullDefined` function checks if a given value is either `null` or
438
- * `undefined` and returns one of two provided values based on the result.
439
- * This function is a convenience method for performing conditional
440
- * operations based on the type of a value.
441
- *
442
- * @param {any} value - The value to be checked. If this is either `null`
443
- * or `undefined`, `thenValue` is returned, otherwise `elseValue`
444
- * is returned.
445
- * @param {function | any} thenValue - The value to be returned if `value`
446
- * is either `null` or `undefined`.
447
- * @param {function | any} elseValue - The value to be returned if `value`
448
- * is not either `null` or `undefined`.
449
- * @returns {*} Returns `thenValue` if `value` is either `null` or
450
- * `undefined`, otherwise returns `elseValue`.
451
- *
452
- * @example
453
- * // Suppose we have a null value and a defined value
454
- * let nullValue = null;
455
- * let definedValue = "I'm defined";
456
- *
457
- * // Using ifNullDefined
458
- * // Output: 'Null or Undefined'
459
- * console.log(
460
- * Object.ifNullDefined(nullValue, 'Null or Undefined', 'Defined')
461
- * );
462
- *
463
- * // Output: 'Defined'
464
- * console.log(
465
- * Object.ifNullDefined(definedValue, 'Null or Undefined', 'Defined')
466
- * );
467
- */
468
- ifNullDefined(value, thenValue, elseValue) {
469
- return isThenElse(toolkit_js_1.is.nullish(value), thenValue, elseValue);
470
- },
471
- /**
472
- * Checks if the provided value is an object.
473
- *
474
- * This function checks if the provided value is an instance of an Object
475
- * or if the value is truthy and its type is 'object'. This is used to
476
- * determine if a value can have properties and methods like an object.
477
- *
478
- * @param {any} value - The value to be checked.
479
- * @returns {boolean} Returns `true` if the value is an object, `false`
480
- * otherwise.
481
- *
482
- * @example
483
- * // Using a string
484
- * console.log(isObject('Hello, world!')); // Output: false
485
- *
486
- * // Using an object
487
- * console.log(isObject({ key: 'value' })); // Output: true
488
- *
489
- * // Using null
490
- * console.log(isObject(null)); // Output: false
491
- */
492
- isObject(value) {
493
- return toolkit_js_1.is.object(value);
494
- },
495
- /**
496
- * Executes a conditional function based on whether the provided value
497
- * is an object or not. This method first checks if the value is an
498
- * object using the `is.object` method from the toolkit. If it is, it
499
- * returns the `thenValue`, otherwise it returns the `elseValue`.
500
- *
501
- * @param {any} value - The value to be checked.
502
- * @param {function | any} thenValue - The value to return if `value`
503
- * is an object.
504
- * @param {function | any} elseValue - The value to return if `value`
505
- * is not an object.
506
- * @returns {*} - Returns `thenValue` if the value is an object,
507
- * otherwise `elseValue`.
508
- *
509
- * @example
510
- * // returns 'Is Object'
511
- * ifObject({}, 'Is Object', 'Not Object')
512
- * // returns 'Not Object'
513
- * ifObject(42, 'Is Object', 'Not Object')
514
- */
515
- ifObject(value, thenValue, elseValue) {
516
- return isThenElse(toolkit_js_1.is.object(value), thenValue, elseValue);
517
- },
518
- /**
519
- * Checks to see if the supplied value is a primitive value.
520
- *
521
- * @param {any} value the value to test to see if it is a primitive value type
522
- * @returns true if the object is considered widely to be a primitive value,
523
- * false otherwise.
524
- */
525
- isPrimitive(value) {
526
- return toolkit_js_1.is.primitive(value);
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(toolkit_js_1.is.primitive(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
- withProperties(prototype, ...properties) {
722
- const props = properties.filter(p => p instanceof property_js_1.Property);
723
- const possible = properties.filter(p => Array.isArray(p));
724
- if (possible.length) {
725
- for (const [key, descriptorOrDataOrAccessorArgs, ...rest] of possible) {
726
- // if arg[1] is a descriptor
727
- if (property_js_1.Property.is.descriptor(descriptorOrDataOrAccessorArgs)) {
728
- props.push(new property_js_1.Property(key, descriptorOrDataOrAccessorArgs));
729
- }
730
- else {
731
- const { get, set } = descriptorOrDataOrAccessorArgs;
732
- const args = [key, descriptorOrDataOrAccessorArgs, ...rest];
733
- if (get || set) {
734
- props.push(property_js_1.Property.accessor(...args));
735
- }
736
- else {
737
- props.push(property_js_1.Property.data(...args));
738
- }
739
- }
740
- }
741
- }
742
- const object = Object.create(prototype ?? Object.prototype);
743
- const sorted = props.toSorted((a, b) => a.key < b.key ? -1 : (a.key > b.key ? 1 : 0));
744
- for (const property of sorted) {
745
- property.apply(object);
746
- }
747
- property_js_1.Property.data(Symbol.for('properties'), sorted).apply(object);
748
- return object;
749
- },
750
- /**
751
- * Retrieves a toolkit object containing various utility functions
752
- * for type checking and introspection.
753
- *
754
- * The toolkit includes many functions. It was designed to read as
755
- * though the toolkit were assigned to the variable `it`.
756
- *
757
- * @example
758
- * const is = ObjectExtensions.patches.toolkit
759
- * console.log(is.object({})) // Output: true
760
- * console.log(is.string('hello')) // Output: true
761
- * console.log(is.number(42)) // Output: true
762
- *
763
- * @returns {object} The toolkit object containing various utility
764
- * functions for type checking and introspection.
765
- */
766
- get toolkit() {
767
- return { as: toolkit_js_1.as, has: toolkit_js_1.has, is: toolkit_js_1.is, si: toolkit_js_1.si };
768
- },
769
- },
770
- });
771
- 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;
772
- /**
773
- * `ObjectPrototypeExtensions` is a constant that applies a patch to the
774
- * Object prototype. This patch extends the Object prototype with additional
775
- * methods and properties, enhancing its functionality.
776
- *
777
- * The `Patch` function takes two arguments: the target object to be patched
778
- * (in this case, `Object.prototype`), and an object containing the methods
779
- * and properties to be added to the target object.
780
- *
781
- * @example
782
- * // Using a method added by ObjectPrototypeExtensions
783
- * const obj = {};
784
- * console.log(obj.isObject()); // Output: true
785
- *
786
- * @const
787
- * @type {Patch}
788
- * @memberof module:object.extensions
789
- */
790
- exports.ObjectPrototypeExtensions = new extension_1.Patch(Object.prototype, {
791
- [extension_1.Patch.kMutablyHidden]: {
792
- /**
793
- * Retrieves the prototype chain entries of the current object.
794
- *
795
- * This method traverses the prototype chain of the current object
796
- * (`this`) and collects an array of entries. Each entry is a pair
797
- * consisting of the prototype object and its property descriptors.
798
- *
799
- * The property descriptors are obtained using the `Reflect.ownKeys`
800
- * method and the `Object.getOwnPropertyDescriptor` function.
801
- *
802
- * @returns {Array} An array of entries, where each entry is a pair
803
- * consisting of a prototype object and its property descriptors.
804
- *
805
- * @example
806
- * const obj = Object.create({ foo: 'bar' });
807
- * console.log(obj.getPrototypeChainEntries());
808
- * // Output: [[{ foo: 'bar' }, { foo: { value: 'bar', writable: true, enumerable: true, configurable: true } }], [Object.prototype, { ... }]]
809
- */
810
- getPrototypeChainEntries() {
811
- return exports.ObjectExtensions.patches.getPrototypeChainEntries(this);
812
- },
813
- /**
814
- * Determines if the current value is an object.
815
- *
816
- * This method checks whether the current value is an object,
817
- * excluding null. It is a convenience wrapper around the
818
- * `pIsObject` function from the `ObjectExtensions` patch.
819
- *
820
- * @name isObject
821
- * @type {function}
822
- * @memberof Object.prototype
823
- * @returns {boolean} `true` if the current value is an object
824
- * (excluding null), `false` otherwise.
825
- *
826
- * @example
827
- * const obj = {};
828
- * console.log(obj.isObject());
829
- * // Output: true
830
- *
831
- * const str = "hello";
832
- * console.log(str.isObject());
833
- * // Output: false
834
- *
835
- * console.log(null.isObject());
836
- * // Output: false
837
- */
838
- get isObject() {
839
- return pIsObject(this);
840
- },
841
- /**
842
- * Checks if the current value is an object and returns one of two
843
- * provided values based on the result. This function is a convenience
844
- * method for performing conditional operations based on the type of
845
- * the current value.
846
- *
847
- * @name ifObject
848
- * @type {function}
849
- * @memberof Object.prototype
850
- * @param {function | any} thenValue - The value to be returned if the
851
- * current value is an object (excluding null).
852
- * @param {function | any} elseValue - The value to be returned if the
853
- * current value is not an object or is null.
854
- * @returns {*} Returns `thenValue` if the current value is an object
855
- * (excluding null), otherwise returns `elseValue`.
856
- *
857
- * @example
858
- * const obj = {};
859
- * console.log(obj.ifObject('Object', 'Not an object'));
860
- * // Output: 'Object'
861
- *
862
- * const str = "hello";
863
- * console.log(str.ifObject('Object', 'Not an object'));
864
- * // Output: 'Not an object'
865
- *
866
- * console.log(null.ifObject('Object', 'Not an object'));
867
- * // Output: 'Not an object'
868
- */
869
- ifObject(thenValue, elseValue) {
870
- return pIfObject(this, thenValue, elseValue);
871
- },
872
- /**
873
- * Checks if the current value is either `null` or `undefined`.
874
- *
875
- * @name isNullDefined
876
- * @type {boolean}
877
- * @memberof Object.prototype
878
- * @returns {boolean} Returns `true` if the current value is either
879
- * `null` or `undefined`, `false` otherwise.
880
- *
881
- * @example
882
- * const obj = null;
883
- * console.log(obj.isNullDefined);
884
- * // Output: true
885
- *
886
- * const str = "hello";
887
- * console.log(str.isNullDefined);
888
- * // Output: false
889
- */
890
- get isNullDefined() {
891
- return pIsNullDefined(this);
892
- },
893
- /**
894
- * Checks if the current value is either `null` or `undefined` and
895
- * returns one of two provided values based on the result.
896
- *
897
- * @name ifNullDefined
898
- * @type {function}
899
- * @memberof Object.prototype
900
- * @param {function | any} thenValue - The value to be returned if the
901
- * current value is either `null` or `undefined`.
902
- * @param {function | any} elseValue - The value to be returned if the
903
- * current value is not `null` or `undefined`.
904
- * @returns {*} Returns `thenValue` if the current value is either
905
- * `null` or `undefined`, otherwise returns `elseValue`.
906
- *
907
- * @example
908
- * const obj = null
909
- * console.log(obj.ifNullDefined('Null or Undefined', 'Defined'))
910
- * // Output: 'Null or Undefined'
911
- *
912
- * const str = "hello"
913
- * console.log(str.ifNullDefined('Null or Undefined', 'Defined'))
914
- * // Output: 'Defined'
915
- */
916
- ifNullDefined(thenValue, elseValue) {
917
- return pIfNullDefined(this, thenValue, elseValue);
918
- },
919
- /**
920
- * Checks if the current value is a primitive type.
921
- *
922
- * Primitive types in JavaScript include `string`, `number`,
923
- * `bigint`, `boolean`, `undefined`, `symbol`, and `null`.
924
- *
925
- * @name isPrimitive
926
- * @type {boolean}
927
- * @memberof Object.prototype
928
- * @returns {boolean} Returns `true` if the current value is a
929
- * primitive type, `false` otherwise.
930
- *
931
- * @example
932
- * const str = "hello"
933
- * console.log(str.isPrimitive)
934
- * // Output: true
935
- *
936
- * const obj = { key: "value" }
937
- * console.log(obj.isPrimitive)
938
- * // Output: false
939
- */
940
- get isPrimitive() {
941
- return pIsPrimitive(this);
942
- },
943
- /**
944
- * Checks if the current value is a primitive type and returns one
945
- * of two provided values based on the result.
946
- *
947
- * Primitive types in JavaScript include `string`, `number`,
948
- * `bigint`, `boolean`, `undefined`, `symbol`, and `null`.
949
- *
950
- * @name ifPrimitive
951
- * @type {function}
952
- * @memberof Object.prototype
953
- * @param {function | any} thenValue - The value to be returned if
954
- * the current value is a primitive type.
955
- * @param {function | any} elseValue - The value to be returned if
956
- * the current value is not a primitive type.
957
- * @returns {*} Returns `thenValue` if the current value is a
958
- * primitive type, otherwise returns `elseValue`.
959
- *
960
- * @example
961
- * const str = "hello"
962
- * console.log(str.ifPrimitive('Primitive', 'Not Primitive'))
963
- * // Output: 'Primitive'
964
- *
965
- * const obj = { key: "value" }
966
- * console.log(obj.ifPrimitive('Primitive', 'Not Primitive'))
967
- * // Output: 'Not Primitive'
968
- */
969
- ifPrimitive(thenValue, elseValue) {
970
- return pIfPrimitive(this, thenValue, elseValue);
971
- },
972
- /**
973
- * Determines if the current value is a valid key for an object.
974
- *
975
- * A valid key is either a string or a symbol. This method is a
976
- * convenience wrapper around the `pIsValidKey` function from the
977
- * `ObjectExtensions` patch.
978
- *
979
- * @name isValidKey
980
- * @type {boolean}
981
- * @memberof Object.prototype
982
- * @returns {boolean} `true` if the current value is a valid key for
983
- * an object (i.e., a string or symbol), `false` otherwise.
984
- *
985
- * @example
986
- * const str = "key"
987
- * console.log(str.isValidKey)
988
- * // Output: true
989
- *
990
- * const sym = Symbol("key")
991
- * console.log(sym.isValidKey)
992
- * // Output: true
993
- *
994
- * const num = 42
995
- * console.log(num.isValidKey)
996
- * // Output: false
997
- */
998
- get isValidKey() {
999
- return pIsValidKey(this);
1000
- },
1001
- /**
1002
- * Checks if the current value is a valid key for an object and returns
1003
- * one of two provided values based on the result. This function is a
1004
- * convenience method for performing conditional operations based on
1005
- * the type of the current value.
1006
- *
1007
- * A valid key is either a string or a symbol.
1008
- *
1009
- * @name ifValidKey
1010
- * @type {function}
1011
- * @memberof Object.prototype
1012
- * @param {function | any} thenValue - The value to be returned if the
1013
- * current value is a valid key for an object.
1014
- * @param {function | any} elseValue - The value to be returned if the
1015
- * current value is not a valid key for an object.
1016
- * @returns {*} Returns `thenValue` if the current value is a valid key
1017
- * for an object, otherwise returns `elseValue`.
1018
- *
1019
- * @example
1020
- * const str = "key"
1021
- * console.log(str.ifValidKey('Valid Key', 'Not a Valid Key'))
1022
- * // Output: 'Valid Key'
1023
- *
1024
- * const num = 42
1025
- * console.log(num.ifValidKey('Valid Key', 'Not a Valid Key'))
1026
- * // Output: 'Not a Valid Key'
1027
- */
1028
- ifValidKey(thenValue, elseValue) {
1029
- return pIfValidKey(this, thenValue, elseValue);
1030
- },
1031
- /**
1032
- * Checks to see if the supplied `value` is both an object, and has the
1033
- * appropriate symbol defined.
1034
- *
1035
- * @param {any} value the value to determine if it contains a defined
1036
- * `Symbol.toStringTag` defined.
1037
- * @returns true if the symbol is defined, false otherwise
1038
- */
1039
- get hasStringTag() {
1040
- return pHasStringTag(this);
1041
- },
1042
- /**
1043
- * Retrieves the string tag of an object. The string tag is a representation
1044
- * of the object's type, as defined by its `Object.prototype.toString`
1045
- * method. This utility method is helpful for getting a more descriptive
1046
- * type of an object than what is returned by the `typeof` operator,
1047
- * especially for custom objects.
1048
- *
1049
- * @param {*} value - The object whose string tag is to be retrieved.
1050
- * @param {boolean} strict - if this is set to true, undefined will be
1051
- * returned whenever a supplied object does not have a
1052
- * `Symbol.toStringTag` defined, period. if false, the default,
1053
- * @returns {string} - The string tag of the object, indicating its type.
1054
- */
1055
- getStringTag(strict = false) {
1056
- return pGetStringTag(this, strict);
1057
- },
1058
- /**
1059
- * Strips an object down to only the keys specified. Optionally, any
1060
- * accessors can be made to retain their context on the source object.
1061
- * This is a passthrough to the static {@link Object.stripTo} function
1062
- *
1063
- * @param {Array<string|symbol>} keys the keys that should appear in the
1064
- * final reduced object
1065
- * @param {boolean} [bindAccessors = true] if this value is true then any
1066
- * accessors from the source object will continue to have their `this`
1067
- * value bound to the source. If the getter or setter on that object is
1068
- * defined using an arrow function, this will not work as intended.
1069
- * @returns {object} an object containing only the keys and symbols
1070
- * specified in the `keys` parameter.
1071
- */
1072
- stripTo(keys, bindAccessors = true) {
1073
- return pStripTo(this, keys, bindAccessors);
1074
- },
1075
- },
1076
- });
1077
- // NOTE to self; this is repeated here otherwise a circular reference from
1078
- // Object<->Function<->Global occurs. See original source in global.this.js
1079
- // {@see globalThis.isThenElse}
1080
- function isThenElse(bv, tv, ev) {
1081
- if (arguments.length > 1) {
1082
- var _then = toolkit_js_1.is.function(tv) ? tv(bv) : tv;
1083
- if (arguments.length > 2) {
1084
- var _else = toolkit_js_1.is.function(ev) ? tv(bv) : ev;
1085
- return bv ? _then : _else;
1086
- }
1087
- return bv || _then;
1088
- }
1089
- return bv;
1090
- }
1091
- //# sourceMappingURL=object.extensions.js.map