Debian Codegraph @

util/build/stringify.js

raw symbols SCIP
1 define([
2 "dojo/json",
3 "./buildControlBase"
4 ], function(json, bc){
5 var
6 spaces = " ",
7 indentFactor = 2,
8
9 setIndentFactor = function(factor){
10 indentFactor = factor;
11 },
12
13 indent = function(n, factor){
14 n = n * (factor || indentFactor);
15 while(spaces.length<n) spaces+= spaces;
16 return spaces.substring(0, n);
17 },
18
19 propName = function(name){
20 return /^[\w\$]+$/.test(name) ?
21 name + ":" :
22 "'" + name + "':";
23 },
24
25 text,
26 unsolved,
27
28 split = function(
29 text //(string) Text to split into lines.
30 ){
31 ///
32 // Split text into a vector lines as given by new-line indicators contained in text
33 ///
34 // Three-step algorithm:
35 // * turn CR-LF or LF-CR into simple LF
36 // * turn lone CR into LF
37 // * then split on LF
38 //
39 // This should be platform-independent
40 return text.replace(/(\r\n)|(\n\r)/g, "\n").replace(/\r/, "\n").split("\n");
41 },
42
43 stringify = function(it, level){
44 if(!level){
45 text = "";
46 unsolved = false;
47 level = 1;
48 }else{
49 level++;
50 }
51 var temp, space, p, i, newline = bc.newline;
52 switch(typeof it){
53 case "undefined":
54 text+= "undefined";
55 break;
56
57 case "boolean":
58 text+= (it ? "true" : "false");
59 break;
60
61 case "number":
62 text+= it.toString();
63 break;
64
65 case "string":
66 text+= json.stringify(it);
67 break;
68
69 case "object":
70 if(it===null){
71 text+= "null";
72 }else if(it instanceof RegExp){
73 text+= it.toString();
74 }else if(it instanceof Array){
75 if(it.length>1){
76 text+= "[" + newline;
77 for(i = 0; i<it.length-1; i++){
78 text+= indent(level);
79 stringify(it[i], level);
80 text+= "," + newline;
81 }
82 text+= indent(level);
83 stringify(it[i], level);
84 text+= newline + indent(level-1) + "]";
85 }else if(it.length){
86 text+= "[";
87 stringify(it[0], level);
88 text+= "]";
89 }else{
90 text+= "[]";
91 }
92 }else{
93 temp = [];
94 for(p in it) temp.push(p);
95 temp.sort();
96 if(temp.length>1){
97 text+= "{" + newline;
98 for(i = 0; i<temp.length-1; i++){
99 text+= indent(level) + propName(temp[i]);
100 stringify(it[temp[i]], level);
101 text+= "," + newline;
102 }
103 text+= indent(level) + propName(temp[i]);
104 stringify(it[temp[i]], level);
105 text+= newline;
106 text+= indent(level-1) + "}";
107 }else if(temp.length){
108 text+= "{" + propName(temp[0]);
109 stringify(it[temp[0]], level);
110 text+= "}";
111 }else{
112 text+= "{}";
113 }
114 }
115 break;
116
117 case "function":
118 space = indent(level);
119 // the V8 engine seems to strip the leading space from the first line of the function?!?
120 var
121 functionText = split(it.toString()),
122 firstLine = functionText.shift(),
123 minSpaces = Number.MAX_VALUE;
124 functionText.forEach(function(line){
125 // ignoring lines that have no non-space chars
126 var match = line.match(/(\s*)\S/);
127 if(match) minSpaces = Math.min(minSpaces, match[1].length);
128 });
129 if(minSpaces==Number.MAX_VALUE){
130 //every line started without any spaces and we never got a match
131 minSpaces = 0;
132 }
133 functionText.unshift(indent(minSpaces, 1) + firstLine);
134 //every line has at least minSpaces indentation...
135 text+= newline + functionText.map(function(line){ return space + line.substring(minSpaces); }).join(newline);
136 break;
137
138 default:
139 text+= "undefined /* unsolved */";
140 unsolved = true;
141 }
142 text.unsolved = unsolved;
143 return text;
144 };
145
146 stringify.setIndentFactor = setIndentFactor;
147 stringify.split = split;
148 return stringify;
149
150 });