From f28667dd367cb071650f158f544ec7c741ac9fcb Mon Sep 17 00:00:00 2001 From: Swizz Date: Sat, 11 Nov 2017 14:35:03 +0100 Subject: [PATCH] Add array support to patch --- src/index.js | 4 ++-- tests/patch.test.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 767e071..b1e1599 100644 --- a/src/index.js +++ b/src/index.js @@ -31,7 +31,7 @@ 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] @@ -39,5 +39,5 @@ export function patch(from, diff) { for (var key in diff) { to[key] = diff[key] } - return to + return array && "length" in to ? Array.from(to) : to } \ No newline at end of file diff --git a/tests/patch.test.js b/tests/patch.test.js index 890c9f6..1c3f21c 100644 --- a/tests/patch.test.js +++ b/tests/patch.test.js @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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) + }) +}) \ No newline at end of file