sandbox-stringifier-object2
by shan10213223
JavaScript
let obj = {num: 0, string: "This is a string", func: function(){}, emptyString: "", null: null, undefined: undefined};
// plain collection - array
function stringifier(input) {
function stringifierBasics (input) {
if (input === null) {
return 'null';
}
if (input === '') {
return '""';
}
if (input === undefined) {
return 'null';
}
let text;
switch (typeof(input)) {
case 'number':
text = input;
break;
case 'string':
//text = input;
text = '"' + input + '"';
break;
case 'function':
text = 'null';
break;
}
return text;
}
if (Array.isArray(input)) {
let outArray = [];
for (let i=0; i<input.length; i++) {
if (Object.prototype.hasOwnProperty.call(input, i)) {
let txt = stringifierBasics (input[i]);
outArray.push(txt);
}
}
return '[' + outArray + ']';
} else {
// plain collections - object
console.log(input);
//let keys = Object.keys(input);
//console.log(keys);
let outObj = {};
let outStr = '';
for (let key in input) {
if (Object.prototype.hasOwnProperty.call(input, key) && (Object.prototype.propertyIsEnumerable.call(input, key))) {
//console.log(input[key]);
if (key === 'undefined') {
} else if (typeof(input[key]) === 'function') {
} else {
//console.log(key, input[key]);
//console.log(typeof(input[key]));
let temp_key = '"' + key + '"';
let temp_val = stringifierBasics (input[key]);
//let temp_val;
/*if (input[key] !== undefined) {
temp_val = undefined;
} else {
temp_val = stringifierBasics (input[key]);
}*/
//console.log(temp_key, temp_val);
//outObj += temp_key +
outObj[temp_key] = temp_val;
outStr += ',' + temp_key + ':' + temp_val;
}
}
}
outStr =...