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

cjs-indexing-hash

v1.3.0

Published

auto-indexing hash - just add index name and logic

Downloads

59

Readme

cjs-indexing-hash

lightweight key-value store with support for auto-updating indexes ( indices? you know what i mean ... )

npm version Coverage Status


install

npm i cjs-indexing-hash


how to use

create new instance (empty)

var create_indexing_hash = require('cjs-indexing-hash'),
    indexing_hash = create_indexing_hash();

create new instance from existing data

> objects

var users = {
      batman: { is_cool: true },
      superman: { is_cool: false }
    },
    indexable_users = create_indexing_hash( users );

> arrays

var users = [
      { id: batman, is_cool: true },
      { id: superman, is_cool: false }
    ],
    indexable_users = create_indexing_hash( users );

> strings

var indexable_string = create_indexing_hash( 'why would you want to do this? who cares! it works!!!' );

get entry

// from object
var batman = indexable_users.get( 'batman' );

// from array
var batman = indexable_users.get( 0 );

// from string
var y = indexable_string.get( '2' );

add entry

indexable_users.add( 'wonder_woman', { is_cool: true });

create index

indexable_users.add_index( 'is-cool', function( entry, add_to_index ){
  if(  entry.is_cool == true ) add_to_index();
});

get keys in index

var cool_users = indexable_users.index_get( 'is-cool' );

get keys in multiple indexes (indices)

var cool_users_with_capes = indexable_users.intersect_indexes([ 'is-cool', 'has-cape' ]);

hooks

default indexing-hash behavior

written in expected hook execution order

// hook: key-created
//       * executes whenever an item is added to indexing-hash
hooks.add( 'key-created', 'update-collection-keys-cache', ({ key, val }) => {})
hooks.add( 'key-created', 'index-new-entry', ({ key, val }) => {})


// hook: key-deleted
//       * executes whenever an item is removed from indexing-hash
hooks.add( 'key-deleted', 'update-collection-keys-cache', ({ key, val }) => {})
hooks.add( 'key-deleted', 'remove-from-indexes', ({ key, val }) => {})

// hook: key-updated
//       * executes whenever an item in indexing-hash is updated
hooks.add( 'key-updated', 'update-indexes', ({ key, val }) => {})

// hook: index-created
//       * executes whenever an index is added to indexing-hash
hooks.add( 'index-created', 'populate-created-index', ({ name }) => {})

create custom indexing-hash behavior

indexable_users.hooks.add( 'key-created', 'log-user-creation', ({ key, val }) => {
      const user = val
      console.log( `action=user-created id=${ key } cool=${ user.is_cool }` )
})

indexable_users.hooks.add( 'index-created', 'log-index-creation', ({ name }) => {
      console.log( `action=index-created name=${ name }` )
})

indexable_users.hooks.add( 'key-deleted', 'send-user-deletion-email', ({ key, val }) => {
      const user_at_deletion = val
      if (user_at_deletion.email) {
            // send email
            console.log( `action=send-deletion-email user=${ key }` )
      }
})