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

context-keys

v5.2.3

Published

Performant and feature rich library for managing context keys.

Downloads

111

Readme

Context Keys

Performant and feature rich library for managing context keys.

Features

  • Universal: this library works both in the browser and in Node.
  • Performant: this library is about as fast as it gets, and it has just 2 tiny first-party dependencies.
  • Flexible: context keys can be primitives, arrays or plain objects.
  • Expressive: expressions are written in a full-fledged subset of JavaScript, this allows you to write complex expressions like isFoo && ( !isBar || settings.foo[3] === "foo" ).
  • Safe: expressions are executed through safex, to ensure they are executed safely.
  • Batching: changes happening within a microtask are batched and coalesced together for performance automatically.

Install

npm install --save context-keys

Usage

The following interface is provided:

type ChangeHandler = ( value: boolean ) => void;
type ChangeAllHandler = () => void;
type Disposer = () => void;
type Expression = string;
type Key = string;
type Value = unknown;

class ContextKeys {

  // Create a new instance, optionally with some context keys
  constructor ( keys?: Record<Key, Value> );

  // Evaluate an expression to a boolean
  eval ( expression: Expression ): boolean;

  // Get the value of a context key
  get ( key: Key ): Value | undefined;

  // Get the values of all context keys
  getAll (): Record<Key, Value | undefined>;

  // Checks if a context key is defined
  has ( key: Key ) : boolean;

  // Remove single context key
  remove ( key: Key ): void;

  // Remove all context keys and change handlers
  reset (): void;

  // Add or update a context key
  set ( key: Key, value: Value ): void;

  // Register a callback which will be called whenever any context key changes
  onChange ( handler: ChangeAllHandler ): Disposer;
  // Register a callback which will be called whenever the value of the expression changes. Call the disposer to unregister the callback
  onChange ( expression: Expr, handler: ChangeHandler ): Disposer;

}

You can use it like this:

import ContextKeys from 'context-keys';

// Create a new instance, with some context keys already

const ck = new ContextKeys ({
  isFoo: true,
  isBar: false
});

// Evaluate an expression

ck.eval ( 'isFoo' ); // => true
ck.eval ( 'isBar' ); // => false
ck.eval ( 'isBaz' ); // => undefined
ck.eval ( 'isFoo && isBar || 123' ); // => 123

// Get the value of a context key

ck.get ( 'isFoo' ); // => true
ck.get ( 'isBar' ); // => false
ck.get ( 'isBaz' ); // => undefined

// Get the values of all context keys

ck.getAll (); // => { isFoo: true, isBar: false }

// Check if a context key is defined

ck.has ( 'isFoo' ); // => true
ck.has ( 'isBar' ); // => true
ck.has ( 'isBaz' ); // => undefined

// Set the value of a context key

ck.set ( isBar, true );
ck.set ( isBaz, 123 );

// Remove a context key

ck.remove ( 'isBaz' );

// Remove all context keys and change handlers

ck.reset ();

// Register a global onChange handler

ck.onChange ( () => {
  console.log ( 'Some context key changed!' );
});

// Register an expression-specific onChange handler

const dispose = ck.onChange ( 'isFoo', value => {
  console.log ( 'The result of the expression changed!' );
});

dispose (); // Dispose of that listener

License

MIT © Fabio Spampinato