Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→ast-loose-comparenext

ast-loose-compare4.2.0

Compare anything: AST, objects, arrays and strings

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — LOOSECOMPARE…
  • API — VERSION
  • API — TYPES
  • DIFFERENCE FR…
  • DIFFERENCES…
  • Changelog

Installation

Quick Take

Examples

  • Treat structurally different empty values as equivalent
  • More Examples
  • Compare objects nested inside arrays as subsets
  • Empty Values

Purpose

To find out, does an object/array/string/nested-mix is a subset or equal to another input.

If this library will encounter two things that contain only empty things, it will report them as equal. Empty things are:

  • strings that trim() to zero length
  • arrays with zero or more empty strings (see previous item)
  • plain objects whose all values of all keys are empty arrays, empty strings or empty plain objects

For example these two are deemed to be equal:

looseCompare(
  {
    a: "a",
    b: "\n \n\n",
  },
  {
    a: "a",
    b: "\t\t \t",
  },
);
// => true

Second input argument can be subset of first-one, notice b values are of a different type, yet both contain only empty space:

looseCompare(
  {
    a: "a",
    b: [[["\n \n\n"]]],
    c: "c",
  },
  {
    a: "a",
    b: { c: { d: "   \t\t \t" } },
  },
);
// => true

API — looseCompare()

The main function looseCompare() is imported like this:

It’s a function which takes two input arguments, each of a type, suitable for JSON:

Function returns either a boolean or undefined.

  • If everything from smallObj matches everything within bigObj, this library returns true.
  • Otherwise, if there’s a mismatch, returns false.
  • For all other cases where inputs are missing/undefined, returns undefined.
  • If both smallObj and bigObj contain the same key and their values contain only empty space (differing or not), they will be considered equal.

API — version

You can import version:

API — types

This package is written in TypeScript and exports the following types:

TypeDescription
JsonValue
Type: JsonValue
JsonValueAny JSON-representable value — string, number, boolean, null, JsonObject or JsonArray.
JsonObject
Type: JsonObject
JsonObjectAn object whose values are all JsonValues.
JsonArray
Type: JsonArray
JsonArrayAn array of JsonValues.
UnknownValueObj
Type: UnknownValueObj
UnknownValueObjA plain object with string keys and values of any type.
import type { JsonArray, JsonObject, JsonValue, UnknownValueObj } from "ast-loose-compare";

Difference from ast-compare

ast-compare of ours does everything that this program does and more.

Differences from _.isMatch

Partial comparisons will match empty array and empty object source values against any array or object value, respectively.

Lodash documentationopens in a new tab

_.isMatchopens in a new tab positively matches empty arrays to everything. This is bad when you are comparing parsed HTML/CSS trees. This library doesn’t do this. In this library, empty array will not be reported as equal to non-empty array, although if both arguments contain something which is empty space, they will be considered equal.

If you want an AST comparison library with a stricter ways towards the empty space equation, check ast-compare.

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab