Skip to content

Commit e17c47f

Browse files
committed
Implementing TYPE.Splice
1 parent 572fca7 commit e17c47f

File tree

7 files changed

+127
-35
lines changed

7 files changed

+127
-35
lines changed

src/const.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const DELETE_KEY = '$d'
22
export const FUNCTION_KEY = '$f'
33
export const REPLACE_KEY = '$r'
4+
export const SPLICE_KEY = '$s'
45
export const ESCAPE_KEY = '$escape'

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import createStoreFactory from './api/createStoreFactory'
55
import applyPatchFactory from './api/applyPatchFactory'
66
import Delete from './types/Delete'
77
import Replace from './types/Replace'
8+
import Splice from './types/Splice'
89

910
function factory() {
10-
const TYPE = { Delete, Replace }
11-
const patchers = [Delete.patch, Replace.patch]
11+
const TYPE = { Delete, Replace, Splice }
12+
const patchers = [Delete.patch, Replace.patch, Splice.patch]
1213
const encoders = [Delete.encode, Replace.encode]
1314
const decoders = [Delete.decode, Replace.decode]
1415
const encode = (object, list = encoders) => converter(object, list)

src/types/Delete.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export default function Delete() {
77
}
88
}
99

10-
Delete.patch = function({ destiny, prop, oldValue, had_prop }) {
11-
if (destiny[prop] instanceof Delete) {
10+
Delete.patch = function({ origin, destiny, prop, oldValue, had_prop }) {
11+
if (origin[prop] instanceof Delete || origin[prop] === Delete) {
1212
delete destiny[prop]
1313
}
1414
if (!had_prop) {

src/types/Replace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export default function Replace(value) {
88
this.value = value
99
}
1010

11-
Replace.patch = function({ destiny, prop, oldValue }) {
12-
if (destiny[prop] instanceof Replace) {
11+
Replace.patch = function({ origin, destiny, prop, oldValue }) {
12+
if (origin[prop] instanceof Replace) {
1313
destiny[prop] = destiny[prop].value
1414
}
1515
return oldValue

src/types/Splice.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { isArray } from '../util/is'
2+
import { ESCAPE_KEY, SPLICE_KEY } from '../const'
3+
import { getUniqueKey } from '../util/get'
4+
5+
export default function Splice(...args) {
6+
if (!(this instanceof Splice)) {
7+
return new Splice(...args)
8+
}
9+
this.args = args
10+
}
11+
12+
Splice.patch = function({ origin, destiny, prop, oldValue }) {
13+
const origin_value = origin[prop]
14+
if (isArray(oldValue) && origin_value instanceof Splice) {
15+
destiny[prop] = oldValue
16+
const { args } = origin_value
17+
const spliced = oldValue.splice.apply(oldValue, args)
18+
const inverted = [args[0], args.length - 2].concat(spliced)
19+
return Splice.apply(null, inverted)
20+
}
21+
return oldValue
22+
}
23+
24+
Splice.encode = function({ value }) {
25+
if (value instanceof Splice) {
26+
return { [SPLICE_KEY]: value.args }
27+
} else if (isValidToDecode({ value })) {
28+
return { [ESCAPE_KEY]: value }
29+
}
30+
return value
31+
}
32+
33+
Splice.decode = function({ value }) {
34+
if (isValidToDecode({ value })) {
35+
return new Splice(value[SPLICE_KEY])
36+
} else if (
37+
isValidToEscape({ value }) &&
38+
isValidToDecode({ value: value[ESCAPE_KEY] })
39+
) {
40+
return value[ESCAPE_KEY]
41+
}
42+
return value
43+
}
44+
45+
function isValidToDecode({ value }) {
46+
return (
47+
getUniqueKey(value) === SPLICE_KEY && value.hasOwnProperty(SPLICE_KEY)
48+
)
49+
}
50+
51+
function isValidToEscape({ value }) {
52+
return getUniqueKey(value) === ESCAPE_KEY
53+
}

src/util/createRequest.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
export default function createRequest() {
2-
let resolve
3-
let reject
4-
const promise = new Promise((res, rej) => {
5-
resolve = res
6-
reject = rej
7-
})
8-
promise.resolve = resolve
9-
promise.reject = reject
10-
return promise
11-
}
12-
13-
// function Request(executor) {
14-
// let resolve
15-
// let reject
16-
// const promise = new Promise((res, rej) => {
17-
// resolve = res
18-
// reject = rej
19-
// // return executor(res, rej)
20-
// })
21-
22-
// promise.__proto__ = Request.prototype
23-
// promise.resolve = resolve
24-
// promise.reject = reject
25-
// return promise
26-
// }
27-
28-
// Request.__proto__ = Promise
29-
// Request.prototype.__proto__ = Promise.prototype
1+
export default function createRequest() {
2+
let resolve
3+
let reject
4+
const promise = new Promise((res, rej) => {
5+
resolve = res
6+
reject = rej
7+
})
8+
promise.resolve = resolve
9+
promise.reject = reject
10+
return promise
11+
}
12+
13+
// function Request(executor) {
14+
// let resolve
15+
// let reject
16+
// const promise = new Promise((res, rej) => {
17+
// resolve = res
18+
// reject = rej
19+
// // return executor(res, rej)
20+
// })
21+
22+
// promise.__proto__ = Request.prototype
23+
// promise.resolve = resolve
24+
// promise.reject = reject
25+
// return promise
26+
// }
27+
28+
// Request.__proto__ = Promise
29+
// Request.prototype.__proto__ = Promise.prototype

test/type_splice.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import test from 'ava'
2+
import { applyPatch, encode, decode, TYPE } from '../'
3+
import { getNewPlain } from '../src/util/get'
4+
import { isPlainObject } from '../src/util/is'
5+
6+
function testBasic(t, patch, expected, recursive = true) {
7+
const encoded = encode(patch)
8+
const decoded = decode(encoded)
9+
t.deepEqual(expected, encoded)
10+
t.deepEqual(patch, decoded)
11+
t.not(patch, encoded)
12+
t.not(encoded, decoded)
13+
}
14+
15+
function testUnpatch(t, target, patch, expected, reverse = true) {
16+
const cloned = getNewPlain(target)
17+
const output = applyPatch(target, patch)
18+
const { unpatch, mutations, result } = output
19+
console.log({ result, cloned })
20+
if (isPlainObject(result)) {
21+
t.is(target, result)
22+
}
23+
target = result
24+
t.deepEqual(target, expected)
25+
if (reverse) {
26+
const output2 = applyPatch(target, unpatch)
27+
t.deepEqual(output2.result, cloned)
28+
}
29+
return { unpatch, mutations }
30+
}
31+
32+
test('patch', function(t) {
33+
const target = { array: ['a', 'b', 'c'] }
34+
const patch = { array: TYPE.Splice(0, 1, 'd') }
35+
const expected = { array: ['d', 'b', 'c'] }
36+
testUnpatch(t, target, patch, expected)
37+
})

0 commit comments

Comments
 (0)