Skip to content

Commit

Permalink
feat(isEmpty): support non-primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
伍程凯 committed Aug 9, 2018
1 parent 47e7898 commit 5820813
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 5 additions & 2 deletions __tests__/specs/isEmpty.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ describe('isEmpty method', function () {
expect(isEmpty(null)).toBe(true)
expect(isEmpty(undefined)).toBe(true)
expect(isEmpty('')).toBe(true)

expect(isEmpty(0)).toBe(false)
expect(isEmpty({})).toBe(false)

expect(isEmpty([])).toBe(true)
expect(isEmpty({})).toBe(true)
expect(isEmpty({ name: '' })).toBe(false)
expect(isEmpty([1, 2, 3])).toBe(false)
})
})
11 changes: 9 additions & 2 deletions src/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isPrimitive from './isPrimitive'

/**
* 判断一个对象是否是 Empty
* @param {Object} obj 判断的对象
Expand All @@ -18,8 +20,13 @@
* // => false
*
* isEmpty({})
* // => false
* // => true
*
* isEmpty([1, 2, 3])
* //=> false
*/
export default function isEmpty (obj: any): boolean {
return obj == null || obj === ''
return isPrimitive(obj)
? obj == null || obj === ''
: Object.keys(obj).length === 0
}

0 comments on commit 5820813

Please sign in to comment.