Skip to content

Commit

Permalink
Add array support to patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Swizz committed Nov 11, 2017
1 parent a7e819a commit f28667d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -31,13 +31,13 @@ export function diff(from, to) {
return patch
}

export function patch(from, diff) {
export function patch(from, diff, array) {
var to = {}
for (var key in from) {
to[key] = from[key]
}
for (var key in diff) {
to[key] = diff[key]
}
return to
return array && "length" in to ? Array.from(to) : to
}
30 changes: 29 additions & 1 deletion tests/patch.test.js
Expand Up @@ -111,6 +111,10 @@ describe("array", () => {
expect(Array.from(patch(from, future.do))).toEqual(to)

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

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

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

test("patch deleted", () => {
Expand All @@ -123,6 +127,10 @@ describe("array", () => {
expect(Array.from(patch(from, future.do))).toEqual(to)

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

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

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

test("patch updated", () => {
Expand All @@ -135,6 +143,10 @@ describe("array", () => {
expect(Array.from(patch(from, future.do))).toEqual(to)

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

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

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

test("patch kept", () => {
Expand All @@ -147,5 +159,21 @@ describe("array", () => {
expect(Array.from(patch(from, future.do))).toEqual(to)

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

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

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

test("do not transform object", () => {
const from = { a: 1, b: 1 }

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

const future = diff(from, to)

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

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

0 comments on commit f28667d

Please sign in to comment.