Skip to content

Commit c599d91

Browse files
committed
Fixing bug with encode modularity. And implemented multiEncode
1 parent eaf1eeb commit c599d91

File tree

8 files changed

+157
-73
lines changed

8 files changed

+157
-73
lines changed

dist/browser.js

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* dop@0.4.0
2+
* dop@0.5.0
33
* www.distributedobjectprotocol.org
44
* (c) 2016 Josema Gonzalez
55
* MIT License.
@@ -659,9 +659,17 @@ dop.emit = function(mutations, action) {
659659

660660
////////// src/api/encode.js
661661

662-
dop.encode = function(data) {
663-
return JSON.stringify(data, dop.core.encode);
664-
};
662+
dop.encode = (function(data, encoder) {
663+
var encoderCache
664+
return function(data, encoder) {
665+
if (typeof encoder != 'function') {
666+
if (encoderCache === undefined)
667+
encoderCache = dop.core.multiEncode(dop.core.encodeSpecial, dop.core.encodeProtocol, dop.core.encodeUtil);
668+
encoder = encoderCache;
669+
}
670+
return JSON.stringify(data, encoder);
671+
}
672+
})();
665673

666674

667675

@@ -2342,9 +2350,9 @@ dop.core.emitNodes = function(action) {
23422350

23432351

23442352

2345-
////////// src/core/protocol/encode.js
2353+
////////// src/core/protocol/encodeProtocol.js
23462354

2347-
dop.core.encode = function(property, value) {
2355+
dop.core.encodeProtocol = function(property, value) {
23482356

23492357
var tof = typeof value;
23502358

@@ -2354,6 +2362,27 @@ dop.core.encode = function(property, value) {
23542362
if (tof == 'undefined') // http://stackoverflow.com/questions/17648150/how-does-json-parse-manage-undefined
23552363
return '~U';
23562364

2365+
return value;
2366+
};
2367+
2368+
2369+
2370+
2371+
////////// src/core/protocol/encodeSpecial.js
2372+
2373+
dop.core.encodeSpecial = function(property, value) {
2374+
return (typeof value == 'string' && value[0] == '~') ? '~'+value : value;
2375+
};
2376+
2377+
2378+
2379+
2380+
////////// src/core/protocol/encodeUtil.js
2381+
2382+
dop.core.encodeUtil = function(property, value) {
2383+
2384+
var tof = typeof value;
2385+
23572386
if (value === Infinity)
23582387
return '~I';
23592388

@@ -2366,24 +2395,17 @@ dop.core.encode = function(property, value) {
23662395
if (tof == 'object' && value instanceof RegExp)
23672396
return '~R' + value.toString();
23682397

2369-
if (tof == 'string' && value[0] === '~') // https://jsperf.com/charat-vs-index/5
2370-
return '~'+value;
2371-
23722398
return value;
2373-
23742399
};
23752400

23762401

2377-
23782402
// // Extending example
2379-
// (function() {
2380-
// var encode = dop.core.encode;
2381-
// dop.core.encode = function(property, value) {
2382-
// if (typeof value == 'boolean')
2383-
// return '~BOOL';
2384-
// return encode(property, value);
2385-
// };
2386-
// })();
2403+
// var encode = dop.core.encodeUtil;
2404+
// dop.core.encodeUtil = function(property, value) {
2405+
// if (typeof value == 'boolean')
2406+
// return '~BOOL';
2407+
// return encode(property, value);
2408+
// };
23872409

23882410

23892411

@@ -2404,6 +2426,26 @@ dop.core.getRejectError = function(error) {
24042426

24052427

24062428

2429+
////////// src/core/protocol/multiEncode.js
2430+
2431+
dop.core.multiEncode = function() {
2432+
var encoders = arguments,
2433+
length = encoders.length, v;
2434+
return function recursion(property, value, index) {
2435+
if (index>=length)
2436+
return value;
2437+
else if (index === undefined) {
2438+
v = value;
2439+
index = 0;
2440+
}
2441+
v = encoders[index](property, value);
2442+
return (v!==value) ? v : recursion(property, value, index+1);
2443+
}
2444+
};
2445+
2446+
2447+
2448+
24072449
////////// src/core/protocol/registerNode.js
24082450

24092451
dop.core.registerNode = function(node) {

dist/browser.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/nodejs.js

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* dop@0.4.0
2+
* dop@0.5.0
33
* www.distributedobjectprotocol.org
44
* (c) 2016 Josema Gonzalez
55
* MIT License.
@@ -465,9 +465,17 @@ dop.emit = function(mutations, action) {
465465

466466
////////// src/api/encode.js
467467

468-
dop.encode = function(data) {
469-
return JSON.stringify(data, dop.core.encode);
470-
};
468+
dop.encode = (function(data, encoder) {
469+
var encoderCache
470+
return function(data, encoder) {
471+
if (typeof encoder != 'function') {
472+
if (encoderCache === undefined)
473+
encoderCache = dop.core.multiEncode(dop.core.encodeSpecial, dop.core.encodeProtocol, dop.core.encodeUtil);
474+
encoder = encoderCache;
475+
}
476+
return JSON.stringify(data, encoder);
477+
}
478+
})();
471479

472480

473481

@@ -2148,9 +2156,9 @@ dop.core.emitNodes = function(action) {
21482156

21492157

21502158

2151-
////////// src/core/protocol/encode.js
2159+
////////// src/core/protocol/encodeProtocol.js
21522160

2153-
dop.core.encode = function(property, value) {
2161+
dop.core.encodeProtocol = function(property, value) {
21542162

21552163
var tof = typeof value;
21562164

@@ -2160,6 +2168,27 @@ dop.core.encode = function(property, value) {
21602168
if (tof == 'undefined') // http://stackoverflow.com/questions/17648150/how-does-json-parse-manage-undefined
21612169
return '~U';
21622170

2171+
return value;
2172+
};
2173+
2174+
2175+
2176+
2177+
////////// src/core/protocol/encodeSpecial.js
2178+
2179+
dop.core.encodeSpecial = function(property, value) {
2180+
return (typeof value == 'string' && value[0] == '~') ? '~'+value : value;
2181+
};
2182+
2183+
2184+
2185+
2186+
////////// src/core/protocol/encodeUtil.js
2187+
2188+
dop.core.encodeUtil = function(property, value) {
2189+
2190+
var tof = typeof value;
2191+
21632192
if (value === Infinity)
21642193
return '~I';
21652194

@@ -2172,24 +2201,17 @@ dop.core.encode = function(property, value) {
21722201
if (tof == 'object' && value instanceof RegExp)
21732202
return '~R' + value.toString();
21742203

2175-
if (tof == 'string' && value[0] === '~') // https://jsperf.com/charat-vs-index/5
2176-
return '~'+value;
2177-
21782204
return value;
2179-
21802205
};
21812206

21822207

2183-
21842208
// // Extending example
2185-
// (function() {
2186-
// var encode = dop.core.encode;
2187-
// dop.core.encode = function(property, value) {
2188-
// if (typeof value == 'boolean')
2189-
// return '~BOOL';
2190-
// return encode(property, value);
2191-
// };
2192-
// })();
2209+
// var encode = dop.core.encodeUtil;
2210+
// dop.core.encodeUtil = function(property, value) {
2211+
// if (typeof value == 'boolean')
2212+
// return '~BOOL';
2213+
// return encode(property, value);
2214+
// };
21932215

21942216

21952217

@@ -2210,6 +2232,26 @@ dop.core.getRejectError = function(error) {
22102232

22112233

22122234

2235+
////////// src/core/protocol/multiEncode.js
2236+
2237+
dop.core.multiEncode = function() {
2238+
var encoders = arguments,
2239+
length = encoders.length, v;
2240+
return function recursion(property, value, index) {
2241+
if (index>=length)
2242+
return value;
2243+
else if (index === undefined) {
2244+
v = value;
2245+
index = 0;
2246+
}
2247+
v = encoders[index](property, value);
2248+
return (v!==value) ? v : recursion(property, value, index+1);
2249+
}
2250+
};
2251+
2252+
2253+
2254+
22132255
////////// src/core/protocol/registerNode.js
22142256

22152257
dop.core.registerNode = function(node) {

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": "0.5.0",
3+
"version": "0.6.0",
44
"main": "./dist/nodejs.js",
55
"browser": "./dist/browser.js",
66
"license": "MIT",

src/api/encode.js

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

2-
dop.encode = function(data, encoder) {
3-
if (typeof encoder != 'function')
4-
encoder = dop.core.encode;
5-
return JSON.stringify(data, encoder);
6-
};
2+
dop.encode = (function(data, encoder) {
3+
var encoderCache
4+
return function(data, encoder) {
5+
if (typeof encoder != 'function') {
6+
if (encoderCache === undefined)
7+
encoderCache = dop.core.multiEncode(dop.core.encodeSpecial, dop.core.encodeProtocol, dop.core.encodeUtil);
8+
encoder = encoderCache;
9+
}
10+
return JSON.stringify(data, encoder);
11+
}
12+
})();

src/core/protocol/encode.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/core/protocol/encodeUtil.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ dop.core.encodeUtil = function(property, value) {
2020

2121

2222
// // 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-
// })();
23+
// var encode = dop.core.encodeUtil;
24+
// dop.core.encodeUtil = function(property, value) {
25+
// if (typeof value == 'boolean')
26+
// return '~BOOL';
27+
// return encode(property, value);
28+
// };
3129

src/core/protocol/multiEncode.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
dop.core.multiEncode = function() {
3+
var encoders = arguments,
4+
length = encoders.length, v;
5+
return function recursion(property, value, index) {
6+
if (index>=length)
7+
return value;
8+
else if (index === undefined) {
9+
v = value;
10+
index = 0;
11+
}
12+
v = encoders[index](property, value);
13+
return (v!==value) ? v : recursion(property, value, index+1);
14+
}
15+
};

0 commit comments

Comments
 (0)