Skip to content

Latest commit

 

History

History
88 lines (59 loc) · 1.48 KB

tpl.readme.md

File metadata and controls

88 lines (59 loc) · 1.48 KB

About

{{pkg.description}}

Supports:

  • JS primitives
  • Arrays
  • Plain objects
  • ES6 Sets / Maps
  • Date
  • RegExp
  • Types with IEquiv implementation

{{meta.status}}

{{repo.supportPackages}}

{{repo.relatedPackages}}

{{meta.blogPosts}}

Installation

{{pkg.install}}

{{pkg.size}}

Dependencies

{{pkg.deps}}

{{repo.examples}}

API

{{pkg.docs}}

import { equiv } from "@thi.ng/equiv";

equiv(
    { a: { b: [1, 2] } },
    { a: { b: [1, 2] } }
);
// true

Implement IEquiv interface

This is useful & required for custom types to take part in equiv checks, by default only plain objects & array are traversed deeply.

Furthermore, by implementing this interface we can better control which internal values / criteria are required to establish equivalence. In this example we exclude the meta property and only check for same type & children equality.

import { IEquiv } from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";

class Node implements IEquiv {

    meta: any;
    children: any[];

    constructor(children: any[], meta?: any) {
        this.children = children;
        this.meta = meta;
    }

    equiv(o: any) {
        return o instanceof Node && equiv(this.children, o.children);
    }
}

equiv(new Node([1,2,3], "foo"), new Node([1,2,3], "bar"));
// true