- Compare Arrays
- Compare Plain Objects
- Compare Strings
It compares data structures, especially, AST's.
We use it to compare two parsed HTML/CSS trees or their branches, but you can compare anything, it will recursively traverse arrays too. For example, it powers ast-delete-object which also works on AST's.
The default mode is similar to Tap asserts
t.match and the option
opts.matchStrictly is similar to
The main function
compare() is imported like this:
It's a function which takes three input arguments:
|Array or Plain object or String||yes||Superset, larger thing.|
|Array or Plain object or String||yes||A set of the above, smaller thing.|
|Plain object||no||An Optional Options Object.|
- If everything from
smallObjmatches everything within
bigObj, this library returns
- Otherwise, if there's a mismatch or something wrong with input args, it returns
None of the input arguments will be mutated by this program, we have unit tests to prove that.
The optional options object has the following shape:
|Boolean||no||Any chunk of whitespace (tabs, spaces, line breaks and so on) will match any other chunk of white space.|
|Boolean||no||When you want to match like |
|Boolean||no||When set to |
|Boolean||no||If off by default, but you can enable wildcards within object value strings.|
The function returns a boolean:
A positive answer is always boolean
A negative answer is either:
- a boolean
false(on default setting, if
- a string, explaining what didn't match (if
You can import
It's a plain object:
The main function calculates the options to be used by merging the options you passed with these defaults.
You can import
Sometimes you just want a yes/no answer is something a subset or equal to something. But sometimes, the whole point of comparison is to inform the user exactly what is mismatching. In the latter cases, set
true. When there is no match, instead of Boolean
false the main function will return a string with an explanatory message.
If you use this setting, you have to anticipate Boolean
true OR something else (Boolean
false or string) coming out from this library.
Partial comparisons will match empty array and empty object source values against any array or object value, respectively.
matches empty arrays to everything. This is bad when you are comparing parsed HTML/CSS
trees. This library doesn't do this. An empty array will not be reported as equal
to a non-empty array.
// in this library: var res = compare(["a", "b", "c"], ); // now, res === false