Martin Karkowski
d911380511
- Fixes: - helpers.jsonSchemaMethods: -> renamed flatten to nested. - Added: - helpers.descriptors: -> parseFunctionToJsonSchema - helpers.jsonSchemaMethods: -> added `flattenSchema` and `reduceSchema`. This Function will create a nested JSON-Schema.
168 lines
4.4 KiB
TypeScript
168 lines
4.4 KiB
TypeScript
/**
|
|
* @author Martin Karkowski
|
|
* @email m.karkowski@zema.de
|
|
* @desc [description]
|
|
*/
|
|
|
|
import { assert, expect } from "chai";
|
|
import { describe, it } from "mocha";
|
|
import { deepClone, flattenObject, rgetattr } from "./objectMethods";
|
|
|
|
describe("objectMethods", function () {
|
|
// Describe the required Test:
|
|
|
|
describe("deepClone", function () {
|
|
it("clone number", function () {
|
|
const data = 1;
|
|
let clone = deepClone(data);
|
|
|
|
clone = 2;
|
|
assert.notDeepEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
});
|
|
it("clone string", function () {
|
|
const data = "test";
|
|
let clone = deepClone(data);
|
|
|
|
clone = "fail";
|
|
assert.notDeepEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
});
|
|
it("clone boolean", function () {
|
|
const data = true;
|
|
let clone = deepClone(data);
|
|
|
|
clone = false;
|
|
assert.notDeepEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
});
|
|
it("clone null", function () {
|
|
const data = null;
|
|
let clone: any = deepClone(data);
|
|
|
|
clone = false;
|
|
assert.notDeepEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
});
|
|
it("clone undefined", function () {
|
|
const data = undefined;
|
|
let clone: any = deepClone(data);
|
|
|
|
clone = false;
|
|
assert.notDeepEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
});
|
|
it("clone array", function () {
|
|
const data = [0, 1, 2];
|
|
const clone = deepClone(data);
|
|
|
|
assert.notStrictEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
clone.pop();
|
|
assert.notDeepEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
});
|
|
it("clone nested object", function () {
|
|
const data = { deep: { nested: "test" } };
|
|
const clone = deepClone(data);
|
|
|
|
assert.notStrictEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
clone.deep.nested = "changed";
|
|
assert.notDeepEqual(
|
|
data,
|
|
clone,
|
|
"Elements have the same identity, but should be differend"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("flattenObject", function () {
|
|
it("convert", function () {
|
|
const data = { deep: { nested: "test" } };
|
|
const result = flattenObject(data);
|
|
assert.isTrue(result.has("deep/nested"), "Key is missing");
|
|
expect(result.get("deep/nested")).to.equal("test");
|
|
});
|
|
it("limit the depth to 1", function () {
|
|
const data = { deep: { nested: "test" } };
|
|
let result = flattenObject(data, {
|
|
maxDepth: 1,
|
|
onlyPathToSimpleValue: false,
|
|
});
|
|
assert.isTrue(result.has("deep"), "Key is missing");
|
|
assert.isFalse(
|
|
result.has("deep/nested"),
|
|
"Key is present, which should not be the case"
|
|
);
|
|
assert.deepEqual(
|
|
result.get("deep"),
|
|
{ nested: "test" },
|
|
"Object are not matching"
|
|
);
|
|
|
|
result = flattenObject(data, {
|
|
maxDepth: 1,
|
|
onlyPathToSimpleValue: true,
|
|
});
|
|
expect(result.size).be.equal(0);
|
|
});
|
|
it("adding a prefix", function () {
|
|
const data = { deep: { nested: "test" } };
|
|
const result = flattenObject(data, {
|
|
prefix: "test",
|
|
});
|
|
assert.isTrue(result.has("test/deep/nested"), "Key is missing");
|
|
assert.deepEqual(
|
|
result.get("test/deep/nested"),
|
|
"test",
|
|
"Object are not matching"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("rgettattr", function () {
|
|
it("pull empty data", function () {
|
|
let data = {};
|
|
let result = rgetattr(data, "test", "default");
|
|
assert.isTrue(
|
|
result === "default",
|
|
"we expected 'default' but got: " + result.toString()
|
|
);
|
|
|
|
result = rgetattr(data, "test", null);
|
|
assert.isTrue(result === null, "we expected 'null' but got: " + result);
|
|
});
|
|
|
|
it("pull data", function () {
|
|
let data = { deep: { nested: "test" } };
|
|
let result = rgetattr(data, "deep/nested", null);
|
|
assert.isTrue(result === "test", "we expected 'test' but got: " + result);
|
|
});
|
|
});
|
|
});
|