Skip to content

Commit

Permalink
Add patch function
Browse files Browse the repository at this point in the history
  • Loading branch information
Swizz committed Nov 11, 2017
1 parent 17a1e2c commit 55dc24d
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 2 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const prevCar = {
- [Understand the patch](#understand-the-patch)
- [API](#api)
- [interface patch](#interface-patch--do-object-undo-object-)
- [function diff](#function-diff-from-object-to-object--patch)
- [function diff](#function-diff-from-object-array-to-object-array--patch)
- [function patch](#function-patch-from-object-array-diff-patch--object)
- [Recipes](#recipes)
- [Operation type](#operation-type)
- [Array time traveling](#array-time-traveling)
Expand Down Expand Up @@ -119,6 +120,17 @@ present object or array properties to apply the patch.
So as we discovered earlier every merge functions will a be great candidates as
our patch function.

### Using patch function

T(a)rdis provide you a patch function, ready to counter all the T(a)rdis diff
function features. This function is aligned with the diff signature to allow you
to take all benefits of T(a)rdis with a sanitized experience.

```js
let past = patch(future, present.undo)
let future = patch(past, present.do)
```

### Using Object.assign

Object.assign is a JS function used to copy the values from one (or more) object(s)
Expand Down Expand Up @@ -172,11 +184,17 @@ This is the patch object, it will hold two properties :
* do : The patch to use to apply the change to a previous state
* undo : The patch to use to undo the change from the current state
#### function diff: (from: object, to: object) => patch
#### function diff: (from: object | array, to: object | array) => patch
The diff function will return the patch object by comparing the second object to
the first one. The patch will help to undo the changes or to redo them later.
#### function patch: (from: object | array, diff: patch) => object
The patch function is an helpful function to merge the from object with the given
diff patch. Object will be merge into a new object. The patch function return
an object with all the keys, the deleted properties will appear as undefined.
## Recipes
### Operation type
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ export function diff(from, to) {

return patch
}

export function patch(from, diff) {
var to = {}
for (var key in from) {
to[key] = from[key]
}
for (var key in diff) {
to[key] = diff[key]
}
return to
}
File renamed without changes.
151 changes: 151 additions & 0 deletions tests/patch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { diff, patch } from "../src"

describe("flat", () => {
test("patch added", () => {
const from = { a: 1, b: 2 }

const to = { a: 1, b: 2, c: 3 }

const future = diff(from, to)

expect(patch(from, future.do)).toEqual(to)

expect(patch(to, future.undo)).toEqual(from)
})

test("patch deleted", () => {
const from = { a: 1, b: 2 }

const to = { a: 1 }

const future = diff(from, to)

expect(patch(from, future.do)).toEqual(to)

expect(patch(to, future.undo)).toEqual(from)
})

test("patch updated", () => {
const from = { a: 1, b: 2 }

const to = { a: 1, b: 1 }

const future = diff(from, to)

expect(patch(from, future.do)).toEqual(to)

expect(patch(to, future.undo)).toEqual(from)
})

test("patch kept", () => {
const from = { a: 1, b: 2 }

const to = { a: 1, b: 2 }

const future = diff(from, to)

expect(patch(from, future.do)).toEqual(to)

expect(patch(to, future.undo)).toEqual(from)
})
})

describe("nested", () => {
test("patch added", () => {
const from = { a: 1, b: { a: 1, b: 2 } }

const to = { a: 1, b: { a: 1, b: 2, c: 3 } }

const future = diff(from, to)

expect(patch(from, future.do)).toEqual(to)

expect(patch(to, future.undo)).toEqual(from)
})

test("patch deleted", () => {
const from = { a: 1, b: { a: 1, b: 2 } }

const to = { a: 1, b: { a: 1 } }

const future = diff(from, to)

expect(patch(from, future.do)).toEqual(to)

expect(patch(to, future.undo)).toEqual(from)
})

test("patch updated", () => {
const from = { a: 1, b: { a: 1, b: 2 } }

const to = { a: 1, b: { a: 1, b: 1 } }

const future = diff(from, to)

expect(patch(from, future.do)).toEqual(to)

expect(patch(to, future.undo)).toEqual(from)
})

test("patch kept", () => {
const from = { a: 1, b: { a: 1, b: 2 } }

const to = { a: 1, b: { a: 1, b: 2 } }

const future = diff(from, to)

expect(patch(from, future.do)).toEqual(to)

expect(patch(to, future.undo)).toEqual(from)
})
})

describe("array", () => {
test("patch added", () => {
const from = [1, 2]

const to = [1, 2, 3]

const future = diff(from, to)

expect(Array.from(patch(from, future.do))).toEqual(to)

expect(Array.from(patch(to, future.undo))).toEqual(from)
})

test("patch deleted", () => {
const from = [1, 2]

const to = [1]

const future = diff(from, to)

expect(Array.from(patch(from, future.do))).toEqual(to)

expect(Array.from(patch(to, future.undo))).toEqual(from)
})

test("patch updated", () => {
const from = [1, 2]

const to = [1, 1]

const future = diff(from, to)

expect(Array.from(patch(from, future.do))).toEqual(to)

expect(Array.from(patch(to, future.undo))).toEqual(from)
})

test("patch kept", () => {
const from = [1, 2]

const to = [1, 2]

const future = diff(from, to)

expect(Array.from(patch(from, future.do))).toEqual(to)

expect(Array.from(patch(to, future.undo))).toEqual(from)
})
})

0 comments on commit 55dc24d

Please sign in to comment.