mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
fix(missing files): added missing files, oops
This commit is contained in:
parent
cf1e167430
commit
24b9bc1e55
39
public/app/core/utils/flatten.ts
Normal file
39
public/app/core/utils/flatten.ts
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2014, Hugh Kennedy
|
||||
// Based on code from https://github.com/hughsk/flat/blob/master/index.js
|
||||
//
|
||||
function flatten(target, opts): any {
|
||||
opts = opts || {};
|
||||
|
||||
var delimiter = opts.delimiter || '.';
|
||||
var maxDepth = opts.maxDepth || 3;
|
||||
var currentDepth = 1;
|
||||
var output = {};
|
||||
|
||||
function step(object, prev) {
|
||||
Object.keys(object).forEach(function(key) {
|
||||
var value = object[key];
|
||||
var isarray = opts.safe && Array.isArray(value);
|
||||
var type = Object.prototype.toString.call(value);
|
||||
var isobject = type === "[object Object]";
|
||||
|
||||
var 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;
|
||||
}
|
||||
|
||||
export = flatten;
|
24
public/test/core/utils/flatten_specs.ts
Normal file
24
public/test/core/utils/flatten_specs.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'
|
||||
|
||||
import flatten = require('app/core/utils/flatten')
|
||||
|
||||
describe("flatten", () => {
|
||||
|
||||
it('should return flatten object', () => {
|
||||
var flattened = flatten({
|
||||
level1: 'level1-value',
|
||||
deeper: {
|
||||
level2: 'level2-value',
|
||||
deeper: {
|
||||
level3: 'level3-value'
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
|
||||
expect(flattened['level1']).to.be('level1-value');
|
||||
expect(flattened['deeper.level2']).to.be('level2-value');
|
||||
expect(flattened['deeper.deeper.level3']).to.be('level3-value');
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user