JSFiddle - React, Tailwind, and code Playground
by Trisox
HTML
<button
onClick="bouwAanvraagOp(JSON.parse(document.querySelectorAll('#textarea')[0].value))">
convert
</button> <br>
<pre></pre>
<textarea rows="60" cols="100" id="textarea" />
JavaScript
function hasOwnProperty(obj, prop) {
if (obj == null) {
return false;
}
// to handle objects with null prototypes (too edge case?)
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function toString(type) {
return Object.prototype.toString.call(type);
}
function getKey(key) {
const intKey = parseInt(key);
if (intKey.toString() === key) {
return intKey;
}
return key;
}
const options = { includeInheritedProps: true };
const objectPath = function (obj) {
return Object.keys(objectPath).reduce((proxy, prop) => {
if (prop === 'create') {
return proxy;
}
/* istanbul ignore else*/
if (typeof objectPath[prop] === 'function') {
proxy[prop] = objectPath[prop].bind(objectPath, obj);
}
return proxy;
}, {});
};
function hasShallowProperty(obj, prop) {
return (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop));
}
function getShallowProperty(obj, prop) {
if (hasShallowProperty(obj, prop)) {
return obj[prop];
}
}
function setPath(obj, path, value, doNotReplace) {
if (typeof path === 'number') {
path = [path];
}
if (!path || path.length === 0) {
return obj;
}
if (typeof path === 'string') {
return setPath(obj, path.split('.').map(getKey), value, doNotReplace);
}
const currentPath = path[0];
const currentValue = getShallowProperty(obj, currentPath);
if (path.length === 1) {
if (currentValue === void 0 || !doNotReplace) {
obj[currentPath] = value;
}
return currentValue;
}
if (currentValue === void 0) {
// check if we assume an array
if (typeof path[1] === 'number') {
obj[currentPath] = [];
} else {
obj[currentPath] = {};
}
}
return setPath(obj[currentPath], path.slice(1), value, doNotReplace);
}
function getPath(obj, path, defaultValue) {
if (typeof path === 'number') {
path = [path];
}
if (!path || path.length === 0) {
return...