JSFiddle - React, Tailwind, and code Playground

by brigand

HTML

<pre id="out"></pre>

JavaScript

const sdk = {
  foo: {
    bar: function baz() {
      return 1 + 2
    },
    quux: 7,
    array: [1, 2, {
      x: 1,
      y: 2
    }]
  }
}


function indent(str) {
  const spaces = ' ' + ' '
  return str.split('\n').map(line => spaces + line).join('\n');
}

function inspect(obj) {
  let s = ``;
  if (typeof obj === 'function') {
    return obj.toString();
  }
  if (!obj || typeof obj !== 'object') {
    return `${typeof obj} ${obj}`
  }

  if (Array.isArray(obj)) {
    return obj.map(item => {
      const s = inspect(item);
      if (/\n/.test(s)) {
        return `-\n` + indent(s)
      } else {
        return `- ${s}`
      }
    }).join('\n')
  }
  return Object.getOwnPropertyNames(obj).map(key => {
    const s = inspect(obj[key]);
    if (/\n/.test(s)) {
      return `${key}:\n` + indent(s)
    } else {
      return `${key}: ${s}`
    }
  }).join('\n')
}


out.textContent = inspect(sdk)