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

js-localizations

v1.0.0

Published

A package with localizations utilities

Downloads

72

Readme

Cloudbeds JS Localization Utilities

NPM Version

A robust utility library to manage and retrieve localized information such as country names, currency formats, and date-time utilities, using ISO standards and internationalization libraries. This library is optimized for performance through caching and includes several modules to cover localization needs.

Features

  • Country: Retrieve country names, states, and translations by country code.
  • Datetime: Format dates, convert timezones, parse and display relative time.
  • Currency: Format currency values, parse currency strings, adjust precision.
  • Language: Access country flags, translate country names, and localize data.

Installation

Install the package using npm:

npm install @cloudbeds/js-localizations

Usage

Importing Features

Method 1: Named Imports

You can import individual functions as needed.

import { getCountryName } from "@cloudbeds/js-localizations";
import { formatCurrency } from "@cloudbeds/js-localizations";

Method 2: Full Module Import

import * as LocalizationUtils from "@cloudbeds/js-localizations";

Module Usage

Country

This module provides functions for country name retrieval, state listing, and country translations.

import { getCountryName, getStatesNameByCountry, getCountryFlag, getCountriesWithTranslations } from "@cloudbeds/js-localizations";

// Retrieve country name in a specific language
const countryName = getCountryName("US", "es"); // Returns "Estados Unidos"

// Fetch list of states for a country
const states = getStatesNameByCountry("US");

// Retrieve country and states information
const countryAndStates = getCountryAndStatesNames("US");

// Get a list of all country names
const countryList = getCountryList();

// Get the flag emoji for a country
const flag = getCountryFlag("US");

// Retrieve country names in multiple languages
const countries = getCountriesWithTranslations(["US", "FR"], "es");

Datetime

Includes utilities for formatting dates, handling timezones, and displaying relative time.

import { formatDate, timeAgo } from "@cloudbeds/js-localizations";

// Format a date
const formattedDate = formatDate(new Date(), "yyyy-MM-dd");

// Display relative time
const relativeTime = timeAgo(new Date("2024-01-01"));

// Parse a date string and format it
const parsedDate = parseAndFormatDate("2024-01-01T12:00:00Z", "yyyy-MM-dd");

Currency

Contains tools to format and parse currency values.

import { formatCurrency, parseCurrency } from "@cloudbeds/js-localizations";

// Format a value as currency
const currencyString = formatCurrency(1234.56, "USD");

// Parse a currency string back to a number
const value = parseCurrency("$1,234.56");

// Extract value from an HTML span
const valueFromHtml = getValueFromHtmlSpan("<span>$1234.56</span>");

// Check if the value is below one penny
const isSubPenny = isValueSubPenny(0.009);

// Round a value that is less than a penny
const roundedValue = roundBelowPenny(0.005);

Language

Registers language locales from JSON files for a list of language codes, returning the successfully registered codes. The function getLanguageOptions then creates an array of language options, where each entry includes a language code, its name (in the original language), and its international names in the provided locales.

import { getLanguageOptions } from "@cloudbeds/js-localizations";

const result = getLanguageOptions(['es', 'de']); // send the supported languages

API

Country

  • getCountryName(code: string, lang: string): string
  • getStatesNameByCountry(code: string): string[]
  • searchCountryByField(field: string, value: string): Country

Datetime

  • formatDate(date: Date, format: string): string
  • timeAgo(date: Date): string
  • formatInTimeZone(date: Date, timezone: string): string

Currency

  • formatCurrency(value: number, currencyCode: string): string
  • parseCurrency(value: string): number
  • getPrecision(value: number): number

Language

  • getLanguageOptions(languageCodes: string[]): Language[]

Configuration

The library uses caching to optimize repeated calls. Caching can be configured using sessionStorage on the frontend and Map in Node.js environments.


Tests

Run tests using Vitest:

npm run test