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

@msschambach/kangajs

v1.3.0

Published

A light wrapper for the Web Storage API.

Downloads

3

Readme

Build Status codecov npm version

Kanga JS

A light wrapper for the Web Storage API.

Features

  • Interfaces with the DOM localStorage and sessionStorage objects
  • Supports storage of Objects and Arrays
  • Is library agnostic, meaning it can work with all the big JavaScript client side frameworks
  • Light weight and easy to use

Installation and Usage

Install from npm

npm install --save @msschambach/kangajs

To use it directly in browsers just copy kanga.web.min.js from the dist folder.

<script type="text/javascript" src="kanga.web.min.js"></script>

To use it with a bundler such as webpack or rollup just import it.

// Javascript or Typescript
import { BrowserStore } from '@msschambach/kangajs'

const store = new BrowserStore();

To use the library is simple, just initiate a new BrowserStore object and you're good to go.

<script type="text/javascript">
  store = new Kanga.BrowserStore(); // Will use localStorage

  store2 = new Kanga.BrowserStore(false); // Will use sessionStorage

  store.set('user',{name:'James Bond', email:'[email protected]', bio:'I spy for a living.'});

  store.log('user'); // {"name":"James Bond","email":"[email protected]","bio":"I spy for a living."}
</script>

API

Constructor

A new BrowserStore instance can be instantiated as shown below:

const store = new Kanga.BrowserStore()();

The constructor accepts one optional boolean paremeter which if set to true will instantiate an instance that maps to the localStorage object and if ignored or set to false it will instantiate an instance mapping to the sessionStorage object.

.set(key, value)

Sets a key to a given value in storage. Analogous to Storage.setItem(). For example:

store.set('user',{name:'James Bond', email:'[email protected]', bio:'I spy for a living.'});

store.set('visit_count', 4);

store.set('browser','mozilla');

.find(key)

This returns the record identified by key. It returns a Record Object, which will be shown a bit later. Analogous to Storage.getItem(). For example:

var user = store.find('user'); // Record {name: "user", data: Object, $storage: Storage, toString: function, save: function…}

console.log(user.data.name): // James Bond

.findAll()

Returns all records in storage as an array of Record objects. For example:

console.log(store.findAll()); // [Record, Record]

.each((record: Record, key: string) => void)

Allows you to perform actions while iterating through all the records in the store. For example:

store.each((record) => {
  record.data.updated = new Date();
  record.save();
});

.findAt(index,[get_value])

Returns the record at index in storage. Analogous to Storage.key().

Has an optional trailing parameter which if set to true will make the function return the actual value of the key. For example:

console.log(store.findAt(1)); // visit_count

console.log(store.findAt(1,true)); // 4

.indexOf(key)

Returns the index of a key in storage. For example:

console.log(store.indexOf('visit_count')); // 1

.delete(key)

Removes a key from storage. Analogous to Storage.removeItem. For example:

store.delete('user');

console.log(store.find('user')); // null

.deleteAll()

Removes all keys from the storage. Analogous to Storage.clear(). For example:

store.deleteAll();

console.log(store.findAll()); // []

.deleteAt(index)

Removes a key at index in storage. For example:

store.deleteAt(0);

console.log(store.findAt(0)); // null

.log(key)

Logs the value of key to the console. For example:

store.log('user'); // {"name":"James Bond","email":"[email protected]","bio":"I spy for a living."}

.mode

The current operation mode, i.e. whether data is being stored in localStorage or sessionStorage. For example:

console.log(store.mode); // sessionStorage

Record

.find() .findAll() and .findAt() all return either a single or an array of Record objects.

These objects have the following API

.toString()

Produces a string value of the data. For example:

var user = store.find('user');

console.log(user.toString()); // {"name":"James Bond","email":"[email protected]","bio":"I spy for a living."}

.save()

Saves and updates the record in storage. For example:

var user = store.find('user');

console.log(user.toString()); // {"name":"James Bond","email":"[email protected]","bio":"I spy for a living."}

user.data.name = "Jason Statham";
user.data.email = "[email protected]";
user.data.bio = "I got moves.";

user.save();

console.log(store.find('user').toString()); // {"name":"Jason Statham","email":"[email protected]","bio":"I got moves."}

.delete()

Deletes or removes the record from storage. For example:

var user = store.find('user');

console.log(user.toString()); // {"name":"James Bond","email":"[email protected]","bio":"I spy for a living."}

user.delete();

console.log(store.find('user').toString()); // null

.data

The value of the record. Usage is shown in the examples above.

.name

The name of the record. Corresponds to the key of the record.

.$storage

A reference to the storage being used.

Copyright Schambach Milimu

Refer to LICENSE