@shopify/address
Advanced tools
Comparing version 3.0.16 to 3.1.0
@@ -6,9 +6,5 @@ 'use strict'; | ||
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js'); | ||
var utilities = require('./utilities.js'); | ||
var format = require('./format.js'); | ||
var loader = require('./loader.js'); | ||
const FIELD_REGEXP = /({\w+})/g; | ||
const LINE_DELIMITER = '_'; | ||
const DEFAULT_FORM_LAYOUT = '{firstName}{lastName}_{company}_{address1}_{address2}_{city}_{country}{province}{zip}_{phone}'; | ||
const DEFAULT_SHOW_LAYOUT = '{lastName} {firstName}_{company}_{address1} {address2}_{city} {province} {zip}_{country}_{phone}'; | ||
const ORDERED_COUNTRIES_CACHE = {}; | ||
@@ -68,4 +64,3 @@ class AddressFormatter { | ||
const country = yield _this3.getCountry(address.country); | ||
const layout = country.formatting.show || DEFAULT_SHOW_LAYOUT; | ||
return layout.split(LINE_DELIMITER).map(fields => utilities.renderLineTemplate(country, fields, address).trim()); | ||
return format.formatAddress(address, country); | ||
})(); | ||
@@ -92,14 +87,3 @@ } | ||
const country = yield _this4.getCountry(countryCode); | ||
const format = country ? country.formatting.edit : DEFAULT_FORM_LAYOUT; | ||
return format.split(LINE_DELIMITER).map(fields => { | ||
const result = fields.match(FIELD_REGEXP); | ||
if (!result) { | ||
return []; | ||
} | ||
return result.map(field => { | ||
return utilities.FIELDS_MAPPING[field]; | ||
}); | ||
}); | ||
return format.buildOrderedFields(country); | ||
})(); | ||
@@ -106,0 +90,0 @@ } |
@@ -7,2 +7,3 @@ 'use strict'; | ||
var loader = require('./loader.js'); | ||
var format = require('./format.js'); | ||
var AddressFormatter = require('./AddressFormatter.js'); | ||
@@ -14,2 +15,4 @@ | ||
exports.loadCountry = loader.loadCountry; | ||
exports.buildOrderedFields = format.buildOrderedFields; | ||
exports.formatAddress = format.formatAddress; | ||
exports["default"] = AddressFormatter["default"]; | ||
@@ -16,0 +19,0 @@ Object.keys(addressConsts).forEach(function (k) { |
@@ -70,2 +70,3 @@ 'use strict'; | ||
exports.FIELDS_MAPPING = FIELDS_MAPPING; | ||
exports.FIELD_REGEXP = FIELD_REGEXP; | ||
exports.renderLineTemplate = renderLineTemplate; |
export * from '@shopify/address-consts'; | ||
export * from './loader'; | ||
export * from './format'; | ||
export { default } from './AddressFormatter'; | ||
//# sourceMappingURL=index.d.ts.map |
import { Address, FieldName, Country } from '@shopify/address-consts'; | ||
export declare const FIELD_REGEXP: RegExp; | ||
export declare const FIELDS_MAPPING: { | ||
@@ -3,0 +4,0 @@ [key: string]: FieldName; |
{ | ||
"name": "@shopify/address", | ||
"version": "3.0.16", | ||
"version": "3.1.0", | ||
"license": "MIT", | ||
@@ -26,4 +26,4 @@ "description": "Address utilities for formatting addresses", | ||
"devDependencies": { | ||
"@shopify/address-mocks": "^2.0.16", | ||
"@shopify/jest-dom-mocks": "^3.0.14" | ||
"@shopify/address-mocks": "^2.0.17", | ||
"@shopify/jest-dom-mocks": "^3.0.15" | ||
}, | ||
@@ -52,3 +52,3 @@ "sideEffects": false, | ||
}, | ||
"gitHead": "82524f19df95eab93f7dcd0a8535bc45562d85c8" | ||
"gitHead": "1fce581b2ea8be844addfa34924163c0b9c6405b" | ||
} |
100
README.md
@@ -20,46 +20,4 @@ # `@shopify/address` | ||
#### `constructor(private locale: string)` | ||
### `AddressFormatter` class | ||
Instantiate the AddressFormatter by passing it a locale. | ||
#### `updateLocale(locale: string)` | ||
Update the current locale of the formatter. Following requests will be in the given locale. | ||
#### `async .getCountry(countryCode: string): Promise<Country>` | ||
Loads and returns data about a given country in the current locale. Country and province names are localized. Province names are sorted based on the locale. | ||
#### `async .getCountries(): Promise<Country[]>` | ||
Loads and returns data for all countries in the current locale. Countries are sorted based on the locale. Zones are also ordered based on the locale. | ||
#### `async .getOrderedFields(countryCode): FieldName[][]` | ||
Returns how to order address fields. | ||
Eg.: | ||
```typescript | ||
[ | ||
['firstName', 'lastName'], | ||
['company'], | ||
['address1'], | ||
['address2'], | ||
['city'], | ||
['country', 'province', 'zip'], | ||
['phone'], | ||
]; | ||
``` | ||
#### `async .format(address: Address): string[]` | ||
Given an address, returns the address ordered for multiline rendering. e.g.: | ||
```typescript | ||
['Shopify', 'Lindenstraße 9-14', '10969 Berlin', 'Germany']; | ||
``` | ||
#### Example Usage | ||
Show an address: | ||
@@ -106,2 +64,58 @@ | ||
#### `constructor(private locale: string)` | ||
Instantiate the AddressFormatter by passing it a locale. | ||
#### `updateLocale(locale: string)` | ||
Update the current locale of the formatter. Following requests will be in the given locale. | ||
#### `async .getCountry(countryCode: string): Promise<Country>` | ||
Loads and returns data about a given country in the current locale. Country and province names are localized. Province names are sorted based on the locale. | ||
#### `async .getCountries(): Promise<Country[]>` | ||
Loads and returns data for all countries in the current locale. Countries are sorted based on the locale. Zones are also ordered based on the locale. | ||
#### `async .getOrderedFields(countryCode): Promise<FieldName[][]>` | ||
Returns how to order address fields for a country code. Fetches the country if not already cached. | ||
#### `async .format(address: Address): Promise<string[]>` | ||
Given an address, returns the address ordered for multiline rendering. Uses the `formatAddress` sync API in the background. | ||
### Sync API | ||
If you already have the input data ready, like a `Country` object, you can use the sync API to get the result right away. | ||
The following functions can be imported as stand-alone utilities. | ||
#### `formatAddress(address: Address, country: Country): string[]` | ||
Given an address and a country, returns the address ordered for multiline rendering. e.g.: | ||
```typescript | ||
['Shopify', 'Lindenstraße 9-14', '10969 Berlin', 'Germany']; | ||
``` | ||
#### `buildOrderedFields(country: Country): FieldName[][]` | ||
Returns how to order address fields for a specific country. | ||
Eg.: | ||
```typescript | ||
[ | ||
['firstName', 'lastName'], | ||
['company'], | ||
['address1'], | ||
['address2'], | ||
['city'], | ||
['country', 'province', 'zip'], | ||
['phone'], | ||
]; | ||
``` | ||
## Testing | ||
@@ -108,0 +122,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
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
40248
38
784
132