zip-code-to-state
v1.0.0
Published
Get US state/territory from ZIP code
Downloads
13
Readme
zip-code-to-state
ZIP codes are tricky. You can usually tell which state a ZIP code is in by looking at the first two digits (19 is Pennslyvania, 90 is California, etc.)
But sometimes these two-digit prefixes are split across several states. 19xxx, for instance, has ZIP codes in Pennsylvania, but also Delaware and Maryland
This uses a USPS dataset to build a small matching table of ZIP codes with shared prefixes or other oddities. This means we can tell the user exactly where each Zip code is located without querying all 40,000+.
(There are about a dozen ZIP codes that span multiple states. In these cases, the library returns an error.)
Installation
npm install zip-code-to-stateUsage
const getStateFromZip = require('get-state-from-zip');
console.log(getStateFromZip('90210')); // Returns 'CA'
console.log(getStateFromZip('19701')); // Returns 'DE'API
getStateFromZip(zipCode)
Returns a two-letter state/territory abbreviation for a given ZIP code.
zipCode(string): A 5-digit US ZIP code- Returns: String (two-letter state/territory abbreviation) or null if invalid/not found
