is-object-empty2
v1.1.1
Published
📦 Tiny utility to check if a value is a plain empty object in JavaScript or TypeScript
Maintainers
Readme
is-object-empty2
- ✨ Simple yet robust NPM package that checks if a value is a plain empty object in
JavaScriptandTypeScript. - ♻️ Works seamlessly with
CommonJS,ESMandTypeScript
- Returns
trueonly for plain objects with no own enumerable properties ({}orObject.create(null)). - Returns
falseforArrays,null,undefined,functions,symbols,BigInt, objects with keys or object with at least one own enumerable property (string or symbol) - Correctly handles:
- Objects created with
Object.create(null) - Frozen or sealed objects
- Proxy objects (both empty and with keys)
- Objects with inherited properties
- Objects with non-enumerable properties
- Nested or deeply nested objects
- Objects created with
📦 Install via NPM
$ npm i is-object-empty2💻 Usage
- Returns
trueonly for empty objects ({}), andfalsefor arrays, null, undefined, or objects with keys. - See examples below
CommonJS
const isObjectEmpty2 = require('is-object-empty2');
console.log(isObjectEmpty2({})); // true
console.log(isObjectEmpty2({ a: 1 })); // false
console.log(isObjectEmpty2([])); // false
console.log(isObjectEmpty2(null)); // false
console.log(isObjectEmpty2(undefined)); // falseESM
import isObjectEmpty2 from 'is-object-empty2';
console.log(isObjectEmpty2({})); // true
console.log(isObjectEmpty2({ a: 1 })); // false
console.log(isObjectEmpty2([])); // false
console.log(isObjectEmpty2(null)); // false
console.log(isObjectEmpty2(undefined)); // falseTypeScript
import isObjectEmpty2 from 'is-object-empty2';
const testValues: unknown[] = [
{},
{ a: 1 },
[],
null,
undefined,
Object.create(null),
{ nested: {} }
];
for (const value of testValues) {
const result: boolean = isObjectEmpty2(value);
console.log(`${JSON.stringify(value)} → ${result}`);
}
/*
--| Expected values:
{} → true
{"a":1} → false
[] → false
null → false
undefined → false
{} → true
{"nested":{}} → false
*/