-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjkl-dumper.js
More file actions
57 lines (52 loc) · 1.61 KB
/
jkl-dumper.js
File metadata and controls
57 lines (52 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Dumper = function () {
return this;
};
Dumper.prototype.dump = function ( data, offset ) {
if ( typeof(offset) == "undefined" ) offset = "";
var nextoff = offset + " ";
switch ( typeof(data) ) {
case "string":
return '"'+this.escapeString(data)+'"';
break;
case "number":
return data;
break;
case "boolean":
return data ? "true" : "false";
break;
case "undefined":
return "null";
break;
case "object":
if ( data == null ) {
return "null";
} else if ( data.constructor == Array ) {
var array = [];
for ( var i=0; i<data.length; i++ ) {
array[i] = this.dump( data[i], nextoff );
}
return "[\n"+nextoff+array.join( ",\n"+nextoff )+"\n"+offset+"]";
} else {
var array = [];
for ( var key in data ) {
var val = this.dump( data[key], nextoff );
key = '"'+this.escapeString( key )+'"';
array[array.length] = key+": "+val;
}
if ( array.length == 1 && ! array[0].match( /[\n\{\[]/ ) ) {
return "{ "+array[0]+" }";
}
return "{\n"+nextoff+array.join( ",\n"+nextoff )+"\n"+offset+"}";
}
break;
default:
return data;
// unsupported data type
break;
}
};
Dumper.prototype.escapeString = function ( str ) {
return str.replace( /\\/g, "\\\\" ).replace( /\"/g, "\\\"" );
};
// ===============================================================
module.exports = new Dumper();