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-anything

v2.0.2

Published

Compares objects and arrays and tells you which props or values are duplicates, and which are only present once.

Downloads

2,884

Readme

Compare anything 🛰

npm i compare-anything

Compares objects and arrays and tells you which props or values are duplicates, and which are only present once.

It works just like you would compare two columns in excel! But who needs excel when you've got JavaScript, am I right? 😃

Meet the family (more tiny utils with TS support)

Usage

It works just like you would expect. You compare(objectA, objectB) and it gives you all kind of info.

You can do all kind of things with compare-anything!

  • Compare object props, to see which props are present in which objects

Compare object props

Which props are present in which objects. Remember, this function only looks at the prop-names! Not the values.

Will return an info object with:

  • props - an array with all props of all objects
  • presentInAll - is the prop present in all passed objects? true/false per prop
  • perProp - an array of objects per prop that had that specific prop
  • presentIn - an array of indexes per prop that had that specific prop (indexes of the params you passed to the function)
import { compareObjectProps } from 'compare-anything'

// only props 'b' and 'c' are present in both ↓
const objectA = {a: '🎴', b: '🎴', c: '🎴'}
const objectB = {b: '🀄️', c: '🀄️', d: '🀄️'}

compareObjectProps(objectA, objectB)
// returns ↓
{
  props: ['a', 'b', 'c', 'd'],
  presentInAll: { a: false, b: true, c: true, d: false },
  perProp: { a: [objectA], b: [objectA, objectB], c: [objectA, objectB], d: [objectB] },
  presentIn: { a: [0], b: [0, 1], c: [0, 1], d: [1] }
}

Compare more than two

You can pass as many arguments as you want!

// keep on adding objects to compare!
compareObjectProps(objectA, objectB, objectC, objectD, objectE)

Compare objects in an array

When you need to compare objects in an array you can use destructuring:

// you can compare an array of objects like so:
compareObjectProps(...objectArray)

Find duplicates based on one prop value

When you need to find duplicate objects based on one single prop value of that object, you can easily do so as follows:

compareObjectProps(
  ...arrayOfObjects.map(obj => {
    return { [obj.idField]: obj }
  })
)

In the example above you can change idField by the actual prop name you need. By making a key out of the value you can easily find duplicates based on just this field.

Nested props

If we require to check even nested props we can use the flatten-anything function like shown below:

import flatten from 'flatten-anything'
import { compareObjectProps } from 'compare-anything'

const objectA = {nested: {a: '🎴', b: '🎴'}}
const objectB = {nested: {a: '🀄️', c: '🀄️'}}

const flatA = flatten(objectA)
// → {'nested.a': '🎴', 'nested.b': '🎴'}
const flatB = flatten(objectB)
// → {'nested.a': '🀄️', 'nested.c': '🀄️'}

compareObjectProps(flatA, flatB)
// returns ↓
{
  props: ['nested.a', 'nested.b', 'nested.c'],
  presentInAll: { 'nested.a': true, 'nested.b': false, 'nested.c': false },
  perProp: { 'nested.a': [objectA, objectB], 'nested.b': [objectA], 'nested.c': [objectB] },
  presentIn: { 'nested.a': [0, 1], 'nested.b': [0], 'nested.c': [1] }
}