fix(missing files): added missing files, oops

This commit is contained in:
Torkel Ödegaard 2015-11-23 18:20:12 +01:00
parent cf1e167430
commit 24b9bc1e55
2 changed files with 63 additions and 0 deletions

View 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;

View 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');
});
});