MCPcopy Index your code
hub / github.com/ddopson/underscore-cli / formatValue

Function formatValue

lib/formatter.js:138–194  ·  view source on GitHub ↗
(ctx, value)

Source from the content-addressed store, hash-verified

136}
137
138function formatValue(ctx, value) {
139 var typeof_value = getTypeOf(value);
140 var atMaxDepth = (ctx.maxDepth !== null && ctx.seen.length >= ctx.maxDepth);
141
142 var formatted = '';
143 switch (typeof_value) {
144 // Handle primitive types
145 case 'null': formatted = 'null'; break;
146 case 'undefined': formatted = 'undefined'; break;
147 case 'string': formatted = ctx.quoteString(value); break;
148 case 'number': formatted = '' + value; break;
149 case 'boolean': formatted = '' + value; break;
150
151 }
152
153 // Special Objects
154 if (ctx.jsonParity) {
155 switch(typeof_value) {
156 case 'date': formatted = JSON.stringify(value); break;
157 case 'error': typeof_value = 'object'; break;
158 case 'regexp': typeof_value = 'object'; break;
159 case 'function': formatted = null; break;// don't print
160 }
161 } else {
162 switch(typeof_value) {
163 case 'date': formatted = ISO8601(value); break;
164 case 'error': formatted = '[' + Error.prototype.toString.call(value) + ']'; break;
165 case 'regexp': formatted = RegExp.prototype.toString.call(value); break;
166 case 'function': formatted = '[Function' + (value.name ? ': ' + value.name : '') + ']'; break;
167 }
168 }
169 if (formatted !== '') {
170 formatted = ctx.stylize(formatted, typeof_value);
171 }
172
173 // Array
174 if (typeof_value === 'array') {
175 if (atMaxDepth) {
176 return ctx.stylize('[...]', 'special');
177 } else {
178 formatted = formatArray(ctx, value);
179 }
180 }
181
182 // Object and "objects" that have properties
183 if (typeof_value === 'object' || (ctx.complexObjects && typeof value === 'object' && value !== null)) {
184 if (atMaxDepth) {
185 if (formatObject(ctx, value, typeof_value) !== '') {
186 formatted += ctx.stylize('{...}', 'special');
187 }
188 } else {
189 formatted += formatObject(ctx, value, typeof_value);
190 }
191 }
192
193 return formatted;
194}
195

Callers 2

formatPropertyFunction · 0.85
formatterFunction · 0.85

Calls 4

getTypeOfFunction · 0.85
ISO8601Function · 0.85
formatArrayFunction · 0.85
formatObjectFunction · 0.85

Tested by

no test coverage detected