Last active
June 25, 2019 11:25
-
-
Save andrewborisov/3cb3c01f9517b048ec82719c37a3a0bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isObjectRecursivelyEmpty = (object) => { | |
if (keys(object).lengh === 0) { | |
return true; | |
} | |
const objKeys = keys(object); | |
const result = objKeys.map((i) => { | |
const currentValue = object[i]; | |
if (currentValue === '') { | |
return true; | |
} | |
if (isFunction(currentValue) || (isArray(currentValue) && !isEmpty(currentValue)) || isNumber(currentValue) || (!isObject(currentValue) && !isEmpty(currentValue) || isBoolean(currentValue)) { | |
return false; | |
} | |
return isObjectRecursivelyEmpty(currentValue); | |
}); | |
return compact(result).length === objKeys.length; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('isObjectRecursivelyEmpty', () => { | |
it('', () => { | |
const mockObject = {}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(true); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: 123 | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(false); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: '123' | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(false); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: { | |
b: {}, | |
c: {}, | |
} | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(true); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: { | |
b: {}, | |
c: 123, | |
} | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(false); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: { | |
b: {}, | |
c: { | |
e: 123, | |
}, | |
} | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(false); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: { | |
b: {}, | |
c: { | |
e: undefined, | |
}, | |
} | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(true); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: { | |
b: {}, | |
c: { | |
e: null | |
}, | |
} | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(true); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: [1,2,3] | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(false); | |
}); | |
it('', () => { | |
const mockObject = { | |
a: [] | |
}; | |
expect(isObjectRecursivelyEmpty(mockObject)).toBe(true); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment