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

compare-object-path

v1.0.4

Published

Util function for comparing two object to check equality of paths given

Downloads

35

Readme

Compare Object Path

Synopsis

This is a js library to allow easy setup for comparing 2 object by using passed paths to compare the equality of one object to the other.

Motivation

I built this to help with larger reactJs components that need a cleaner way of implementing shouldComponentUpdate. However, in building it I found it was just an object comparison util function so made it generic to be used in more that just reactJs.

Installation

npm install compare-object-path

Basic Usage

import arePathsDiff from 'compare-object-path';

export default class testComponent extends Component {

  shouldComponentUpdate(nextProps) {
    // will only update if 'test.path.i.care.about' or 
    // 'test.other.path.i.care.about' changes between current and next props 
    return arePathsDiff([
      'test.path.i.care.about',
      'test.other.path.i.care.about'
      ], this.props, nextProps);
  }

  render() {
    return (<div>test</div>);
  }
}

Params

The arePathsDiff function takes 4 params with the first 3 being required

| param | description | required | | ------------ |:---------------------------------------------------------------------------------------------------------------:| ---------:| | passedPaths | These are the paths that you wish to compare between first and second object. | true | | firstObject | First object for comparison. For reactjs, this is usually this.props. | true | | nextProps | Second object for comparison. For reactjs, this is nextProps that come from shouldComponentUpdate function. | true | | passedConfig | This is the config passed by the user to overwrite the default config. | false |

passedPaths

These are the paths that you wish to compare between first and second object. They must always be passed as an array of strings|strings[]|objects.

Examples

// array of strings
arePathsDiff([
      'test.path.i.care.about',
      'test.other.path.i.care.about'
      ], this.props, nextProps);

// array of strings and string arrays
arePathsDiff([
      'test.path.i.care.about',
      ['test','other','path','i','care','about']
      ], this.props, nextProps);

// array of string and objects with/without omit
arePathsDiff([
      'test.path.i.care.about',
      ['test','other','path','i','care','about'],
      {
        path: 'test.my.path.i.care.about'
      },
      {
         path: 'test.my.path.i.care.about',
         omit: [
           'function1',
           ['var1', 'function2']
         ]
      },
      ], this.props, nextProps);

passedPaths param can take an object as a path and this object looks like:

{
  path: string|string[] (required, this is the path you want to check),
  omit: string|string[] (optional, omit that starts at your path end)
}

This option is great for very complex objects where you may want to check the entirety of one object property. However, on another property within the same object you may want to check the whole object property but omit a few inner properties.

passedConfig (optional)

This is the config passed by the user to overwrite the default config. This is the fourth param to arePathsDiff can be passed and it has 2 options available.

{
  defaultValue: boolean (default: true, this value is return when an error occurs),
  omitPathsOnly: boolean (default: false, this is used to assume you want to 
    check all object properties and used passed paths as omits)
}

Examples

Default Value usage:
import arePathsDiff from 'compare-object-path';

export default class testComponent extends Component {

  shouldComponentUpdate(nextProps) {
    // no paths are passed causing an error, by default we would return true, but in this case 
    // we set a default of false so the funciton would return false
    return arePathsDiff(null, this.props, nextProps, { defaultValue: false });
  }

  render() {
    return (<div>test</div>);
  }
}
Omit Paths Only usage:
I use this a lot for child components in reactJs where all props that are passed to the child should be checked EXCEPT for things like, in my case, functions
import arePathsDiff from 'compare-object-path'

export default class testComponent extends Component {

  shouldComponentUpdate(nextProps) {
    // we want to check all of props but remove function1 
    // and function2 from props before checking
    return arePathsDiff([
      'function1',
      'function2'
      ], this.props, nextProps, { omitPathsOnly: true });
  }

  render() {
    return (<div>test</div>);
  }
}

Tests

To see tests run

npm run tests
  • Requires Node Version 4+ to run tests.

License

MIT