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

searchhash

v1.2.12

Published

Simple module to search into object literals for values or keys

Downloads

617

Readme

Coverage Status noai

searchHash

> npm install searchhash

This module allows to search into an object literal at any level for:

  • key
  • value
  • key && value

RegExp can be used also, for example the following calls are valid:

import sh from 'searchhash'
import obj from './../your/data.json'

// for key
const Kres = sh.forKey(obj, 'name'),
    rxKres = sh.forKey(obj, /name/),

    // for value
    Vres = sh.forValue(obj, 'Frances'),
    rxVres = sh.forValue(obj, /^fran/i),

    // for key && value
    KVres = sh.forKeyValue(obj, {key:'name', value : 'Frances'}),
    rxKVres = sh.forKeyValue(obj, {key:/name/, value : /^fran/i}),

    // to search You can as well use a function:
    KresFun = sh.forKey(obj, k => ['one', 'two'].includes(k)),
    VresFun = sh.forValue(obj, v => v % 2 === 0),
    KVresFun = sh.forKeyValue(obj, {
        key: k => ['one', 'two'].includes(k),
        value: v => v % 2 === 0
    });

Some options can be passed in a third generic parameter:

  • limit [Integer]: stop searching whenever this number of results is reached (default Infinity)
  • min [Integer]: start from that depth level, included (default 0)
  • max [Integer]: do not go deeper than that level, included (default Infinity)
  • sorter [function]: a function to be used to sort the results

for example

rxKres = sh.forKey(obj, /name/, { limit: 10, min: 2, max: 4});

will find at most 10 elements but only at a deepness between 2 and 4.

The result is an array containing 0 or more elements. The following is one of the element returned from a search:

{ 
    obj: { a: 4, c: 'end' },  // the parent obj containig the result
    value: 4,                 // the value corresponding to the result
    key: 'a',                 // the key corresponding to the result
    parentKey: 'b',           // the key of the parent element
    path: 'b/b/b/a',          // the absolute path in the obj (in case)
                              //    of arrays may contain numbers for the 
                              //    indexes
    getter: [Function]        // a function that returns the result getting
                              //    it from the original obj
    container: 'b/b/b',       // path for the container
    parentContainer: 'b/b',   // path for the grandcontainer
    regexp: true,             // in case regexp is used in search contains 
                              //    the match result
    level: 3                  // depth of the result
}

To run the tests and know more about the implementation just get a look at the README.md into the source folder.