diff --git a/src/libs/GooglePlacesUtils.js b/src/libs/GooglePlacesUtils.js deleted file mode 100644 index 8723598264fb..000000000000 --- a/src/libs/GooglePlacesUtils.js +++ /dev/null @@ -1,51 +0,0 @@ -import _ from 'underscore'; - -/** - * 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: - * [{ - * long_name: "New York", - * short_name: "New York", - * types: [ "locality", "political" ] - * }] - * - * @param {Array} addressComponents - * @param {Object} fieldsToExtract – has shape: {addressType: 'keyToUse'} - * @returns {Object} - */ -function getAddressComponents(addressComponents, fieldsToExtract) { - const result = _.mapObject(fieldsToExtract, () => ''); - _.each(addressComponents, (addressComponent) => { - _.each(addressComponent.types, (addressType) => { - if (!_.has(fieldsToExtract, addressType) || !_.isEmpty(result[addressType])) { - return; - } - const value = addressComponent[fieldsToExtract[addressType]] ? addressComponent[fieldsToExtract[addressType]] : ''; - result[addressType] = value; - }); - }); - return result; -} - -/** - * 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) { - const fieldsToExtract = ['country', 'state', 'city', 'street']; - const result = {}; - _.each(fieldsToExtract, (fieldToExtract, index) => { - const fieldTermIndex = addressTerms.length - (index + 1); - result[fieldToExtract] = fieldTermIndex >= 0 ? addressTerms[fieldTermIndex].value : ''; - }); - return result; -} - -export { - // eslint-disable-next-line import/prefer-default-export - getAddressComponents, - getPlaceAutocompleteTerms, -}; diff --git a/src/libs/GooglePlacesUtils.ts b/src/libs/GooglePlacesUtils.ts new file mode 100644 index 000000000000..43fac2b9a785 --- /dev/null +++ b/src/libs/GooglePlacesUtils.ts @@ -0,0 +1,53 @@ +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[]; +}; +type FieldsToExtract = Record>; + +/** + * 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: + * [{ + * long_name: "New York", + * short_name: "New York", + * types: [ "locality", "political" ] + * }] + */ +function getAddressComponents(addressComponents: AddressComponent[], fieldsToExtract: FieldsToExtract): 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 && result[addressType] === '')) { + return; + } + const value = addressComponent[fieldsToExtract[addressType]] ? addressComponent[fieldsToExtract[addressType]] : ''; + result[addressType] = value; + }); + }); + return result; +} + +type AddressTerm = {value: string}; +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: GetPlaceAutocompleteTermsResultKey[] = ['country', 'state', 'city', 'street']; + const result: GetPlaceAutocompleteTermsResult = {}; + fieldsToExtract.forEach((fieldToExtract, index) => { + const fieldTermIndex = addressTerms.length - (index + 1); + result[fieldToExtract] = fieldTermIndex >= 0 ? addressTerms[fieldTermIndex].value : ''; + }); + return result; +} + +export {getAddressComponents, getPlaceAutocompleteTerms};