Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customComparator #5

Open
miedzikd opened this issue Feb 10, 2021 · 3 comments
Open

customComparator #5

miedzikd opened this issue Feb 10, 2021 · 3 comments

Comments

@miedzikd
Copy link

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

@Throyer
Copy link
Owner

Throyer commented Feb 12, 2021

I liked the idea I think it is very easy to implement, I started an implementation in a new branch

The idea is to do something very close to what you suggested:

  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: (from, to) => new Date(from).toISOString() !== new Date(to).toISOString()
    }
  ]; 
 
  const audit = new Audit({ options });
 
  const result = audit.diff(before, after);
 
  // result:
  // []

the only difference is that I'm going to pass a function that receives both values and the person is free to compare the way they want :D

forgive my english from google translator... :C

@miedzikd
Copy link
Author

Hello @Throyer :)
Any progress here?

@Throyer
Copy link
Owner

Throyer commented Aug 27, 2021

Hello @Throyer :)
Any progress here?

Unfortunately I'm not having time because of work :C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants