Skip to content

Commit

Permalink
Raise test coverage back up
Browse files Browse the repository at this point in the history
  • Loading branch information
Diane Huxley committed May 4, 2023
1 parent 317652b commit 838ca2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Decentralized Web Node (DWN) SDK

Code Coverage
![Statements](https://img.shields.io/badge/statements-94.16%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-93.84%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-91.97%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-94.16%25-brightgreen.svg?style=flat)
![Statements](https://img.shields.io/badge/statements-94.2%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-93.94%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-91.97%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-94.2%25-brightgreen.svg?style=flat)

## Introduction

Expand Down
7 changes: 5 additions & 2 deletions src/utils/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ export function isEmptyObject(obj: unknown): boolean {
*/
export function removeEmptyObjects(obj: Record<string, unknown>): void {
Object.keys(obj).forEach(key => {
if (typeof(obj[key]) === 'object') {
// recursive remove empty object or array properties in nested objects
removeEmptyObjects(obj[key] as Record<string, unknown>);
}

if (isEmptyObject(obj[key])) {
delete obj[key];
} else if (typeof(obj[key]) === 'object') {
removeEmptyObjects(obj[key] as Record<string, unknown>); // recursive remove empty object or array properties in nested objects
}
});
}
Expand Down
15 changes: 14 additions & 1 deletion tests/utils/object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { removeUndefinedProperties } from '../../src/utils/object.js';
import { removeEmptyObjects, removeUndefinedProperties } from '../../src/utils/object.js';

describe('Object', () => {
describe('removeUndefinedProperties', () => {
Expand All @@ -24,4 +24,17 @@ describe('Object', () => {
expect(mockObject).to.deep.equal(expectedResult);
});
});

describe('removeEmptyObjects', () => {
it('should remove all empty objects', () => {
const obj = {
foo : {},
bar : { baz: {} },
buzz : 'hello'
};
removeEmptyObjects(obj);

expect(obj).to.deep.equal({ buzz: 'hello' });
});
});
});

0 comments on commit 838ca2c

Please sign in to comment.