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

@pikapower9080/easy-storage

v1.0.4

Published

Lightweight library of localStorage helper functions.

Downloads

40

Readme

@pikapower9080/easy-storage

easy-storage is a JavaScript library that makes localStorage better by storing multiple values under one key.

Advantages of single-key storage

  • Reduces risk of conflict between keys when multiple apps are running on one domain (e.g. localhost)
  • Allows for the backing up, overwriting, and clearing of all stored data without touching data stored by other apps or userscripts

Usage

EasyStorage is made for use with ESM modules. It is documented with JSDoc for smart intellisense features. Here is a basic example of how to use the library:

import EasyStorage from '@pikapower9080/easy-storage'

const storage = new EasyStorage({
    key: "my-app-name"
})

if (!storage.get("first-visit", false)) {
    alert("Welcome!")
    storage.set("first-visit", new Date().getTime())
}

Additional methods are available and should be auto-completed by your editor.

Options

You can pass these options when you call new EasyStorage({})

| Key | Description | Default | | ------------------- | ---------------------------------------------------------------------------------- | -------- | | key | localStorage key to store all data in | config | | default | Default values to set if there is no data stored or the data is corrupted | {} | | migration.enabled | Enables the migration system which can move user data from an old key to a new one | false | | migration.old_key | The old key to migrate old user data from (migration.enabled must be true) | N/A |

Documentation

EasyStorage

Main class for EasyStorage.

Kind: global class

easyStorage.keys

Array of keys present in storage.

Kind: instance property of EasyStorage
Example

console.log(storage.keys.length)

easyStorage.getAll() ⇒ object

Gets all stored data as an object.

Kind: instance method of EasyStorage

easyStorage.set(key, value)

Set the value for a key and save it to localStorage.

Kind: instance method of EasyStorage

| Param | Type | Description | | --- | --- | --- | | key | string | The key to be stored inside of the main localStorage key | | value | any | Value to set |

easyStorage.get(key, [defaultValue]) ⇒ any

Get the value for a key.

Kind: instance method of EasyStorage
Returns: any - The stored value

| Param | Type | Description | | --- | --- | --- | | key | string | The key to get the value from inside of the main localStorage key | | [defaultValue] | any | Value to return if the data is not present |

easyStorage.clearAll()

Clear all stored data.

Kind: instance method of EasyStorage

easyStorage.setAll(data)

Replace all stored data with an object.

Kind: instance method of EasyStorage

| Param | Type | Description | | --- | --- | --- | | data | object | Object to replace stored data with |

easyStorage.getAllOfType(type) ⇒ object

Gets all values of a certain type.

Kind: instance method of EasyStorage
Returns: object - Object with filtered values

| Param | Type | Description | | --- | --- | --- | | type | string | Type to get |

easyStorage.getAllIf(predicate)

Gets all key/value pairs where the specified condition is true.

Kind: instance method of EasyStorage

| Param | Type | Description | | --- | --- | --- | | predicate | function | Predicate to check for key/value pairs to be included |

Example

storage.getAllIf((key, value, index) => key.includes('@'))

Example

storage.getAllIf((key, value, index) => {
      if (index % 2 === 0) {
        return true
      }
   })

easyStorage.toArray([flat]) ⇒ object

Returns all values in array format.

Kind: instance method of EasyStorage
Returns: object - Array of keys and values

| Param | Type | Description | | --- | --- | --- | | [flat] | boolean | Makes the array a list of objects with key and value properties |

easyStorage.forEach(callback)

Executes a callback function for each stored key/value pair.

Kind: instance method of EasyStorage

| Param | Type | Description | | --- | --- | --- | | callback | function | Function to be executed for each key |