@nejs/basic-extensions 1.0.0 → 1.1.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.
@@ -45,7 +45,47 @@ export const ObjectExtensions = new Patch(Object, {
45
45
  * @returns {string} - The string tag of the object, indicating its type.
46
46
  */
47
47
  getStringTag(value) {
48
- return /(\w+)]/.exec(Object.prototype.toString.call(value))[1];
48
+ return /\s(.+)]/.exec(Object.prototype.toString.call(value))[1];
49
+ },
50
+
51
+ /**
52
+ * Strips an object down to only the keys specified. Optionally, any
53
+ * accessors can be made to retain their context on the source object.
54
+ *
55
+ * @param {object} object the object to pare down
56
+ * @param {Array<string|symbol>} keys the keys that should appear in the
57
+ * final reduced object
58
+ * @param {boolean} [bindAccessors = true] if this value is true
59
+ * then any accessors from the source object will continue to have their
60
+ * `this` value bound to the source. If the getter or setter on that object
61
+ * is defined using an arrow function, this will not work as intended.
62
+ * @returns {object} an object containing only the keys and symbols specified
63
+ * in the `keys` parameter.
64
+ */
65
+ stripTo(object, keys, bindAccessors = true) {
66
+ const result = {}
67
+
68
+ if (!Array.isArray(keys)) {
69
+ return result
70
+ }
71
+
72
+ for (let key of keys) {
73
+ if (Reflect.has(object, key)) {
74
+ const descriptor = Object.getOwnPropertyDescriptor(object, key)
75
+ if (Reflect.has(descriptor, 'get') || Reflect.has(descriptor, 'set')) {
76
+ if (bindAccessors) {
77
+ descriptor.get = descriptor?.get?.bind(object)
78
+ descriptor.set = descriptor?.set?.bind(object)
79
+ }
80
+ Object.defineProperty(result, descriptor)
81
+ }
82
+ else {
83
+ Object.defineProperty(result, descriptor)
84
+ }
85
+ }
86
+ }
87
+
88
+ return result
49
89
  },
50
90
 
51
91
  /**
@@ -1,4 +1,5 @@
1
1
  import { Patch } from '@nejs/extension'
2
+ import { ObjectExtensions } from './objectextensions.js'
2
3
 
3
4
  /**
4
5
  * The `ReflectExtensions` class is a patch applied to the built-in JavaScript
@@ -30,6 +31,32 @@ export const ReflectExtensions = new Patch(Reflect, {
30
31
  )
31
32
  },
32
33
 
34
+ ownDescriptors(object) {
35
+ const result = {}
36
+ const revertOnDone = () => revertOnDone.doIt ? ObjectExtensions.revert() : ''
37
+ revertOnDone.doIt = false
38
+
39
+ if (!Object.isObject) {
40
+ revertOnDone.doIt = true
41
+ ObjectExtensions.apply()
42
+ }
43
+
44
+ if (!Object.isObject(object)) {
45
+ revertOnDone()
46
+ return {}
47
+ }
48
+
49
+ const keys = Reflect.ownKeys(object)
50
+
51
+ for (const key of keys) {
52
+ result[key] = Object.getOwnPropertyDescriptor(key)
53
+ }
54
+
55
+ revertOnDone()
56
+
57
+ return result
58
+ },
59
+
33
60
  /**
34
61
  * The function checks if an object has at least one of the specified keys.
35
62
  *
@@ -86,4 +113,4 @@ export const ReflectExtensions = new Patch(Reflect, {
86
113
  values(object) {
87
114
  return Reflect.entries.map(([,value]) => value)
88
115
  }
89
- })
116
+ })
@@ -0,0 +1,25 @@
1
+ import { Patch } from '@nejs/extension';
2
+
3
+ /**
4
+ * `StringExtensions` is a patch for the JavaScript built-in `String` class. It
5
+ * adds utility methods to the `String` class without modifying the global namespace
6
+ * directly. This patch includes methods for key validation, object type checking,
7
+ * and retrieving the string tag of an object. These methods are useful for
8
+ * enhancing the capabilities of the standard `String` class with additional
9
+ * utility functions.
10
+ */
11
+ export const StringExtensions = new Patch(String, {
12
+ /**
13
+ * The `isString` method does exactly what one would it expect. It returns
14
+ * true if the string matches typeof or instanceof as a string.
15
+ *
16
+ * @param {*} value checks to see if the `value` is a string
17
+ * @returns {boolean} `true` if it is a `String`, `false` otherwise
18
+ */
19
+ isString(value) {
20
+ if (value && (typeof value === 'string' || value instanceof String)) {
21
+ return value.length > 0
22
+ }
23
+ return false
24
+ },
25
+ });
@@ -0,0 +1,25 @@
1
+ import { Patch } from '@nejs/extension';
2
+
3
+ /**
4
+ * `SymbolExtensions` is a patch for the JavaScript built-in `Symbol` class. It
5
+ * adds utility methods to the `Symbol` class without modifying the global namespace
6
+ * directly. This patch includes methods for key validation, object type checking,
7
+ * and retrieving the string tag of an object. These methods are useful for
8
+ * enhancing the capabilities of the standard `Symbol` class with additional
9
+ * utility functions.
10
+ */
11
+ export const SymbolExtensions = new Patch(Symbol, {
12
+ /**
13
+ * The `isSymbol` method does exactly what one would it expect. It returns
14
+ * true if the string matches typeof or instanceof as a symbol.
15
+ *
16
+ * @param {*} value checks to see if the `value` is a string
17
+ * @returns {boolean} `true` if it is a `Symbol`, `false` otherwise
18
+ */
19
+ isSymbol(value) {
20
+ if (value && (typeof value === 'symbol')) {
21
+ return true
22
+ }
23
+ return false
24
+ },
25
+ });