-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export class SwingletreeUtil { | ||
public static flattenObject(obj: any) { | ||
const segment: any = {}; | ||
|
||
for (const i in obj) { | ||
if (!obj.hasOwnProperty(i)) continue; | ||
|
||
if (obj[i] instanceof Object && obj[i] !== null) { | ||
const flatObject = this.flattenObject(obj[i]); | ||
for (const x in flatObject) { | ||
if (!flatObject.hasOwnProperty(x)) continue; | ||
segment[i + "." + x] = flatObject[x]; | ||
} | ||
} else { | ||
segment[i] = obj[i]; | ||
} | ||
} | ||
return segment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use strict"; | ||
|
||
import { suite, test, describe } from "mocha"; | ||
import { expect, assert } from "chai"; | ||
import * as chai from "chai"; | ||
import * as sinon from "sinon"; | ||
import { SwingletreeUtil } from "../src/util"; | ||
|
||
chai.use(require("sinon-chai")); | ||
|
||
const sandbox = sinon.createSandbox(); | ||
|
||
describe("ConfigurationService", () => { | ||
|
||
describe("Utilities", () => { | ||
it("should flatten objects", () => { | ||
const testObj = { | ||
a: { | ||
b: "c", | ||
d: { | ||
e: "f", | ||
g: "h", | ||
array: [ 1, 2, 3 ] | ||
} | ||
} | ||
}; | ||
|
||
const result = SwingletreeUtil.flattenObject(testObj); | ||
|
||
expect(result["a.d.e"]).to.be.equal("f"); | ||
expect(result["a.d.g"]).to.be.equal("h"); | ||
expect(result["a.b"]).to.be.equal("c"); | ||
expect(result["a.d.array.0"]).to.be.equal(1); | ||
expect(result["a.d.array.1"]).to.be.equal(2); | ||
expect(result["a.d.array.2"]).to.be.equal(3); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters