spence-s / human-object-diff

An object diff you can read to your mom
https://www.npmjs.com/package/human-object-diff
MIT License
15 stars 6 forks source link

return array of objects instead array of string #9

Open miedzikd opened 3 years ago

miedzikd commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 1 year ago

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?