JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<body>
<pre class="output">
</pre>
</body>
</html>
Babel + JSX
const toCamel = (s) => {
return s.replace(/([-_][a-z])/ig, ($1) => {
return $1.toUpperCase()
.replace('-', '')
.replace('_', '');
});
};
const isArray = function (a) {
return Array.isArray(a);
};
const isObject = function (o) {
return o === Object(o) && !isArray(o) && typeof o !== 'function';
};
const keysToCamel = function (o) {
if (isObject(o)) {
const n = {};
Object.keys(o)
.forEach((k) => {
n[toCamel(k)] = keysToCamel(o[k]);
});
return n;
} else if (isArray(o)) {
return o.map((i) => {
return keysToCamel(i);
});
}
return o;
};
const t1 = {
best_chili: {
chili_ingredients: [
'beef',
'dried chilis',
'fresh tomatoes',
'cumin',
'onions',
'onion-powder',
'peppers',
],
chili_steps: {
step_1: '',
step_2: '',
},
},
serves: 6,
pairs_with: [
{
'french-bread': {},
},
{
'rye-croutons': {},
},
],
};
const output = document.querySelector('.output');
const write = (t) => {
const p = document.createElement('p');
const txt = document.createTextNode(t);
p.appendChild(txt);
output.appendChild(p);
};
write('testing...\n');
write(`${JSON.stringify(t1)}\n\n${JSON.stringify(keysToCamel(t1))}`);