Browse Source

fix(missing files): added missing files, oops

Torkel Ödegaard 10 years ago
parent
commit
24b9bc1e55
2 changed files with 63 additions and 0 deletions
  1. 39 0
      public/app/core/utils/flatten.ts
  2. 24 0
      public/test/core/utils/flatten_specs.ts

+ 39 - 0
public/app/core/utils/flatten.ts

@@ -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 - 0
public/test/core/utils/flatten_specs.ts

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