ast-compare examples

Table of Contents

Quick Take

import { strict as assert } from "assert";
import { compare } from "ast-compare";

// Find out, does an object/array/string/nested-mix is a subset or equal to another input:
assert.equal(
  compare(
    {
      a: {
        b: "d",
        c: [],
        e: "f",
        g: "h",
      },
    },
    {
      a: {
        b: "d",
        c: [],
      },
    }
  ),
  true
);

Compare Arrays

import { strict as assert } from "assert";
import { compare } from "ast-compare";

assert.equal(
  compare(["a", "b", "c"], ["a", "b"]),
  true
);
// true, because second is a subset of first

assert.equal(
  compare(["a", "b", "c"], ["b", "a"]),
  false
);
// => false, because order is wrong

assert.equal(
  compare(["a", "b"], ["a", "b", "c"]),
  false
);
// => false, because second is not a subset of first (it's opposite)

assert.equal(
  compare(
    [{ a: "b" }, { c: "d" }, { e: "f" }],
    [{ a: "b" }, { c: "d" }]
  ),
  true
);
// => plain objects nested in arrays

Compare Plain Objects

import { strict as assert } from "assert";
import { compare } from "ast-compare";

// Find out, does an object/array/string/nested-mix is a subset or equal to another input:
assert.equal(
  compare(
    { a: "1", b: "2", c: "3" },
    { a: "1", b: "2" }
  ),
  true
);
// true, because second (smallObj) is subset of (or equal) first (bigObj).

assert.equal(
  compare(
    { a: "1", b: "2" },
    { a: "1", b: "2", c: "3" }
  ),
  false
);
// => false, because second (smallObj) is not a subset (or equal) to first (bigObj).

Compare Strings

import { strict as assert } from "assert";
import { compare } from "ast-compare";

assert.equal(compare("a\nb", "a\nb"), true);

assert.equal(compare("a", "b"), false);

opts.hungryForWhitespace

import { strict as assert } from "assert";
import { compare } from "ast-compare";

// by default, key values will be strictly matched using `===`
assert.equal(
  compare(
    { a: "\n\n\n", b: "\t\t\t", c: "whatever" },
    { a: "\r\r\r", b: "       " },
    {
      hungryForWhitespace: false,
    }
  ),
  false
);

// whitespace is matched leniently with the following option:
assert.equal(
  compare(
    { a: "\n\n\n", b: "\t\t\t", c: "whatever" },
    { a: "\r\r\r", b: "       " },
    {
      hungryForWhitespace: true,
    }
  ),
  true
);

// the fun doesn't stop here, any "empty" structures will be
// reported as matching:
assert.equal(
  compare(
    {
      a: { z: "\n\n\n" },
      b: ["\t\t\t"],
      c: "whatever",
    },
    { a: [[[[["\r\r\r"]]]]], b: { c: { d: " " } } },
    {
      hungryForWhitespace: true, // <--- !
    }
  ),
  true // <--- !!!
);
// "empty" thing is:
//   - string that trims to zero-length
//   - array with zero or more whitespace strings only
//   - plain object with zero or more keys with "empty" values
//     (empty arrays, empty plain objects or empty strings)

opts.verboseWhenMismatches

import { strict as assert } from "assert";
import { compare } from "ast-compare";

// by default, returns a boolean without explanation
assert.equal(
  compare(
    { a: "1", b: "2" },
    { a: "1", b: "2", c: "3" },
    {
      verboseWhenMismatches: false, // <---
    }
  ),
  false
);

assert.equal(
  compare(
    { a: "1", b: "2" },
    { a: "1", b: "2", c: "3" },
    {
      verboseWhenMismatches: true, // <---
    }
  ),
  'The given object has key "c" which the other-one does not have.'
);

// when opts.verboseWhenMismatches is enabled, a negative result is
// string (explanation). A positive result is boolean "true".
assert.equal(
  compare(
    { a: "1", b: "2" },
    { a: "1", b: "2" },
    {
      verboseWhenMismatches: true, // <---
    }
  ),
  true
);