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

tree-object-diff

v2.0.3

Published

A utility for comparing and finding differences between tree-structured objects

Downloads

336

Readme

logo

TreeObjectDiff is a small, fast and zero-dependency utility for comparing and identifying differences between tree-structured objects. Unlike tools such as microdiff or deep-object-diff, which can only detect property additions, deletions, and value modifications in objects, TreeObjectDiff provides more comprehensive change detection, including node additions, deletions, moves, updates, and unchanged states. Inspired by Vue's virtual DOM diff algorithm, this tool supports both level-by-level and cross-level diffing of tree-structured nodes. TreeObjectDiff allows identification of all node movements regardless of their position in the tree hierarchy and provides a more comprehensive diff result.

Minizipped Size (from Bundlephobia) License dependency Count downloads

Features

  1. Identifies Moved Elements: Uses unique identifiers for cross-level comparison.
  2. Detects Changes: Detects node additions, deletions, moves, and updates.
  3. Customizable Comparison: Supports custom functions for complex data value comparisons.
  4. Detailed Reports: Provides comprehensive diff reports.
  5. Lightweight: No dependencies required.
  6. TypeScript Ready: Fully compatible with TypeScript.

It is worth mentioning that for the comparison of node movement, nodes at the same level will not be compared directly based on the index. Instead, the minimum number of movement changes will be given (based on the longest_increasing_subsequence algorithm).

Installation

npm i --save tree-object-diff

Usage

The tree object structure is as follows:

type TreeNode<TValues> = { id: number | string; children?: TreeNode<TValues>[] } & TValues;
type Tree<TValues> = TreeNode<TValues>;

The diff result structure is as follows:

type DiffTreeNode<TValues> = {
  id: number | string;
  change: Change;
  detail: {
    newValue?: Omit<TreeNode<TValues>, "id" | "children">;
    newPath?: string[];
    oldValue?: Omit<TreeNode<TValues>, "id" | "children">;
    oldPath?: string[];
  };
  children: DiffTreeNode<TValues>[];
};
type DiffTree<TValue> = [DiffTreeNode<TValue>] | [DiffTreeNode<TValue>, DiffTreeNode<TValue>];

Where a Change is:

type Change =
  | [CHANGE_TYPE.Unchanged]
  | [CHANGE_TYPE.Added]
  | [CHANGE_TYPE.Deleted]
  | [CHANGE_TYPE.Moved]
  | [CHANGE_TYPE.Updated]
  | [CHANGE_TYPE.Moved, CHANGE_TYPE.Updated];

Here's a basic example of how to use TreeObjectDiff to compare two tree objects:

import { diff } from "tree-object-diff";

const oldTreeObject = {
  id: "root",
  value: "root",
  children: [
    { id: "1", value: "a", children: [] },
    { id: "2", value: "b", children: [] },
    { id: "3", value: "c", children: [] },
    { id: "4", value: "d", children: [] },
    { id: "5", value: "e", children: [] },
    { id: "6", value: "f", children: [] },
  ],
};
const newTreeObject = {
  id: "root",
  value: "root",
  children: [
    { id: "5", value: "e", children: [] },
    { id: "1", value: "a", children: [] },
    { id: "2", value: "b", children: [] },
    { id: "7", value: "g", children: [] },
    { id: "3", value: "c", children: [] },
    { id: "6", value: "ff", children: [] },
  ],
};

const diffResult = diff(oldTreeObject, newTreeObject);

[
//   {
//     "id": "root",
//     "change": ["unchanged"],
//     "detail": {
//       "newValue": { "value": "root" },
//       "newPath": [],
//       "oldValue": { "value": "root" },
//       "oldPath": []
//     },
//     "children": [
//       {
//         "id": "5",
//         "change": ["moved"],
//         "detail": {
//           "newValue": { "value": "e" },
//           "newPath": ["children", "0"],
//           "oldValue": { "value": "e" },
//           "oldPath": ["children", "4"]
//         },
//         "children": []
//       },
//       {
//         "id": "1",
//         "change": ["unchanged"],
//         "detail": {
//           "newValue": { "value": "a" },
//           "newPath": ["children", "1"],
//           "oldValue": { "value": "a" },
//           "oldPath": ["children", "0"]
//         },
//         "children": []
//       },
//       {
//         "id": "2",
//         "change": ["unchanged"],
//         "detail": {
//           "newValue": { "value": "b" },
//           "newPath": ["children", "2"],
//           "oldValue": { "value": "b" },
//           "oldPath": ["children", "1"]
//         },
//         "children": []
//       },
//       {
//         "id": "4",
//         "change": ["deleted"],
//         "detail": {
//           "oldValue": { "value": "d" },
//           "oldPath": ["children", "3"]
//         },
//         "children": []
//       },
//       {
//         "id": "7",
//         "change": ["added"],
//         "detail": {
//           "newValue": { "value": "g" },
//           "newPath": ["children", "3"]
//         },
//         "children": []
//       },
//       {
//         "id": "3",
//         "change": ["unchanged"],
//         "detail": {
//           "newValue": { "value": "c" },
//           "newPath": ["children", "4"],
//           "oldValue": { "value": "c" },
//           "oldPath": ["children", "2"]
//         },
//         "children": []
//       },
//       {
//         "id": "6",
//         "change": ["updated"],
//         "detail": {
//           "newValue": { "value": "ff" },
//           "newPath": ["children", "5"],
//           "oldValue": { "value": "f" },
//           "oldPath": ["children", "5"]
//         },
//         "children": []
//       }
//     ]
//   }
// ]

Each node's identifier is id, which is used to detect additions, deletions, and movements. To compare if a node's value has changed, we stringify the node object (excluding the children field) and compare the results. Fortunately, we also support custom value comparison: Node value comparison is used to detect updates. You can customize these behaviors by providing your own comparison functions.

import { diff } from "tree-object-diff";

const sameNodeValue = (oldNode, newNode) => oldNode.curValue === newNode.curValue;
const diffDetail = diff(oldTree, newTree, { valueEquality: sameNodeValue });

License

MIT