
Security News
PEP 810 Proposes Explicit Lazy Imports for Python 3.15
An opt-in lazy import keyword aims to speed up Python startups, especially CLIs, without the ecosystem-wide risks that sank PEP 690.
tz-geo-currency
Advanced tools
A JavaScript library offering an alternative to IP-based geolocation, using timezone information to infer a user's country and currency.
A JavaScript library offering an alternative to IP-based geolocation, using timezone information to infer a user's country and currency.
see Motivation
npm install tz-geo-currency
yarn add tz-geo-currency
getCountry()
Outputs the most probable user's country (in ISO 3166-1 alpha-2) based on timezone, e.g., get 'US'
if TZ is America/New_York
.
For the accuracy and limitations, see FAQ
import { TL } from 'tz-geo-currency'; // TL stands for TimeLocate
const country = TL.getCountry(); // 'US'
getCountries()
Outputs an array of all possible countries based on timezone
const countries = TL.getCountries(); //['US'], assume the TZ is same
getCurrencies()
Outputs an array of currency codes (in ISO 4217) of the most probable country, which derived from timezone
const currencies = TL.getCurrencies(); // ['USD'], assume the TZ is same
getCurrenciesFromCountry(countryCode: string)
Outputs an array of currency codes on given country
const currencies = TL.getCurrenciesFromCountry('JP'); // ['JPY']
TL.getCountry()
is at least 89% accurate. Use TL._reportAccuracy()
to see specific stats and understand its accuracy for your needs. In cases where your application doesn't encounter complex timezones, the accuracy may align closely with IP Geolocation methods (which is around 99%).
Here's the brief output of TL._reportAccuracy()
from latest version.
{
accuracy: 89.42307692307693,
inaccurateList: [
[ 'Africa/Abidjan', [Array] ],
[ 'Africa/Johannesburg', [Array] ],
[ 'Africa/Lagos', [Array] ],
[ 'Africa/Maputo', [Array] ],
[ 'Africa/Nairobi', [Array] ],
[ 'America/Panama', [Array] ],
[ 'America/Phoenix', [Array] ],
[ 'America/Puerto_Rico', [Array] ],
[ 'America/Toronto', [Array] ],
[ 'Asia/Bangkok', [Array] ],
[ 'Asia/Dubai', [Array] ],
[ 'Asia/Kuching', [Array] ],
[ 'Asia/Qatar', [Array] ],
[ 'Asia/Riyadh', [Array] ],
[ 'Asia/Singapore', [Array] ],
[ 'Asia/Yangon', [Array] ],
[ 'Europe/Belgrade', [Array] ],
[ 'Europe/Berlin', [Array] ],
[ 'Europe/Brussels', [Array] ],
[ 'Europe/Helsinki', [Array] ],
[ 'Europe/London', [Array] ],
[ 'Europe/Paris', [Array] ],
[ 'Europe/Prague', [Array] ],
[ 'Europe/Rome', [Array] ],
[ 'Europe/Simferopol', [Array] ],
[ 'Europe/Zurich', [Array] ],
[ 'Indian/Maldives', [Array] ],
[ 'Pacific/Auckland', [Array] ],
[ 'Pacific/Guadalcanal', [Array] ],
[ 'Pacific/Guam', [Array] ],
[ 'Pacific/Pago_Pago', [Array] ],
[ 'Pacific/Port_Moresby', [Array] ],
[ 'Pacific/Tarawa', [Array] ]
]
}
Note that it might still work if the user's timezone is in inaccurateList
. Since getCountry
will return the first country in the IANA TZ dataset, which is the country of most-populous city compared to other's
Currently, the library identifies country and currency. We're focused on reliably providing these key details. With the country code, you can easily fetch more specific metadata as needed for your application.
In 2024, it remains not straightforward for global web applications to automatically detect users' countries and currencies, which are essential for processing payments and customizing product displays. The Geolocation API, while capable of accurately locating users, is often considered too invasive for the mere purpose of content localization, as it requires user consent upon page load.
Traditionally, services use IP geolocation to identify user locations, linking IP addresses to physical locations for precise localization. While effective, it introduces challenges related to complexity and cost associated with maintaining accurate IP geolocation databases.
An alternative, inspired by a Stack Overflow suggestion, leverages timezone information to infer countries and currencies. This method, bypassing the need for extensive databases, simplifies implementation and reduces operational costs.
FAQs
A JavaScript library offering an alternative to IP-based geolocation, using timezone information to infer a user's country and currency.
The npm package tz-geo-currency receives a total of 537 weekly downloads. As such, tz-geo-currency popularity was classified as not popular.
We found that tz-geo-currency demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
An opt-in lazy import keyword aims to speed up Python startups, especially CLIs, without the ecosystem-wide risks that sank PEP 690.
Security News
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.