|
1 | | -dop={core:{}} |
2 | | -var data = { |
3 | | - function: function(){}, |
4 | | - NaN: NaN, |
5 | | - Infinity: -Infinity, |
6 | | - undefined: undefined, |
7 | 1 |
|
| 2 | +dop.core.encode = function(property, value) { |
| 3 | + return dop.core.encodeProtocol( |
| 4 | + property, dop.core.encodeUtil( |
| 5 | + property, dop.core.encodeSpecial(property, value))); |
8 | 6 | }; |
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 | | -// })(); |
72 | 7 |
|
| 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 | +// }; |
0 commit comments