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

@useful-tools/localstorage

v1.2.0

Published

A set of functions to use localstorage as if it were a database

Downloads

3

Readme

Localstorage

This package is designed to use localstorage as if it were a no-sql database, allowing to subscribe to changes.

Available methods

Please refer to integration tests to know how the library works.

It should set an entire collection to a specific value and then retrieve it

import LocalStorageDatabase from '@useful-tools/localstorage'

const DEFAULT_COLLECTION = 'test';
const value = [{ name: 'test_document'}];
LocalStorageDatabase.setCollection(DEFAULT_COLLECTION, value);

const currentCollectionValue = LocalStorageDatabase.getCollection(DEFAULT_COLLECTION);

expect(currentCollectionValue).to.be.an('array');
expect(currentCollectionValue).to.has.length(1);
expect(currentCollectionValue[0]).to.be.an('object');
expect(currentCollectionValue[0].name).to.eql('test_document');

It should be able to create a new document, generating a random id if it has not one, and retrieve the document later

import LocalStorageDatabase from '@useful-tools/localstorage'

const document = {
    name: 'test_document'
}

LocalStorageDatabase.createDocument(DEFAULT_COLLECTION, document);
const searchedDocument = LocalStorageDatabase.searchDocument(DEFAULT_COLLECTION, 'name', 'test_document');

expect(searchedDocument).to.be.an('array');
expect(searchedDocument).to.has.length(1);
expect(searchedDocument[0]).to.be.an('object');
expect(searchedDocument[0]).to.has.property('name');
expect(searchedDocument[0]).to.has.property('id');
expect(searchedDocument[0].name).to.eql('test_document');

It should be able to entirely replace a document by another one different

import LocalStorageDatabase from '@useful-tools/localstorage'

const initialDocument = {
    name: 'John Doe',
    job: 'Developer'
}

const replaceDocument = {
    name: 'John Doe',
    fanOf: 'Chuck Norris'
}
// Create the initial document
LocalStorageDatabase.createDocument(DEFAULT_COLLECTION, initialDocument);
// Then replace each document containing John Doe as name, by the replaceDocument
LocalStorageDatabase.setDocument(DEFAULT_COLLECTION, replaceDocument, 'name', 'John Doe');

const currentDocument = LocalStorageDatabase.searchDocument(DEFAULT_COLLECTION, 'name', 'John Doe');

expect(currentDocument).to.be.an('array');
expect(currentDocument).to.has.length(1);
expect(currentDocument[0]).to.be.an('object');
expect(currentDocument[0]).to.has.property('name');
expect(currentDocument[0]).to.has.property('id');
expect(currentDocument[0]).to.has.property('fanOf');
expect(currentDocument[0]).to.not.has.property('job');
expect(currentDocument[0].fanOf).to.eql('Chuck Norris');

It should be able to partially update a document

import LocalStorageDatabase from '@useful-tools/localstorage'

const document = {
    name: 'John Doe',
    job: 'Developer',
    languages: ['Javascript', 'Golang', 'Swift'],
    phoneNumber: '722222222'
}

const updateDocument = {
    phoneNumber: '733333333'
}

LocalStorageDatabase.createDocument(DEFAULT_COLLECTION, document);
LocalStorageDatabase.updateDocument(DEFAULT_COLLECTION, updateDocument, 'name', 'John Doe');

const currentDocument = LocalStorageDatabase.searchDocument(DEFAULT_COLLECTION, 'name', 'John Doe');
expect(currentDocument).to.be.an('array');
expect(currentDocument).to.has.length(1);
expect(currentDocument[0]).to.be.an('object');
expect(currentDocument[0]).to.has.property('name');
expect(currentDocument[0]).to.has.property('job');
expect(currentDocument[0]).to.has.property('languages');
expect(currentDocument[0]).to.has.property('phoneNumber');
expect(currentDocument[0].phoneNumber).to.eql('733333333');

It should be able to delete an entire collection

import LocalStorageDatabase from '@useful-tools/localstorage'

const value = [{ name: 'test_document'}];

LocalStorageDatabase.setCollection(DEFAULT_COLLECTION, value);
const initialCollectionValue = LocalStorageDatabase.getCollection(DEFAULT_COLLECTION);
LocalStorageDatabase.deleteCollection(DEFAULT_COLLECTION);
const postDeleteCollectionValue = LocalStorageDatabase.getCollection(DEFAULT_COLLECTION);

expect(initialCollectionValue).to.be.an('array');
expect(initialCollectionValue).to.has.length(1);
expect(postDeleteCollectionValue).to.be.an('array');
expect(postDeleteCollectionValue).to.has.length(0);

It should be able to delete an entire collection

import LocalStorageDatabase from '@useful-tools/localstorage'

const value = [{ name: 'test_document'}];

LocalStorageDatabase.setCollection(DEFAULT_COLLECTION, value);
const initialCollectionValue = LocalStorageDatabase.getCollection(DEFAULT_COLLECTION);
LocalStorageDatabase.deleteCollection(DEFAULT_COLLECTION);
const postDeleteCollectionValue = LocalStorageDatabase.getCollection(DEFAULT_COLLECTION);

expect(initialCollectionValue).to.be.an('array');
expect(initialCollectionValue).to.has.length(1);
expect(postDeleteCollectionValue).to.be.an('array');
expect(postDeleteCollectionValue).to.has.length(0);

### It should be able to delete a document

import LocalStorageDatabase from '@useful-tools/localstorage'

const document = {
    name: 'test_document'
}

LocalStorageDatabase.createDocument(DEFAULT_COLLECTION, document);
const existingDocument = LocalStorageDatabase.searchDocument(DEFAULT_COLLECTION, 'name', 'test_document');
LocalStorageDatabase.deleteDocument(DEFAULT_COLLECTION, 'name', 'test_document');
const nonExistingDocument = LocalStorageDatabase.searchDocument(DEFAULT_COLLECTION, 'name', 'test_document');

expect(existingDocument).to.be.an('array');
expect(existingDocument).to.have.length(1);
expect(nonExistingDocument).to.be.an('array');
expect(nonExistingDocument).to.have.length(0);

It should be able to send notifications to subscribers when a specific collection is altered

import LocalStorageDatabase from '@useful-tools/localstorage'

const defaultDocument = {test: 'document'};
LocalStorageDatabase.createDocument(DEFAULT_COLLECTION, defaultDocument);

const callback = sinon.stub();
const unsubscribe = LocalStorageDatabase.subscribeToLocalStorage(DEFAULT_COLLECTION, callback);

const document = {
    name: 'new_document'
}
LocalStorageDatabase.createDocument(DEFAULT_COLLECTION, document);
LocalStorageDatabase.deleteDocument(DEFAULT_COLLECTION, 'name', 'new_document');
unsubscribe();

expect(callback.callCount).to.eql(2);
expect(callback.getCall(0).calledWithExactly([defaultDocument, document])).to.eql(true);
expect(callback.getCall(1).calledWithExactly([defaultDocument])).to.eql(true);