Skip to content

Instantly share code, notes, and snippets.

View WyrdNexus's full-sized avatar
🏡
🖥🖥🖥

Tony Chiboucas WyrdNexus

🏡
🖥🖥🖥
View GitHub Profile
@WyrdNexus
WyrdNexus / stringify.js
Created November 19, 2022 09:20 — forked from bennettmcelwee/stringify.js
Version of JSON.stringify limitied to a specific depth.
function stringifyMaxDepth (obj, depth = 1) {
// recursion limited by depth arg
if (!obj || typeof obj !== 'object') return JSON.stringify(obj)
let curDepthResult = '"<?>"' // too deep
if (depth > 0) {
curDepthResult = Object.keys(obj)
.map( (key) => {
let val = stringifyMaxDepth(obj[key], depth - 1)
if (val === undefined) val = 'null'