Skip to content

Commit 16f5bfb

Browse files
committed
feat(helpers): adds format-address helper
1 parent 7ea082d commit 16f5bfb

5 files changed

Lines changed: 67 additions & 2 deletions

File tree

packages/web/titanium/address-input/manual-address-dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { DOMEvent } from '../types/dom-event';
1414
import { allowDialogOverflow, preventDialogOverflow } from '../hacks/dialog-overflow-hacks';
1515
import { dialogZIndexHack } from '../hacks/dialog-zindex-hack';
1616
import { reportValidityIfError } from '../hacks/report-validity-if-error';
17-
import { caStates, usStates } from './utils/states-abbr-to-titlecase';
18-
import { countries } from './utils/country-abbr-to-titlecase';
17+
import { caStates, usStates } from '../helpers/address/states-abbr-to-titlecase';
18+
import { countries } from '../helpers/address/country-abbr-to-titlecase';
1919

2020
@customElement('manual-address-dialog')
2121
export class ManualAddressDialog extends LitElement {

packages/web/titanium/address-input/utils/country-abbr-to-titlecase.ts renamed to packages/web/titanium/helpers/address/country-abbr-to-titlecase.ts

File renamed without changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { IAddress, Person } from '@leavittsoftware/lg-core-typescript';
2+
import { html, TemplateResult } from 'lit';
3+
import { join } from 'lit/directives/join.js';
4+
import { getCountryName } from './get-country-name';
5+
6+
export function formatAddressParts(address: Partial<IAddress> | Partial<Person>, options: formatAddressOptions = {}) {
7+
if (!address) {
8+
return {};
9+
}
10+
11+
const street1 = 'Street1' in address ? address.Street1?.trim() : 'CompanyAddressStreet1' in address ? address.CompanyAddressStreet1?.trim() : '';
12+
const street2 = 'Street2' in address ? address.Street2?.trim() : 'CompanyAddressStreet2' in address ? address.CompanyAddressStreet2?.trim() : '';
13+
const city = 'City' in address ? address.City?.trim() : 'CompanyAddressCity' in address ? address.CompanyAddressCity?.trim() : '';
14+
const state = 'State' in address ? address.State?.trim() : 'CompanyAddressState' in address ? address.CompanyAddressState?.trim() : '';
15+
const zip = 'Zip' in address ? address.Zip?.trim() : 'CompanyAddressZip' in address ? address.CompanyAddressZip?.trim() : '';
16+
const country = 'Country' in address ? address.Country?.trim() : 'CompanyAddressCountry' in address ? address.CompanyAddressCountry?.trim() : '';
17+
18+
const line1 = street1;
19+
const line2 = street2;
20+
const line3 = `${[[city, state].join(' ').trim(), zip].filter((o) => o).join(', ')}`;
21+
const line4 = getCountryName(country, options?.excludeCountryIfUSA);
22+
23+
return {
24+
line1: line1 && !options?.excludeLine1 ? line1 : undefined,
25+
line2: line2 && !options?.excludeLine2 ? line2 : undefined,
26+
line3: line3 && !options?.excludeLine3 ? line3 : undefined,
27+
line4: line4 && !options?.excludeLine4 ? line4 : undefined,
28+
};
29+
}
30+
31+
export function formatAddress(
32+
address: Partial<IAddress> | Partial<Person>,
33+
options: formatAddressOptions = {},
34+
joiner: TemplateResult | string = html`<br />`
35+
) {
36+
const parts = formatAddressParts(address, options);
37+
const partsArray = Object.values(parts).filter((part) => part !== undefined && part !== '');
38+
39+
if (partsArray.length === 0) {
40+
return null;
41+
}
42+
return join(partsArray, joiner);
43+
}
44+
45+
export type formatAddressOptions = {
46+
excludeCountryIfUSA?: boolean;
47+
excludeLine1?: boolean;
48+
excludeLine2?: boolean;
49+
excludeLine3?: boolean;
50+
excludeLine4?: boolean;
51+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { countries } from './country-abbr-to-titlecase';
2+
3+
export function getCountryName(countryAbbr: string | null | undefined, excludeCountryIfUSA: boolean = false): string {
4+
if (!countryAbbr) {
5+
return '';
6+
}
7+
8+
if (excludeCountryIfUSA && countryAbbr.toLowerCase() === 'us') {
9+
return '';
10+
}
11+
12+
const country = countries.find((c) => c.abbreviation.toLowerCase() === countryAbbr.toLowerCase());
13+
return country ? country.name : countryAbbr;
14+
}

packages/web/titanium/address-input/utils/states-abbr-to-titlecase.ts renamed to packages/web/titanium/helpers/address/states-abbr-to-titlecase.ts

File renamed without changes.

0 commit comments

Comments
 (0)