0.2.0 • Published 2 years ago

@zerodep/locale.settings v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@zerodep/locale.settings

minified size minified+gzipped size tree shaking language types

coverage last commit vulnerabilities

app version

A utility to get the locales of the browser or Node environment.

Useful features:

  • deduplicates the values it finds from multiple sources (in both browser & node)
  • normalizes values to a xx, xx-XX, xx-###, xx-XX-xxxx shape
    • language first (2-letter lowercase)
    • region/country code (2-letter uppercase or 3-numbers)
    • subregion code (if returned by the environment)
  • has a configurable fallback locale (with a default of en-US)
  • returns values in order of specificity (language+region first, only language second)

tl;dr

A short explanation / quick reference:

import { localeGet } from '@zerodep/locale.settings';

// probable response for an American developer
localeGet(); // ["en-US", "en"]

// using a custom fallback, for a Canadian developer
localeGet('de-DE'); // ["en-CA", "en-GB", "en", "de-DE"]

Table of Contents

Install

This utility is available from multiple @zerodep packages, enabling developers to select the most appropriately sized package (for both kb and capability) for different use cases. We believe one size does not fit all or most. See @zerodep/app, @zerodep/utils and @zerodep/is.

For Server & Build Tooling

For Node, or when compiling via babel, rollup, swc, tsc, webpack, etc... these are the instructions for you.

// all @zerodep features, capabilities and utilities
npm install @zerodep/app

// entire set of @zerodep locale solutions
npm install @zerodep/locale

// only the locale.settings package
npm install @zerodep/locale.settings

Of course, you may use yarn, pnpm, or the package manager of your choice. Only npm examples are shown for brevity.

Of course, you may use yarn, pnpm, or the package manager of your choice. Only npm examples are shown for brevity.

Browser Direct

If you are using the script directly in a browser via a <script> tag or importing it into your own scripts, these are the instructions for you. We support both ESM and UMD formats.

<!-- for ES Modules (ESM) -->
<script type="module">
  import { isBigInt } from 'https://cdn.jsdelivr.net/npm/@zerodep/locale.settings/esm.js';
  // ...your code here
</script>

<!--  OR  -->

<!--  for Universal Modules (UMD) - all @zerodep functions are in the global "zd" namespace -->
<script src="https://cdn.jsdelivr.net/npm/@zerodep/locale.settings/umd.js"></script>
<script>
  // example of "zd" prefix
  const result = zd.isBigInt(8675309n);
</script>

This package may be found on both jsDelivr and unpkg in UMD, ESM and CJS formats.

How to Use

This package exports the following:

  • Functions
    • set - allows setting one, some or all configurations
    • getOne - returns one specific configuration
    • getCurrencyDefaults - returns all of the currency settings
    • getAll - returns all settings
  • Interfaces
    • LocaleSettings - The various locale-specific options that may be set

Signature

Typescript declarations:

declare const localeSettings: {
  set: (settings?: LocaleSettings | undefined) => void;
  getOne: (
    setting: Setting
  ) =>
    | number
    | boolean
    | 'symbol'
    | TypesLocales
    | TypesCurrencies
    | TypesTimeZones
    | 'narrowSymbol'
    | 'code'
    | 'name'
    | 'standard'
    | 'accounting'
    | 'full'
    | 'long'
    | 'medium'
    | 'short'
    | 'numeric'
    | '2-digit'
    | 'narrow'
    | 'shortOffset'
    | 'longOffset'
    | 'shortGeneric'
    | 'longGeneric'
    | 'always'
    | 'auto'
    | 'false'
    | 'min2'
    | 'halfExpand'
    | 'ceil'
    | 'floor'
    | 'expand'
    | 'trunc'
    | 'halfCeil'
    | 'halfFloor'
    | 'halfTrunc'
    | 'halfEven'
    | undefined;
  getCurrencyDefaults: () => CurrencyOptions;
  getAll: () => LocaleSettings;
};

// configuration interface
interface LocaleSettings {
  locale?: TypesLocales;
  currency?: TypesCurrencies;
  currencyDisplay?: 'symbol' | 'narrowSymbol' | 'code' | 'name';
  currencySign?: 'standard' | 'accounting';
  dateStyle?: 'full' | 'long' | 'medium' | 'short';
  timeStyle?: 'full' | 'long' | 'medium' | 'short';
  timeZone?: TypesTimeZones;
  hour12?: boolean;
  year?: 'numeric' | '2-digit';
  month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
  day?: 'numeric' | '2-digit';
  hour?: 'numeric' | '2-digit';
  minute?: 'numeric' | '2-digit';
  second?: 'numeric' | '2-digit';
  fractionalSecondDigits?: 0 | 1 | 2 | 3;
  timeZoneName?: 'long' | 'short' | 'shortOffset' | 'longOffset' | 'shortGeneric' | 'longGeneric';
  unitDisplay?: 'long' | 'short' | 'narrow';
  useGrouping?: 'always' | 'auto' | 'false' | 'min2';
  roundingMode?:
    | 'halfExpand'
    | 'ceil'
    | 'floor'
    | 'expand'
    | 'trunc'
    | 'halfCeil'
    | 'halfFloor'
    | 'halfTrunc'
    | 'halfEven';
  minimumFractionDigits?: undefined | number;
  maximumFractionDigits?: undefined | number;
}

Examples

All examples assume ESM or CJS packages. If using a UMD package remember to prefix with the zd namespace, e.g. zd.getAll(...).

import { getOne, getAll } from '@zerodep/locale.settings';

// probable results for an American
getOne('currency'); // "USD"

// probable results for an American
getAll();
// {
//   locale: 'en-US',
//   currency: undefined,
//   currencyDisplay: 'narrowSymbol',
//   currencySign: 'standard',
//   dateStyle: 'medium',
//   timeStyle: 'medium',
//   hour12: false,
//   year: 'numeric',
//   month: '2-digit',
//   day: '2-digit',
//   hour: '2-digit',
//   minute: '2-digit',
//   second: '2-digit',
//   fractionalSecondDigits: 0,
//   timeZoneName: 'short',
//   unitDisplay: 'short',
//   useGrouping: 'auto',
//   roundingMode: 'halfExpand',
// }

Related Packages

The following @zerodep packages may be helpful or more appropriate for your specific case:

Advantages of @zerodep Packages

We help make source code more readable, more secure, faster to craft, less likely to have hidden defects, and easier to maintain.

  • Zero npm dependencies - completely eliminates all risk of supply-chain attacks, decreases node_modules folder size
  • Fully typed - typescript definitions are provided for every package for a better developer experience
  • Semantically named - package and method names are easy to grok, remember, use, and read
  • Documented - actually useful documentation with examples and helpful tips
  • Intelligently Packaged - multiple npm packages of different sizes available allowing a menu or a-la-carte composition of capabilities
  • 100% Tested - all methods and packages are fully unit tested
  • ESM & CJS - has both ecmascript modules and common javascript exports, both are fully tree-shakable
  • CDN Available - available on fast content delivery networks in UMD, CJS and ESM formats
  • FP Inspired - gently opinionated to encourage functional programming style for cleaner and more maintainable software
  • Predictably Versioned - semantically versioned for peace-of-mind upgrading, this includes changelogs
  • MIT Licensed - permissively licensed for maximum usability

Support

All @zerodep packages are built for the ES2020 specification. Should you need to support older environments you will need to add appropriate polyfills. All packages are tested on the following platforms/browsers:

Browsers

  • Chrome - last 2 major versions
  • Firefox - last 2 major versions
  • Safari - last 2 major versions
  • Edge - last 2 major versions
  • Android - last 2 major versions
  • iOS - last 2 major versions

Node

  • v16.x - Gallium LTS
  • v14.x - Fermium LTS

It is likely the package will work on other technologies and version, however development and testing effort is only spent on the above.

Semver

All @zerodep packages, including this one, adhere to Semantic Versioning practices:

  • major versions: correlates with breaking changes to one or more method signatures
  • minor versions: includes addition of new functionality or backwards-compatible software improvements
  • patch versions: are reserved for copy changes, documentation enhancements and bug fixes

The above said, a security best practice is to pin your software packages to specific versions and only upgrade to more recent releases after careful inspection of any CHANGELOG, release notes and all software changes.

Resources

License

MIT

0.2.0

2 years ago

0.1.0

2 years ago