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

object-compare-2

v0.0.1

Published

A TypeScript utility for comparing objects and detecting changes

Downloads

85

Readme

object-compare-2

A TypeScript utility for comparing objects and detecting changes between them. This package provides robust object comparison with support for deep comparison, custom comparators, and field filtering.

Installation

npm install object-compare-2
# or
yarn add object-compare-2
# or
pnpm add object-compare-2

Features

  • 🔍 Deep object comparison
  • 🎯 Custom field comparators
  • ⚡ Shallow comparison option
  • 🚫 Field ignoring
  • ✨ TypeScript support
  • 📅 Built-in Date object support
  • 🔄 Array comparison
  • ❌ Nullish value handling

Usage

Basic Usage

import { getChangedFields, hasChanges } from "object-compare-2";

const original = {
  name: "John",
  age: 30,
  address: {
    street: "Main St",
    city: "Boston",
  },
  tags: ["user", "admin"],
};

const current = {
  name: "John",
  age: 31,
  address: {
    street: "Main St",
    city: "New York",
  },
  tags: ["user", "admin", "manager"],
};

// Get changed fields
const changes = getChangedFields(current, original);
console.log(changes);
// Output:
// {
//   age: 31,
//   address: { street: 'Main St', city: 'New York' },
//   tags: ['user', 'admin', 'manager']
// }

// Check if there are any changes
const hasAnyChanges = hasChanges(current, original);
console.log(hasAnyChanges); // true

Advanced Usage

With Custom Comparators

interface User {
  name: string;
  age: number;
  address: {
    street: string;
    city: string;
    country: string;
  };
  lastLogin: Date;
  permissions: string[];
}

const current: User = {
  name: "John",
  age: 30,
  address: {
    street: "Main St",
    city: "Boston",
    country: "USA",
  },
  lastLogin: new Date("2024-01-01"),
  permissions: ["read", "write"],
};

const original: User = {
  name: "John",
  age: 30,
  address: {
    street: "Second St",
    city: "Boston",
    country: "USA",
  },
  lastLogin: new Date("2024-01-02"),
  permissions: ["read"],
};

const changes = getChangedFields(current, original, {
  customComparators: {
    // Only compare city and country for address
    address: (curr, orig) =>
      curr.city === orig.city && curr.country === orig.country,
    // Compare dates ignoring time
    lastLogin: (curr, orig) => curr.toDateString() === orig.toDateString(),
    // Check if arrays have same length
    permissions: (curr, orig) => curr.length === orig.length,
  },
});

With Ignore Fields

const changes = getChangedFields(current, original, {
  ignoreFields: ["lastLogin", "permissions"], // These fields will be ignored in comparison
});

Handling Nullish Values

const current = {
  name: "John",
  age: null,
  title: undefined,
};

const original = {
  name: "John",
  age: 30,
  title: "Developer",
};

// Include null/undefined changes
const changes1 = getChangedFields(current, original, {
  includeNullish: true,
});
// Output: { age: null, title: undefined }

// Exclude null/undefined changes
const changes2 = getChangedFields(current, original, {
  includeNullish: false,
});
// Output: {}

Shallow Comparison

const changes = getChangedFields(current, original, {
  deep: false, // Only compare object references
});

API Reference

getChangedFields<T>

Gets the changed fields between two objects by comparing their values.

function getChangedFields<T extends Record<string, any>>(
  currentValue: T,
  originalValue: T,
  options?: CompareOptions<T>
): Partial<T>;

Options

interface CompareOptions<T> {
  /**
   * If true, includes fields that changed to undefined/null
   * If false, omits fields that changed to undefined/null
   * @default false
   */
  includeNullish?: boolean;

  /**
   * Custom comparison function for specific fields
   */
  customComparators?: {
    [K in keyof T]?: (current: T[K], original: T[K]) => boolean;
  };

  /**
   * If true, performs deep comparison of objects and arrays
   * If false, performs shallow comparison
   * @default true
   */
  deep?: boolean;

  /**
   * Fields to ignore during comparison
   */
  ignoreFields?: Array<keyof T>;
}

hasChanges<T>

Checks if an object has any changes compared to its original state.

function hasChanges<T extends Record<string, any>>(
  currentValue: T,
  originalValue: T,
  options?: CompareOptions<T>
): boolean;

Supported Types

  • Primitives (string, number, boolean)
  • Arrays
  • Objects
  • Date objects
  • null/undefined

TypeScript Support

The package is written in TypeScript and includes type definitions. It provides full type inference for your objects and custom comparators.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.