hypnagaga/src/components/Theme/utils/flatten.js
Jon McClure 3b28eb57b7 docs
2022-08-18 22:40:03 +01:00

39 lines
995 B
JavaScript

function isBuffer(obj) {
return (
obj &&
obj.constructor &&
typeof obj.constructor.isBuffer === 'function' &&
obj.constructor.isBuffer(obj)
);
}
const transformKey = (key) => key.replace(/[^a-z0-9-]/gi, '');
export default function flatten(target) {
const delimiter = '-';
const output = {};
function step(object, prev, currentDepth = 1) {
Object.keys(object).forEach(function (key) {
const value = object[key];
const isArray = Array.isArray(value);
const type = Object.prototype.toString.call(value);
const isbuffer = isBuffer(value);
const isObject = type === '[object Object]' || type === '[object Array]';
const newKey = prev
? prev + delimiter + transformKey(key)
: transformKey(key);
if (!isArray && !isbuffer && isObject && Object.keys(value).length) {
return step(value, newKey, currentDepth + 1);
}
output[newKey] = value;
});
}
step(target);
return output;
}