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

key-guard

v1.0.1

Published

A module to create keys based on rules.

Downloads

1

Readme

key-guard

What is this about?

Orignally this module was created to keep track of keys in redis. But you can use it for any other application of course. Redis is an awesome key/value store, but you can easily lose track of all keys used in production, because there is no schema that every developer of your team has to follow.

Install

npm install key-guard --save

How to use?

const coreKey = require("key-guard")(options);

optionsis an object with following attributes:

  • namespace - string - A string that is prefixed to every generated key.
  • minLength- number - Min length of generated key (default: 1).
  • maxLength- number - Max length of generated key (default: 100).
  • fragments- object - A fragment is a string that is part of the key and separated by fragments.delimiter.
  • fragments.delimiter- string - Delimiter between every key fragement (default: :).
  • fragments.regexp- regexp - Regular expression that every key fragment is checked against (default: /^[a-zA-Z0-9]+$/).
  • fragments.count.min- number - Min number of fragments (default: 2).
  • fragments.count.max- number - Max number of fragments (default: 5).
  • fragments.len.min- number - Min length of fragment string (default: 2).
  • fragments.len.max- number - Max length of fragment string (default: 20).
{
  namespace: 'myApp',
  minLength: 1,
  maxLength: 100,
  fragments: {
    delimiter: ':',
    count: {
      min: 2,
      max: 5
    },
    len: {
      min: 2,
      max: 20
    },
    regexp: /^[a-zA-Z0-9]+$/
  }
}

Example

const keyGuard = require("key-guard");
let coreKey = keyGuard({
  namespace: "myApp"
});
let key = coreKey().update("users").update("283").get();
console.log(key);
// => "myApp:users:283"

You can also overwrite the namespace or entire prefixes:

let key = coreKey("newNamespace:records").update("2384").get();
console.log(key);
// => "newNamespace:records:2384"

Pro tip #1

Create one file that creates the core key and the is included in every other page that needs generated keys:

coreKey.js

const keyGuard = require("key-guard");
module.exports = keyGuard({
  namespace: "myApp"
});

index.js

const coreKey = require("./coreKey.js");
let key = coreKey().update("users").update("283").get();
console.log(key);
// => "myApp:users:283"

Pro tip #2

Usually scripts use keys for different fragments/sections multiple times. Declare a base key on top of each script or within coreKey.js.

coreKey.js

const keyGuard = require("key-guard");
module.exports = keyGuard({
  namespace: "myApp"
});

users.js

const coreKey = require("./coreKey.js");
const usersKey = coreKey().update("users");

console.log(usersKey.update("283").get());
console.log(usersKey.update("284").get());
console.log(usersKey.update("285").get());
// => "myApp:users:283"
// => "myApp:users:284"
// => "myApp:users:285"

Pro tip #3

If you do not want to have any literals within you scripts, load variables from config files or environment variables.

coreKey.js

const keyGuard = require("key-guard");
module.exports = keyGuard({
  namespace: process.env.REDIS_NAMESPACE_KEY
});

> REDIS_NAMESPACE_KEY=myApp node index.js