不要怂,就是干,撸起袖子干!

Commit ecb08689 by Samuela Keller Committed by Sushant

fix(utils): flattenObject for null values (#10293)

1 parent c9b85241
Showing with 32 additions and 2 deletions
......@@ -399,8 +399,8 @@ function flattenObjectDeep(value) {
function flattenObject(obj, subPath) {
Object.keys(obj).forEach(key => {
const pathToProperty = subPath ? `${subPath}.${key}` : key;
if (typeof obj[key] === 'object') {
flattenObject(obj[key], flattenedObj, pathToProperty);
if (typeof obj[key] === 'object' && obj[key] !== null) {
flattenObject(obj[key], pathToProperty);
} else {
flattenedObj[pathToProperty] = _.get(obj, key);
}
......
......@@ -212,4 +212,34 @@ describe(Support.getTestDialectTeaser('Utils'), () => {
});
}
});
describe('flattenObjectDeep', () => {
it('should return the value if it is not an object', () => {
const value = 'non-object';
const returnedValue = Utils.flattenObjectDeep(value);
expect(returnedValue).to.equal(value);
});
it('should return correctly if values are null', () => {
const value = {
name: 'John',
address: {
street: 'Fake St. 123',
city: null,
coordinates: {
longitude: 55.6779627,
latitude: 12.5964313
}
}
};
const returnedValue = Utils.flattenObjectDeep(value);
expect(returnedValue).to.deep.equal({
name: 'John',
'address.street': 'Fake St. 123',
'address.city': null,
'address.coordinates.longitude': 55.6779627,
'address.coordinates.latitude': 12.5964313
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!