Giter VIP home page Giter VIP logo

searchhash's Introduction

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.

searchhash's People

Contributors

fedeghe avatar signavio-fghedina avatar

Stargazers

 avatar conyH avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

zheeno jacob-abe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.