Created
October 18, 2022 11:07
-
-
Save ArtemAvramenko/04dbb5056149be0ec756c8dd6d4223b1 to your computer and use it in GitHub Desktop.
Truncates long strings and arrays in object. It is useful for logging
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 truncateObject = o => { | |
if (!o) { | |
return o; | |
} | |
if (typeof o === 'string') { | |
return o.substring(0, 64); | |
} | |
if (Array.isArray(o)) { | |
return o.slice(0, 5).map(x => truncateObject(x)); | |
} | |
if (typeof o === 'object') { | |
const r = {}; | |
for (let i in o) { | |
r[i] = truncateObject(o[i]); | |
} | |
return r; | |
} | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment