GithubHelp home page GithubHelp logo

dig-deep's Introduction

DigDeep

DigDeep will look up key/value pairs in a nested hash by a given key. It will recursively dig each nested object and return all matching key/values.

Ruby's dig method can perform similar tasks, but you need to specify the path to the target key.

With this gem, you will only need to specify the target key and it will do the digging.

Installation

Add this line to your application's Gemfile:

gem 'dig-deep'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dig-deep

Test

$ bundle exec rspec spec

Usage

Example 1:

Given the following hash:

data = {
  a: {
    b: {
      c: "abc"
    }
  }
}

Ruby .dig:

data.dig(:a, :b, :c)   // "abc"

With DigDeep:

require 'dig-deep'

data.dig_deep(:c)      // "abc"

Example 2:

Dig up all :email from arrays of objects

data = {
  contacts: [{
    name: "John",
    email: "[email protected]"
  }, {
    name: "Mary",
    email: "[email protected]"
  }],
  work: {
    contacts: [{
      name: "ACME Corp.",
      email: "[email protected]"
    }, {
      name: "Asdf Inc.",
      email: "[email protected]"
    }]
  }
}
require 'dig-deep'

data.dig_deep(:email)  // ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]

Example 3:

Object with boolean, string, array...

data = {
  :l1 => {
    :l2 => {
      :l3 => {
        :l4a => "Level 4",
        :l4b => {
          :l5a => false,
          :l5b => {
            :l6 => ["apple", "orange"]
          }
        } 
      }
    }
  },
  :l7 => true,
  :l8 => {
    :l9 => 9876
  }
}
require 'dig-deep'

data.dig_deep(:l4a)   // "Level 4"

data.dig_deep(:l5a)   // false

data.dig_deep(:l6)    // ["apple", "orange"]

data.dig_deep(:l9)    // 9876

data.dig_deep(:xyz)   // nil   (returns nil when key is not found)

Example 4:

Object with symbol and string keys

data = {
  :email  => "[email protected]",
  :level1 => {
    "email" => "[email protected]"
  }
}
require 'dig-deep'

data.dig_deep(:email)   // ["[email protected]", "[email protected]"]

data.dig_deep('email')  // ["[email protected]", "[email protected]"]

Contributing

Bug reports and pull requests are welcome!

Fork it, create your new branch, push and create a pull request.

License

The gem is available as open source under the terms of the MIT License.

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.