Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including object-deep-search with all npm packages installed. Try it out:

var objectDeepSearch = require("object-deep-search")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

object-deep-search v0.0.7

Search through object properties recursevly with a complex filter.

NPM version

object-deep-search

Search through object properties recursively with a complex filter. Inspired by jscodeshift find method. It was created to help in testing react components with Jest and react-test-renderer, but can be used anywhere.

Help

There are two main functions:

find(object, filter)

Return array properties of object that match the filter.

findFirst(object, filter)

Return first property of object that match the filter.

Arguments

  • Object - object to recursively search through.
  • Filter - object that defines structure (keys and values) to search. If property of object has all keys that filter has (property can also contain other keys) and this keys has the same values, the property will be returned as matched.

    In case if filter key is Array (see children in examples). Value of object property should have all items that filter value has.

Examples

Main reason for creating this package was to help with react testing. React-test-renderer renders React components to pure JavaScript objects. And if we need to simulate click on button we should find this button in component tree.

import {findFirst} from 'object-deep-search'
...

const component = renderer.create(<TestComponent/>)
const tree = component.toJSON()

//define filter
const filter = {
  type: 'button',
  props: {
    className: 'my-button-classname',
    children: ['Submit']
  }
}

//Simulate button onClick method
findFirst(tree, filter).props.onClick()

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free