is-object-empty2 1.0.5 → 1.0.7
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.
- package/index.d.ts +2 -0
- package/index.js +3 -1
- package/package.json +1 -1
- package/test/index.test.js +34 -0
package/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* Checks if a given value is a plain object and has no own properties.
|
|
3
3
|
* Returns `true` only for empty plain objects (`{}`), and `false` for arrays, null, undefined, or objects with keys.
|
|
4
4
|
*
|
|
5
|
+
* Handles edge cases like objects with enumerable symbols, getters/setters, and prototype properties.
|
|
6
|
+
*
|
|
5
7
|
* @param {unknown} obj - The value to check.
|
|
6
8
|
* @returns {boolean} - `true` if the value is an empty object, otherwise `false`.
|
|
7
9
|
*
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* is-object-empty2 - 📦 Tiny utility to check if a value is a plain empty object in JavaScript and TypeScript
|
|
3
|
-
* @version: v1.0.
|
|
3
|
+
* @version: v1.0.7
|
|
4
4
|
* @link: https://github.com/tutyamxx/is-object-empty2
|
|
5
5
|
* @license: MIT
|
|
6
6
|
**/
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
* Checks if a given value is a plain object with no own properties.
|
|
11
11
|
* Returns `true` only for empty objects (`{}`), and `false` for arrays, null, undefined, or objects with keys.
|
|
12
12
|
*
|
|
13
|
+
* Handles edge cases like objects with enumerable symbols, getters/setters, and prototype properties.
|
|
14
|
+
*
|
|
13
15
|
* @param {unknown} obj - The value to check.
|
|
14
16
|
* @returns {boolean} `true` if the value is an empty plain object, otherwise `false`.
|
|
15
17
|
*
|
package/package.json
CHANGED
package/test/index.test.js
CHANGED
|
@@ -95,11 +95,13 @@ describe('isObjectEmpty2', () => {
|
|
|
95
95
|
|
|
96
96
|
test('Object with multiple Symbol properties is considered non-empty', () => {
|
|
97
97
|
const obj = {};
|
|
98
|
+
|
|
98
99
|
const sym1 = Symbol('test1');
|
|
99
100
|
const sym2 = Symbol('test2');
|
|
100
101
|
|
|
101
102
|
obj[sym1] = 123;
|
|
102
103
|
obj[sym2] = 456;
|
|
104
|
+
|
|
103
105
|
expect(isObjectEmpty2(obj)).toBe(false);
|
|
104
106
|
});
|
|
105
107
|
|
|
@@ -113,8 +115,40 @@ describe('isObjectEmpty2', () => {
|
|
|
113
115
|
|
|
114
116
|
test('Object with only non-enumerable properties is considered empty', () => {
|
|
115
117
|
const obj = {};
|
|
118
|
+
|
|
116
119
|
Object.defineProperty(obj, 'prop1', { value: 1, enumerable: false });
|
|
117
120
|
Object.defineProperty(obj, 'prop2', { value: 2, enumerable: false });
|
|
121
|
+
|
|
118
122
|
expect(isObjectEmpty2(obj)).toBe(true);
|
|
119
123
|
});
|
|
124
|
+
|
|
125
|
+
test('Objects with prototype properties are considered non-empty', () => {
|
|
126
|
+
function Parent() {
|
|
127
|
+
this.inherited = 'value';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
Parent.prototype.inheritedProp = 'prototype-value';
|
|
131
|
+
|
|
132
|
+
const child = new Parent();
|
|
133
|
+
expect(isObjectEmpty2(child)).toBe(false);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('Objects with getter/setter properties', () => {
|
|
137
|
+
const obj = {};
|
|
138
|
+
|
|
139
|
+
Object.defineProperty(obj, 'computed', {
|
|
140
|
+
get() { return 'value'; },
|
|
141
|
+
set() { /* setter */ },
|
|
142
|
+
enumerable: true
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(isObjectEmpty2(obj)).toBe(false);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('Object.create(null) with enumerable properties', () => {
|
|
149
|
+
const obj = Object.create(null);
|
|
150
|
+
|
|
151
|
+
obj.a = 1;
|
|
152
|
+
expect(isObjectEmpty2(obj)).toBe(false);
|
|
153
|
+
});
|
|
120
154
|
});
|