forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: moved geolocation utils from wccom to here (woocommerce#38356)
- Loading branch information
Showing
11 changed files
with
26,590 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: dev | ||
|
||
Moved geolocation country matching functions to @woocommerce/onboarding |
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,4 @@ | ||
{ | ||
"rootDir": "./src", | ||
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" | ||
} |
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
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,79 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import stringSimilarity from 'string-similarity'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getMappingRegion } from './location-mapping'; | ||
|
||
/** | ||
* Country state option. | ||
*/ | ||
export type CountryStateOption = { | ||
key: string; | ||
label: string; | ||
}; | ||
|
||
export type Location = { | ||
country_short?: string; | ||
region?: string; | ||
city?: string; | ||
}; | ||
|
||
/** | ||
* Returns a country option for the given location. | ||
*/ | ||
export const findCountryOption = ( | ||
countryStateOptions: CountryStateOption[], | ||
location: Location | undefined, | ||
minimumSimilarity = 0.7 | ||
) => { | ||
if ( ! location ) { | ||
return null; | ||
} | ||
|
||
let match = null; | ||
let matchSimilarity = minimumSimilarity; | ||
// eslint-disable-next-line @wordpress/no-unused-vars-before-return -- don't want to put this inside the loop | ||
const mappingRegion = getMappingRegion( location ); | ||
|
||
for ( const option of countryStateOptions ) { | ||
// Country matches exactly. | ||
if ( option.key === location.country_short ) { | ||
return option; | ||
} | ||
|
||
// Countries have regions such as 'US:CA'. | ||
const countryCode = option.key.split( ':' )[ 0 ]; | ||
if ( | ||
countryCode === location.country_short && | ||
option.label.includes( '—' ) | ||
) { | ||
const wcRegion = option.label.split( '—' )[ 1 ].trim(); | ||
|
||
// Region matches exactly with mapping. | ||
if ( mappingRegion === wcRegion ) { | ||
return option; | ||
} | ||
|
||
// Find the region with the highest similarity. | ||
const similarity = Math.max( | ||
stringSimilarity.compareTwoStrings( | ||
wcRegion, | ||
location.region || '' | ||
), | ||
stringSimilarity.compareTwoStrings( | ||
wcRegion, | ||
location.city || '' | ||
) | ||
); | ||
if ( similarity >= matchSimilarity ) { | ||
match = option; | ||
matchSimilarity = similarity; | ||
} | ||
} | ||
} | ||
return match; | ||
}; |
58 changes: 58 additions & 0 deletions
58
packages/js/onboarding/src/utils/countries/location-mapping.ts
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,58 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Location } from '.'; | ||
|
||
/** | ||
* This file is used to map a location to a WC region label | ||
* so that we can find the correct country option | ||
* since WPCOM geolocation returns a different | ||
* region or city name. | ||
*/ | ||
|
||
// Key is the country code, value is an object with keys as region/city names and values as WC region labels. | ||
const MAPPING: Record< string, Record< string, string > > = { | ||
PH: { | ||
'National Capital Region': __( 'Metro Manila', 'woocommerce' ), | ||
}, | ||
IT: { | ||
Rome: __( 'Roma', 'woocommerce' ), | ||
}, | ||
}; | ||
|
||
/** | ||
* Returns a WC mapping region name for the given country, region and city. | ||
*/ | ||
export const getMappingRegion = ( { | ||
country_short: countryCode, | ||
region = '', | ||
city = '', | ||
}: Location ) => { | ||
if ( ! countryCode ) { | ||
return null; | ||
} | ||
|
||
const countryMapping = MAPPING[ countryCode ]; | ||
if ( ! countryMapping ) { | ||
return null; | ||
} | ||
|
||
const regionMapping = countryMapping[ region ]; | ||
if ( regionMapping ) { | ||
return regionMapping; | ||
} | ||
|
||
const cityMapping = countryMapping[ city ]; | ||
if ( cityMapping ) { | ||
return cityMapping; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export default MAPPING; |
Oops, something went wrong.