From 6a13230f6a642186a6711ffa67924c7369a9a86d Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Mon, 4 Sep 2023 11:59:56 +0200 Subject: [PATCH 1/5] ts migration googleplacesutils --- ...glePlacesUtils.js => GooglePlacesUtils.ts} | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) rename src/libs/{GooglePlacesUtils.js => GooglePlacesUtils.ts} (71%) diff --git a/src/libs/GooglePlacesUtils.js b/src/libs/GooglePlacesUtils.ts similarity index 71% rename from src/libs/GooglePlacesUtils.js rename to src/libs/GooglePlacesUtils.ts index 8723598264fb..fb44382cbe14 100644 --- a/src/libs/GooglePlacesUtils.js +++ b/src/libs/GooglePlacesUtils.ts @@ -1,5 +1,13 @@ import _ from 'underscore'; +type AddressComponent = { + // eslint-disable-next-line @typescript-eslint/naming-convention + long_name: string; + // eslint-disable-next-line @typescript-eslint/naming-convention + short_name: string; + types: string[]; +}; + /** * Finds an address component by type, and returns the value associated to key. Each address component object * inside the addressComponents array has the following structure: @@ -8,16 +16,12 @@ import _ from 'underscore'; * short_name: "New York", * types: [ "locality", "political" ] * }] - * - * @param {Array} addressComponents - * @param {Object} fieldsToExtract – has shape: {addressType: 'keyToUse'} - * @returns {Object} */ -function getAddressComponents(addressComponents, fieldsToExtract) { +function getAddressComponents(addressComponents: AddressComponent[], fieldsToExtract: Record) { const result = _.mapObject(fieldsToExtract, () => ''); - _.each(addressComponents, (addressComponent) => { - _.each(addressComponent.types, (addressType) => { - if (!_.has(fieldsToExtract, addressType) || !_.isEmpty(result[addressType])) { + addressComponents.forEach((addressComponent) => { + addressComponent.types.forEach((addressType) => { + if (!(addressType in fieldsToExtract) || addressType in result) { return; } const value = addressComponent[fieldsToExtract[addressType]] ? addressComponent[fieldsToExtract[addressType]] : ''; From 2d3dd69da1ead8ec2b97ebc7be57bb28ffa5cc98 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Tue, 5 Sep 2023 12:21:58 +0200 Subject: [PATCH 2/5] [TS migration] Migrate 'GooglePlacesUtils.js' lib to TypeScript --- src/libs/GooglePlacesUtils.ts | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/libs/GooglePlacesUtils.ts b/src/libs/GooglePlacesUtils.ts index fb44382cbe14..de5379b27f97 100644 --- a/src/libs/GooglePlacesUtils.ts +++ b/src/libs/GooglePlacesUtils.ts @@ -1,5 +1,3 @@ -import _ from 'underscore'; - type AddressComponent = { // eslint-disable-next-line @typescript-eslint/naming-convention long_name: string; @@ -17,11 +15,13 @@ type AddressComponent = { * types: [ "locality", "political" ] * }] */ -function getAddressComponents(addressComponents: AddressComponent[], fieldsToExtract: Record) { - const result = _.mapObject(fieldsToExtract, () => ''); +function getAddressComponents(addressComponents: AddressComponent[], fieldsToExtract: Record>): Record { + const result: Record = {}; + Object.keys(fieldsToExtract).forEach((key) => (result[key] = '')); + addressComponents.forEach((addressComponent) => { addressComponent.types.forEach((addressType) => { - if (!(addressType in fieldsToExtract) || addressType in result) { + if (!(addressType in fieldsToExtract) || !(addressType in result && result[addressType] === '')) { return; } const value = addressComponent[fieldsToExtract[addressType]] ? addressComponent[fieldsToExtract[addressType]] : ''; @@ -31,25 +31,22 @@ function getAddressComponents(addressComponents: AddressComponent[], fieldsToExt return result; } +type AddressTerm = {value: string}; +type GetPlaceAutocompleteTermsResultKeys = 'country' | 'state' | 'city' | 'street'; +type GetPlaceAutocompleteTermsResult = Partial>; + /** * Finds an address term by type, and returns the value associated to key. Note that each term in the address must * conform to the following ORDER: - * - * @param {Array} addressTerms - * @returns {Object} */ -function getPlaceAutocompleteTerms(addressTerms) { +function getPlaceAutocompleteTerms(addressTerms: AddressTerm[]): GetPlaceAutocompleteTermsResult { const fieldsToExtract = ['country', 'state', 'city', 'street']; - const result = {}; - _.each(fieldsToExtract, (fieldToExtract, index) => { + const result: GetPlaceAutocompleteTermsResult = {}; + fieldsToExtract.forEach((fieldToExtract, index) => { const fieldTermIndex = addressTerms.length - (index + 1); - result[fieldToExtract] = fieldTermIndex >= 0 ? addressTerms[fieldTermIndex].value : ''; + result[fieldToExtract as GetPlaceAutocompleteTermsResultKeys] = fieldTermIndex >= 0 ? addressTerms[fieldTermIndex].value : ''; }); return result; } -export { - // eslint-disable-next-line import/prefer-default-export - getAddressComponents, - getPlaceAutocompleteTerms, -}; +export {getAddressComponents, getPlaceAutocompleteTerms}; From f6b49a51f4c2ad7de78259a0bce2f87a8aa8de6d Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Tue, 5 Sep 2023 12:39:10 +0200 Subject: [PATCH 3/5] Remove assertion --- src/libs/GooglePlacesUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/GooglePlacesUtils.ts b/src/libs/GooglePlacesUtils.ts index de5379b27f97..1ffceedc0267 100644 --- a/src/libs/GooglePlacesUtils.ts +++ b/src/libs/GooglePlacesUtils.ts @@ -40,11 +40,11 @@ type GetPlaceAutocompleteTermsResult = Partial */ function getPlaceAutocompleteTerms(addressTerms: AddressTerm[]): GetPlaceAutocompleteTermsResult { - const fieldsToExtract = ['country', 'state', 'city', 'street']; + const fieldsToExtract: GetPlaceAutocompleteTermsResultKeys[] = ['country', 'state', 'city', 'street']; const result: GetPlaceAutocompleteTermsResult = {}; fieldsToExtract.forEach((fieldToExtract, index) => { const fieldTermIndex = addressTerms.length - (index + 1); - result[fieldToExtract as GetPlaceAutocompleteTermsResultKeys] = fieldTermIndex >= 0 ? addressTerms[fieldTermIndex].value : ''; + result[fieldToExtract] = fieldTermIndex >= 0 ? addressTerms[fieldTermIndex].value : ''; }); return result; } From 2640751b335a26fba9f352d124077bc9786da52c Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Tue, 5 Sep 2023 12:41:28 +0200 Subject: [PATCH 4/5] Move FieldsToExtract into seperate type --- src/libs/GooglePlacesUtils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/GooglePlacesUtils.ts b/src/libs/GooglePlacesUtils.ts index 1ffceedc0267..433fbd5fd318 100644 --- a/src/libs/GooglePlacesUtils.ts +++ b/src/libs/GooglePlacesUtils.ts @@ -5,6 +5,7 @@ type AddressComponent = { short_name: string; types: string[]; }; +type FieldsToExtract = Record>; /** * Finds an address component by type, and returns the value associated to key. Each address component object @@ -15,7 +16,7 @@ type AddressComponent = { * types: [ "locality", "political" ] * }] */ -function getAddressComponents(addressComponents: AddressComponent[], fieldsToExtract: Record>): Record { +function getAddressComponents(addressComponents: AddressComponent[], fieldsToExtract: FieldsToExtract): Record { const result: Record = {}; Object.keys(fieldsToExtract).forEach((key) => (result[key] = '')); From 576071709afa26f353aab54f772aa7d1a3e8fcd5 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Mon, 11 Sep 2023 09:53:29 +0200 Subject: [PATCH 5/5] Rename GetPlaceAutocompleteTermsResultKeys to GetPlaceAutocompleteTermsResultKey --- src/libs/GooglePlacesUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/GooglePlacesUtils.ts b/src/libs/GooglePlacesUtils.ts index 433fbd5fd318..43fac2b9a785 100644 --- a/src/libs/GooglePlacesUtils.ts +++ b/src/libs/GooglePlacesUtils.ts @@ -33,15 +33,15 @@ function getAddressComponents(addressComponents: AddressComponent[], fieldsToExt } type AddressTerm = {value: string}; -type GetPlaceAutocompleteTermsResultKeys = 'country' | 'state' | 'city' | 'street'; -type GetPlaceAutocompleteTermsResult = Partial>; +type GetPlaceAutocompleteTermsResultKey = 'country' | 'state' | 'city' | 'street'; +type GetPlaceAutocompleteTermsResult = Partial>; /** * Finds an address term by type, and returns the value associated to key. Note that each term in the address must * conform to the following ORDER: */ function getPlaceAutocompleteTerms(addressTerms: AddressTerm[]): GetPlaceAutocompleteTermsResult { - const fieldsToExtract: GetPlaceAutocompleteTermsResultKeys[] = ['country', 'state', 'city', 'street']; + const fieldsToExtract: GetPlaceAutocompleteTermsResultKey[] = ['country', 'state', 'city', 'street']; const result: GetPlaceAutocompleteTermsResult = {}; fieldsToExtract.forEach((fieldToExtract, index) => { const fieldTermIndex = addressTerms.length - (index + 1);