tapjs/tcompare

matchStrict proposal

Closed this issue · 3 comments

fleg commented

How about strict match version? Which is exactly like match except this:

If the object and pattern are loosely equal, then pass

It would be very convenient in cases when object contains some dynamic fields, for example identificators or dates, and I don't care of it content, but rest of the fields should be matching strictly.

So, I'm not sure how this actually ends up differing from t.hasStrict? Could you just use that?

fleg commented

hasStrict doesn't allow matching by class constructor, for example:

t.hasStrict(
  {
    id: 1,
    createdAt: new Date(),
    name: 'foo',
    level: 2,
    items: ['bar', 'baz'],
  },
  {
    id: Number,
    createdAt: Date,
    name: 'foo',
    level: 2,
    items: ['bar', 'baz']
  }
);

ends up with error

    not ok 1 - should contain all provided fields strictly
      ---
      diff: |
        --- expected
        +++ actual
        @@ -1,6 +1,6 @@
         Object {
        -  "id": Function Number(),
        -  "createdAt": Function Date(),
        +  "id": 1,
        +  "createdAt": 2021-02-16T15:34:51.377Z,
           "level": 2,
           "name": "foo",
           "items": Array [

match is comparing loosely, so I want this to fail

  t.matchStrict(
    {
      id: 1,
      createdAt: new Date(),
      level: 2,
      name: 'foo',
      items: ['bar', 'baz'],
    },
    {
      id: Number,
      createdAt: Date,
      level: '2', // mind the string
      name: 'foo',
      items: ['bar', 'baz']
    }
  );

and this to pass

  t.matchStrict(
    {
      id: 1,
      createdAt: new Date(),
      level: '2',
      name: 'foo',
      items: ['bar', 'baz'],
    },
    {
      id: Number,
      createdAt: Date,
      level: '2', 
      name: 'foo',
      items: ['bar', 'baz']
    }
  );

"Match, but fail if (a == b) && !(a === b)" is going to be the MatchStrict implementation in the next version. In every other way, it'll be identical to Match.