npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tzlocator

v2.1.2

Published

Lightweight privacy respecting geolocation library with no dependencies.

Downloads

114

Readme

tzlocator

Lightweight privacy respecting geolocation library with no dependencies.

Key features

  • 🥂 No third-party API calls
  • 👀 Works without browser prompt
  • 🪙 No reverse IP lookups
  • 🌏 Country and currency data
  • 🔒 Privacy respecting
  • 🪶 Lightweight with no dependencies
  • 🎓 Fully typed

Installation

// npm
npm install tzlocator
// yarn
yarn add tzlocator
// pnpm
pnpm add tzlocator

Usage

Browser

Single page application

import { Tzlocator, getBrowserTimezone } from "tzlocator"

const tzlocator = new TzLocator()
const location = tzlocator.get(getBrowserTimezone())

Server side rendering

import { getBrowserTimezone } from "tzlocator"

const timezone = getBrowserTimezone()
// send the timezone to the server
document.cookie = `timezone=${timezone};`

Server

import { getRequestCookies } from "my-project"
import { Tzlocator } from "tzlocator"

const tzlocator = new TzLocator()

function getLocation(request: Request) {
	// retrieves the browser's timezone from a cookie
	const cookies = getRequestCookies(request)
	return tzlocator.get(cookies.timezone)
}

Configuration

You can modify tzlocator's configuration by passing an object to its class constructor (e.g. new Tzlocator({})).

{
	include, /*
	An array of functions that take the Tzlocator's `get` method result as a
	parameter and return true if it should be included or false otherwise.
	*/
	exclude, /*
	An array of functions that take the Tzlocator's `get` method result as a
	parameter and return true if it should be excluded or false otherwise
	*/
	fallback, /*
	A string indicating the timezone when the Tzlocator's `get` method can't
	find the given timezone, either because it's not included/excluded or
	because it doesn't exist.
	*/
}

Tzlocator

Used to get the location information (Locator) using a timezone. Can be instanciated with or without a configuration object.

class Tzlocator {
	/**
	 * Returns a boolean indicating if the `timezone` has an assigned
	 * CountryCode.
	 */
	static exists(timezone: string): timezone is Timezone {}

	/**
	 * Returns the Locator corresponding to the `timezone`.
	 * If `useFallback` is true and the `timezone` cannot be found in the
	 * included Locator pool it will return the set config fallback.
	 * Else if there's no set fallback or if `useFallback` is false it will
	 * return undefined.
	 */
	get(timezone: string, useFallback = true) {}

	/**
	 * Returns a boolean indicating if the `timezone` has a valid assigned
	 * Locator.
	 */
	has() {}

	/**
	 * Returns an array of all the valid timezones for the current instance.
	 */
	timezones() {}

	/**
	 * Returns an array of all the valid currencies for the current instance.
	 */
	currencies() {}

	/**
	 * Returns an array of all the valid languages for the current instance.
	 */
	languages() {}

	/**
	 * Returns an array of all the valid locators for the current instance.
	 */
	locators() {}
}

Locator

Location information class. Can be instanciated using a country code (e.g. DE).

class Locator {
	code // country's code
	name: // country's name
	native: // country's native name
	prefix: // country's phone code
	continent: // continent
	measurement: // country's measurement system
	currency: // country's currency information
	locales: // country's locales

	/**
	 * Returns a boolean indicating if the `countryCode` has an assigned
	 * Locator.
	 */
	static exists(countryCode: string): countryCode is CountryCode {}

	/**
	 * Returns the languages corresponding to each locale.
	 */
	getLanguages() {}

	/**
	 * Returns the main locale for this country.
	 */
	getMainLocale() {}

	/**
	 * Returns the main language for this country.
	 */
	getMainLanguage() {}
}

Currency

Currency information class. Can be instanciated using a currency code (e.g. EUR).

class Currency {
	code:  // currency's code
	symbol: // currency's symbol
	name: // currency's name

	/**
	 * Returns a boolean indicating if the `currencyCode` has an assigned
	 * Currency.
	 */
	static exists(currencyCode: string): currencyCode is CurrencyCode {}
}

Language

Language information class. Can be instanciated using a language code (e.g. en).

class Language {
	code: // language's code
	name: // language's name
	native: // language's native name
	scripts: // language's scripts

	/**
	 * Returns a boolean indicating if the `languageCode` has an assigned
	 * Language.
	 */
	static exists(languageCode: string): languageCode is LanguageCode {}
}

Thanks