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

simplestorage.js

v0.2.1

Published

Cross-browser key-value store database to store data locally in the browser

Downloads

1,722

Readme

simpleStorage

Cross-browser key-value store database to store data locally in the browser.

simpleStorage is a fork of jStorage that only includes the minimal set of features. Basically it is a wrapper for native JSON + localStorage with some TTL magic mixed in.

The module has no dependencies, you can use it as a standalone script (introduces simpleStorage global) or as an AMD module. All modern browsers (including mobile) are supported, older browsers (IE7, Firefox 3) are not.

simpleStorage is very small - about 1kB in size when minimized and gzipped.

Build Status

Install

Quickest way to get up and running woulr be to use jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/simplestorage/0.2.1/simpleStorage.min.js"></script>

Otherwise you can download simpleStorage.js or install it with bower:

bower install simpleStorage

and include the following script in your web application: bower_components/simpleStorage/simpleStorage.js

or install with npm

npm install simplestorage.js

Support simpleStorage development

Donate to author

If you want to support with Bitcoins, then my wallet address is 15Z8ADxhssKUiwP3jbbqJwA21744KMCfTM

Usage

simpleStorage API is a subset of jStorage with slight modifications, so for most cases it should work out of the box if you are converting from jStorage. Main difference is between return values - if an action failed because of an error (storage full, storage not available, invalid data used etc.), you get the error object as the return value. jStorage never indicated anything if an error occurred.

Possible error codes (from err.code):

  • "LS_NOT_AVAILABLE" means that localStorage is not supported by this browser
  • "LS_DISABLE" means that localStorage is supported by this browser but it can't be used for whatever reason (privacy mode, manual disabling etc.)
  • "LS_QUOTA_EXCEEDED" means that the allocated quota for localStorage is all used up or would be if current value is stored
  • anything else, no idea

set(key, value[, options])

Store or update a value in local storage.

simpleStorage.set(key, value[, options])

Where

  • key - the key for the value
  • value - value to be stored (can be any JSONeable value)
  • options - optional options object. Currently the only available option is TTL which sets the time-to-live (TTL) value in milliseconds for the given key/value
// the following entry expires in 100 seconds
simpleStorage.set(key, value, {TTL: 100000})

Return values

  • true - value was stored
  • false - value was not stored
  • Error object - value was not stored because of an error. See error.code for explanation

get(key)

Retrieve a value from local storage.

value = simpleStorage.get(key)

Where

  • key - the key to be retrieved

Method returns the value for a key or undefined if the key was not found.

hasKey(key)

Checks if there's a value with the given key in the local storage.

simpleStorage.hasKey(key)

Where

  • key - the key to be checked

Method returns true if the given key exists, false otherwise.

deleteKey(key)

Removes a value from local storage.

simpleStorage.deleteKey(key)

Return values

  • true - value was deleted
  • false - value was not found
  • Error object - value was not deleted because of an error. See error.code for explanation

setTTL(key, ttl)

Set a millisecond timeout. When the timeout is reached, the key is removed automatically from local storage.

simpleStorage.setTTL(key, ttl)

Where

  • key - the key to be updated
  • ttl - timeout in milliseconds. If the value is 0, timeout is cleared from the key

Return values

  • true - ttl was set
  • false - value was not found
  • Error object - ttl was not set because of an error. See error.code for explanation

getTTL(key)

Retrieve remaining milliseconds for a key with TTL

ttl = simpleStorage.getTTL(key)

Where

  • key - the key to be checked

Return values

  • finite number - remaining milliseconds
  • Infinity - TTL is not set for the selected key
  • false - selected key does not exist or is expired

flush()

Clear all values

simpleStorage.flush()

Return values

  • true - storage was flushed
  • Error object - storage was not flushed because of an error. See error.code for explanation

index()

Retrieve all used keys as an array

list = simpleStorage.index()

Returns an array of keys.

storageSize()

Get used storage in symbol count

simpleStorage.storageSize()

canUse()

Check if local storage can be used

simpleStorage.canUse()

Returns true if storage is available

Demo

See demo here.

Tests

See QUnit tests here.

License

Unlicense