Skip to content

Commit 72dd7e7

Browse files
committed
Add isEmptyObject() method
1 parent b8c4f5c commit 72dd7e7

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/is-empty-object.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// -----------------------------------------------------------------------------
2+
// Deps
3+
// -----------------------------------------------------------------------------
4+
5+
import { isEmptyObject } from './is-empty-object';
6+
7+
// -----------------------------------------------------------------------------
8+
// Tests
9+
// -----------------------------------------------------------------------------
10+
11+
const testCases: {
12+
parameters: Parameters<typeof isEmptyObject>;
13+
result: ReturnType<typeof isEmptyObject>;
14+
}[] = [
15+
{
16+
parameters: [{}],
17+
result: true
18+
},
19+
{
20+
parameters: [{ key: 'value' }],
21+
result: false
22+
},
23+
{
24+
parameters: [new Date()],
25+
result: true
26+
},
27+
{
28+
parameters: [new RegExp('x')],
29+
result: true
30+
}
31+
];
32+
33+
describe('isEmptyObject tool', () => {
34+
testCases.forEach(({ parameters, result }, i) => {
35+
test(`test #${i + 1}: ${parameters.join(', ')}`, () => {
36+
const bool = isEmptyObject(...parameters);
37+
expect(bool).toStrictEqual(result);
38+
});
39+
});
40+
});

src/is-empty-object.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// -----------------------------------------------------------------------------
2+
// Deps
3+
// -----------------------------------------------------------------------------
4+
5+
import { isPlainObject } from './is-plain-object';
6+
7+
// -----------------------------------------------------------------------------
8+
// Helper
9+
// -----------------------------------------------------------------------------
10+
11+
/**
12+
* @param sample
13+
* @return {boolean}
14+
*/
15+
export function isEmptyObject(sample: any) {
16+
return isPlainObject(sample) && Object.keys(sample).length === 0;
17+
}

0 commit comments

Comments
 (0)