From 29ad52b222769bd8893ede964a45b874a5e68461 Mon Sep 17 00:00:00 2001 From: Hedi EDELBLOUTE Date: Wed, 6 Sep 2023 17:55:39 +0200 Subject: [PATCH] update sortKeys method + unit tests --- .../src/families/cosmos/helpers.ts | 39 ++++++------- .../src/families/cosmos/helpers.unit.test.ts | 57 ++++++++++++++++++- 2 files changed, 76 insertions(+), 20 deletions(-) diff --git a/libs/ledger-live-common/src/families/cosmos/helpers.ts b/libs/ledger-live-common/src/families/cosmos/helpers.ts index b771e729ebbb..5de36b31c7c4 100644 --- a/libs/ledger-live-common/src/families/cosmos/helpers.ts +++ b/libs/ledger-live-common/src/families/cosmos/helpers.ts @@ -14,25 +14,26 @@ export const getMainMessage = (messages: CosmosMessage[]): CosmosMessage => { return sortedTypes[0]; }; -const order = unordered => - Object.keys(unordered) - .sort() - .reduce((obj, key) => { - obj[key] = unordered[key]; - return obj; - }, {}); +export const sortObjectKeysDeeply = src => { + let out; -export const sortObjectKeysDeeply = object => { - for (let [key, value] of Object.entries(object)) { - if (Array.isArray(value)) { - const newArray = new Array(); - for (const element of value) { - newArray.push(sortObjectKeysDeeply(element)); - } - object[key] = newArray; - } else if (typeof value === "object") { - object[key] = sortObjectKeysDeeply(value); - } + if (Array.isArray(src)) { + return src.map(function (item) { + return sortObjectKeysDeeply(item); + }); } - return order(object); + + if (typeof src === "object") { + out = {}; + + Object.keys(src) + .sort((a, b) => a.localeCompare(b)) + .forEach(function (key) { + out[key] = sortObjectKeysDeeply(src[key]); + }); + + return out; + } + + return src; }; diff --git a/libs/ledger-live-common/src/families/cosmos/helpers.unit.test.ts b/libs/ledger-live-common/src/families/cosmos/helpers.unit.test.ts index c73f27c817ca..079168226a4d 100644 --- a/libs/ledger-live-common/src/families/cosmos/helpers.unit.test.ts +++ b/libs/ledger-live-common/src/families/cosmos/helpers.unit.test.ts @@ -1,4 +1,4 @@ -import { getMainMessage } from "./helpers"; +import { getMainMessage, sortObjectKeysDeeply } from "./helpers"; import { parseAmountStringToNumber } from "./logic"; describe("getMainMessage", () => { @@ -76,3 +76,58 @@ describe("parseAmountStringToNumber", () => { ).toEqual("10000"); }); }); + +describe("sortObjectKeysDeeply", () => { + it("should sort object keys on one level", () => { + const unsorted = { b: "value", a: 1 }; + const sorted = sortObjectKeysDeeply(unsorted); + expect(Object.keys(sorted)[0]).toEqual("a"); + expect(Object.keys(sorted)[1]).toEqual("b"); + }); + + it("should not ruin already sorted object", () => { + const alreadySorted = { a: undefined, b: 1 }; + const sorted = sortObjectKeysDeeply(alreadySorted); + expect(Object.keys(sorted)[0]).toEqual("a"); + expect(Object.keys(sorted)[1]).toEqual("b"); + }); + + it("should sort on multiple levels", () => { + const unsorted = { + b: undefined, + a: { + d: "val", + c: [], + }, + }; + const sorted = sortObjectKeysDeeply(unsorted); + expect(Object.keys(sorted)[0]).toEqual("a"); + expect(Object.keys(sorted)[1]).toEqual("b"); + const child = sorted["a"]; + expect(Object.keys(child)[0]).toEqual("c"); + expect(Object.keys(child)[1]).toEqual("d"); + }); + + it("should not change array order", () => { + const objectWithArray = { + arr: [2, 1], + }; + const sorted = sortObjectKeysDeeply(objectWithArray); + const array = sorted["arr"] as Array; + + expect(array[0]).toEqual(2); + expect(array[1]).toEqual(1); + }); + + it("should sort array items", () => { + const objectWithArray = { + arr: [{ b: undefined, a: "value" }, 1], + }; + const sorted = sortObjectKeysDeeply(objectWithArray); + const array = sorted["arr"] as Array; + const elementInArrayKeys = Object.keys(array[0]); + + expect(elementInArrayKeys[0]).toEqual("a"); + expect(elementInArrayKeys[1]).toEqual("b"); + }); +});