Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

i18n-postal-address

joaocarmo82.6kMIT0.9.2TypeScript support: included

A JavaScript library to produce international postal addresses formatted by region for node and the web

address, formatter, i18n, node, postal, web

readme

i18n-postal-address

npm version Tests

A JavaScript library to produce international postal addresses formatted by region for Node.js and the web.

Try it out in the playground!

Installation

npm install i18n-postal-address

# or

pnpm 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

// 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

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('Portugal')
  .setFirstName('John')
  .setHonorific('Mr.')
  .setLastName('Pestana')
  .setPostalCode('2700-242')
  .setSecondName('Lopes')
  .setFormat({
    country: 'AR',
    type: 'personal',
  })

console.log(myAddressPersonal.toArray())

console.log(myAddressPersonal.toString())

toArray()

[ [ 'Mr.', 'John', 'Lopes' ],
  [ 'Pestana' ],
  [ 'Rua do Pastel, 19' ],
  [ '2700-242', 'Aveiro' ],
  [ 'Portugal' ] ]

toString()

Mr. John Lopes
Pestana
Rua do Pastel, 19
2700-242 Aveiro
Portugal

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

/*
  Input one country and one type,
  Define whether text transformations should be executed

  country: 'CA' | ...
  type: 'business' | 'english' | 'default' | 'french' | 'personal'
  useTransforms: true | false
*/

const postalAddress = new PostalAddress()

postalAddress.setFormat({ country, type, useTransforms })

Custom Formats

Additional formats can be added or existing ones can be replaced

/*
  The country should be an uppercase ISO 3166-1 alpha-2 code

  The format should be an array of array of strings or objects
    - The strings should be valid attribute names
    - The objects should contain the attribute (required)
    - The objects may contain an array of transforming functions that will be
      applied sequentially over the given attribute with the following signature
        (string) => string
*/

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

Experimental string parsing

If you're interested in parsing a string into an object, you can use the new experimental support for parsing addresses using some existing libraries. However, note that these are optional and not required for the primary functionality of this library.

libpostal

This is the most complete and comprehensive library for parsing addresses. You need to be using the node version as it's not supported on the web.

Optional Requirements for libpostal Support

If you wish to utilize the libpostal experimental features, follow these steps:

  1. Install libpostal following their installation instructions, or execute this script if you're on a *nix machine.
  2. Install node-postal as an optional dependency in your project, after installing libpostal. This is not required for the core functionality but only if you want to use the libpostal features.

Remember, these steps are optional and are only for users interested in the experimental string parsing feature.

Experimental usage

Notice the import is slightly different, you should import this library as i18n-postal-address/strings.

const PostalAddress = require('i18n-postal-address/strings').default

const postalAddress = new PostalAddress()

postalAddress.fromString(
  'Barboncino 781 Franklin Ave, Crown Heights, Brooklyn, NY 11238',
)

console.log(postalAddress.toString())
Barboncino 781 Franklin Ave
Crown Heights Brooklyn
NY 11238

Background

Why

The libpostal library 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

pnpm build

Test

pnpm test:unit

pnpm test:functional

changelog

Changelog

The changelog will no longer be maintained. Instead, the information will be available in the releases.

0.6.1

May 4, 2022

  • Fixes an issue with the custom formats that prevented new countries from being used (#38)

0.6.0

Apr 3, 2022

  • Implements a string parser for the node version, check the documentation for more information

0.5.0

Mar 28, 2022

  • Adds support for a full or partial preset state to be passed to the constructor

0.4.3

Mar 21, 2021

  • Marked the arguments in the output() method as optional

0.4.2

Jan 28, 2021

  • Fixes the exported typings (#17)

0.4.1

Jan 9, 2021

0.4.0

Nov 12, 2020

  • Added support for custom formats through the new API .addFormat()

0.3.0

Oct 18, 2020

  • Added transforms and definitions to append commas in certain formats (#8)

0.2.0

Aug 17, 2020

  • Added support for Method Chaining

0.1.0

Jul 26, 2020

  • Updated the dependencies to resolve some security vulnerabilities
  • Updated the directory structure
  • Added GitHub actions for CI/CD
  • Converted to TypeScript

0.0.7-2 (no release)

Aug 11, 2019

  • Added more tests to Jest
  • Added a to-do list
  • Updated the dependencies to resolve security vulnerabilities:

0.0.7

Jun 13, 2019

0.0.6

Sep 8, 2018

0.0.5

Jul 24, 2018

  • Removed the method setUseTransforms and added it as an option to setFormat
  • Added Jest for testing

0.0.4

Jul 8, 2018

  • Added an option to ignore transforms
  • Fixed documentation

0.0.3

Jul 8, 2018

  • Added a capitalization transform to some attributes

0.0.2

Jul 4, 2018

  • Publish to npm repository

0.0.1

Jul 1, 2018

  • Initial release