diff --git a/__tests__/specs/isEmpty.spec.js b/__tests__/specs/isEmpty.spec.js index 26b3b89..1ba8b2c 100644 --- a/__tests__/specs/isEmpty.spec.js +++ b/__tests__/specs/isEmpty.spec.js @@ -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) }) }) diff --git a/src/isEmpty.ts b/src/isEmpty.ts index 2e7bb82..4ac43b1 100644 --- a/src/isEmpty.ts +++ b/src/isEmpty.ts @@ -1,3 +1,5 @@ +import isPrimitive from './isPrimitive' + /** * 判断一个对象是否是 Empty * @param {Object} obj 判断的对象 @@ -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 }