Skip to content

Commit 2c6e6b1

Browse files
committed
Making Swap type
1 parent 02a7a38 commit 2c6e6b1

File tree

10 files changed

+145
-75
lines changed

10 files changed

+145
-75
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"main": "dist/dop.js",
55
"browser": "dist/dop.umd.js",
66
"license": "MIT",

src/const.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
export const ESCAPE_KEY = '$escape'
12
export const DELETE_KEY = '$d'
23
export const FUNCTION_KEY = '$f'
34
export const REPLACE_KEY = '$r'
45
export const SPLICE_KEY = '$s'
56
export const INNER_KEY = '$i'
6-
export const ESCAPE_KEY = '$escape'
7+
export const SWAP_KEY = '$w'

src/types/Delete.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { ESCAPE_KEY, DELETE_KEY } from '../const'
2-
import { getUniqueKey } from '../util/get'
32
import { isValidToEscape } from '../util/isValid'
3+
import { getUniqueKey } from '../util/get'
44

55
export default function Delete() {
66
if (!(this instanceof Delete)) {
77
return new Delete()
88
}
99
}
1010

11-
Delete.patch = function({ patch, target, prop, old_value, had_prop }) {
11+
Delete.patch = function ({ patch, target, prop, old_value, had_prop }) {
1212
if (patch[prop] instanceof Delete || patch[prop] === Delete) {
1313
delete target[prop]
1414
}
@@ -18,7 +18,7 @@ Delete.patch = function({ patch, target, prop, old_value, had_prop }) {
1818
return old_value
1919
}
2020

21-
Delete.encode = function({ value }) {
21+
Delete.encode = function ({ value }) {
2222
if (value instanceof Delete || value === Delete) {
2323
return { [DELETE_KEY]: 0 } // we don't go deeper
2424
} else if (isValidToDecodeDelete({ value })) {
@@ -27,7 +27,7 @@ Delete.encode = function({ value }) {
2727
return value
2828
}
2929

30-
Delete.decode = function({ value }) {
30+
Delete.decode = function ({ value }) {
3131
if (isValidToDecodeDelete({ value })) {
3232
return new Delete()
3333
} else if (

src/types/Function.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { ESCAPE_KEY, FUNCTION_KEY } from '../const'
2+
import { isValidToEscape, isValidToDecode } from '../util/isValid'
23
import { getUniqueKey } from '../util/get'
34
import { isInteger, isFunction } from '../util/is'
4-
import { isValidToDecode, isValidToEscape } from '../util/isValid'
55

66
export default function Function() {}
77

8-
Function.encode = function({
8+
Function.encode = function ({
99
value,
1010
remote_functions,
1111
local_functions,
12-
registerLocalFunctionFromEncode
12+
registerLocalFunctionFromEncode,
1313
}) {
1414
if (isFunction(value)) {
1515
if (remote_functions.has(value)) return null
@@ -23,10 +23,10 @@ Function.encode = function({
2323
return value
2424
}
2525

26-
Function.decode = function({
26+
Function.decode = function ({
2727
value,
2828
remote_functions_id,
29-
createRemoteFunction
29+
createRemoteFunction,
3030
}) {
3131
if (
3232
getUniqueKey(value) === FUNCTION_KEY &&

src/types/Inner.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ESCAPE_KEY, INNER_KEY } from '../const'
22
import { isValidToEscape } from '../util/isValid'
3-
import { isArray, isPlainObject } from '../util/is'
43
import { getUniqueKey } from '../util/get'
4+
import { isArray, isPlainObject } from '../util/is'
55
import Delete from './Delete'
66

77
export default function Inner(patch) {
@@ -13,7 +13,7 @@ export default function Inner(patch) {
1313
this.patch = patch
1414
}
1515

16-
Inner.patch = function({ patch, target, prop, old_value, applyPatch }) {
16+
Inner.patch = function ({ patch, target, prop, old_value, applyPatch }) {
1717
const patch_value = patch[prop]
1818
if (patch_value instanceof Inner) {
1919
target[prop] = old_value
@@ -42,7 +42,7 @@ Inner.patch = function({ patch, target, prop, old_value, applyPatch }) {
4242
return old_value
4343
}
4444

45-
Inner.encode = function({ value, encode }) {
45+
Inner.encode = function ({ value, encode }) {
4646
if (value instanceof Inner) {
4747
return { [INNER_KEY]: encode(value.patch) }
4848
} else if (isValidToDecodeInner({ value })) {
@@ -51,7 +51,7 @@ Inner.encode = function({ value, encode }) {
5151
return value
5252
}
5353

54-
Inner.decode = function({ value, decode }) {
54+
Inner.decode = function ({ value, decode }) {
5555
if (isValidToDecodeInner({ value })) {
5656
return new Inner(decode(value[INNER_KEY]))
5757
} else if (

src/types/Replace.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ESCAPE_KEY, REPLACE_KEY } from '../const'
2-
import { isValidToDecode, isValidToEscape } from '../util/isValid'
2+
import { isValidToEscape, isValidToDecode } from '../util/isValid'
33

44
export default function Replace(value) {
55
if (!(this instanceof Replace)) {
@@ -8,15 +8,15 @@ export default function Replace(value) {
88
this.value = value
99
}
1010

11-
Replace.patch = function({ patch, target, prop, old_value }) {
11+
Replace.patch = function ({ patch, target, prop, old_value }) {
1212
if (patch[prop] instanceof Replace) {
1313
target[prop] = patch[prop].value
1414
return new Replace(old_value)
1515
}
1616
return old_value
1717
}
1818

19-
Replace.encode = function({ value }) {
19+
Replace.encode = function ({ value }) {
2020
if (value instanceof Replace) {
2121
return { [REPLACE_KEY]: value.value } // we don't go deeper
2222
} else if (isValidToDecode({ value, key: REPLACE_KEY })) {
@@ -25,7 +25,7 @@ Replace.encode = function({ value }) {
2525
return value
2626
}
2727

28-
Replace.decode = function({ value }) {
28+
Replace.decode = function ({ value }) {
2929
if (isValidToDecode({ value, key: REPLACE_KEY })) {
3030
return new Replace(value[REPLACE_KEY])
3131
} else if (

src/types/Splice.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { isArray } from '../util/is'
2-
import { isValidToEscape } from '../util/isValid'
31
import { ESCAPE_KEY, SPLICE_KEY } from '../const'
2+
import { isValidToEscape } from '../util/isValid'
43
import { getUniqueKey } from '../util/get'
4+
import { isArray } from '../util/is'
55

66
export default function Splice(...args) {
77
if (!(this instanceof Splice)) {
@@ -10,7 +10,7 @@ export default function Splice(...args) {
1010
this.args = args
1111
}
1212

13-
Splice.patch = function({ patch, target, prop, old_value }) {
13+
Splice.patch = function ({ patch, target, prop, old_value }) {
1414
const patch_value = patch[prop]
1515
if (patch_value instanceof Splice) {
1616
target[prop] = old_value
@@ -27,7 +27,7 @@ Splice.patch = function({ patch, target, prop, old_value }) {
2727
return old_value
2828
}
2929

30-
Splice.encode = function({ value }) {
30+
Splice.encode = function ({ value }) {
3131
if (value instanceof Splice) {
3232
return { [SPLICE_KEY]: value.args }
3333
} else if (isValidToDecode({ value, key: SPLICE_KEY })) {
@@ -36,7 +36,7 @@ Splice.encode = function({ value }) {
3636
return value
3737
}
3838

39-
Splice.decode = function({ value }) {
39+
Splice.decode = function ({ value }) {
4040
if (isValidToDecode({ value, key: SPLICE_KEY })) {
4141
return Splice.apply(null, value[SPLICE_KEY])
4242
} else if (

src/types/Swap.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { ESCAPE_KEY, SWAP_KEY } from '../const'
2+
import { isValidToEscape } from '../util/isValid'
3+
import { getUniqueKey } from '../util/get'
4+
5+
export default function Swap(...args) {
6+
if (!(this instanceof Swap)) {
7+
return new Swap(...args)
8+
}
9+
this.args = args
10+
}
11+
12+
Swap.patch = function ({ patch, target, prop, old_value }) {
13+
const patch_value = patch[prop]
14+
if (patch_value instanceof Swap) {
15+
target[prop] = old_value
16+
if (isArray(old_value)) {
17+
const array = old_value
18+
const swaps = patch_value.args
19+
if (array.length > 0 && swaps.length > 1) {
20+
const total = swaps.length - 1
21+
for (let index = 0; index < total; index += 2) {
22+
const swap_a = swaps[index]
23+
const swap_b = swaps[index + 1]
24+
const temp_item = array[swap_a]
25+
array[swap_a] = array[swap_b]
26+
array[swap_b] = temp_item
27+
}
28+
}
29+
}
30+
}
31+
}
32+
33+
Swap.encode = function ({ value }) {
34+
if (value instanceof Swap) {
35+
return { [SWAP_KEY]: value.args }
36+
} else if (isValidToDecode({ value, key: SWAP_KEY })) {
37+
return { [ESCAPE_KEY]: value }
38+
}
39+
return value
40+
}
41+
42+
Swap.decode = function ({ value }) {
43+
if (isValidToDecode({ value, key: SWAP_KEY })) {
44+
return Swap.apply(null, value[SWAP_KEY])
45+
} else if (
46+
isValidToEscape({ value }) &&
47+
isValidToDecode({ value: value[ESCAPE_KEY], key: SWAP_KEY })
48+
) {
49+
return value[ESCAPE_KEY]
50+
}
51+
return value
52+
}
53+
54+
function isValidToDecode({ value }) {
55+
return getUniqueKey(value) === SWAP_KEY && isArray(value[SWAP_KEY])
56+
}

0 commit comments

Comments
 (0)