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

stash-it-plugin-prefixsuffix

v1.1.0

Published

Prefix / suffix plugin for stash-it

Downloads

2

Readme

logo-stash-it-color-dark 2x

stash-it-plugin-prefixSuffix

build status Coverage Status

Prefix / suffix plugin for stash-it.

The problem

Imagine there are couple of teams working on a product that uses stash-it and the same storage. Each team would like to use any keys to store the items, and not to be worried that some names are already taken.

Solution

Using this plugin, you're able to create cache instances that will make sure that all of the keys you use will have (either or both) prefix / suffix of your choosing included automatically.

Installation

npm i stash-it-plugin-prefixsuffix --save

Usage

import { createCache } from 'stash-it';
import createPrefixSuffixPlugin from 'stash-it-plugin-prefixsuffix';

// You can use any adapter
import createMemoryAdapter from 'stash-it-adapter-memory';

const team1Prefix = 'team1';
const team2Prefix = 'team2';
const cache = createCache(createMemoryAdapter());

const team1PrefixPlugin = createPrefixSuffixPlugin({ prefix: team1Prefix });
const team2PrefixPlugin = createPrefixSuffixPlugin({ prefix: team2Prefix });

const team1CacheInstance = cache.registerPlugins([ team1PrefixPlugin ]);
const team2CacheInstance = cache.registerPlugins([ team2PrefixPlugin ]);

// TEAM 1
team1CacheInstance.hasKey('key'); // false
team1CacheInstance.setItem('key', '11_TEAM_11');

const item = team1CacheInstance.getItem('key');

item.value; // 11_TEAM_11
item.key; // team1key

// TEAM 2
team2CacheInstance.hasKey('key'); // false
team2CacheInstance.setItem('key', '22_TEAM_22');

const item = team2CacheInstance.getItem('key');

item.value; // 22_TEAM_22
item.key; // team2key

Plugin extends cache instance with two methods: getPrefix() and getSuffix(). They return prefix / suffix passed upon plugin's creation.

import createPrefixSuffixPlugin from 'stash-it-plugin-prefixsuffix';

const prefix = 'somePrefix';
const suffix = 'someSuffix';

// assuming you already have cache instance created

const plugin = createPrefixSuffixPlugin({ prefix, suffix });
const cacheWithPrefixSuffix = cache.registerPlugins([ plugin ]);

cacheWithPrefixSuffix.getPrefix(); // somePrefix
cacheWithPrefixSuffix.getSuffix(); // someSuffix

API

Plugin takes an object, as an argument, upon creation. This object must contain at least one of the properties: prefix and / or suffix. Can have both.

const plugin1 = createPrefixSuffixPlugin({ prefix, suffix }); // OK
const plugin2 = createPrefixSuffixPlugin({ prefix }); // OK
const plugin3 = createPrefixSuffixPlugin({ suffix }); // OK
const plugin4 = createPrefixSuffixPlugin(); // will throw

prefix and suffix must be a string, containing any characters you want.

String(s) will be concatenated with key like so: ${prefix}${key}{$suffix}.

But as a user, you only need to pass the key when using stash-it's API.