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 🙏

© 2026 – Pkg Stats / Ryan Hefner

diflect

v0.0.4

Published

Diflect is a micro-library to check diffs between objects

Downloads

9

Readme

Diflect

Diflect is a lightweight utility library for comparing objects and arrays in JavaScript/TypeScript. It computes the differences between two objects or arrays, providing details on added, removed, and updated keys or values.

Features

  • Object Differences: Compare two objects to identify added, removed, and updated keys.
  • Array Differences: Compare arrays efficiently using optimized algorithms.
  • Nested Structures: Handles deep nested objects and arrays seamlessly.
  • Custom Comparison: Supports custom comparison functions for flexible use cases.
  • Ignored Keys: Specify keys to ignore during comparison.
  • Performance: Leverages efficient algorithms for array and object comparison, suitable for large datasets.

Installation

Install Diflect using npm or yarn:

npm install diflect
# or
yarn add diflect

Usage

Importing Diflect

import { objectDiff } from 'diflect';

Example: Object Difference

const base = { a: 1, b: 2, c: 3 };
const target = { a: 1, b: 20, d: 4 };

const diff = objectDiff(base, target);

console.log(diff);
// Output:
// {
//   added: { d: 4 },
//   removed: { c: 3 },
//   updated: { b: 20 },
// }

Example: Nested Objects

const base = { nested: { x: 1, y: 2 } };
const target = { nested: { x: 10, y: 2 } };

const diff = objectDiff(base, target);

console.log(diff);
// Output:
// {
//   added: {},
//   removed: {},
//   updated: {
//     nested: {
//       added: {},
//       removed: {},
//       updated: { x: 10 },
//     },
//   },
// }

Example: Arrays

const base = { a: [1, 2, 3], b: [3, 4] };
const target = { a: [1, 2, 4], b: [3, 4, 5] };

const diff = objectDiff(base, target);

console.log(diff);
// Output:
// {
//   added: {},
//   removed: {},
//   updated: {
//     a: { added: [4], removed: [3] },
//     b: { added: [5], removed: [] },
//   },
// }

Example: Ignore Keys

const base = { a: 1, b: 2, c: 3 };
const target = { a: 10, b: 20, c: 3 };

const diff = objectDiff(base, target, { ignoreKeys: ['c'] });

console.log(diff);
// Output:
// {
//   added: {},
//   removed: {},
//   updated: { a: 10, b: 20 },
// }

Example: Custom Comparison Function

const base = { a: '1', b: '2' };
const target = { a: 1, b: 2 };

const diff = objectDiff(base, target, {
  compareFn: (a, b) => String(a) === String(b),
});

console.log(diff);
// Output:
// {
//   added: {},
//   removed: {},
//   updated: {},
// }

API Documentation

objectDiff<T>(base: T, target: T, options?: ObjectDiffOptions): ObjectDiff<T>

Parameters

  • base: The base object to compare from.
  • target: The target object to compare to.
  • options:
    • ignoreKeys (string[]): Keys to ignore during comparison.
    • compareFn ((a: any, b: any) => boolean): Custom comparison function.

Returns

An object with the following structure:

interface ObjectDiff<T> {
  added: Partial<T>; // Keys added in the target object
  removed: Partial<T>; // Keys removed from the base object
  updated: Partial<T>; // Keys updated in the target object
}

diffArrays<T>(base: T[], target: T[]): { added: T[]; removed: T[] }

Parameters

  • base: The base array to compare from.
  • target: The target array to compare to.

Returns

An object with the following structure:

{
  added: T[]; // Elements present in `target` but not in `base`
  removed: T[]; // Elements present in `base` but not in `target`
}

Roadmap

  • Custom Deep Comparison: Extend support for custom comparison in nested structures.
  • Performance Enhancements: Optimize nested structure comparisons for very large objects.
  • Type Refinements: Add stricter TypeScript typing for deeply nested comparisons.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

  1. Fork the repository.
  2. Create a new branch: git checkout -b my-feature-branch
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push the branch: git push origin my-feature-branch
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.