grafana/public/app/core/utils/flatten.ts
Lukas Siatka 2c9eed360d
Chore: fixes few strict-null errors (#23775)
* Chore: fixes strict-null-error in Login > ChangePassword component

* Chore: fixes strict-null-error in Login > LoginForm component

* Chore: fixes strict-null-errors in OrgActionBar component and components that render it

* Chore: fixes strict-null-errors in PageHeader component

* Chore: fixes strict-null-errors in PermissionList components

* Chore: fixes strict-null-errors in loki language provider

* Chore: fixes strict-null-errors in loki datasource

* Chore: fixes strict-null-errors in panel_editor > EditorTabBody component

* Chore: fixes strict-null-errors in flatten utility

* Chore: fixes strict-null-errors in search component

* Chore: fixes strict-null-errors in SharedPreferences component

* Chore: fixes strict-null-errors in MetricSelect component

* Chore: updates type on a param to type on argument

* Chore: updates strict-null errors count from 791 to 757

* Chore: updates PageHeader - adds null checks

* Chore: updates PageHeader - updates null checks

* Chore: updates PageHeader null checks to longer format

* Chore: updates strict-null fixes

* Chore: updates error count limit in ci-frontend-metrics
2020-05-07 13:53:05 +02:00

38 lines
1.0 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?: any; maxDepth?: any; safe?: any }): any {
opts = opts || {};
const delimiter = opts.delimiter || '.';
let maxDepth = opts.maxDepth || 3;
let currentDepth = 1;
const output: any = {};
function step(object: any, prev: string | null) {
Object.keys(object).forEach(key => {
const value = object[key];
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;
}