@shopify/address
Advanced tools
Comparing version 3.1.1 to 3.2.0
@@ -9,4 +9,12 @@ 'use strict'; | ||
const ORDERED_COUNTRIES_CACHE = {}; | ||
const ORDERED_COUNTRIES_CACHE = new Map(); | ||
class AddressFormatter { | ||
/** | ||
* Useful in tests or any situation where the cache has undesirable | ||
* side-effects. | ||
*/ | ||
static resetCache() { | ||
ORDERED_COUNTRIES_CACHE.clear(); | ||
} | ||
constructor(locale) { | ||
@@ -25,10 +33,6 @@ this.locale = locale; | ||
return _rollupPluginBabelHelpers.asyncToGenerator(function* () { | ||
let country = _this.loadCountryFromCache(countryCode); | ||
const country = _this.loadCountryFromCache(countryCode); | ||
if (country) { | ||
return country; | ||
} | ||
country = yield loader.loadCountry(_this.locale, countryCode); | ||
return country; | ||
if (country) return country; | ||
return loader.loadCountry(_this.locale, countryCode); | ||
})(); | ||
@@ -41,4 +45,6 @@ } | ||
return _rollupPluginBabelHelpers.asyncToGenerator(function* () { | ||
const cachedCountries = ORDERED_COUNTRIES_CACHE.get(_this2.locale); | ||
if (cachedCountries) return cachedCountries; | ||
const countries = yield loader.loadCountries(_this2.locale); | ||
ORDERED_COUNTRIES_CACHE[_this2.locale] = countries; | ||
ORDERED_COUNTRIES_CACHE.set(_this2.locale, countries); | ||
return countries; | ||
@@ -93,9 +99,7 @@ })(); | ||
loadCountryFromCache(countryCode) { | ||
if (ORDERED_COUNTRIES_CACHE[this.locale]) { | ||
return ORDERED_COUNTRIES_CACHE[this.locale].find(country => { | ||
return country.code === countryCode; | ||
}); | ||
} | ||
return null; | ||
const cachedCountries = ORDERED_COUNTRIES_CACHE.get(this.locale); | ||
if (!cachedCountries) return null; | ||
return cachedCountries.find(({ | ||
code | ||
}) => code === countryCode); | ||
} | ||
@@ -102,0 +106,0 @@ |
@@ -12,2 +12,3 @@ 'use strict'; | ||
exports.CountryLoaderError = loader.CountryLoaderError; | ||
exports.loadCountries = loader.loadCountries; | ||
@@ -14,0 +15,0 @@ exports.loadCountry = loader.loadCountry; |
@@ -62,3 +62,2 @@ 'use strict'; | ||
}()); | ||
class CountryLoaderError extends Error { | ||
@@ -85,3 +84,4 @@ constructor(errors) { | ||
exports.CountryLoaderError = CountryLoaderError; | ||
exports.loadCountries = loadCountries; | ||
exports.loadCountry = loadCountry; |
import { Address, FieldName, Country } from '@shopify/address-consts'; | ||
export default class AddressFormatter { | ||
private locale; | ||
/** | ||
* Useful in tests or any situation where the cache has undesirable | ||
* side-effects. | ||
*/ | ||
static resetCache(): void; | ||
constructor(locale: string); | ||
@@ -5,0 +10,0 @@ updateLocale(locale: string): void; |
@@ -1,4 +0,7 @@ | ||
import { Country } from '@shopify/address-consts'; | ||
import { Country, ResponseError } from '@shopify/address-consts'; | ||
export declare const loadCountries: (locale: string) => Promise<Country[]>; | ||
export declare const loadCountry: (locale: string, countryCode: string) => Promise<Country>; | ||
export declare class CountryLoaderError extends Error { | ||
constructor(errors: ResponseError); | ||
} | ||
//# sourceMappingURL=loader.d.ts.map |
{ | ||
"name": "@shopify/address", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "description": "Address utilities for formatting addresses", | ||
"devDependencies": { | ||
"@shopify/address-mocks": "^2.0.18", | ||
"@shopify/address-mocks": "^2.1.0", | ||
"@shopify/jest-dom-mocks": "^3.0.16" | ||
@@ -52,3 +52,3 @@ }, | ||
}, | ||
"gitHead": "70feded397084daea784a8c5a523a61ac4fdb34c" | ||
"gitHead": "e79049dcb6087caaadfe3cf58205b98b0df285db" | ||
} |
@@ -24,3 +24,3 @@ # `@shopify/address` | ||
```typescript | ||
```ts | ||
import AddressFormatter from '@shopify/address'; | ||
@@ -88,2 +88,6 @@ | ||
#### `AddressFormatter.resetCache(): void` | ||
Resets the internal cache. Useful to avoid side-effects in test suite. | ||
### Sync API | ||
@@ -125,7 +129,11 @@ | ||
``` | ||
```ts | ||
import {fetch} from '@shopify/jest-dom-mocks'; | ||
import {mockCountryRequests} from '@shopify/address/tests'; | ||
import AddressFormatter from '@shopify/address'; | ||
beforeEach(mockCountryRequests); | ||
beforeEach(() => { | ||
AddressFormatter.resetCache(); // to avoid side-effects. | ||
mockCountryRequests(); | ||
}); | ||
afterEach(fetch.restore); | ||
@@ -132,0 +140,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41756
804
140