deep-equals

Deep equality checker (as static extension).
https://github.com/Mahdrentys/Haxe-Deep-Equals

To install, run:

haxelib install deep-equals 1.0.1 

See using Haxelib in Haxelib documentation for more information.

README.md

Haxe Deep Equals

Haxe deep-equals library performs a recursive deep equality check by value on any type of variable. It can be used as a static extension.

Usage

import haxe.ds.Vector;
import deepequals.DeepEquals;
using deepequals.DeepEquals;

enum MyAwesomeEnum
{
    Value1;
    Value2;
    Value3(param:String);
}

class MyAwesomeClass
{

}

class Main
{
    public static function main():Void
    {
        // It works on basic types
        DeepEquals.deepEquals(1, 1); // returns true
        1.deepEquals(1); // returns true
        1.5.deepEquals(1.5); // returns true
        true.deepEquals(true); // returns true
        false.deepEquals(false); // returns true
        "abc".deepEquals("abc"); // returns true
        DeepEquals.deepEquals(null, null); // returns true

        // It works on arrays, vectors, maps and anonymous structures
        [0, 1, 2].deepEquals([0, 1, 2]); // returns true
        [0, 1, 2].deepEquals([0, 1, 3]); // returns false

        Vector.fromArrayCopy([0, 1, 2]).deepEquals(Vector.fromArrayCopy([0, 1, 2])); // returns true
        Vector.fromArrayCopy([0, 1]).deepEquals(Vector.fromArrayCopy([0, 1, 2])); // returns false

        ["a" => 1, "b" => 2].deepEquals(["a" => 1, "b" => 2]); // returns true
        ["a" => 1, "b" => 2].deepEquals(["a" => 1, "b" => 3]); // returns false

        ({a: 1, b: "3"}).deepEquals({a: 1, b: "3"}); // returns true
        ({a: 1, b: "4"}).deepEquals({a: 1, b: "3"}); // returns false

        // It works on types
        Array.deepEquals(Array); // returns true
        Array.deepEquals(String); // returns false
        MyAwesomeEnum.deepEquals(MyAwesomeEnum); // returns true

        // It works on enum values, and it performs a deep equality checks on their parameters
        Value1.deepEquals(Value1); // returns true
        Value1.deepEquals(Value2); // returns false
        Value3("abc").deepEquals(Value3("abc")); // returns true
        Value3("abc").deepEquals(Value3("abcd")); // returns false

        // It works also on whatever object by deeply checking its properties (both public and private).
        // But sometimes, for extern classes that don't have attributes for example, the check can't be performed.
        // So, you can define custom checking functions for a specific class:

        DeepEquals.handle(MyAwesomeClass, function(a:MyAwesomeClass, b:MyAwesomeClass):Bool
        {
            // You get the two instances to compare, and you can return true or false
            return true;
        });

        // PS: It's with this handle function that Strings, Arrays, Vectors, Maps and Dates are checked

        // In haxe, we can't check if two functions are equal.
        // So, by default, they are assumed as equal:
        (function():Bool { return true; }).deepEquals(function():Bool { return false; }); // returns true

        // If you to consider that the functions aren't equal, you can provide the last optional argument: public static function deepEquals(a:Dynamic, b:Dynamic, equalFunctions = true):Bool
        (function():Bool { return true; }).deepEquals(function():Bool { return true; }); // returns false
    }
}
Contributors
Mahdrentys
Version
1.0.1
Published
7 years ago
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub

⌂