Skip to content

Commit

Permalink
AG-18170 improve isEmptyObject - should not be function #268
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/scriptlets from fix/AG-18170 to master

Squashed commit of the following:

commit 0390052
Merge: 180e595 d85c776
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 13 20:46:10 2022 +0300

    Merge branch 'master' into fix/AG-18170

commit 180e595
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 13 18:05:41 2022 +0300

    fix version

commit 2febad3
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 13 16:51:14 2022 +0300

    update changelog

commit 19c6c85
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 13 15:47:46 2022 +0300

    improve helper

commit f52e9a1
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 13 15:47:04 2022 +0300

    improve helper and add tests

commit 817493a
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 13 14:50:36 2022 +0300

    improve isEmptyObject - should not be function
  • Loading branch information
stanislav-atr committed Dec 13, 2022
1 parent d85c776 commit c33826b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Scriptlets and Redirect Resources Changelog

## v1.7.13

### Fixed

* `isEmptyObject` helper not counting `prototype` as an object property

## v1.7.10

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/object-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const getObjectFromEntries = (entries) => {
* @param {Object} obj
* @returns {boolean}
*/
export const isEmptyObject = (obj) => Object.keys(obj).length === 0;
export const isEmptyObject = (obj) => Object.keys(obj).length === 0 && !obj.prototype;

/**
* Checks whether the obj is an empty object
Expand Down
1 change: 1 addition & 0 deletions tests/helpers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import './match-stack.test';
import './noop.test';
import './number-utils.test';
import './string-utils.test';
import './object-utils.test';
26 changes: 26 additions & 0 deletions tests/helpers/object-utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { isEmptyObject } from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';

module(name);

test('Test isEmptyObject works for different inputs', async (assert) => {
const emptyObj = {};
const obj = { a: 1 };
const emptyArr = [];
const arr = [1, 2, 3];
function func() {}

assert.ok(isEmptyObject(emptyObj), 'empty object returns true');
assert.ok(isEmptyObject(emptyArr), 'empty array returns true');

assert.notOk(isEmptyObject(obj), 'non-empty object returns false');
assert.notOk(isEmptyObject(arr), 'non-empty array returns false');

assert.notOk(isEmptyObject(EventTarget));
assert.notOk(isEmptyObject(Array));
assert.notOk(isEmptyObject(Object));
assert.notOk(isEmptyObject(Function));
assert.notOk(isEmptyObject(func));
});

0 comments on commit c33826b

Please sign in to comment.