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

@nexucis/kvsearch

v0.9.1

Published

KVSearch ========

Downloads

49,180

Readme

KVSearch

Installation

npm install @nexucis/kvsearch

Usage

  1. Filter a list of object using a list of index
import { KVSearch } from '@nexucis/kvsearch';

const list = [
    {
        labels: { instance: 'demo.org', job: 'demo' },
        scrapePool: 'scrapePool demo'
    },
    {
        labels: { instance: 'k8s.org', job: 'constellation' },
        scrapePool: 'galaxy'
    }
]
const search = new KVSearch({
    indexedKeys: [
        'labels',
        'scrapePool',
    ],
});
search.filter('demo', list) // will match the first object
search.filter('constellation', list) // won't match any object present in the list, since the attribute `labels.jop` is not indexed

Here the indexed list says:

  • Since labels value is an object, then check if the pattern is matching a key in the object return by labels
  • Same thing for scrapePool, excepting it returns a string, so the code won't loop other a list of key, it will just check if the value of scrapePool is matching the pattern

Note that the matching is using the lib @nexucis/fuzzy, so it's not an exact match used.

  1. Filter a list of object using a list of index, with some regexp
import { KVSearch } from '@nexucis/kvsearch';

const list = [
    {
        labels: { instance: 'demo.org', job: 'demo' },
        scrapePool: 'scrapePool demo'
    },
    {
        labels: { instance: 'k8s.org', job: 'constellation' },
        scrapePool: 'galaxy'
    }
]
const search = new KVSearch({
    indexedKeys: [
        'labels',
        ['labels', /.*/],
        'scrapePool',
    ],
});
search.filter('constel', list) // will match only the 2nd object

The difference here is we indexed the attributes of the labels object. In this example, by using the Regexp /.*/ we indexed every attribute of the object labels. That's why the pattern constellation is matching the second object. But we could also just index the field job of the labels like that ['labels', 'job']. It would have worked as well.

  1. Filter a list of object using a specific query.

Using a list of index is simple, but it always used a fuzzy match. Probably sometimes you would like to do an exact match or a negative match depending on the context.

You can do it by creating your own query like that:

import { KVSearch } from '@nexucis/kvsearch';

const list = [
    {
        labels: { instance: 'demo.org', job: 'demo' },
        scrapePool: 'scrapePool demo'
    },
    {
        labels: { instance: 'k8s.org', job: 'constellation' },
        scrapePool: 'galaxy'
    }
]
const search = new KVSearch();
search.filterWithQuery({ keyPath: ['labels', /.*/], match: 'exact', pattern: 'constellation' })
  1. Filter a list of object using a complex query.

It's possible to combine query together, so you can write multiple conditions.

import { KVSearch } from '@nexucis/kvsearch';

const list = [
    {
        labels: { instance: 'demo.org', job: 'demo' },
        scrapePool: 'scrapePool demo'
    },
    {
        labels: { instance: 'k8s.org', job: 'constellation' },
        scrapePool: 'galaxy'
    },
    {
        labels: { instance: 'awx.com', job: 'constellation' },
        scrapePool: 'galaxy',
    }
]
const search = new KVSearch();
search.filterWithQuery({
    operator: 'and',
    left: {
        keyPath: ['scrapePool'],
        match: 'fuzzy',
        pattern: 'gal'
    },
    right: {
        keyPath: ['labels', 'instance'],
        match: 'exact',
        pattern: 'awx.com'
    }
}) // this query is matching the last element of the list.

Note as it can be painful to write the query himself, a support to write it with a string in the codemirror editor is coming soon.

For the moment, the codemirror support doesn't handle the regexp.