Skip to content

customComparator #5

@miedzikd

Description

@miedzikd

What do you think about adding a new parameter to an options called "customComparator"? Could work like customFormatter, but be used not to display differences but to compare values?

I think it can be very useful, e.g. when comparing dates, e.g. stored in db (UTC timezone) vs from request (in local time zone) ...

it could also be used in comparing objects or arrays, e.g. someone wants to check just the number of elements in an array

Extended example from readme:

  import { Audit } from "entity-diff";
 
  const before = {
    name: "Mason",
    age: 20,
    updatedAt: "2021-03-02T20:08:13.000Z"
  };
 
  const after = {
    name: "Mason",
    age: 20,
    updatedAt: "2021-03-02T21:08:13+01:00"
  };
 
  const options = [
    {
      key: "updatedAt",
      customFormatter: date => new Date(date).toISOString(),
      customComparator: date => new Date(date).toISOString()
    }
  ]; 
 
  const audit = new Audit({ options });
 
  const result = audit.diff(before, after);
 
  // result:
  // []

In this case result will be empty because 2021-03-02T20:08:13.000Z === 2021-03-02T21:08:13+01:00

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions