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

local-storage-to-object

v3.0.0

Published

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

Downloads

31

Readme

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