Skip to content

Commit b5bf6ec

Browse files
committed
Swap type done
1 parent 2c6e6b1 commit b5bf6ec

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

src/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import Function from './types/Function'
1010
import Array from './types/Array'
1111
import Delete from './types/Delete'
1212
import Replace from './types/Replace'
13-
import Splice from './types/Splice'
1413
import Inner from './types/Inner'
14+
import Splice from './types/Splice'
15+
import Swap from './types/Swap'
1516

1617
function factory() {
1718
const patchers = []
@@ -34,8 +35,9 @@ function factory() {
3435
addType(Array)
3536
addType(Delete)
3637
addType(Replace)
37-
addType(Splice)
3838
addType(Inner)
39+
addType(Splice)
40+
addType(Swap)
3941

4042
return {
4143
version,
@@ -50,9 +52,10 @@ function factory() {
5052
TYPE: {
5153
Delete,
5254
Replace,
55+
Inner,
5356
Splice,
54-
Inner
55-
}
57+
Swap,
58+
},
5659
}
5760
}
5861

src/types/Swap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ESCAPE_KEY, SWAP_KEY } from '../const'
22
import { isValidToEscape } from '../util/isValid'
33
import { getUniqueKey } from '../util/get'
4+
import { isArray } from '../util/is'
45

56
export default function Swap(...args) {
67
if (!(this instanceof Swap)) {
@@ -25,9 +26,11 @@ Swap.patch = function ({ patch, target, prop, old_value }) {
2526
array[swap_a] = array[swap_b]
2627
array[swap_b] = temp_item
2728
}
29+
return Swap.apply(null, swaps.slice(0).reverse())
2830
}
2931
}
3032
}
33+
return old_value
3134
}
3235

3336
Swap.encode = function ({ value }) {

test/type_inner.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ test('Editing subobject and creating a new one', function (t) {
124124
const patch = TYPE.Inner({ 0: { a: true }, 1: { b: true } })
125125
const expected = [{ a: true }, { b: true }]
126126
testPatchUnpatch(t, target, patch, expected)
127+
console.log(target)
127128
})
128129

129130
test('Encoding unpatch', function (t) {

test/type_splice.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ test('API', function (t) {
3434
t.is(mutations.length, 1)
3535
t.is(result.array, target.array)
3636
t.true(unpatch.array instanceof TYPE.Splice)
37-
// t.deepEqual(unpatch.array.args, [1, 1])
37+
t.deepEqual(unpatch.array.args, [1, 1])
3838
})
3939

4040
test('Patching a non array must do nothing', function (t) {
4141
const target = { array: 1234 }
42-
const patch = { array: TYPE.Splice({ 0: 'b' }) }
42+
const patch = { array: TYPE.Splice(0, 0, 5678) }
4343
const expected = { array: 1234 }
4444
const { mutations } = testPatchUnpatch(t, target, patch, expected)
4545
t.is(mutations.length, 0)

test/type_swap.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import test from 'ava'
2+
import { applyPatch } from '../'
3+
import { TYPE } from '../'
4+
import { testEncodeDecode, testPatchUnpatch } from './utils'
5+
6+
test('Valid type', function (t) {
7+
const patch = { convert: TYPE.Swap(0, 1) }
8+
const expected = { convert: { $w: [0, 1] } }
9+
testEncodeDecode(t, patch, expected)
10+
})
11+
12+
test('Escape', function (t) {
13+
const patch = { escape: { $w: [0, 1] } }
14+
const expected = { escape: { $escape: { $w: [0, 1] } } }
15+
testEncodeDecode(t, patch, expected)
16+
})
17+
18+
test('Ignore', function (t) {
19+
const patch = { ignore: { $w: { a: 2 } } }
20+
const expected = { ignore: { $w: { a: 2 } } }
21+
testEncodeDecode(t, patch, expected)
22+
})
23+
24+
test('API', function (t) {
25+
const target = { array: ['a', 'b'] }
26+
const patch = { array: TYPE.Swap(0, 1) }
27+
const expected = { array: ['b', 'a'] }
28+
const { unpatch, result, mutations } = testPatchUnpatch(
29+
t,
30+
target,
31+
patch,
32+
expected
33+
)
34+
t.is(mutations.length, 1)
35+
t.is(result.array, target.array)
36+
t.true(unpatch.array instanceof TYPE.Swap)
37+
t.deepEqual(unpatch.array.args, [1, 0])
38+
})
39+
40+
test('Patching a non array must do nothing', function (t) {
41+
const target = { array: 1234 }
42+
const patch = { array: TYPE.Swap(0, 1) }
43+
const expected = { array: 1234 }
44+
const { mutations } = testPatchUnpatch(t, target, patch, expected)
45+
t.is(mutations.length, 0)
46+
})
47+
48+
test('Multiple mutations', function (t) {
49+
const target = { array: ['a', 'b', 'c', 'd'] }
50+
const patch = { array: TYPE.Swap(0, 2, 1, 3) }
51+
const expected = { array: ['c', 'd', 'a', 'b'] }
52+
testPatchUnpatch(t, target, patch, expected)
53+
})
54+
55+
test('Combining swaps', function (t) {
56+
const target = { array: ['a', 'b', 'c'] }
57+
const patch = { array: TYPE.Swap(0, 1, 2, 1) }
58+
const expected = { array: ['b', 'c', 'a'] }
59+
testPatchUnpatch(t, target, patch, expected)
60+
})
61+
62+
test('Repeating swaps', function (t) {
63+
const target = { array: ['a', 'b'] }
64+
const patch = { array: TYPE.Swap(0, 1, 1, 0) }
65+
const expected = { array: ['a', 'b'] }
66+
testPatchUnpatch(t, target, patch, expected)
67+
})
68+
69+
test('Swaping do not mutate inner objects, must be the same instances', function (t) {
70+
const target = { array: [{ a: 1 }, { b: 2 }] }
71+
const a = target.array[0]
72+
const b = target.array[1]
73+
const patch = { array: TYPE.Swap(0, 1) }
74+
const expected = { array: [{ b: 2 }, { a: 1 }] }
75+
testPatchUnpatch(t, target, patch, expected, false)
76+
t.is(a, target.array[1])
77+
t.is(b, target.array[0])
78+
})

0 commit comments

Comments
 (0)