Skip to content

Instantly share code, notes, and snippets.

@andrewborisov
Last active June 25, 2019 11:25
Show Gist options
  • Save andrewborisov/3cb3c01f9517b048ec82719c37a3a0bd to your computer and use it in GitHub Desktop.
Save andrewborisov/3cb3c01f9517b048ec82719c37a3a0bd to your computer and use it in GitHub Desktop.
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;
}
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