Wondering what’s next for npm?Check out our public roadmap! »

    @thi.ng/equiv
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.41 • Public • Published

    equiv

    npm version npm downloads Twitter Follow

    This project is part of the @thi.ng/umbrella monorepo.

    About

    Extensible deep value equivalence checking for any data types.

    Supports:

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

    Status

    STABLE - used in production

    Search or submit any issues for this package

    Installation

    yarn add @thi.ng/equiv
    // ES module
    <script type="module" src="https://unpkg.com/@thi.ng/equiv?module" crossorigin></script>
    
    // UMD
    <script src="https://unpkg.com/@thi.ng/equiv/lib/index.umd.js" crossorigin></script>

    Package sizes (gzipped, pre-treeshake): ESM: 487 bytes / CJS: 543 bytes / UMD: 614 bytes

    Dependencies

    None

    API

    Generated API 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

    Authors

    Karsten Schmidt

    If this project contributes to an academic publication, please cite it as:

    @misc{thing-equiv,
      title = "@thi.ng/equiv",
      author = "Karsten Schmidt",
      note = "https://thi.ng/equiv",
      year = 2016
    }

    License

    © 2016 - 2021 Karsten Schmidt // Apache Software License 2.0

    Install

    npm i @thi.ng/equiv

    DownloadsWeekly Downloads

    3,532

    Version

    1.0.41

    License

    Apache-2.0

    Unpacked Size

    30.5 kB

    Total Files

    10

    Last publish

    Collaborators

    • avatar