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

Update deep-diff.d.ts #11637

Closed
wants to merge 5 commits into from
Closed

Update deep-diff.d.ts #11637

wants to merge 5 commits into from

Conversation

forabi
Copy link
Contributor

@forabi forabi commented Sep 30, 2016

Documentation: https://github.com/flitbit/diff

Improvements compared to the current typings:

  • Introduces and uses a DiffableObject<T> type instead of a generic object, this allows VS Code to understand what the result of a function call to diff will be.
  • Introduces a BaseDiff<T> interface which is extended for each kind of diffs. This allows VS Code to perform control flow analysis in TypeScript >= 2.0.
  • For ArrayDiff, the first item in the path array is a number
  • Add descriptions for all functions and types
  • diff() may return undefined for indentical objects

Example

import deepDiff from 'deep-diff';
// or import { diff as deepDiff } from 'deep-diff';

type landmark = { [id: string]: GeometricalObject } | { };

const diffs = deepDiff(
  this.props.landmarks,
  nextProps.landmarks
); // diffs is of type Diff<GeometricalObject>[] or undefined;

diffs.forEach(diff => /* do something */) // Error: diffs is possibly undefined

if (diffs) {
  // diffs is now of type Diff<GeometricalObject>[]
  shouldRerender = true;
  diffs.forEach(diff => {
    // diff is of type Diff<GeometricalObject>
    if (diff.kind === 'N') {
      // diff is now of type NewDiff<GeometricalObject>
      // rhs is of type GeometricalObject;
      const object = geometricalObjectToFabricObject(diff.rhs, diff.path[0]);


      // Property lhs is undefined for diffs of kind N
      diff.lhs // Error: Property 'lhs' does not exist on type 'NewDiff<GeometricalObject>'
      if (object) {
        // diff.path is of type string[];
        objectMap.set(diff.path[0], object);
        landmarksGroup.add(object);
      }
    } else if (diff.kind === 'E') {
      // diff is of type EditDiff<GeometricalObject>
      // diff has properties: lhs, rhs
      objectMap.get(diff.path[0]).set(diff.path[1], diff.rhs);
    } else if (diff.kind === 'D') {
      // diff is of type DeleteDiff<GeometricalObject>
      // Property rhs is undefined for diffs of kind N
      diff.rhs // Error: Property 'rhs' does not exist on type 'NewDiff<GeometricalObject>'
      objectMap.get(diff.path[1]).remove();
      objectMap.delete(diff.path[0]);
    } else if (diff.kind === 'A') {
      // diff is of type ArrayDiff<GeometricalObject>
      // diff has properties: index, item
      // diff.path[0] is of type number
      // diff.path[1] is of type string
    }
  });
}

Improvements compared to the current typings:

* Introduces and uses a DiffableObject<T> instead of a generic object, this allows VS Code to understand what the result of a function call to `diff` will be.
* Introduces a BaseDiff<T> interface which is extended for each kind of Diffs, this allows VS Code to perform control flow analysis in TypeScript >= 2.0.
* For ArrayDiff, the first item in the path array is a number
* Add descriptions for all functions and types

```typescript
import deepDiff from 'deep-diff';
// or import { diff as deepDiff } from 'deep-diff';

type landmark = { [id: string]: GeometricalObject } | { };

const diffs = deepDiff(
  this.props.landmarks,
  nextProps.landmarks
); // diffs is of type Diff<GeometricalObject>[];
if (diffs) {
  shouldRerender = true;
  diffs.forEach(diff => {
    // diff is of type Diff<GeometricalObject>
    if (diff.kind === 'N') {
      // diff is now of type NewDiff<GeometricalObject>
     // rhs is of type GeometricalObject;
      const object = geometricalObjectToFabricObject(diff.rhs, diff.path[0]);
      

      // Property lhs is undefined for diffs of kind N
      diff.lhs // Error: Property 'lhs' does not exist on type 'NewDiff<GeometricalObject>'
      if (object) {
        // diff.path is of type string[];
        objectMap.set(diff.path[0], object);
        landmarksGroup.add(object);
      }
    } else if (diff.kind === 'E') {
      // diff is of type EditDiff<GeometricalObject>
      // diff has properties: lhs, rhs
      objectMap.get(diff.path[0]).set(diff.path[1], diff.rhs);
    } else if (diff.kind === 'D') {
      // diff is of type DeleteDiff<GeometricalObject>
     // Property rhs is undefined for diffs of kind N
      diff.rhs // Error: Property 'rhs' does not exist on type 'NewDiff<GeometricalObject>'
      objectMap.get(diff.path[1]).remove();
      objectMap.delete(diff.path[0]);
    }  else if (diff.kind === 'A') {
        // diff is of type ArrayDiff<GeometricalObject>
        // diff has properties: index, item
        // diff.path[0] is of type number
        // diff.path[1] is of type string
     }
  });
```
@dt-bot
Copy link
Member

dt-bot commented Sep 30, 2016

deep-diff/deep-diff.d.ts

to author (@ZauberNerd). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?

@@ -1,42 +1,142 @@
// Type definitions for deep-diff
// Project: https://github.com/flitbit/diff/
// Definitions by: ZauberNerd <https://github.com/ZauberNerd/>
// Definitions by: forabi <https://github.com/forabi/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't remove original author's name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restored the original author

@ZauberNerd
Copy link
Contributor

👍 from my side - but the travis build is still failing.

@yuit
Copy link
Contributor

yuit commented Nov 8, 2016

@forabi Thank you for the PR, could you fix the CI-failure? once that is done I will merge it in

@forabi
Copy link
Contributor Author

forabi commented Nov 9, 2016

I realized my usage of deep-diff was a very specific case. I had to do diffing of objects that could be represented as a map, i.e

{ [id: string]: GeometricalObject };

The library can do diffs on objects with properties assigned to different types of objects, so I don't think making the tests pass for my use case is a good idea.

Hopefully, once mappable and partial types land in TS, I will fix this.

@paulvanbrenk
Copy link
Contributor

paulvanbrenk commented Nov 30, 2016

@forabi can you fix the Travis issues? This may require you to merge the base branch in.
If no response in 7 days, I'll close this PR.

@paulvanbrenk paulvanbrenk added the Revision needed This PR needs code changes before it can be merged. label Nov 30, 2016
@ghost
Copy link

ghost commented Dec 8, 2016

@forabi Mappable types are in 2.1 now, so please make a PR to the types-2.0 branch. Instructions here.

@ghost ghost closed this Dec 8, 2016
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Revision needed This PR needs code changes before it can be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants