i18n-postal-address

A JavaScript library to produce international postal addresses formatted by
region.
Installation
npm install i18n-postal-address
yarn add i18n-postal-address
Usage
As an ESM module (web)
Load i18n-postal-address directly from skypack (CDN).
<script type="module">
import PostalAddress from 'https://cdn.skypack.dev/i18n-postal-address'
</script>
Demo available in codepen.
On the web (self-hosted)
It's exposed through the window global object as explained below.
index.html
<head>
<script type="text/javascript" src="./postal-address.js"></script>
<script type="text/javascript" src="./app.js"></script>
</head>
app.js
var PostalAddress = window.PostalAddress.default
var myAddress = new PostalAddress()
With a bundler / Node.js
With a bundler (e.g. webpack) or in Node.js you can just require / import it.
import PostalAddress from 'i18n-postal-address'
const myAddress = new PostalAddress()
Example
The methods can be chained one after the other for a cleaner code.
const myAddressPersonal = new PostalAddress()
myAddressPersonal
.setAddress1('Rua do Pastel, 19')
.setCity('Aveiro')
.setCountry('Brazil')
.setFirstName('John')
.setHonorific('Mr.')
.setLastName('Pestana')
.setPostalCode('2700-242')
.setSecondName('Lopes')
.setOutputFormat('array')
.setFormat({
country: 'AR',
type: 'personal',
})
console.log(myAddressPersonal.output())
console.log(myAddressPersonal.toString())
output()
[ [ 'Mr.', 'John', 'Lopes' ],
[ 'Pestana' ],
[ 'Rua do Pastel, 19' ],
[ '2700-242', 'Aveiro' ],
[ 'Brazil' ] ]
toString()
Mr. John Lopes
Pestana
Rua do Pastel, 19
2700-242 Aveiro
Brazil
Available Class Methods
Address Attributes
setAddress1(string)
setAddress2(string)
setAddressNum(string)
setCity(string)
setCompanyName(string)
setCountry(string)
setDo(string)
setDong(string)
setFirstLastName(string)
setFirstName(string)
setGu(string)
setHonorific(string)
setJobTitle(string)
setLastName(string)
setPostalCode(string)
setPrefecture(string)
setProvince(string)
setRegion(string)
setRepublic(string)
setSecondLastName(string)
setSecondName(string)
setSi(string)
setState(string)
setTitle(string)
Options
These affect the output format
const postalAddress = new PostalAddress()
postalAddress.setFormat({ country, type, useTransforms })
Custom Formats
Additional formats can be added or existing ones can be replaced
const addCommaAfter = (value) => `${value},`
const postalAddress = new PostalAddress()
postalAddress.addFormat({
country: 'PT',
format: [
[{ attribute: 'lastName', transforms: [addCommaAfter] }, 'firstName'],
['city', 'postalCode'],
['country'],
],
})
Valid attributes
address1
address2
addressNum
city
companyName
country
countryAlpha2
do
dong
firstLastName
firstName
gu
honorific
jobTitle
lastName
postalCode
prefecture
province
region
republic
secondLastName
secondName
si
state
title
Background
Why
The great libpostal is not available
on the web.
Need to present postal addresses for different regions worldwide stored in
individual parts (company name, address, postal code, city, county, country,
...).
Inspiration
Disclaimer: It doesn't mean this library tries to recreate any of these
MSDN > ... > Globalization and Localization >
Appendix V International Address Formats
Query Address Data
PostalAddress
Forking / Contributing
Build
yarn build
Test
yarn test:unit
yarn test:functional