Skip to content

A JavaScript library to produce international postal addresses formatted by region

License

Notifications You must be signed in to change notification settings

maxifuchs/i18n-postal-address

 
 

Repository files navigation

i18n-postal-address

npm version jest

A JavaScript library to produce international postal addresses formatted by region

Installation

$ npm install i18n-postal-address

Usage

On the web

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 / Node.js

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()

Example

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

Available Class Methods

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 })

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

$ npm run build

Test

$ npm test

About

A JavaScript library to produce international postal addresses formatted by region

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 95.5%
  • HTML 4.5%