Skip to content

Commit eaf1eeb

Browse files
committed
Making encode more modular
1 parent 6ed5e89 commit eaf1eeb

File tree

7 files changed

+77
-86
lines changed

7 files changed

+77
-86
lines changed

src/api/encode.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2-
dop.encode = function(data) {
3-
return JSON.stringify(data, dop.core.encode);
2+
dop.encode = function(data, encoder) {
3+
if (typeof encoder != 'function')
4+
encoder = dop.core.encode;
5+
return JSON.stringify(data, encoder);
46
};

src/core/protocol/encode.js

Lines changed: 16 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,19 @@
1-
dop={core:{}}
2-
var data = {
3-
function: function(){},
4-
NaN: NaN,
5-
Infinity: -Infinity,
6-
undefined: undefined,
71

2+
dop.core.encode = function(property, value) {
3+
return dop.core.encodeProtocol(
4+
property, dop.core.encodeUtil(
5+
property, dop.core.encodeSpecial(property, value)));
86
};
9-
dop.core.encode1 = function(property, value) {
10-
11-
var tof = typeof value;
12-
13-
if (value === Infinity)
14-
return '~I';
15-
16-
if (value === -Infinity)
17-
return '~i';
18-
19-
if (tof == 'number' && isNaN(value))
20-
return '~N';
21-
22-
if (tof == 'object' && value instanceof RegExp)
23-
return '~R' + value.toString();
24-
25-
return value;
26-
27-
};
28-
dop.core.encode2 = function(property, value) {
29-
30-
var tof = typeof value;
31-
if (value === '~i')
32-
return 'LOOOL';
33-
if (tof == 'function')
34-
return '~F';
35-
36-
if (tof == 'undefined') // http://stackoverflow.com/questions/17648150/how-does-json-parse-manage-undefined
37-
return '~U';
38-
39-
if (tof == 'string' && value[0] === '~') // https://jsperf.com/charat-vs-index/5
40-
return '~'+value;
41-
42-
return value;
43-
44-
};
45-
46-
47-
48-
encode = function(data, encoders) {
49-
var length = encoders.length;
50-
return JSON.stringify(data, function recursion(property, value, index) {
51-
if (index === undefined)
52-
index = 0;
53-
if (index<length) {
54-
value = encoders[index](property, value);
55-
return (index<length-1) ? recursion(property, value, index+1) : value;
56-
}
57-
})
58-
}
59-
60-
console.log(encode(data, [dop.core.encode1,dop.core.encode2]));
61-
62-
63-
// // Extending example
64-
// (function() {
65-
// var encode = dop.core.encode;
66-
// dop.core.encode = function(property, value) {
67-
// if (typeof value == 'boolean')
68-
// return '~BOOL';
69-
// return encode(property, value);
70-
// };
71-
// })();
727

8+
// dop.core.multiEncode = function() {
9+
// var encoders = arguments,
10+
// length = encoders.length;
11+
// return function recursion(property, value, index) {
12+
// if (index === undefined)
13+
// index = 0;
14+
// if (index<length) {
15+
// value = encoders[index](property, value);
16+
// return (index<length-1) ? recursion(property, value, index+1) : value;
17+
// }
18+
// }
19+
// };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
dop.core.encodeProtocol = function(property, value) {
3+
4+
var tof = typeof value;
5+
6+
if (tof == 'function')
7+
return '~F';
8+
9+
if (tof == 'undefined') // http://stackoverflow.com/questions/17648150/how-does-json-parse-manage-undefined
10+
return '~U';
11+
12+
return value;
13+
};

src/core/protocol/encodeSpecial.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
dop.core.encodeSpecial = function(property, value) {
3+
return (typeof value == 'string' && value[0] == '~') ? '~'+value : value;
4+
};

src/core/protocol/encodeUtil.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
dop.core.encodeUtil = function(property, value) {
3+
4+
var tof = typeof value;
5+
6+
if (value === Infinity)
7+
return '~I';
8+
9+
if (value === -Infinity)
10+
return '~i';
11+
12+
if (tof == 'number' && isNaN(value))
13+
return '~N';
14+
15+
if (tof == 'object' && value instanceof RegExp)
16+
return '~R' + value.toString();
17+
18+
return value;
19+
};
20+
21+
22+
// // Extending example
23+
// (function() {
24+
// var encode = dop.core.encodeUtil;
25+
// dop.core.encodeUtil = function(property, value) {
26+
// if (typeof value == 'boolean')
27+
// return '~BOOL';
28+
// return encode(property, value);
29+
// };
30+
// })();
31+

test/encode_decode.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ var data = {
1919
inf: '~I',
2020
fun: '~F',
2121
nan: '~N',
22-
reg: '~R',
22+
reg: '~R'
2323
};
2424

2525

2626
test('Multiple data', function(t) {
27-
var decoded = dop.decode(dop.encode(data));
27+
var encoded = dop.encode(data);
28+
var decoded = dop.decode(encoded);
2829
t.deepEqual(decoded, data, 'deepEqual');
2930
data.NaN = NaN;
3031
data.function = function(){};
3132
decoded = dop.decode(dop.encode(data));
3233
t.equal(JSON.stringify(decoded), JSON.stringify(data), 'Stringify');
3334
t.equal(typeof decoded.function, 'function', 'Has function');
34-
// console.log( decoded);
35+
console.log( decoded);
3536
t.end();
3637
});
3738

test/mutations_objects.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ function maketest(t, collectorServer, checkactions) {
2323
var objectClientCopy = dop.util.merge({},objectClient);
2424

2525

26-
var actionServer = decode(encode(removeObjects(collectorServer.getAction())));
27-
var unaction = decode(encode(removeObjects(collectorServer.getUnaction())));
26+
var actionServer = decode(encode(collectorServer.getAction()));
27+
var unaction = decode(encode(collectorServer.getUnaction()));
2828
collectorServer.destroy();
2929
var collectorClient = dopClient.setAction(attachObjects(actionServer, objectClient));
30-
var actionClient = decode(encode(removeObjects(collectorClient.getAction())));
30+
var actionClient = decode(encode(collectorClient.getAction()));
3131
var collectorClientTwo = dopClientTwo.setAction(attachObjects(actionClient, objectClientTwo));
3232
consolelog("### Mutations length: " + collectorServer.mutations.length, collectorClient.mutations.length, collectorClientTwo.mutations.length );
33-
var actionClientTwo = removeObjects(collectorClientTwo.getAction());
33+
var actionClientTwo = collectorClientTwo.getAction();
3434

3535
consolelog("### After server: " + encode(objectServer));
3636
consolelog("### After client: " + encode(objectClient));
@@ -43,7 +43,7 @@ function maketest(t, collectorServer, checkactions) {
4343
t.deepEqual(objectClientTwo, objectServer, 'deepEqual objectClientTwo');
4444
t.equal(encode(objectClientTwo), encode(objectServer), 'equal objectClientTwo');
4545
if (checkactions!==false)
46-
t.equal(encode(removeObjects(actionServer)), encode(actionClientTwo), 'equal encode actions');
46+
t.equal(encode(actionServer), encode(actionClientTwo), 'equal encode actions');
4747

4848
// Unaction
4949
dopClient.setAction(attachObjects(unaction, objectClient));
@@ -55,13 +55,6 @@ function maketest(t, collectorServer, checkactions) {
5555
}
5656

5757

58-
59-
// helpers
60-
function removeObjects(actions) {
61-
for (var object_id in actions)
62-
delete actions[object_id].object;
63-
return actions;
64-
}
6558
function attachObjects(actions, obj) {
6659
for (var object_id in actions)
6760
actions[object_id].object = obj;

0 commit comments

Comments
 (0)