-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use sublocality when locality is missing - AddressSearch #5950
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
40ed45d
Use sublocality when locality is missing
aldo-expensify b07aa79
Add check on missing zip code and state
aldo-expensify 81317d4
Add console.debug when address result is invalid
aldo-expensify ef09215
Log bad addresses to server
aldo-expensify ae984be
Refactor AddressSearch functions and add tests
aldo-expensify 605a6aa
Merge branch 'main' of github.com:Expensify/App into aldo_sublocality…
aldo-expensify d49068f
Reorganize exports
aldo-expensify 13d91cc
Add [AddressSearch] to Log.hmmm
aldo-expensify 23ad37f
Add documentation to GooglePlacesUtils.js
aldo-expensify 0c844d9
Rename address component validation function
aldo-expensify f14b2ec
Merge branch 'main' of github.com:Expensify/App into aldo_sublocality…
aldo-expensify 62d6055
Add missing import
aldo-expensify File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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 {String} type | ||
* @param {String} key | ||
* @returns {String|undefined} | ||
*/ | ||
function getAddressComponent(addressComponents, type, key) { | ||
return _.chain(addressComponents) | ||
.find(component => _.contains(component.types, type)) | ||
.get(key) | ||
.value(); | ||
} | ||
|
||
/** | ||
* Validates this contains the minimum address components | ||
* | ||
* @param {Array} addressComponents | ||
* @returns {Boolean} | ||
*/ | ||
function isAddressValidForVBA(addressComponents) { | ||
if (!addressComponents) { | ||
return false; | ||
} | ||
if (!_.some(addressComponents, component => _.includes(component.types, 'street_number'))) { | ||
// Missing Street number | ||
return false; | ||
} | ||
if (!_.some(addressComponents, component => _.includes(component.types, 'postal_code'))) { | ||
// Missing zip code | ||
return false; | ||
} | ||
if (!_.some(addressComponents, component => _.includes(component.types, 'administrative_area_level_1'))) { | ||
// Missing state | ||
return false; | ||
} | ||
if (!_.some(addressComponents, component => _.includes(component.types, 'locality')) | ||
&& !_.some(addressComponents, component => _.includes(component.types, 'sublocality'))) { | ||
// Missing city | ||
return false; | ||
} | ||
if (_.some(addressComponents, component => _.includes(component.types, 'post_box'))) { | ||
// Reject PO box | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
export { | ||
getAddressComponent, | ||
isAddressValidForVBA, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import {getAddressComponent, isAddressValidForVBA} from '../../src/libs/GooglePlacesUtils'; | ||
|
||
describe('GooglePlacesUtilsTest', () => { | ||
describe('isAddressValidForVBA', () => { | ||
it('should reject Google Places result with missing street number', () => { | ||
// This result appears when searching for "25220 Quail Ridge Road, Escondido, CA, 97027" | ||
const googlePlacesRouteResult = { | ||
address_components: [ | ||
{ | ||
long_name: 'Quail Ridge Road', | ||
short_name: 'Quail Ridge Rd', | ||
types: ['route'], | ||
}, | ||
{ | ||
long_name: 'Escondido', | ||
short_name: 'Escondido', | ||
types: ['locality', 'political'], | ||
}, | ||
{ | ||
long_name: 'San Diego County', | ||
short_name: 'San Diego County', | ||
types: ['administrative_area_level_2', 'political'], | ||
}, | ||
{ | ||
long_name: 'California', | ||
short_name: 'CA', | ||
types: ['administrative_area_level_1', 'political'], | ||
}, | ||
{ | ||
long_name: 'United States', | ||
short_name: 'US', | ||
types: ['country', 'political'], | ||
}, | ||
{ | ||
long_name: '92027', | ||
short_name: '92027', | ||
types: ['postal_code'], | ||
}, | ||
], | ||
formatted_address: 'Quail Ridge Rd, Escondido, CA 92027, USA', | ||
place_id: 'EihRdWFpbCBSaWRnZSBSZCwgRXNjb25kaWRvLCBDQSA5MjAyNywgVVNBIi4qLAoUChIJIQBiT7Pz24ARmaXMgCMhqAUSFAoSCXtDwoFe89uAEd_FlncPyNEB', | ||
types: ['route'], | ||
}; | ||
const isValid = isAddressValidForVBA(googlePlacesRouteResult.address_components); | ||
expect(isValid).toStrictEqual(false); | ||
}); | ||
|
||
it('should accept Google Places result with missing locality if sublocality is available', () => { | ||
// This result appears when searching for "64 Noll Street, Brooklyn, NY, USA" | ||
const brooklynAddressResult = { | ||
address_components: [ | ||
{ | ||
long_name: '64', | ||
short_name: '64', | ||
types: ['street_number'], | ||
}, | ||
{ | ||
long_name: 'Noll Street', | ||
short_name: 'Noll St', | ||
types: ['route'], | ||
}, | ||
{ | ||
long_name: 'Bushwick', | ||
short_name: 'Bushwick', | ||
types: ['neighborhood', 'political'], | ||
}, | ||
{ | ||
long_name: 'Brooklyn', | ||
short_name: 'Brooklyn', | ||
types: ['sublocality_level_1', 'sublocality', 'political'], | ||
}, | ||
{ | ||
long_name: 'Kings County', | ||
short_name: 'Kings County', | ||
types: ['administrative_area_level_2', 'political'], | ||
}, | ||
{ | ||
long_name: 'New York', | ||
short_name: 'NY', | ||
types: ['administrative_area_level_1', 'political'], | ||
}, | ||
{ | ||
long_name: 'United States', | ||
short_name: 'US', | ||
types: ['country', 'political'], | ||
}, | ||
{ | ||
long_name: '11206', | ||
short_name: '11206', | ||
types: ['postal_code'], | ||
}, | ||
{ | ||
long_name: '4604', | ||
short_name: '4604', | ||
types: ['postal_code_suffix'], | ||
}, | ||
], | ||
formatted_address: '64 Noll St, Brooklyn, NY 11206, USA', | ||
// eslint-disable-next-line max-len | ||
place_id: 'EiM2NCBOb2xsIFN0LCBCcm9va2x5biwgTlkgMTEyMDYsIFVTQSJQEk4KNAoyCReOha8HXMKJETjOQzBxX7M3Gh4LEO7B7qEBGhQKEgmJzguI-VvCiRFYR8sAAcN5KAwQQCoUChIJH0FG4AZcwokRvrvwkhWA_6A', | ||
types: ['street_address'], | ||
}; | ||
const isValid = isAddressValidForVBA(brooklynAddressResult.address_components); | ||
expect(isValid).toStrictEqual(true); | ||
}); | ||
}); | ||
describe('getAddressComponent', () => { | ||
it('should find address components by type', () => { | ||
const addressComponents = [ | ||
{ | ||
long_name: 'Bushwick', | ||
short_name: 'Bushwick', | ||
types: ['neighborhood', 'political'], | ||
}, | ||
{ | ||
long_name: 'Brooklyn', | ||
short_name: 'Brooklyn', | ||
types: ['sublocality_level_1', 'sublocality', 'political'], | ||
}, | ||
{ | ||
long_name: 'New York', | ||
short_name: 'NY', | ||
types: ['administrative_area_level_1', 'political'], | ||
}, | ||
{ | ||
long_name: 'United States', | ||
short_name: 'US', | ||
types: ['country', 'political'], | ||
}, | ||
{ | ||
long_name: '11206', | ||
short_name: '11206', | ||
types: ['postal_code'], | ||
}, | ||
]; | ||
expect(getAddressComponent(addressComponents, 'sublocality', 'long_name')).toStrictEqual('Brooklyn'); | ||
expect(getAddressComponent(addressComponents, 'administrative_area_level_1', 'short_name')).toStrictEqual('NY'); | ||
expect(getAddressComponent(addressComponents, 'postal_code', 'long_name')).toStrictEqual('11206'); | ||
expect(getAddressComponent(addressComponents, 'doesn-exist', 'long_name')).toStrictEqual(undefined); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left this here even if it not used in the tests just as a way to remember these addresses that gave up problems.