is-object-empty2 1.0.2 → 1.0.4
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/.gitattributes +5 -0
- package/README.md +1 -1
- package/index.js +5 -2
- package/package.json +1 -1
- package/test/index.test.js +8 -0
package/.gitattributes
ADDED
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
# Simple yet robust NPM package that checks if a value is a plain empty object in `JavaScript` and `TypeScript`.
|
|
9
9
|
|
|
10
10
|
- Returns `true` **only** for plain objects with no own enumerable properties (`{}` or `Object.create(null)`).
|
|
11
|
-
- Returns `false` for
|
|
11
|
+
- Returns `false` for `Arrays`, `null`, `undefined`, `functions`, `symbols`, `BigInt`, objects with keys or object with at least one own enumerable property (string or symbol)
|
|
12
12
|
- Correctly handles:
|
|
13
13
|
- Objects created with `Object.create(null)`
|
|
14
14
|
- Frozen or sealed objects
|
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.4
|
|
4
4
|
* @link: https://github.com/tutyamxx/is-object-empty2
|
|
5
5
|
* @license: MIT
|
|
6
6
|
**/
|
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
* isObjectEmpty2(null); // false
|
|
21
21
|
* isObjectEmpty2(undefined); // false
|
|
22
22
|
*/
|
|
23
|
-
const isObjectEmpty2 =
|
|
23
|
+
const isObjectEmpty2 = obj => (!!obj && typeof obj === 'object')
|
|
24
|
+
&& !Array.isArray(obj)
|
|
25
|
+
&& !(Object.keys(obj)?.length ?? 0)
|
|
26
|
+
&& !Object.getOwnPropertySymbols(obj)?.some(s => Object.getOwnPropertyDescriptor(obj, s)?.enumerable ?? false);
|
|
24
27
|
|
|
25
28
|
module.exports = isObjectEmpty2;
|
package/package.json
CHANGED
package/test/index.test.js
CHANGED
|
@@ -84,4 +84,12 @@ describe('isObjectEmpty2', () => {
|
|
|
84
84
|
Object.defineProperty(obj, 'hidden', { value: 123, enumerable: false });
|
|
85
85
|
expect(isObjectEmpty2(obj)).toBe(true);
|
|
86
86
|
});
|
|
87
|
+
|
|
88
|
+
test('Object with Symbol property is considered non-empty', () => {
|
|
89
|
+
const obj = {};
|
|
90
|
+
const sym = Symbol('test');
|
|
91
|
+
|
|
92
|
+
obj[sym] = 123;
|
|
93
|
+
expect(isObjectEmpty2(obj)).toBe(false);
|
|
94
|
+
});
|
|
87
95
|
});
|