包详细信息

local-storage-to-object

Rod2811MIT3.0.0

It allows treating the data that is stored and extracted in the browser's localStorage, always as an object.

local storage, local storage object, localstorage, storage

自述文件

local-storage-to-object

This package contains the same functionality as window.localStorage, as it makes use of it, with the difference that everything that is stored in and fetched from window.localStorage will always be treated as an object.

Important

Note: This package is weighted to be used only on the client side, since it is necessary to use the window property of the browser.

Table of Contents

Installation

Using npm:

npm install --save local-storage-to-object

npm i local-storage-to-object

Basic Usage

For this example we will name it LocalStorage, but you can use anything else.

Example:

import LocalStorage from 'local-storage-to-object';

function myFuction() {
  // Do something...


  // Add values
  LocalStorage.setItem('keyLocalStorage', { isTesting: true });

  // Get values
  // Get an object stored in localStorage
  const valueLS = LocalStorage.getItem('keyLocalStorage');
  // valueLS = { isTesting: true }

  // Acceda a un valor específico del objeto almacenado en localStorage
  const _valueLS = LocalStorage.getItem('keyLocalStorage', 'isTesting');
  // _valueLS = true

  // Delete values
  // To delete a key and its value
  LocalStorage.removeItem('keyLocalStorage');

  // Multiple keys can also be deleted
  LocalStorage.removeItem(['keyLocalStorage']);

  // To completely clean the localStorage
  LocalStorage.removeItem();


  // Do something...
}

Example to store several keys at once:

import LocalStorage from 'local-storage-to-object';

function myFuction() {
  // Do something...


  LocalStorage.setItems([
    { key: 'keyLocalStorage', value: { isTesting: true } }
    { key: 'keyExample', value: { id: '123456', value: 10 } }
  ]);


  // Do something...
}

Example to get multiple keys at once. You can also access one of several values ​​of the returned object, and in case those values ​​do not exist, a default value is assigned to that value.:

import LocalStorage from 'local-storage-to-object';

function myFuction() {
  // Do something...


  LocalStorage.setItems([
    { key: 'keyLocalStorage', value: { isTesting: true } },
    { key: 'keyExample', value: { isExample: 1, data: null } }
  ]);

  const valueLS = LocalStorage.getItems([
    { key: 'keyLocalStorage' },
    { key: 'keyLocalStorage', value: 'isTesting' },
    { key: 'keyLocalStorage', value: 'isTesting.val' },
    {
      key: 'keyExample',
      value: ['isExample', 'data', 'id'],
      defaultValue: 'default'
    },
    { key: 'keyExample', value: ['isExample', 'data'] },
    { key: 'keyExample', value: 'data', defaultValue: [] },
    { key: 'keyFake' },
    { key: 'keyFake', value: 'items' }
  ]);
  /*
    valueLS = [
      { key: 'keyLocalStorage', localStorageData: { isTesting: true } },
      { key: 'keyLocalStorage', localStorageData: true },
      { key: 'keyLocalStorage', localStorageData: '*' },
      {
        key: 'keyExample',
        localStorageData: { isExample: 1, data: 'default', id: 'default' }
      },
      { key: 'keyExample', localStorageData: { isExample: 1, data: '*' } },
      { key: 'keyExample', localStorageData: [] },
      { key: 'keyFake', localStorageData: {} },
      { key: 'keyFake', localStorageData: '*' }
    ]
  */


  // Do something...
}

Example to obtain the name of a stored key, through the index:

import LocalStorage from 'local-storage-to-object';

function myFuction() {
  // Do something...


  LocalStorage.setItems([
    { key: 'keyLocalStorage', value: { isTesting: true } },
    { key: 'keyExample', value: { isExample: 1 } }
  ]);

  const valueLS = LocalStorage.getKeyName(0);
  // valueLS = 'keyLocalStorage'


  // Do something...
}

Example to count the number of stored keys:

import LocalStorage from 'local-storage-to-object';

function myFuction() {
  // Do something...


  LocalStorage.setItems([
    { key: 'keyLocalStorage', value: { isTesting: true } },
    { key: 'keyExample', value: { isExample: 1 } }
  ]);

  const valueLS = LocalStorage.getCapacity();
  // valueLS = 2


  // Do something...
}

Read the tests of each of the methods in the tests to get a clearer idea of ​​the capabilities of their use.

 

Methods Reference

Description of each of the methods.


setItem()

  LocalStorage.setItem(key, value);

Before storing a new value in localStorage, it is validated if the 'key' exists inside localStorage, in order to be able to merge the new values ​​under the same object.

  • If the 'key' already exists, it will extract from localStorage, the object that matches the 'key', and its values ​​will be merged with those of the new object, so that the new values ​​can overwrite one or several values ​​of the existing ones, in addition to add new values ​​to the object.

  • If the 'key' does not exist, it will simply be added with its respective value object.

    Parameters:

    | Name | Type | Default | Description | | -----|------|---------|------------ | | key | string | N/A | Name on which the values ​​will be stored in localStorage | | value | object | N/A | Value or values ​​to store in localStorage |

    Does not return any value.


setItems()

  LocalStorage.setItems(items);

It allows to store several keys with their respective objects, at the same time.

  • For each element that exists in the array, first get the localStorage object that matches the 'key' of the current element and combine the existing values ​​with the new values. This new value is stored under the current 'key'.

    Parameters:

    | Name | Type | Default | Description | | -----|------|---------|------------ | | items | SetItemsLocalStorage | N/A | Array of objects to store in localStorage |

    Does not return any value.


getItem()

  LocalStorage.getItem(key, value?, defaultValue?);

To get a value, look inside localStorage for the object that matches the value of 'key', and return the entire object.

  • If the value is not found, an empty object will be returned.

  • From the object returned from localStorage, a single value can be accessed instead of the entire object.

  • You can set a default value, in case you want to access an attribute of the object returned by localStorage, and it does not exist.

  • If the second argument is passed as an array, multiple objects can be requested, and those that are not found will be returned with the default value assigned.

    Parameters:

    | Name | Type | Default | Description | | -----|------|---------|------------ | | key | string | N/A | Name of values ​​stored in localStorage | | value | string \| string[] | N/A | Value or values ​​that you want to access | | defaultValue | any | * | Default value in case value does not exist |

    Return an object that matches the value of 'key' or an empty object.


getItems()

  LocalStorage.getItems(items);

Allows you to get multiple keys from localStorage, as well as the object assigned to each key.

  • For each element that exists in the array, it allows obtaining the value corresponding to that 'key', a single element of that 'key' or multiple elements existing in the current 'key'.

    Parameters:

    | Name | Type | Default | Description | | -----|------|---------|------------ | | items | GetItemsLocalStorage | N/A | Array of objects to get from localStorage |

    Return an array of objects matching each of the 'key' or an empty array.


removeItem()

  LocalStorage.removeItem(key?);

You can completely clean localStorage or remove one or more 'keys'. Keep in mind that when removing 'keys', their assigned objects will also be removed.

  • If 'key' is passed as a string, the matching 'key' will be removed, as well as its assigned object.

  • If the 'key' is passed as an array, all matching 'keys' will be removed, as well as their assigned objects.

  • If the 'key' is not passed, the entire localStorage will be cleaned, regardless of whether there are values ​​that were added with this functionality or not.

    Parameters:

    | Name | Type | Default | Description | | -----|------|---------|------------ | | key | string \| string[] | N/A | Name(s) on which to delete the values ​​in localStorage |

    Does not return any value.


getKeyName()

  LocalStorage.getKeyName();

When passed a number n, this method returns the name of the nth key in storage.

Return the name of the index found, if it does not exist it returns an empty string.


getCapacity()

  LocalStorage.getCapacity();

Get the number of 'keys' stored in localStorage.

Devuelve el número de 'claves' almacenadas en localStorage.

 

Data Types

SetItemsLocalStorage

This data type is an array of objects, where each object has the following structure.

Interface:

Name Type Description
key string Name on which the values ​​will be stored in localStorage
value object Value or values ​​to store in localStorage

GetItemsLocalStorage

This data type is an array of objects, where each object has the following structure.

Interface:

Name Type Description
key string Name of values ​​stored in localStorage
value string \ string[] Value or values ​​that you want to access
defaultValue any Default value in case value does not exist

 

Resources

License

MIT

 

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[3.0.0] - 2023-10-24

Fixed

  • Error when marking module type within package.json is corrected

[2.0.1] - 2023-10-24

Fixed

  • Prevent localStorage from being used if it is not available, this supports Next.js / Node.js if running server side.

[2.0.0] - 2023-10-24

Changed

  • Project dependencies are updated to provide support
  • Removed private property 'storage' from class 'LocalStorage'
  • 'localStorage' is accessed directly and no longer through the 'window' object
  • The weight of the production package is reduced
  • The package of type 'module' is specified within the package.json

[1.4.1] - 2022-11-10

Fixed

  • The source attribute of the package.json is removed, this throws alerts in the console.

[1.4.0] - 2022-08-30

Added

  • Added support for nodejs, main

Changed

--- The code is transpiled with rollup

[1.3.1] - 2022-08-20

Changed

  • The name of the testing folder is modified by tests, as well as its references.

[1.3.0] - 2022-08-20

Added

  • Data types are added to be able to extract when importing the module.
  • gitHooks are added.

Changed

  • The links of the README.md that point to the tests are adjusted.
  • The structure of src is modified.
  • The dist folder, the repository is not sent.
  • Changed 'commonjs' module build type to 'AMD'.

[1.2.0] - 2022-08-17

Changed

  • Testing folder is moved from src/ to the root of the project.
  • Move typescript from Dependencies to devDependencies.
  • eslint and prettier configuration files are adjusted.
  • The jest configuration is passed to a jest.config.js in the root of the project.

[1.1.3] - 2022-08-16

Added

  • MIT license added.

[1.1.2] - 2022-08-15

Fixed

  • With getItem or getItems, it allows returning a 'false' value as valid and not returning the default value.

[1.1.1] - 2022-08-15

Added

  • README.md is updated and "Table of Contents" and "Resources" are added to it.

Changed

  • In getItems function, add default value for multi value inside each item.
  • Tests are updated.

Fixed

  • Fixed bug in README.md anchors.

[1.1.0] - 2022-08-14

Added

  • setItems function, allows to store multiple keys and their respective objects.
  • getItems function, allows to obtain multiple keys and their respective objects.
  • getKeyName function, returns the name of the nth stored key.
  • getCapacity function, get the number of 'keys' stored in localStorage.

[1.0.0] - 2022-08-14

Added

  • Package init