Equal
Deep equality comparison utilities for various data types.
Equal provides comprehensive equality checking that goes beyond simple reference comparison. It supports deep comparison of arrays, maps, and anonymous objects, making it useful for data validation, testing, and change detection.
Supported Types
- Arrays: Element-by-element comparison (shallow)
- StringMap: Key-value comparison
- IntMap: Key-value comparison
- Anonymous Objects: Field-by-field comparison (recursive)
- Primitives: Standard equality
Usage Examples
// Array comparison
var a1 = [1, 2, 3];
var a2 = [1, 2, 3];
Equal.equal(a1, a2); // true
// Object comparison
var o1 = {x: 10, y: 20, data: [1, 2]};
var o2 = {x: 10, y: 20, data: [1, 2]};
Equal.equal(o1, o2); // true
// Map comparison
var m1 = new StringMap<Int>();
m1.set("a", 1);
var m2 = new StringMap<Int>();
m2.set("a", 1);
Equal.equal(m1, m2); // true
Limitations
- Array comparison is shallow (doesn't recurse into nested arrays)
- Class instances are compared by reference only
- Circular references will cause infinite recursion
Static Members
Performs deep equality comparison between two values.
Compares values based on their type:
- Same reference: true
- Arrays: Element-by-element comparison
- Maps: Key-value comparison
- Anonymous objects: Field comparison (recursive)
- Other: Reference equality
Name | Type | Description |
---|---|---|
a |
Dynamic | First value to compare |
b |
Dynamic | Second value to compare |
Returns | Description |
---|---|
Bool | True if values are deeply equal, false otherwise * haxe // Simple values Equal.equal(5, 5); // true Equal.equal("hello", "hello"); // true * // Arrays Equal.equal([1, 2, 3], [1, 2, 3]); // true Equal.equal([1, 2], [1, 2, 3]); // false * // Objects Equal.equal( {name: "John", age: 30}, {name: "John", age: 30} ); // true |
Compares two anonymous objects by their fields.
Performs recursive comparison of all fields in both objects. Objects are considered equal if they have the same fields with equal values (using the equal() function).
Name | Type | Description |
---|---|---|
a |
Any | First object to compare |
b |
Any | Second object to compare |
Returns | Description |
---|---|
Bool | True if all fields match, false otherwise * haxe var obj1 = {x: 10, y: {z: 20}}; var obj2 = {x: 10, y: {z: 20}}; Equal.objectFieldsEqual(obj1, obj2); // true * var obj3 = {x: 10, y: 20, extra: true}; Equal.objectFieldsEqual(obj1, obj3); // false (different fields) |
Compares two arrays element by element.
Arrays are equal if they have the same length and all corresponding elements are equal (using == comparison).
Name | Type | Description |
---|---|---|
a |
Array<Any> | First array |
b |
Array<Any> | Second array |
Returns | Description |
---|---|
Bool | True if arrays are equal |
stringMapEqual(a: haxe.ds.StringMap<Any>, b: haxe.ds.StringMap<Any>): Bool
Compares two StringMaps by their key-value pairs.
Maps are equal if they have the same keys and all values for corresponding keys are equal (using == comparison).
Name | Type | Description |
---|---|---|
a |
haxe.ds.StringMap<Any> | First StringMap |
b |
haxe.ds.StringMap<Any> | Second StringMap |
Returns | Description |
---|---|
Bool | True if maps have identical key-value pairs * haxe var map1 = new StringMap<Int>(); map1.set("a", 1); map1.set("b", 2); * var map2 = new StringMap<Int>(); map2.set("b", 2); map2.set("a", 1); * Equal.stringMapEqual(map1, map2); // true (order doesn't matter) |
intMapEqual(a: haxe.ds.IntMap<Any>, b: haxe.ds.IntMap<Any>): Bool
Compares two IntMaps by their key-value pairs.
Maps are equal if they have the same keys and all values for corresponding keys are equal (using == comparison).
Name | Type | Description |
---|---|---|
a |
haxe.ds.IntMap<Any> | First IntMap |
b |
haxe.ds.IntMap<Any> | Second IntMap |
Returns | Description |
---|---|
Bool | True if maps have identical key-value pairs * haxe var map1 = new IntMap<String>(); map1.set(1, "one"); map1.set(2, "two"); * var map2 = new IntMap<String>(); map2.set(2, "two"); map2.set(1, "one"); * Equal.intMapEqual(map1, map2); // true |