Skip to content

Commit

Permalink
Extended isempty behavior (#722)
Browse files Browse the repository at this point in the history
* Make isEmpty work with custom objects providing an isEmpty function or property.

* Removed console.error

* Updated bundles
  • Loading branch information
hbel committed Feb 16, 2024
1 parent 988f925 commit d7ed1e7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions dist/rambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,11 @@ function isEmpty(input) {
const inputType = type(input);
if (['Undefined', 'NaN', 'Number', 'Null'].includes(inputType)) return false;
if (!input) return true;
if (type(input.isEmpty) === 'Function') {
return input.isEmpty();
} else if (input.isEmpty) {
return input.isEmpty;
}
if (inputType === 'Object') {
return Object.keys(input).length === 0;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/rambda.umd.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions source/isEmpty.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export function isEmpty(input){
return false
if (!input) return true

if (type(input.isEmpty) === 'Function') {
return input.isEmpty();
} else if (input.isEmpty) {
return !!input.isEmpty;
}


if (inputType === 'Object'){
return Object.keys(input).length === 0
}
Expand Down
4 changes: 4 additions & 0 deletions source/isEmpty.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ test('happy', () => {
expect(isEmpty(0)).toBeFalse()
expect(isEmpty(NaN)).toBeFalse()
expect(isEmpty([ '' ])).toBeFalse()
expect(isEmpty({ isEmpty: false})).toBeFalse()
expect(isEmpty({ isEmpty: () => false})).toBeFalse()
expect(isEmpty({ isEmpty: true})).toBeTrue()
expect(isEmpty({ isEmpty: () => true})).toBeTrue()
})
6 changes: 6 additions & 0 deletions src/isEmpty.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export function isEmpty(input){
return false
if (!input) return true

if (type(input.isEmpty) === 'Function') {
return input.isEmpty();
} else if (input.isEmpty) {
return input.isEmpty;
}

if (inputType === 'Object'){
return Object.keys(input).length === 0
}
Expand Down

0 comments on commit d7ed1e7

Please sign in to comment.