@shopify/address
Advanced tools
Comparing version 2.4.5 to 2.5.0
@@ -11,2 +11,8 @@ # Changelog | ||
## 2.5.0 - 2019-01-08 | ||
- Removed `address2Key`, `zoneKey` and `zipKey` | ||
- Expose `labels` on `Country` object that stores all translated labels needed to show a form. | ||
- Rename `Province to`Zone`on FieldName enum, and renamed`Province`interface to`Zone`. | ||
## 2.4.0 - 2018-10-11 | ||
@@ -13,0 +19,0 @@ |
@@ -10,4 +10,3 @@ import { Address, FieldName, Country } from './types'; | ||
getOrderedFields(countryCode: string): Promise<FieldName[][]>; | ||
getTranslationKey(countryCode: string, key: FieldName): Promise<string>; | ||
private loadCountryFromCache; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var types_1 = require("./types"); | ||
var utilities_1 = require("./utilities"); | ||
@@ -114,25 +113,2 @@ var loader_1 = require("./loader"); | ||
}; | ||
AddressFormatter.prototype.getTranslationKey = function (countryCode, key) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var country; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.getCountry(countryCode)]; | ||
case 1: | ||
country = _a.sent(); | ||
switch (key) { | ||
case types_1.FieldName.Province: | ||
return [2 /*return*/, country.provinceKey]; | ||
case types_1.FieldName.Zip: | ||
return [2 /*return*/, country.zipKey]; | ||
case types_1.FieldName.Address2: | ||
return [2 /*return*/, country.address2Key]; | ||
default: | ||
return [2 /*return*/, key]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
AddressFormatter.prototype.loadCountryFromCache = function (countryCode) { | ||
@@ -139,0 +115,0 @@ if (ORDERED_COUNTRIES_CACHE[this.locale]) { |
@@ -1,2 +0,2 @@ | ||
declare const query = "\nquery countries($locale: SupportedLocale!) {\n countries(locale: $locale) {\n name\n code\n phoneNumberPrefix\n address2Key\n provinceKey\n zipKey\n formatting {\n edit\n show\n }\n provinces {\n name\n code\n }\n }\n}\n\nquery country($countryCode: SupportedCountry!, $locale: SupportedLocale!) {\n country(countryCode: $countryCode, locale: $locale) {\n name\n code\n phoneNumberPrefix\n address2Key\n provinceKey\n zipKey\n formatting {\n edit\n show\n }\n provinces {\n name\n code\n }\n }\n}\n"; | ||
declare const query = "\nquery countries($locale: SupportedLocale!) {\n countries(locale: $locale) {\n name\n code\n continent\n phoneNumberPrefix\n labels {\n address1\n address2\n city\n company\n country\n firstName\n lastName\n phone\n postalCode\n zone\n }\n formatting {\n edit\n show\n }\n zones {\n name\n code\n }\n }\n}\n\nquery country($countryCode: SupportedCountry!, $locale: SupportedLocale!) {\n country(countryCode: $countryCode, locale: $locale) {\n name\n code\n continent\n phoneNumberPrefix\n labels {\n address1\n address2\n city\n company\n country\n firstName\n lastName\n phone\n postalCode\n zone\n }\n formatting {\n edit\n show\n }\n zones {\n name\n code\n }\n }\n}\n"; | ||
export default query; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var query = "\nquery countries($locale: SupportedLocale!) {\n countries(locale: $locale) {\n name\n code\n phoneNumberPrefix\n address2Key\n provinceKey\n zipKey\n formatting {\n edit\n show\n }\n provinces {\n name\n code\n }\n }\n}\n\nquery country($countryCode: SupportedCountry!, $locale: SupportedLocale!) {\n country(countryCode: $countryCode, locale: $locale) {\n name\n code\n phoneNumberPrefix\n address2Key\n provinceKey\n zipKey\n formatting {\n edit\n show\n }\n provinces {\n name\n code\n }\n }\n}\n"; | ||
var query = "\nquery countries($locale: SupportedLocale!) {\n countries(locale: $locale) {\n name\n code\n continent\n phoneNumberPrefix\n labels {\n address1\n address2\n city\n company\n country\n firstName\n lastName\n phone\n postalCode\n zone\n }\n formatting {\n edit\n show\n }\n zones {\n name\n code\n }\n }\n}\n\nquery country($countryCode: SupportedCountry!, $locale: SupportedLocale!) {\n country(countryCode: $countryCode, locale: $locale) {\n name\n code\n continent\n phoneNumberPrefix\n labels {\n address1\n address2\n city\n company\n country\n firstName\n lastName\n phone\n postalCode\n zone\n }\n formatting {\n edit\n show\n }\n zones {\n name\n code\n }\n }\n}\n"; | ||
exports.default = query; |
@@ -6,4 +6,4 @@ export declare enum FieldName { | ||
City = "city", | ||
Zip = "zip", | ||
Province = "province", | ||
PostalCode = "zip", | ||
Zone = "province", | ||
Address1 = "address1", | ||
@@ -14,5 +14,2 @@ Address2 = "address2", | ||
} | ||
export declare type ProvinceKey = 'COUNTY' | 'EMIRATE' | 'GOVERNORATE' | 'PREFECTURE' | 'PROVINCE' | 'REGION' | 'STATE_AND_TERRITORY' | 'STATE'; | ||
export declare type ZipKey = 'POSTAL_CODE' | 'POSTCODE' | 'PINCODE' | 'ZIP_CODE'; | ||
export declare type Address2Key = 'APT_SUITE_ETC' | 'APT_UNIT_NUMBER'; | ||
export interface Address { | ||
@@ -30,3 +27,3 @@ company?: string; | ||
} | ||
export interface Province { | ||
export interface Zone { | ||
code: string; | ||
@@ -48,6 +45,16 @@ name: string; | ||
code: string; | ||
continent: string; | ||
phoneNumberPrefix: number; | ||
address2Key: Address2Key; | ||
provinceKey: ProvinceKey; | ||
zipKey: ZipKey; | ||
labels: { | ||
address1: string; | ||
address2: string; | ||
city: string; | ||
company: string; | ||
country: string; | ||
firstName: string; | ||
lastName: string; | ||
phone: string; | ||
postalCode: string; | ||
zone: string; | ||
}; | ||
formatting: { | ||
@@ -57,3 +64,3 @@ edit: string; | ||
}; | ||
provinces: Province[]; | ||
zones: Zone[]; | ||
} | ||
@@ -60,0 +67,0 @@ export interface ResponseError { |
@@ -9,4 +9,4 @@ "use strict"; | ||
FieldName["City"] = "city"; | ||
FieldName["Zip"] = "zip"; | ||
FieldName["Province"] = "province"; | ||
FieldName["PostalCode"] = "zip"; | ||
FieldName["Zone"] = "province"; | ||
FieldName["Address1"] = "address1"; | ||
@@ -13,0 +13,0 @@ FieldName["Address2"] = "address2"; |
@@ -10,4 +10,4 @@ "use strict"; | ||
'{city}': types_1.FieldName.City, | ||
'{zip}': types_1.FieldName.Zip, | ||
'{province}': types_1.FieldName.Province, | ||
'{zip}': types_1.FieldName.PostalCode, | ||
'{province}': types_1.FieldName.Zone, | ||
'{address1}': types_1.FieldName.Address1, | ||
@@ -37,6 +37,4 @@ '{address2}': types_1.FieldName.Address2, | ||
break; | ||
case types_1.FieldName.Province: | ||
line = line.replace("{" + types_1.FieldName.Province + "}", address.province | ||
? getProvince(country.provinces, address.province).name | ||
: ''); | ||
case types_1.FieldName.Zone: | ||
line = line.replace("{" + types_1.FieldName.Zone + "}", address.province ? getZone(country.zones, address.province).name : ''); | ||
break; | ||
@@ -52,8 +50,8 @@ default: | ||
else { | ||
return line.trim(); | ||
return line.trim().replace(' ', ' '); | ||
} | ||
} | ||
exports.renderLineTemplate = renderLineTemplate; | ||
function getProvince(provinces, provinceCode) { | ||
return (provinces.find(function (province) { return province.code === provinceCode; }) || { | ||
function getZone(zones, zoneCode) { | ||
return (zones.find(function (zone) { return zone.code === zoneCode; }) || { | ||
name: '', | ||
@@ -60,0 +58,0 @@ code: '', |
{ | ||
"name": "@shopify/address", | ||
"version": "2.4.5", | ||
"version": "2.5.0", | ||
"license": "MIT", | ||
@@ -26,7 +26,10 @@ "description": "Address utilities for formatting addresses.", | ||
"devDependencies": { | ||
"@shopify/address-mocks": "^1.0.6", | ||
"@shopify/jest-dom-mocks": "^2.1.1", | ||
"@shopify/address-mocks": "^1.1.0", | ||
"@shopify/jest-dom-mocks": "^2.1.2", | ||
"typescript": "~3.0.1" | ||
}, | ||
"sideEffects": false | ||
"sideEffects": false, | ||
"dependencies": { | ||
"tslib": "^1.9.3" | ||
} | ||
} |
@@ -33,3 +33,3 @@ # `@shopify/address` | ||
Loads and return data about a all countries in the given locale. Countries are ordered alphabetically based on the locale. Provinces are also ordered based on the locale. | ||
Loads and return data about a all countries in the given locale. Countries are ordered alphabetically based on the locale. Zones are also ordered based on the locale. | ||
@@ -62,42 +62,2 @@ #### `async .getOrderedFields(countryCode): FieldName[][]` | ||
#### `async .getTranslationKey(countryCode: string, key: FieldName): string` | ||
Get the translation key for a given field for a given country. Eg: | ||
```typescript | ||
await getTranslationKey('CA', 'province'); // => "PROVINCE" | ||
await getTranslationKey('US', 'province'); // => "STATE" | ||
await getTranslationKey('JA', 'province'); // => "PREFECTURE" | ||
await getTranslationKey('CA', 'zip'); // => "POSTAL_CODE" | ||
await getTranslationKey('US', 'zip'); // => "ZIP_CODE" | ||
await getTranslationKey('CA', 'address2'); // => "APT_UNIT_NUMBER" | ||
await getTranslationKey('JA', 'address2'); // => "APT_SUITE_ETC" | ||
``` | ||
#### Translation keys | ||
The label used to designate zip code and provinces are not the same from a country to another | ||
- `ProvinceKey` is one of the following | ||
- `COUNTY` | ||
- `EMIRATE` | ||
- `GOVERNORATE` | ||
- `PREFECTURE` | ||
- `PROVINCE` | ||
- `REGION` | ||
- `STATE_AND_TERRITORY` | ||
- `STATE` | ||
- `ZipKey` is one of the following | ||
- `POSTAL_CODE` | ||
- `POSTCODE` | ||
- `PINCODE` | ||
- `ZIP_CODE` | ||
- `Address2Key` is one of the following | ||
- `APT_SUITE_ETC` | ||
- `APT_UNIT_NUMBER` | ||
#### Example Usage | ||
@@ -104,0 +64,0 @@ |
@@ -91,20 +91,2 @@ import {Address, FieldName, Country} from './types'; | ||
async getTranslationKey( | ||
countryCode: string, | ||
key: FieldName, | ||
): Promise<string> { | ||
const country = await this.getCountry(countryCode); | ||
switch (key) { | ||
case FieldName.Province: | ||
return country.provinceKey; | ||
case FieldName.Zip: | ||
return country.zipKey; | ||
case FieldName.Address2: | ||
return country.address2Key; | ||
default: | ||
return key; | ||
} | ||
} | ||
private loadCountryFromCache( | ||
@@ -111,0 +93,0 @@ countryCode: string, |
@@ -6,6 +6,16 @@ const query = ` | ||
code | ||
continent | ||
phoneNumberPrefix | ||
address2Key | ||
provinceKey | ||
zipKey | ||
labels { | ||
address1 | ||
address2 | ||
city | ||
company | ||
country | ||
firstName | ||
lastName | ||
phone | ||
postalCode | ||
zone | ||
} | ||
formatting { | ||
@@ -15,3 +25,3 @@ edit | ||
} | ||
provinces { | ||
zones { | ||
name | ||
@@ -27,6 +37,16 @@ code | ||
code | ||
continent | ||
phoneNumberPrefix | ||
address2Key | ||
provinceKey | ||
zipKey | ||
labels { | ||
address1 | ||
address2 | ||
city | ||
company | ||
country | ||
firstName | ||
lastName | ||
phone | ||
postalCode | ||
zone | ||
} | ||
formatting { | ||
@@ -36,3 +56,3 @@ edit | ||
} | ||
provinces { | ||
zones { | ||
name | ||
@@ -39,0 +59,0 @@ code |
import {fetch} from '@shopify/jest-dom-mocks'; | ||
import {mockCountryRequests} from '../../../address-mocks/src'; | ||
import {Address, FieldName} from '../types'; | ||
import {Address} from '../types'; | ||
import AddressFormatter from '..'; | ||
@@ -46,7 +46,6 @@ import {toSupportedLocale} from '../loader'; | ||
'phoneNumberPrefix', | ||
'address2Key', | ||
'provinceKey', | ||
'zipKey', | ||
'labels', | ||
'formatting', | ||
'provinces', | ||
'zones', | ||
'continent', | ||
]); | ||
@@ -128,3 +127,3 @@ }); | ||
'日本', | ||
'〒100-8994東京都目黒区八重洲1-5-3', | ||
'〒100-8994 東京都 目黒区 八重洲1-5-3', | ||
'Shopify', | ||
@@ -145,3 +144,3 @@ '田中 恵子様', | ||
'日本', | ||
'〒100-8994目黒区八重洲1-5-3', | ||
'〒100-8994 目黒区 八重洲1-5-3', | ||
'Shopify', | ||
@@ -172,44 +171,2 @@ '田中 恵子様', | ||
describe('getTranslationKey()', () => { | ||
it('translates based on the country province key', async () => { | ||
const addressFormatter = new AddressFormatter('ja'); | ||
const result = await addressFormatter.getTranslationKey( | ||
'JP', | ||
FieldName.Province, | ||
); | ||
expect(result).toBe('PREFECTURE'); | ||
}); | ||
it('translates based on the country zip key', async () => { | ||
const addressFormatter = new AddressFormatter('ja'); | ||
const result = await addressFormatter.getTranslationKey( | ||
'JP', | ||
FieldName.Zip, | ||
); | ||
expect(result).toBe('POSTAL_CODE'); | ||
}); | ||
it('translates based on the country address2 key', async () => { | ||
const addressFormatter = new AddressFormatter('ja'); | ||
const result = await addressFormatter.getTranslationKey( | ||
'JP', | ||
FieldName.Address2, | ||
); | ||
expect(result).toBe('APT_SUITE_ETC'); | ||
}); | ||
it('translates based on the country key', async () => { | ||
const addressFormatter = new AddressFormatter('ja'); | ||
const result = await addressFormatter.getTranslationKey( | ||
'JP', | ||
FieldName.Country, | ||
); | ||
expect(result).toBe('country'); | ||
}); | ||
}); | ||
describe('toSupportedLocale', () => { | ||
@@ -216,0 +173,0 @@ it('changes the lowercase locale to uppercase', () => { |
@@ -54,3 +54,3 @@ import {fixtures} from '../../../address-mocks/src/fixtures'; | ||
it('returns empty string for province if country does not have provinces', () => { | ||
it('returns empty string for province if country does not have zones', () => { | ||
const template = '{province}'; | ||
@@ -57,0 +57,0 @@ expect( |
@@ -6,4 +6,4 @@ export enum FieldName { | ||
City = 'city', | ||
Zip = 'zip', | ||
Province = 'province', | ||
PostalCode = 'zip', | ||
Zone = 'province', | ||
Address1 = 'address1', | ||
@@ -15,16 +15,2 @@ Address2 = 'address2', | ||
export type ProvinceKey = | ||
| 'COUNTY' | ||
| 'EMIRATE' | ||
| 'GOVERNORATE' | ||
| 'PREFECTURE' | ||
| 'PROVINCE' | ||
| 'REGION' | ||
| 'STATE_AND_TERRITORY' | ||
| 'STATE'; | ||
export type ZipKey = 'POSTAL_CODE' | 'POSTCODE' | 'PINCODE' | 'ZIP_CODE'; | ||
export type Address2Key = 'APT_SUITE_ETC' | 'APT_UNIT_NUMBER'; | ||
export interface Address { | ||
@@ -44,3 +30,3 @@ company?: string; | ||
export interface Province { | ||
export interface Zone { | ||
code: string; | ||
@@ -59,6 +45,16 @@ name: string; | ||
code: string; | ||
continent: string; | ||
phoneNumberPrefix: number; | ||
address2Key: Address2Key; | ||
provinceKey: ProvinceKey; | ||
zipKey: ZipKey; | ||
labels: { | ||
address1: string; | ||
address2: string; | ||
city: string; | ||
company: string; | ||
country: string; | ||
firstName: string; | ||
lastName: string; | ||
phone: string; | ||
postalCode: string; | ||
zone: string; | ||
}; | ||
formatting: { | ||
@@ -68,3 +64,3 @@ edit: string; | ||
}; | ||
provinces: Province[]; | ||
zones: Zone[]; | ||
} | ||
@@ -71,0 +67,0 @@ |
@@ -1,2 +0,2 @@ | ||
import {Address, FieldName, Country, Province} from './types'; | ||
import {Address, FieldName, Country, Zone} from './types'; | ||
@@ -11,4 +11,4 @@ const FIELD_REGEXP = /({\w+})/g; | ||
'{city}': FieldName.City, // eslint-disable-line id-length | ||
'{zip}': FieldName.Zip, // eslint-disable-line id-length | ||
'{province}': FieldName.Province, // eslint-disable-line id-length | ||
'{zip}': FieldName.PostalCode, // eslint-disable-line id-length | ||
'{province}': FieldName.Zone, // eslint-disable-line id-length | ||
'{address1}': FieldName.Address1, // eslint-disable-line id-length | ||
@@ -47,8 +47,6 @@ '{address2}': FieldName.Address2, // eslint-disable-line id-length | ||
break; | ||
case FieldName.Province: | ||
case FieldName.Zone: | ||
line = line.replace( | ||
`{${FieldName.Province}}`, | ||
address.province | ||
? getProvince(country.provinces, address.province).name | ||
: '', | ||
`{${FieldName.Zone}}`, | ||
address.province ? getZone(country.zones, address.province).name : '', | ||
); | ||
@@ -64,9 +62,9 @@ break; | ||
} else { | ||
return line.trim(); | ||
return line.trim().replace(' ', ' '); | ||
} | ||
} | ||
function getProvince(provinces: Province[], provinceCode: string): Province { | ||
function getZone(zones: Zone[], zoneCode: string): Zone { | ||
return ( | ||
provinces.find(province => province.code === provinceCode) || { | ||
zones.find(zone => zone.code === zoneCode) || { | ||
name: '', | ||
@@ -73,0 +71,0 @@ code: '', |
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
39941
1
1045
117
+ Addedtslib@^1.9.3
+ Addedtslib@1.14.1(transitive)