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

keystore2

v1.1.6

Published

Globally reusable value / reference storage that supports all data types; and 2 because all the first ones are inferior.

Downloads

30

Readme

KeyStore2 Build Status Node Security Status

Globally reusable value / reference storage that supports all data types; and 2 because all the first ones are inferior.

Simply put, instead of polluting your functions & promises w/ multiple arguments / single-arity functions, these keystore2 contexts gives your code more readability, and code reusability.

Best use cases / works best with:

  • Heavy asynchronous workloads (ie: working w/ multiple objects at the same time)
  • Chained promises
  • Pure functions that never sacrifices reusability.

Install

$ npm install --save keystore2

$ npm update
# updating an old version installed

Usage

Basics

const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

_KeyStore.set('a', 123);
// true

_KeyStore.get('a');
// 123

_KeyStore.has('a');
// true

_KeyStore.has('b');
// false

Using contexts

const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

_KeyStore.pullContext('myContext').set('b', 456);
// true

_KeyStore.pullContext('myContext').get('b');
// 456

_KeyStore.clearContext('myContext');
// true

_KeyStore.pullContext('myContext').get('b');
// undefined

Nested contexts

const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

_KeyStore.pullContext('myContext').pullContext('mySubContext').set('c', 789);
// true

_KeyStore.pullContext('myContext').pullContext('mySubContext').get('c');
// 789

Random contexts (Added in 1.1.0)

const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

var randomContext = _KeyStore.pullRandomContext();

randomContext.id;
// uuidv4 random id, like '0b99b82f-62cf-4275-88b3-de039020f14e'

randomContext.set('a', 5);
// true

randomContext.get('a');
// 5

// Random contexts can also be nested!

randomContext.pullContext('nestedInRandom').set('c', 123);
// true

randomContext.pullContext('nestedInRandom').get('c');
// 123

Real-world example

const gulp = require('gulp');
const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

function FirstPromise(_context){

	var _KeyStoreContext = _KeyStore.pullContext(_context);
	
	return new Promise(function(resolve, reject){
	
		// promise chain ejecting if context-based error is present
		if(_KeyStoreContext.has('error') === true){
			reject(_context);
		}
		
		// some tasks..
		var _someKey = _KeyStoreContext.get('someKey');
		
		// on success
		_KeyStoreContext.set('newKey', 'sampleStringValue');
		resolve(_context);
		
		// OR
		
		// on failure:
		reject('some error message');
		
		// or this, if you wanna store it in the context.
		_KeyStoreContext.set('error', 'some error message.');
		reject(_context);
		
	});
	
}

// function SecondPromise...

// function ThirdPromise...

gulp.task(`test`, () => {
	return FirstPromise('myContext')
		.then((_context)=>SecondPromise(_context))
			.catch((err)=>{throw new Error(err);})
		.then((_context)=>ThirdPromise(_context))
			.catch((err)=>{throw new Error(err);});
});

Notes, References, Todo's, etc.

  • Changelog:
    • 1.1.0
      • keystore.pullRandomContext()
        • creates a uuidv4-based randomized context.
      • keystore.pullRandomContext().id
        • 'id' retrieves the generated UUIDv4 for the random context.
      • nested random contexts
        • random contexts can be nested just like pulled contexts
      • keystore.has(key)
        • to check if key is set, returns true or false.
      • readme updates
        • lol
  • Todo's:
    • Context & Key Storage Adapters, ie, using redis or other remote cache microservices.
  • References:
    • Jed's UUIDv4 Implementation, used in randomized context implementation.
      • https://www.npmjs.com/package/uuid-random
    • Mozilla Developer Network, for all the inner-workings.
      • https://developer.mozilla.org/en-US/docs/Web/JavaScript
    • XO for linting
      • https://github.com/sindresorhus/xo
    • AVA for testing
      • https://github.com/avajs/ava

Testing w/ xo & ava

npm install && npm test

License

MIT © servercharlie