Skip to content

Commit

Permalink
Rewrite getUnbindValue to fix tests in Creator
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Jul 6, 2023
1 parent fb046c2 commit 9fd1a5c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 25 deletions.
18 changes: 2 additions & 16 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,15 @@ export class Helpers {
return array;
}
public static getUnbindValue(value: any): any {
return Helpers.getUnbindValueCore(value, [], 1);
}
private static getUnbindValueCore(value: any, objects: Array<any>, level: number): any {
if(Array.isArray(value)) {
const res = [];
for(let i = 0; i < value.length; i ++) {
res.push(Helpers.getUnbindValueCore(value[i], [], 1));
res.push(Helpers.getUnbindValue(value[i]));
}
return res;
}
if (!!value && value instanceof Object && !(value instanceof Date)) {
const valLevel = Helpers.getObjectLevel(objects, value);
if(valLevel > -1 && valLevel < level) return undefined;
objects.push({ obj: value, level: level });
const res: any = {};
const keys = Object.keys(value);
keys.forEach(key => {
const val = Helpers.getUnbindValueCore(value[key], objects, level + 1);
if(val !== undefined) {
res[key] = val;
}
});
return res;
return JSON.parse(JSON.stringify(value));
}
return value;
}
Expand Down
9 changes: 0 additions & 9 deletions tests/helperstests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,6 @@ QUnit.test("getUnbindValue function", function(assert) {
assert.notStrictEqual(arr[2], unbindArr[2], "nested object in new array are not strict equal");
assert.deepEqual(arr, unbindArr, "Arrays are equals");
});
QUnit.test("getUnbindValue function, do not serialize cycle references", function(assert) {
const obj1: any = { a: 1 };
const obj2: any = { b: 2, obj1: obj1 };
obj1.obj2 = obj2;
const res1 = Helpers.getUnbindValue(obj1);
assert.deepEqual(res1, { a: 1, obj2: { b: 2 } }, "obj1->obj2->obj1");
const res2 = Helpers.getUnbindValue(obj2);
assert.deepEqual(res2, { b: 2, obj1: { a: 1 } }, "obj2->obj1->obj2");
});
QUnit.test("convertDateToString/convertDateTimeToString functions", function(assert) {
const d = new Date(2022, 11, 24, 10, 55, 33, 3);
assert.equal(Helpers.convertDateToString(d), "2022-12-24", "convertDateToString");
Expand Down

0 comments on commit 9fd1a5c

Please sign in to comment.