Function isSetEqual

Returns true if the two passed Sets or Arrays representing sets of items, are set-equal (i.e. they contain the same items, irrespective of items order).

By default, the comparison is done with strict equality (i.e ===), but you can pass 1 or 2 custom comparators.

The second comparator:

  • defaults to a _.flip version of the first one (which defaults to strict === equality)

  • is used on the 2nd array, to check if it contains the item from the 1st array

Based on lodash _.differenceWith.

Examples:

 // primitives
 z.isSetEqual([1, 2, 3], [3, 2, 1]) // true
 z.isSetEqual([1, 2, 3], [4, 3, 2, 1]) // false
 z.isSetEqual([1, 2, 3, 'John', 'Doe'], ['John', 3, 'Doe', 2, 1]) // true

 // objects / refs
 z.isSetEqual([{ a: 1 }, { b: 2 }, { c: 3 }], [{ c: 3 }, { b: 2 }, { a: 1 }]) // false
 z.isSetEqual([{ a: 1 }, { b: 2 }, { c: 3 }], [{ c: 3 }, { b: 2 }, { a: 1 }], _.isEqual) // true

 // custom comparator
 z.isSetEqual(
   [{ a: 1 }, { b: 2 }, { c: 3 }],
   [{ c: 3 }, { b: 2 }, { a: 1, extraPropButObjectIn1stArray_isLikeThis:111 }],
   z.isLike
 ) // true

 z.isSetEqual(
   [{ a: 1, extraPropButObjectIn2ndArray_isFlipLikeThis:111  }, { b: 2 }, { c: 3 }],
   [{ c: 3 }, { b: 2 }, { a: 1}],
   _.flip(z.isLike)
 ) // true

@todo(222): typings / Generic args

  • Parameters

    • set1: any[] | Set<any>

      1st Array set

    • set2: any[] | Set<any>

      2nd Array set

    • Optionalcomparator1: ((a: any, b: any) => boolean)

      Function used to compare items from the 1st array with items from the 2nd array

        • (a, b): boolean
        • Parameters

          • a: any
          • b: any

          Returns boolean

    • Optionalcomparator2: ((a: any, b: any) => boolean)

      Function used to compare items from the 2nd array with items from the 1st array

        • (a, b): boolean
        • Parameters

          • a: any
          • b: any

          Returns boolean

    Returns boolean