1 | //TODO: handle reviver/dehydrate function like normal |
||
2 | //and handle indentation, like normal. |
||
3 | //if anyone needs this... please send pull request. |
||
4 | |||
5 | exports.stringify = function stringify (o) { |
||
6 | if('undefined' == typeof o) return o |
||
0 ignored issues
–
show
|
|||
7 | |||
8 | if(o && Buffer.isBuffer(o)) |
||
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
9 | return JSON.stringify(':base64:' + o.toString('base64')) |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
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 (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
10 | |||
11 | if(o && o.toJSON) |
||
12 | o = o.toJSON() |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
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 (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
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) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
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 += ',' |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
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 (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
25 | first = false |
||
26 | if (array) { |
||
27 | if(o[k] == undefined) |
||
0 ignored issues
–
show
|
|||
28 | s += 'null' |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
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 (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
29 | else |
||
30 | s += stringify(o[k]) |
||
31 | } else if (o[k] !== void(0)) { |
||
0 ignored issues
–
show
|
|||
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) |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
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 (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
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') |
||
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
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 (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
53 | else |
||
54 | return /^:/.test(value) ? value.substring(1) : value |
||
0 ignored issues
–
show
|
|||
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.