Skip to content

Commit e751a09

Browse files
committed
fix(isEmpty): suport empty array
1 parent d99b593 commit e751a09

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/is-empty.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
* @param value - the value to check
33
* @returns `true` if value is empty, else `false`
44
*/
5-
export const isEmpty = (value: string | number | Record<string, unknown>): boolean => {
6-
if (value === null) {
5+
export const isEmpty = (value: string | number | unknown[] | Record<string, unknown>): boolean => {
6+
if (value === null || value === undefined) {
77
return true;
8-
} else if (typeof value !== 'number' && value === '') {
8+
} else if (typeof value === 'string' && !value.length) {
99
return true;
10-
} else if (value === 'undefined' || value === undefined) {
11-
return true;
12-
} else if (value !== null && typeof value === 'object' && !Object.keys(value).length) {
10+
} else if (typeof value === 'object' && !Object.keys(value).length) {
1311
return true;
1412
} else {
1513
return false;

src/spec/is-empty.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ describe('isEmpty', () => {
2121
expect(result).toBeTruthy();
2222
});
2323

24+
test('empty array is empty', () => {
25+
const result = isEmpty([]);
26+
expect(result).toBeTruthy();
27+
});
28+
2429
test('`foo` is not empty', () => {
2530
const result = isEmpty('foo');
2631
expect(result).toBeFalsy();

0 commit comments

Comments
 (0)