Skip to content

Instantly share code, notes, and snippets.

@myfonj
Last active February 2, 2024 19:48
Show Gist options
  • Save myfonj/7e92c1e8636ea85f71ce55494878812d to your computer and use it in GitHub Desktop.
Save myfonj/7e92c1e8636ea85f71ce55494878812d to your computer and use it in GitHub Desktop.
Object Stringify to Wall of Text With Full Paths
/**
* Wall of text
* @param {Object} inputVar
* @returns {string}
* @description Takes nested object ("JSON") and returns its serialized "wall of text" representation:
* - Each scalar value is printed on its own line, after its full path.
* - Strings are quoted.
* - Complex values are converted to their string representation.
* - Dates are converted to ISO 8601 strings.
* Input:
* ```
* {a:{b:{c:"d",e:[1,{f:"g"},3,["."]]}}}
* ```
* Output:
* ```
* a.b.c = "d"
* a.b.e[0] = 1
* a.b.e[1].f = "g"
* a.b.e[2] = 3
* a.b.e[3][0] = "."
* ```
* @example:
* ```JavaScript
* var obj = {
* a: 1,
* b: "two",
* c: {
* d: new Date(),
* e: "four",
* f: NaN
* },
* last: [
* 1,
* "x",
* null,
* {
* "k": "v",
* "l": [4, 2]
* },
* ["a", "b", "c"],
* NaN,
* 1e100,
* /asd/gi,
* 0
* ]
* };
* objectToWallOfText(obj);
* // returns:
* a = 1
* b = two
* c.d = 2023-11-14T12:33:16.312Z
* c.e = four
* c.f = NaN
* last[0] = 1
* last[1] = x
* last[2] = null
* last[3].k = v
* last[3].l[0] = 4
* last[3].l[1] = 2
* last[4][0] = a
* last[4][1] = b
* last[4][2] = c
* last[5] = NaN
* last[6] = 1e+100
* last[7] = /asd/gi
* last[8] = 0
*
* ```
*/
function wat(inputVar, options = {}) {
options = Object.assign(
{
includeInitializers: false,
sort: false,
addSemicolons: false,
},
options
);
let currentPath = '';
let result = [];
walker(inputVar);
if (options.sort) {
result = result.sort();
}
if (options.addSemicolons) {
return result.join(";\n") + ';';
} else {
return result.join("\n");
}
/**
* @param {any} currentValue
*/
function walker(currentValue) {
if (typeof currentValue === "object"
&& currentValue != null
&& currentValue.constructor != Date
&& currentValue.constructor != RegExp
)
{
if (options.includeInitializers) {
let val;
if (Array.isArray(currentValue)) {
val = '[]';
} else if (currentValue.constructor == Object) {
val = '{}';
} else {
val = 'new ' + currentValue.constructor.name + '()';
}
if (currentPath == '') {
result.push('// = ' + val);
} else {
result.push(currentPath + " = " + val);
}
}
for (const key in currentValue) {
if (currentValue.hasOwnProperty(key)) {
const prevPath = currentPath;
let currentKey;
if (Array.isArray(currentValue)) {
currentKey = `[${key}]`;
} else if (currentPath == '') {
currentKey = `${key}`;
} else {
currentKey = `.${key}`;
}
const element = currentValue[key];
currentPath += currentKey;
walker(element);
currentPath = prevPath;
}
}
} else {
let val;
if (currentValue == null || typeof currentValue == 'string') {
val = JSON.stringify(currentValue);
} else if (typeof currentValue?.toISOString == 'function') {
// Date -> ISO 8601
val = `new Date("${currentValue.toISOString()}")`
} else {
val = currentValue?.toString?.()
}
// || JSON.stringify(currentValue);
if (currentPath == '') {
result.push(val);
} else {
result.push(currentPath + " = " + val);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment