From b782b906721d063ec2918a023b28fd54fa3c0de8 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 9 Nov 2023 18:24:56 +0530 Subject: [PATCH] fix: addressing minor issues related to naming, used lodash.isNil instead of identifying the valid values manually, added test-cases Signed-off-by: Sai Sankeerth --- src/helpers/geoLocation.ts | 10 ++--- src/v0/util/index.js | 2 +- .../utils/getFirstMatchingKeyAndValue.test.ts | 38 +++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/helpers/geoLocation.ts b/src/helpers/geoLocation.ts index eb010ea3b9..b1f1c8b455 100644 --- a/src/helpers/geoLocation.ts +++ b/src/helpers/geoLocation.ts @@ -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; }, diff --git a/src/v0/util/index.js b/src/v0/util/index.js index 5d1ea6adeb..da7a3ded14 100644 --- a/src/v0/util/index.js +++ b/src/v0/util/index.js @@ -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 }; diff --git a/test/utils/getFirstMatchingKeyAndValue.test.ts b/test/utils/getFirstMatchingKeyAndValue.test.ts index 84191066cd..8481dd1ba2 100644 --- a/test/utils/getFirstMatchingKeyAndValue.test.ts +++ b/test/utils/getFirstMatchingKeyAndValue.test.ts @@ -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', () => {