mirror of
https://github.com/grafana/grafana.git
synced 2025-02-16 18:34:52 -06:00
* some fixes * some more fixes * some canvas fixes * use removeProperty instead * few more * fixy fix * undo changes to ticks for now
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
// Copyright (c) 2014, Hugh Kennedy
|
|
// Based on code from https://github.com/hughsk/flat/blob/master/index.js
|
|
//
|
|
export default function flatten(
|
|
target: object,
|
|
opts?: { delimiter?: string; maxDepth?: number; safe?: boolean }
|
|
): Record<string, unknown> {
|
|
opts = opts || {};
|
|
|
|
const delimiter = opts.delimiter || '.';
|
|
let maxDepth = opts.maxDepth || 3;
|
|
let currentDepth = 1;
|
|
const output: Record<string, unknown> = {};
|
|
|
|
function step(object: object, prev: string | null) {
|
|
Object.entries(object).forEach(([key, value]) => {
|
|
const isarray = opts?.safe && Array.isArray(value);
|
|
const type = Object.prototype.toString.call(value);
|
|
const isobject = type === '[object Object]';
|
|
|
|
const newKey = prev ? prev + delimiter + key : key;
|
|
|
|
if (!opts?.maxDepth) {
|
|
maxDepth = currentDepth + 1;
|
|
}
|
|
|
|
if (!isarray && isobject && Object.keys(value).length && currentDepth < maxDepth) {
|
|
++currentDepth;
|
|
return step(value, newKey);
|
|
}
|
|
|
|
output[newKey] = value;
|
|
});
|
|
}
|
|
|
|
step(target, null);
|
|
|
|
return output;
|
|
}
|