Skip to content

Commit

Permalink
fix: addressing minor issues related to naming, used lodash.isNil ins…
Browse files Browse the repository at this point in the history
…tead of identifying the valid values manually, added test-cases

Signed-off-by: Sai Sankeerth <[email protected]>
  • Loading branch information
Sai Sankeerth committed Nov 9, 2023
1 parent a825926 commit b782b90
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/helpers/geoLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ export default class GeoLocationHelper {
return {};
}
const { value: address, key: addressKey } = GeoLocationHelper.getAddressKeyAndValue(message);
const addressFieldMapping = {
const addressFieldToGeoFieldMap = {
city: 'city',
country: 'country',
postalCode: 'postal',
state: 'region',
};

const mappedAddress = Object.entries(addressFieldMapping).reduce(
(agg, [identifyAddressKey, geoKey]) => {
if (!address?.[identifyAddressKey] && msg?.context?.geo?.[geoKey]) {
return { [identifyAddressKey]: msg.context.geo[geoKey], ...agg };
const mappedAddress = Object.entries(addressFieldToGeoFieldMap).reduce(
(agg, [addressFieldKey, geoFieldKey]) => {
if (!address?.[addressFieldKey] && msg?.context?.geo?.[geoFieldKey]) {
return { [addressFieldKey]: msg.context.geo[geoFieldKey], ...agg };
}
return agg;
},
Expand Down
2 changes: 1 addition & 1 deletion src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ const getFirstMatchingKeyAndValue = (message, sourceKeys) => {
// eslint-disable-next-line no-restricted-syntax
for (const sourceKey of srcKeys) {
const val = get(message, sourceKey);
if (val || val === false || val === 0) {
if (!lodash.isNil(val)) {
// return only if the value is valid.
// else look for next possible source in precedence
return { value: val, key: sourceKey };
Expand Down
38 changes: 38 additions & 0 deletions test/utils/getFirstMatchingKeyAndValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,44 @@ const getFirstMatchKVCases = [
key: '1.c.d',
},
},
{
description: 'should get value as "0"',
input: {
message: {
player: 'Roger Federer',
sport: 'Tennis',
rivals: {
spain: 0,
serbia: 'Novak Djokovic',
switzerland: 'Stan Wawrinka',
},
},
sourceKeys: ['spain.rivals', 'rivals.spain'],
},
expectedOutput: {
value: 0,
key: 'rivals.spain',
},
},
{
description: 'show get value as false',
input: {
message: {
player: 'Roger Federer',
sport: 'Tennis',
rivals: {
spain: false,
serbia: 'Novak Djokovic',
switzerland: 'Stan Wawrinka',
},
},
sourceKeys: ['spain.rivals', 'rivals.spain'],
},
expectedOutput: {
value: false,
key: 'rivals.spain',
},
},
];

describe('getFirstMatchingKeyAndValue tests', () => {
Expand Down

0 comments on commit b782b90

Please sign in to comment.