Total Complexity | 25 |
Complexity/F | 8.33 |
Lines of Code | 54 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | //TODO: handle reviver/dehydrate function like normal |
||
5 | exports.stringify = function stringify (o) { |
||
6 | if('undefined' == typeof o) return o |
||
|
|||
7 | |||
8 | if(o && Buffer.isBuffer(o)) |
||
9 | return JSON.stringify(':base64:' + o.toString('base64')) |
||
10 | |||
11 | if(o && o.toJSON) |
||
12 | o = o.toJSON() |
||
13 | |||
14 | if(o && 'object' === typeof o) { |
||
15 | var s = '' |
||
16 | var array = Array.isArray(o) |
||
17 | s = array ? '[' : '{' |
||
18 | var first = true |
||
19 | |||
20 | for(var k in o) { |
||
21 | var ignore = 'function' == typeof o[k] || (!array && 'undefined' === typeof o[k]) |
||
22 | if(Object.hasOwnProperty.call(o, k) && !ignore) { |
||
23 | if(!first) |
||
24 | s += ',' |
||
25 | first = false |
||
26 | if (array) { |
||
27 | if(o[k] == undefined) |
||
28 | s += 'null' |
||
29 | else |
||
30 | s += stringify(o[k]) |
||
31 | } else if (o[k] !== void(0)) { |
||
32 | s += stringify(k) + ':' + stringify(o[k]) |
||
33 | } |
||
34 | } |
||
35 | } |
||
36 | |||
37 | s += array ? ']' : '}' |
||
38 | |||
39 | return s |
||
40 | } else if ('string' === typeof o) { |
||
41 | return JSON.stringify(/^:/.test(o) ? ':' + o : o) |
||
42 | } else if ('undefined' === typeof o) { |
||
43 | return 'null'; |
||
44 | } else |
||
45 | return JSON.stringify(o) |
||
46 | } |
||
47 | |||
48 | exports.parse = function (s) { |
||
49 | return JSON.parse(s, function (key, value) { |
||
50 | if('string' === typeof value) { |
||
51 | if(/^:base64:/.test(value)) |
||
52 | return new Buffer(value.substring(8), 'base64') |
||
53 | else |
||
54 | return /^:/.test(value) ? value.substring(1) : value |
||
55 | } |
||
56 | return value |
||
57 | }) |
||
58 | } |
||
59 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.