Skip to content

Commit 6364c4c

Browse files
committed
addType
1 parent 28069e3 commit 6364c4c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import merge from './util/merge'
22
import converter from './util/converter'
3+
import { isFunction } from './util/is'
34
import createNodeFactory from './api/createNodeFactory'
45
import createStoreFactory from './api/createStoreFactory'
56
import applyPatchFactory from './api/applyPatchFactory'
@@ -14,6 +15,11 @@ function factory() {
1415
const decoders = [Delete.decode, Replace.decode]
1516
const encode = (object, list = encoders) => converter(object, list)
1617
const decode = (object, list = decoders) => converter(object, list)
18+
const addType = ({ patch, encode, decode }) => {
19+
if (isFunction(patch)) patchers.push(patch)
20+
if (isFunction(encode)) encoders.push(encode)
21+
if (isFunction(decode)) decoders.push(decode)
22+
}
1723

1824
const applyPatch = applyPatchFactory(patchers)
1925
const createStore = createStoreFactory(applyPatch)
@@ -27,6 +33,7 @@ function factory() {
2733
applyPatch,
2834
createNode,
2935
createStore,
36+
addType,
3037
TYPE
3138
}
3239
}

test/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const exported = [
1010
'applyPatch',
1111
'createNode',
1212
'createStore',
13+
'addType',
1314
'TYPE'
1415
]
1516

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import test from 'ava'
2-
import { applyPatch, encode, decode, TYPE } from '../'
2+
import { applyPatch, encode, decode, addType } from '../'
3+
import Splice from '../src/types/splice'
34
import { getNewPlain } from '../src/util/get'
45
import { isPlainObject } from '../src/util/is'
56

7+
addType(Splice)
8+
69
function testBasic(t, patch, expected) {
710
const encoded = encode(patch)
811
const decoded = decode(encoded)
@@ -29,7 +32,7 @@ function testUnpatch(t, target, patch, expected, reverse = true) {
2932
}
3033

3134
test('Valid type', function(t) {
32-
const patch = { convert: TYPE.Splice(0, 1, 'world') }
35+
const patch = { convert: Splice(0, 1, 'world') }
3336
const expected = { convert: { $s: [0, 1, 'world'] } }
3437
testBasic(t, patch, expected)
3538
})
@@ -48,35 +51,35 @@ test('Ignore', function(t) {
4851

4952
test('removing and adding', function(t) {
5053
const target = { array: ['a', 'b', 'c'] }
51-
const patch = { array: TYPE.Splice(0, 1, 'd') }
54+
const patch = { array: Splice(0, 1, 'd') }
5255
const expected = { array: ['d', 'b', 'c'] }
5356
testUnpatch(t, target, patch, expected)
5457
})
5558

5659
test('removing', function(t) {
5760
const target = { array: ['a', 'b', 'c'] }
58-
const patch = { array: TYPE.Splice(0, 1) }
61+
const patch = { array: Splice(0, 1) }
5962
const expected = { array: ['b', 'c'] }
6063
testUnpatch(t, target, patch, expected)
6164
})
6265

6366
test('removing middle', function(t) {
6467
const target = { array: ['a', 'b', 'c'] }
65-
const patch = { array: TYPE.Splice(1, 1) }
68+
const patch = { array: Splice(1, 1) }
6669
const expected = { array: ['a', 'c'] }
6770
testUnpatch(t, target, patch, expected)
6871
})
6972

7073
test('adding', function(t) {
7174
const target = { array: ['a', 'b', 'c'] }
72-
const patch = { array: TYPE.Splice(3, 0, 'd') }
75+
const patch = { array: Splice(3, 0, 'd') }
7376
const expected = { array: ['a', 'b', 'c', 'd'] }
7477
testUnpatch(t, target, patch, expected)
7578
})
7679

7780
// test('negative (not supported yet)', function(t) {
7881
// const target = { array: ['angel', 'clown', 'mandarin', 'sturgeon'] }
79-
// const patch = { array: TYPE.Splice(-2, 1) }
82+
// const patch = { array: Splice(-2, 1) }
8083
// const expected = { array: ['angel', 'clown', 'sturgeon'] }
8184
// testUnpatch(t, target, patch, expected, false)
8285
// })

0 commit comments

Comments
 (0)