Socket
Socket
Sign inDemoInstall

zip-to-tz

Package Overview
Dependencies
14
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    zip-to-tz

Converts a zipcode to a timezone


Version published
Weekly downloads
1.6K
decreased by-3.15%
Maintainers
1
Install size
476 kB
Created
Weekly downloads
 

Readme

Source

zip-to-tz

This is a re-implementation of DoubleDor's node-zipcode-to-timezone, but optimized for space so that you can use it in the browser with less bloat.

We utilize the same source data in zipcodes.json and generate a map of only the upperbounds. The numercially ordered zipcodes (indexes into timezone-map.json), don't produce timezone indexes that are strictly monotonically increasing (for obvious reasons), so while this isn't a perfect solution, the Gzipped size of the optimized zipcode map is 2.8% of the original Gzipped zipcode map.

Since we don't have an exhaustive list of all zipcodes, we use a binary search function to find where the zipcode lies within the map of upper bounds and return the result.

This module assumes that the zipcode is valid. The original module returned undefined for unknown or invalid zipcodes, because we don't have an exhaustive list, we'll return a timezone for potentially invalid zipcodes.

This module basically trades space for time as the lookup isn't a direct attribute read, but a binary search of the optimized zipcode map keys until we find what range the target zipcode lies in.

entriessizesize (gzipped)
zipcodes map41962672KB107KB
upper bounded map102614KB3KB

e.g. Instead of:

{
  ...
  '11111': 0,
  '11112': 0,
  ...
  '11914': 0,
  '11915': 1,
  '11916': 0,
  '11917': 1,
  '11918': 0,
  '11919': 1,
  '11920': 1,
  ...
  '13587': 1,
  '13588': 1,
  '13589': 2,
  '13590': 2,
  ...
  '15623': 2,
  '13624': 2,
  '13625': 3,
  '13626': 3,
  ...
}

We'd keep track of:

  ...
  '11914': 0,
  '11915': 1,
  '11916': 0,
  '11917': 1,
  '11918': 0,
  '13588': 1,
  '13624': 2,
  '13625': //wherever 3 ends
  ...

Installation

npm install --save zip-to-tz

Usage

var zipToTz = require( 'zip-to-tz' );

var tz = zipToTz( '94110' );
console.log( tz ); // America/Los_Angeles
import zipToTz from 'zip-to-tz'

const tz = zipToTz( '94110' );
console.log( tz ); // America/Los_Angeles

Other info

This project uses the timezone list found here:

https://sourceforge.net/projects/zip2timezone/files/

Keywords

FAQs

Last updated on 14 Mar 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc