Last active
October 3, 2023 13:02
-
-
Save tomfun/ee091275d4543de45b3f4396febd55bf to your computer and use it in GitHub Desktop.
Print the shape of input with used values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const printValues = (data) => { | |
const getType = (value) => { | |
if (Array.isArray(value)) return 'array'; | |
if (null === value) return 'null'; | |
return typeof value; | |
}; | |
const initResult = () => ({ _type: new Set([]), _values: new Set() }) | |
const updateValues = (result = initResult(), item) => { | |
const type = getType(item); | |
result._type.add(type); | |
if (type === 'object') { | |
Object.keys(item).forEach((key) => (result[key] = updateValues(result[key], item[key]))); | |
} else if (type === 'array') { | |
return item.reduce(updateValues, result) | |
} else { | |
result._values.add(item); | |
} | |
return result; | |
}; | |
return updateValues(initResult(), data); | |
}; | |
const simplifyOutput = (output) => { | |
const simplified = {}; | |
for (const key in output) { | |
if (output[key]._type) { | |
simplified[key] = [...output[key]._type]; | |
} | |
} | |
return simplified; | |
}; | |
const convertToArrays = (output, maxValuesLength = -1, converted = {}) => { | |
if (output._type) { | |
converted._type = [...output._type]; | |
converted._values = [...output._values].filter((_, i) => i % Math.ceil(output._values.size / maxValuesLength) === 0); | |
} | |
for (const key in output) { | |
if (['_type', '_values'].includes(key)) { | |
continue; | |
} | |
converted[key] = convertToArrays(output[key], maxValuesLength, converted[key]) | |
} | |
return converted; | |
}; | |
printValues([ | |
{"a": "string"}, | |
{"array": [1, 2, 3]}, | |
{"obj": [{"1": 1}, {"1": 2}]}, | |
]) | |
/* | |
{ | |
a: { | |
_type: new Set(['string']), | |
_values: new Set(['string']) | |
}, | |
array: { | |
_type: new Set(['array', 'number']), | |
_values: new Set([1, 2, 3]) | |
}, | |
obj: { | |
1: { | |
_type: new Set(['number']), | |
_values: new Set([1, 2]) | |
}, | |
_type: new Set(['array', 'object']), | |
_values: new Set() // empty set | |
} | |
} | |
*/ | |
simplifyOutput(printValues([ | |
{"a": "string"}, | |
{"array": [1, 2, 3]}, | |
{"obj": [{"1": 1}, {"1": 2}]}, | |
])) | |
/* | |
{ | |
"a": [ | |
"string" | |
], | |
"array": [ | |
"array", | |
"number" | |
], | |
"obj": [ | |
"array", | |
"object" | |
] | |
} | |
*/ | |
convertToArrays(printValues([ | |
{"a": "string"}, | |
{"array": [1, 2, 3]}, | |
{"obj": [{"1": 1}, {"1": 2}]}, | |
])) | |
/* | |
{ | |
"_type": [ | |
"array", | |
"object" | |
], | |
"_values": [], | |
"a": { | |
"_type": [ | |
"string" | |
], | |
"_values": [ | |
"string" | |
] | |
}, | |
"array": { | |
"_type": [ | |
"array", | |
"number" | |
], | |
"_values": [ | |
1 | |
] | |
}, | |
"obj": { | |
"1": { | |
"_type": [ | |
"number" | |
], | |
"_values": [ | |
1 | |
] | |
}, | |
"_type": [ | |
"array", | |
"object" | |
], | |
"_values": [] | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment