Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions object/isEmptyObjectLiteral.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Checks if the given object is an empty object literal.
* An empty object literal is defined as an object with no own properties
* and whose prototype is `Object.prototype`.
* @param {object} obj - The object to check.
* @returns {boolean} - Returns `true` if the object is an empty object literal, otherwise `false`.
* @example
* isEmptyObjectLiteral({}); // true
* isEmptyObjectLiteral(Object.create({})); // true
* isEmptyObjectLiteral({ a: 1 }); // false
* isEmptyObjectLiteral([]); // false
* isEmptyObjectLiteral(null); // false
* isEmptyObjectLiteral(Object.create(null)); // false
*/
export default function isEmptyObjectLiteral(obj) {
return (
obj !== null &&
typeof obj === 'object' &&
Array.isArray(obj) === false &&
Object.keys(obj).length === 0
);
}
1 change: 1 addition & 0 deletions test/specs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import('./object/filter.spec.js');
import('./object/getNestedValue.spec.js');
import('./object/hasBinary.spec.js');
import('./object/instanceOf.spec.js');
import('./object/isEmptyObjectLiteral.spec.js');
import('./object/isPromise.spec.js');
import('./object/isSame.spec.js');
import('./object/merge.spec.js');
Expand Down
19 changes: 19 additions & 0 deletions test/specs/object/isEmptyObjectLiteral.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import isEmptyObjectLiteral from '../../../object/isEmptyObjectLiteral.js';

describe('object/isEmptyObjectLiteral', () => {

it('should determine whether object is an empty object literal, aka `{}`', () => {

// ok
expect(isEmptyObjectLiteral({})).to.equal(true);
expect(isEmptyObjectLiteral(Object.create({}))).to.equal(true);
expect(isEmptyObjectLiteral(Object.create(null))).to.equal(true);

// not ok
expect(isEmptyObjectLiteral({a: 1})).to.equal(false);
expect(isEmptyObjectLiteral([])).to.equal(false);
expect(isEmptyObjectLiteral(null)).to.equal(false);

});

});