Skip to content

Commit

Permalink
Add isEmptyObject
Browse files Browse the repository at this point in the history
  • Loading branch information
VovanR committed Dec 27, 2019
1 parent 79f6ea5 commit 3817776
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/isEmptyObject/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Object is empty or not
*
* {@link https://stackoverflow.com/a/34491966/1284255}
* @version 0.0.1
*
* @param {Object} obj
* @returns {boolean}
*
* @example
* isEmptyObject({}); //=> true
* isEmptyObject({foo: 1}); //=> false
*/
function isEmptyObject(obj) {
for (let key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}

return true;
}

module.exports = isEmptyObject;
10 changes: 10 additions & 0 deletions src/isEmptyObject/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import test from 'ava';
import isEmptyObject from '.';

test('should return `true` for empty object', t => {
t.is(isEmptyObject({}), true);
});

test('should return `false` for not empty object', t => {
t.is(isEmptyObject({foo: 1}), false);
});

0 comments on commit 3817776

Please sign in to comment.