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

return array of objects instead array of string #9

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

return array of objects instead array of string #9

miedzikd opened this issue Feb 10, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@miedzikd
Copy link

Hi! :)
At start I want to say that I love your library! Is really helpfull to generate change history log...
in my project we want use translated history logs... and will be a lot easier if diff instead returning array of strings will return array with changes objects e.g.

[
  {
    type: 'N',
    newValue: 'NEWVALUE',
    dotPath: 'DOTPATH',
    field: 'FIELD'
  },
  {
    type: 'E',
    oldValue: 'OLDVALUE',
    newValue: 'NEWVALUE',
    dotPath: 'DOTPATH',
    field: 'FIELD'
  },
]

what do you think about that?

Greetings from Poland :)
Daniel

@spence-s
Copy link
Owner

spence-s commented Feb 13, 2021

Thank you, glad you find it useful! Have you taken a look at just using the raw deep diff library?? This library is just a wrapper around that with a couple of edits that make it a little nicer. If you prefer my library since my syntax is a little simpler, I will add this as an option. Let me know! Also happy to accept PRs if you beat me to it.

@spence-s spence-s added the enhancement New feature or request label Feb 13, 2021
@spence-s
Copy link
Owner

spence-s commented Feb 13, 2021

Actually, just looked and this is exposed already, but the api is a little weird, here is how you would get this:

const HumanDiff = require('human-object-diff');
const differ = new HumanDiff({...options});

console.log(differ.diff({foo: 'bar'}, {foo: 'baz'})) // -> '"Foo", with a value of "bar" (at Obj.foo) was changed to "baz"'
console.log(differ.sentenceDiffs) // ->
// sentenceDiffs: [
//    DiffSentence {
//      diff: [Diff],
//      FIELD: 'Foo',
//      OLDVALUE: 'bar',
//      NEWVALUE: 'baz',
//      DOTPATH: 'Obj.foo',
//      INDEX: undefined,
//      POSITION: undefined,
//      template: '"FIELD", with a value of "OLDVALUE" (at DOTPATH) was changed to "NEWVALUE"',
//      format: [Function: bound format]
//    }
//  ]

differ.sentenceDiffs will always have the array of values for the last diff that was performed.

Hopefully this will suit your needs!

@spence-s spence-s reopened this Feb 13, 2021
@spence-s
Copy link
Owner

I am going to add this as an option to return the objects from diff instead of the strings in a new minor version. Will keep this open until I do.

@gustawdaniel
Copy link
Contributor

there are three types that we have for differences:

string | Change | Diff

when Change is union of two

Change =
  | {
      path: string[];
      dotPath: string;
      kind: 'I' | 'R';
      index: number;
      val: unknown;
    }
  | {
      path: string[];
      dotPath: string;
      kind: keyof DiffConfig['templates'];
      isArray: boolean;
      lhs: unknown;
      rhs: unknown;
      index: number | undefined;
      val: unknown;
    };

and diff is

export default class Diff {
  public readonly isArray: boolean;
  public readonly lhs: unknown;
  public readonly rhs: unknown;
  public index: number | undefined;
  public readonly path: unknown[] | undefined;
  public val: unknown; 
  public readonly dotPath: string;
  readonly kind: 'N' | 'D' | 'A' | 'E';
  private readonly item: deepDiff.Diff<unknown, unknown> | undefined;
  private readonly hasNestedChanges: boolean;
}

are we going to return these diffs as them are saved now, or introduce unification?

@gustawdaniel gustawdaniel mentioned this issue Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants