searchhash
Simple module to search into object literals for values or keys
Last updated 7 months ago by fedeghe .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install searchhash 
SYNC missed versions from official npm registry.

Coverage Status Build Status

searchHash

> npm install searchhash

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

  • key
  • value
  • key:value

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

var sh = require('searchHash'),
    obj = require("your/data.json");

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

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

// for key:value
var 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:
var 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 this deepness level, included (default 0)
  • max [Integer]: do not go deeper than that level, included (default Infinity)

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 object containing a always a results array which contains 0 or more elements, and a timeElapsed field which contains the total time spent in the search. The following is one of the results element from a forKey search on a dummy obj, see test1.js):

{ 
    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
    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 README.md into the source folder.

Current Tags

  • 1.1.7                                ...           latest (7 months ago)

32 Versions

  • 1.1.7                                ...           7 months ago
  • 1.1.6                                ...           7 months ago
  • 1.1.5                                ...           7 months ago
  • 1.1.4                                ...           7 months ago
  • 1.1.3                                ...           7 months ago
  • 1.1.2                                ...           7 months ago
  • 1.1.1                                ...           8 months ago
  • 1.1.0                                ...           8 months ago
  • 1.0.27                                ...           9 months ago
  • 1.0.26                                ...           a year ago
  • 1.0.25                                ...           a year ago
  • 1.0.24                                ...           a year ago
  • 1.0.23                                ...           a year ago
  • 1.0.22                                ...           a year ago
  • 1.0.21                                ...           a year ago
  • 1.0.20                                ...           a year ago
  • 1.0.19                                ...           a year ago
  • 1.0.18                                ...           a year ago
  • 1.0.17                                ...           2 years ago
  • 1.0.16                                ...           2 years ago
  • 1.0.15                                ...           3 years ago
  • 1.0.14                                ...           3 years ago
  • 1.0.13                                ...           3 years ago
  • 1.0.11                                ...           3 years ago
  • 1.0.10                                ...           3 years ago
  • 1.0.9                                ...           3 years ago
  • 1.0.8                                ...           3 years ago
  • 1.0.7                                ...           3 years ago
  • 1.0.6                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 32
This Month 32
Last Day 32
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (16)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |