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

betterstorage

v2.0.4

Published

A better storage wrapper

Downloads

9

Readme

BetterStorage

Downloads Downloads NPM Version Dependencies Dev Dependencies License Build Status Code Climate Test Coverage

A better storage wrapper

This library adds a nice api wrapper for many storage methods.

This library now defaults to promise based returns for all methods. Synchronous methods are available via import { BetterStroage } from 'betterstorage'.

Basic Usage

import BetterStorage from 'betterstorage';

const BasicStore = new BetterStorage();
BasicStore.set('Test', { test: 'test' }).then(() =>{
  BasicStore.has('Test').then((has) => {
    if (has) {
      BasicStore.get('Test').then((value) => {
        console.log(value); // { test: 'test' }
      });
    }
  });
});

You can prefix your items in the store

import { BetterStorage } from 'betterstorage';
const AwesomeStore = new BetterStorage('Awesome');
AwesomeStore.set('Test', { test: 'test' });
AwesomeStore.has('Test') // true
AwesomeStore.get('Test'); // { test: 'test' }

This will prefix Test with Awesome like so Awesome-Test.

Changing from local storage

By defualt the store uses local storage. To change this pass the name or the object as the second parameter.

import { BetterStorage } from 'betterstorage';
const AwesomeLocalStore = new BetterStorage('Awesome', 'local');
const AwesomeSessionStore = new BetterStorage('Awesome', 'session');

All methods

contructor new BetterStorage(prefix = '', kind = 'local', overrides = {}) See Overrides section for more details on that param

setPrefix setPrefix(prefix = '', changeExisting = false) Allows for changing the prefix of the store. If changeExisting is true and you currently have a prefix all items that were prefixed will have their prefix changed.

setKind setKind(kind, migrateData = false) This method allows you to change the type of storage that is being used. This is the same as the second param in the constructor. If migrateData is true and you currently have a prefix all items that were prefixed will be moved to the new storage type.

set set(key, value) Sets a item into the store

get get(key) Gets an item from the store

remove remove(key) Removes an item from the store

has has(key) Checks if key exists in the store

clear clear(full = false) Clears the store of items you have set. If there is no prefix set or if full is true then the entire store is cleared.

getLength getLength() This return the number of items with the set prefix.

getFullLength getFullLength() This returns the number of all items in the store.

key key(n) This returns the key at index out of prefixed items.

fullKey_ fullKey(n) This returns the key at index out of all items in the store.

Overrides

You can pass in methods to override the default actions for betterstorage. This is most useful if you need a different storage type then the default 'local' and 'session'. When using the default async storage api then these methods must return promises. When using synchronous these methods should not return promises

The methods that you can override are

  • doSet
  • doGet
  • doRemove
  • doClear
  • doLength
  • doFullLength
  • doKey
  • doFullKey
  • doHas

Example:

import BetterStorage from 'betterstorage';
import { setItem, getItem, removeItem, clear } from 'redux-effects-localStorage';

const ReduxEffectsStore = new BetterStorage('Effects', 'local', {
  doGet: (key) => dispatch(getItem(key)),
  doSet: (key, value) => dispatch(setItem(key, value)),
  doRemove: (key) => dispatch(removeItem(key)),
  doClear: () =>  dispatch(clear()),
});

ReduxEffectsStore.set('Test', { test: 'test' }).then(() => {
  ReduxEffectsStore.has('Test').then(has => {
    if (has) {
      ReduxEffectsStore.get('Test').then((data) => { console.log(data); });
    }
  });
});