ast-deep-contains
    TypeScript icon, indicating that this package has built-in type declarations

    4.0.1 • Public • Published

    ast-deep-contains

    Like t.same assert on array of objects, where element order doesn't matter.

    Install

    This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required:

    npm i ast-deep-contains

    If you need a legacy version which works with require, use version 3.1.0

    Quick Take

    import { strict as assert } from "assert";
    import { deepContains } from "ast-deep-contains";
    
    const gathered = [];
    const errors = [];
    
    const reference = [
      { c: "2" }, // will end up not used
      { a: "1", b: "2", c: "3" },
      { x: "8", y: "9", z: "0" },
    ];
    
    const structureToMatch = [
      { a: "1", b: "2", c: "3" }, // matches but has different position in the source
      { x: "8", y: "9" }, // "z" missing
    ];
    
    // This program pre-matches first, then matches objects as a set-subset
    deepContains(
      reference,
      structureToMatch,
      (leftSideVal, rightSideVal) => {
        // This callback does the pre-matching and picks the key pairs for you.
        // It's up to you what you will do with left- and right-side
        // values - we normally feed them to unit test asserts but here we just push
        // to array:
        gathered.push([leftSideVal, rightSideVal]);
      },
      (err) => {
        errors.push(err);
      }
    );
    
    // imagine instead of pushing pairs into array, you fed them into assert
    // function in unit tests:
    assert.deepEqual(gathered, [
      ["1", "1"],
      ["2", "2"],
      ["3", "3"],
      ["8", "8"],
      ["9", "9"],
    ]);
    assert.equal(errors.length, 0);

    Documentation

    Please visit codsen.com for a full description of the API and examples.

    Contributing

    To report bugs or request features or assistance, raise an issue on GitHub.

    Licence

    MIT License

    Copyright (c) 2010-2021 Roy Revelt and other contributors

    ok codsen star

    Install

    npm i ast-deep-contains

    DownloadsWeekly Downloads

    109

    Version

    4.0.1

    License

    MIT

    Unpacked Size

    44.7 kB

    Total Files

    9

    Last publish

    Collaborators

    • royston