// https://jsfiddle.net/ElMoonLite/fb6cagL5/
// boilerplate lib stuff:
function value_to_string(value) {
// with typehints:
// purpose of output is mostly for debugging:
// quickly see what type a value is, and easy copy paste of values.
if (value === null || value === undefined || value === true || value === false || value === Infinity || value === -Infinity ) {
// NB: These will be returned as same string:
// undefined, null, false, true, Infinity, -Infinity
return '' + value;
}
if (typeof value === 'number' && isNaN(value) ) {
// special case, detecting if value is NaN (and ONLY NaN) is tricky:
return 'NaN';
}
var t = typeof value;
if (t === 'string') {
// return 'string:' + value;
// wrap in quotes
// pick a quote type that has least occurences in the string (so less escaping needed):
var cnt_1 = value.split("'").length - 1;
var cnt_2 = value.split('"').length - 1;
// note <= : if equal, prefer first (singlequotes).
if (cnt_1 <= cnt_2) {
return "'" + value.replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'";
} else {
return '"' + value.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
}
} else if (t === 'number') {
// return 'number ' + value;
return '' + value;
} else if (Array.isArray(value)) {
// TODO: recursive for array?
// TODO: recursion loop protection? maxdepth? break on 2nd visit same value?
// e.g. arr = []; arr[0] = arr; value_to_string(a1); Uncaught RangeError: Maximum call stack size exceeded
// return '[' + value + ']';
// return 'Array(' + value.length + ') [' + value + '];
return 'Array(' + value.length + ') [' + value.map(value_to_string).join(', ') + ']';
} else if (t === 'object') {
// TODO: recursive for object?
// TODO: recursion loop protection? maxdepth? break on 2nd visit same value?
// e.g. o = {}; o[0] = o; value_to_string(o); Uncaught RangeError: Maximum call stack size exceeded
var default_tostring = (''+value);
var is_default_tostring =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.