Skip to content

Commit df1e1d7

Browse files
committed
Small changes
1 parent f7acb07 commit df1e1d7

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"main": "dist/dop.js",
55
"browser": "dist/dop.umd.js",
66
"license": "MIT",
7-
"url": "http://www.distributedobjectprotocol.org",
7+
"url": "https://distributedobjectprotocol.org/",
88
"repository": {
99
"type": "git",
1010
"url": "git+https://github.com/DistributedObjectProtocol/dop.git"

src/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ import Replace from './types/Replace'
1010
// import Splice from './types/Splice'
1111

1212
function factory() {
13-
const TYPE = { Delete, Replace }
14-
const patchers = [Delete.patch, Replace.patch]
15-
const encoders = [Delete.encode, Replace.encode]
16-
const decoders = [Delete.decode, Replace.decode]
13+
const patchers = []
14+
const encoders = []
15+
const decoders = []
1716
const encode = (object, list = encoders) => converter(object, list)
1817
const decode = (object, list = decoders) => converter(object, list)
18+
const applyPatch = applyPatchFactory(patchers)
19+
const createStore = createStoreFactory(applyPatch)
20+
const createNode = createNodeFactory({ encoders, decoders })
1921
const addType = ({ patch, encode, decode }) => {
2022
if (isFunction(patch)) patchers.push(patch)
2123
if (isFunction(encode)) encoders.push(encode)
2224
if (isFunction(decode)) decoders.push(decode)
2325
}
2426

25-
const applyPatch = applyPatchFactory(patchers)
26-
const createStore = createStoreFactory(applyPatch)
27-
const createNode = createNodeFactory({ encoders, decoders })
27+
addType(Delete)
28+
addType(Replace)
2829

2930
return {
3031
version,
@@ -36,7 +37,7 @@ function factory() {
3637
createNode,
3738
createStore,
3839
addType,
39-
TYPE
40+
TYPE: { Delete, Replace }
4041
}
4142
}
4243

test/.addType.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import test from 'ava'
2+
import { applyPatch, encode, decode, addType } from '../'
3+
import Splice from '../src/types/Splice'
4+
import { getNewPlain } from '../src/util/get'
5+
import { isPlainObject } from '../src/util/is'
6+
7+
// Defining a type that push elements to an array
8+
function Push(...elements) {
9+
if (!(this instanceof Push)) {
10+
return new Push(...elements)
11+
}
12+
this.elements = elements
13+
}
14+
Push.patch = function({ origin, destiny, prop, oldValue }) {
15+
const originValue = origin[prop]
16+
if (isArray(oldValue) && originValue instanceof Push) {
17+
destiny[prop] = oldValue
18+
return Push.apply(null, originValue.elements)
19+
}
20+
return oldValue
21+
}
22+
Push.encode = function({ value }) {
23+
if (value instanceof Push) {
24+
return { $push: value.elements }
25+
}
26+
return value
27+
}
28+
Push.decode = function({ value }) {
29+
if (getUniqueKey(value) === '$push' && isArray(value['$push'])) {
30+
return Push.apply(null, value['$push'])
31+
}
32+
return value
33+
}
34+
35+
addType(Push)

0 commit comments

Comments
 (0)