44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7- import { Address , AddressValidationResponse , Granularity , ValidationResult } from '../utils/googlemaps_types.js' ;
7+ import { Address , AddressValidation , Granularity } from '../utils/googlemaps_types.js' ;
88
99
1010/** Suggested action to take for this validation result. */
@@ -32,8 +32,8 @@ function isUSA(address: Address): boolean {
3232 return address . postalAddress ?. regionCode === 'US' ;
3333}
3434
35- function isMissingNonSubpremiseComponent ( result : ValidationResult ) : boolean {
36- const missingComponents = result . address . missingComponentTypes || [ ] ;
35+ function isMissingNonSubpremiseComponent ( result : AddressValidation ) : boolean {
36+ const missingComponents = result . address ? .missingComponentTypes || [ ] ;
3737 return ( missingComponents . length > 1 ) ||
3838 ( ( missingComponents . length === 1 ) &&
3939 ( missingComponents [ 0 ] !== SUBPREMISE ) ) ;
@@ -44,47 +44,53 @@ function isMissingNonSubpremiseComponent(result: ValidationResult): boolean {
4444 * `ROUTE` level. `PREMISE`, `SUBPREMISE`, and `PREMISE_PROXIMITY` are all
4545 * considered as good as `ROUTE` or better.
4646 */
47- function hasValidationGranularityOther ( result : ValidationResult ) : boolean {
47+ function hasValidationGranularityOther ( result : AddressValidation ) : boolean {
4848 return ! result . verdict ?. validationGranularity ||
4949 result . verdict . validationGranularity === Granularity . OTHER ;
5050}
5151
52- function hasSuspiciousComponent ( result : ValidationResult ) : boolean {
53- return result . address . addressComponents . some (
54- c => c . confirmationLevel === 'UNCONFIRMED_AND_SUSPICIOUS' ) ;
52+ function hasSuspiciousComponent ( result : AddressValidation ) : boolean {
53+ return ! ! ( result . address ?. components . some (
54+ c => c . confirmationLevel === 'UNCONFIRMED_AND_SUSPICIOUS' ) ) ;
5555}
5656
57- function hasUnresolvedToken ( result : ValidationResult ) : boolean {
58- return ( result . address . unresolvedTokens || [ ] ) . length > 0 ;
57+ function hasUnresolvedToken ( result : AddressValidation ) : boolean {
58+ return ! ! result . address &&
59+ ( result . address . unresolvedTokens || [ ] ) . length > 0 ;
5960}
6061
6162/**
6263 * Returns true if the result has an inference for a component other than the
6364 * postal code, administrative area (1, 2, or 3), or country.
6465 */
65- function hasMajorInference ( result : ValidationResult ) : boolean {
66+ function hasMajorInference ( result : AddressValidation ) : boolean {
6667 const minorComponents = new Set ( [
6768 POSTAL_CODE , POSTAL_CODE_SUFFIX , ADMINISTRATIVE_AREA_LEVEL_1 ,
6869 ADMINISTRATIVE_AREA_LEVEL_2 , ADMINISTRATIVE_AREA_LEVEL_3 , COUNTRY
6970 ] ) ;
70- return result . address . addressComponents . some (
71- c => c . isInferred && ! minorComponents . has ( c . componentType ) ) ;
71+ return ! ! result . address &&
72+ result . address . components . some (
73+ c => c . isInferred && ! minorComponents . has ( c . componentType ) )
7274}
7375
74- function hasReplacement ( result : ValidationResult ) : boolean {
76+ function hasReplacement ( result : AddressValidation ) : boolean {
7577 return ! ! result . verdict ?. hasReplacedComponents ;
7678}
7779
7880/**
7981 * Returns true if this is a US address that is missing a subpremise component
8082 * (and nothing else).
8183 */
82- function isMissingExactlyUSASubpremise ( result : ValidationResult ) : boolean {
83- return isUSA ( result . address ) &&
84+ function isMissingExactlyUSASubpremise ( result : AddressValidation ) : boolean {
85+ return ! ! result . address && isUSA ( result . address ) &&
8486 ( result . address . missingComponentTypes ?. length === 1 ) &&
8587 ( result . address . missingComponentTypes [ 0 ] === SUBPREMISE ) ;
8688}
8789
90+ function isIncompleteResult ( { verdict, address} : AddressValidation ) : boolean {
91+ return ! verdict || ! address ;
92+ }
93+
8894/**
8995 * This is a JavaScript function that analyzes an Address Validation API
9096 * response and outputs a single recommended follow-up action you should take
@@ -137,18 +143,19 @@ function isMissingExactlyUSASubpremise(result: ValidationResult): boolean {
137143 * @param response - A response object from the Address Validation API in the
138144 * Maps JS SDK.
139145 */
140- export function suggestValidationAction ( response : AddressValidationResponse ) :
146+ export function suggestValidationAction ( response : AddressValidation ) :
141147 ValidationSuggestion {
142- const result = response . result ;
143- if ( isMissingNonSubpremiseComponent ( result ) ||
144- hasValidationGranularityOther ( result ) || hasSuspiciousComponent ( result ) ||
145- hasUnresolvedToken ( result ) ) {
148+ if ( isIncompleteResult ( response ) ||
149+ isMissingNonSubpremiseComponent ( response ) ||
150+ hasValidationGranularityOther ( response ) ||
151+ hasSuspiciousComponent ( response ) || hasUnresolvedToken ( response ) ) {
146152 return { suggestedAction : SuggestedAction . FIX } ;
147- } else if ( hasMajorInference ( result ) || hasReplacement ( result ) ) {
153+ }
154+ if ( hasMajorInference ( response ) || hasReplacement ( response ) ) {
148155 return { suggestedAction : SuggestedAction . CONFIRM } ;
149- } else if ( isMissingExactlyUSASubpremise ( result ) ) {
150- return { suggestedAction : SuggestedAction . ADD_SUBPREMISES } ;
151- } else {
152- return { suggestedAction : SuggestedAction . ACCEPT } ;
153156 }
157+ if ( isMissingExactlyUSASubpremise ( response ) ) {
158+ return { suggestedAction : SuggestedAction . ADD_SUBPREMISES } ;
159+ }
160+ return { suggestedAction : SuggestedAction . ACCEPT } ;
154161}
0 commit comments