A JavaScript library to produce international postal addresses formatted by region
$ npm install i18n-postal-address
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="./foo.js"></script>
</head>
foo.js
// define myAddress
var PostalAddress = window.PostalAddress.default;
var myAddress = new PostalAddress();
// ...
With a bundler (e.g. webpack) or in Node.js you can just require / import it.
import PostalAddress from 'i18n-postal-address'
// define myAddress
const myAddress = new PostalAddress()
const myAddressPersonal = new PostalAddress()
myAddressPersonal.setAddress1('Rua do Pastel, 19')
myAddressPersonal.setCity('Aveiro')
myAddressPersonal.setCountry('Brazil')
myAddressPersonal.setFirstName('John')
myAddressPersonal.setHonorific('Mr.')
myAddressPersonal.setLastName('Pestana')
myAddressPersonal.setPostalCode('2700-242')
myAddressPersonal.setSecondName('Lopes')
myAddressPersonal.setOutputFormat('array')
myAddressPersonal.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
Address Attributes
setAddress1
setAddress2
setAddressNum
setCity
setCompanyName
setCountry
setDo
setDong
setFirstLastName
setFirstName
setGu
setHonorific
setJobTitle
setLastName
setPostalCode
setPrefecture
setProvince
setRegion
setRepublic
setSecondLastName
setSecondName
setSi
setState
setTitle
Options
These affect the output format
/*
Input one country and one type,
Define whether text transformations should be executed
country: 'CA', ...
type: 'business', 'english', 'default', 'french', 'personal'
useTransforms: true, false
*/
setFormat({ country, type, useTransforms })
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, ...).
Disclaimer: It doesn't mean this library tries to recreate any of these
MSDN > ... > Globalization and Localization > Appendix V International Address Formats
Build
$ npm run build
Test
$ npm test