55 */
66
77import { AddressParser } from "@sroussey/parse-address" ;
8- import { Address } from "./AddressSchema" ;
9- import { COUNTRY_STATE_CODE_ARRAY , KEYWORD_STATE_OR_COUNTRY_MAP } from "./AddressSchemaCodes" ;
108import { hasCompanyEnding } from "../company/CompanyNormalization" ;
9+ import { Address , type CountryCode , type StateOrCountryCode } from "./AddressSchema" ;
10+ import { COUNTRY_STATE_CODE_ARRAY , KEYWORD_STATE_OR_COUNTRY_MAP } from "./AddressSchemaCodes" ;
1111
1212export type AddressImport = {
1313 // required fields
1414 city ?: string | null ;
15- stateOrCountry ?: string | null ;
15+ stateOrCountry ?: StateOrCountryCode | string | null ;
16+ countryCode ?: CountryCode | string | null ;
1617 // optional fields
1718 stateOrCountryDescription ?: string | null ;
1819 street1 ?: string | null ; // if street 1 is null, street 2 or 3 must have a value
1920 street2 ?: string | null ;
2021 street3 ?: string | null ;
2122 country ?: string | null ;
22- countryCode ?: string | null ;
2323 zipCode ?: string | null ;
2424 isForeignLocation ?: boolean | null ;
2525 foreignStateTerritory ?: string | null ;
@@ -245,18 +245,26 @@ export function normalizeAddress(importAddress: AddressImport | null): Address |
245245 let street3 = cleanStreet ( importAddress . street3 ) ;
246246 let zip = importAddress . zipCode ?. trim ( ) || null ;
247247 let city = importAddress . city ?. trim ( ) || null ;
248- let state_or_country = importAddress . stateOrCountry ?. trim ( ) || null ;
248+ let state_or_country : StateOrCountryCode | null =
249+ ( importAddress . stateOrCountry ?. trim ( ) as StateOrCountryCode | null ) || null ;
249250
250251 [ street1 , street2 , street3 ] = consolidateStreetAddresses ( street1 , street2 , street3 ) ;
251252
252253 state_or_country =
253- state_or_country || findCountryByKeyword ( [ street1 , street2 , street3 , city , state_or_country ] ) ;
254+ state_or_country ||
255+ ( findCountryByKeyword ( [
256+ street1 ,
257+ street2 ,
258+ street3 ,
259+ city ,
260+ state_or_country ,
261+ ] ) as StateOrCountryCode | null ) ;
254262
255263 let country_code = findCountryCode (
256264 state_or_country ,
257265 importAddress . country ,
258266 importAddress . countryCode
259- ) ;
267+ ) as CountryCode | null ;
260268
261269 city = normalizeCity ( city ?? "" ) ;
262270
@@ -278,8 +286,9 @@ export function normalizeAddress(importAddress: AddressImport | null): Address |
278286 street2 = importAddress . street2 || null ;
279287 street3 = importAddress . street3 || null ;
280288 city = city || importAddress . city || null ;
281- state_or_country = state_or_country || importAddress . stateOrCountry || null ;
282- country_code = country_code || importAddress . countryCode || null ;
289+ state_or_country =
290+ state_or_country || ( importAddress . stateOrCountry as StateOrCountryCode ) || null ;
291+ country_code = country_code || ( importAddress . countryCode as CountryCode ) || null ;
283292 zip = zip || importAddress . zipCode || null ;
284293 }
285294
0 commit comments