1.0.0 • Published 2 years ago

localstorage-language v1.0.0

Weekly downloads
-
License
SEE IN LICENSE
Repository
-
Last release
2 years ago

localstorage-language

A better way to interact and store data in a localstorage container.

Table of content

Installation

A better way to store and retrieve clients' favorite language for your web apps!

npm i localstorage-localstorage

Usage

This module allows to get and set client's using language is its local storage.

get([options])

Get the i18n tag stored in the local storage.

Returns

  • string - i18n tag stored in the local storage.
  • undefined - No i18n tag is stored in the local storage.

Parameters

NameTypeFacultativeDescription
optionsGetOptions:white_check_mark:Getter's options

Throw

  • CannotParse - The loaded value cannot be parsed.
  • I18nNotString - The loaded i18n tag is not a string.
  • InvalidI18n - The loaded i18n tag isn't valid (according to i18n-locales).

Examples

// { language: "en" }
get(); // "en"
// { language: "en" }
get({ key: "locale" }); // undefined
// { locale: "en" }
get({ key: "locale" }); // "en"

set(newLanguage[, options])

Set a i18n tag in the localstorage.

Returns

  • void.

Parameters

NameTypeFacultativeDescription
newLanguagestring:x:New i18n tag to store
optionsSetOptions:white_check_mark:Getter's options

Throw

  • MissingI18n - The i18n tag is not provided (undefined, null, empty string, ...).
  • I18nNotString - The i18n tag is not a string.
  • InvalidI18n - The i18n tag doesn't exist (according to i18n-locales).

Examples

// {}
set("en");
// { language: "en" }
// {}
set("en", { key: "locale" });
// { locale: "en" }

GetOptions

get() options.

Content

NameTypeFacultativeDescription
destroyboolean:white_check_mark:If strictly true, will destroy the entry after calling this function (even if an error occurred)
destroyOnInvalidboolean:white_check_mark:If strictly true, will destroy the entry only if an error occurred
keystring:white_check_mark:Custom entry's key (default: language). Must be a non-empty string (or will be undefined)

SetOptions

set() options.

Content

NameTypeFacultativeDescription
keystring:white_check_mark:Custom entry's key (default: language). Must be a non-empty string (or will be undefined)

Errors

NameDescription
CannotParseCannot parse language's local storage entry
KeyNotStringThe local storage key is not a string
InvalidI18nThe i18n tag doesn't exists
I18nNotStringThe i18n tag is not a string
MissingI18nTagMissing i18n tag

API structure

localstorage-language
  |
  |- get([options])
  |- set(newLanguage[, options])
  |
  |- GetOptions
  |- SetOptions
  |
  \- errors
      |
      |- CannotParse
      |- KeyNotString
      |- InvalidI18n
      |- I18nNotString
      \- MissingI18nTag

Contributing

Feel free to open an issue if you want to report bugs or discuss about suggestions!

License

This package is published under the MIT license.

Credits to dependencies

Using multiple modules: