Skip to content

Commit

Permalink
fix(757): sorting numbers in table correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Schottkyc137 authored Jun 9, 2021
1 parent 99f2ff6 commit aac4129
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { entityListSortingAccessor } from "./sorting-accessor";

describe("entityListSortingAccessor", () => {
function expectObjectToContain(obj: object, expected: any[], type: string) {
let index = 0;
for (const key of Object.keys(obj)) {
const accessed = entityListSortingAccessor(obj, key);
expect(accessed).toEqual(expected[index]);
expect(typeof accessed).toBe(type);
index += 1;
}
}
it("should return a string for string-objects", () => {
const obj = {
a: "ABC",
b: "B",
c: "Hello, World!",
};
expectObjectToContain(obj, ["ABC", "B", "Hello, World!"], "string");
});

it("should return numbers for number-objects", () => {
const obj = {
a: 1,
b: 2.0,
c: 10e3,
};
expectObjectToContain(obj, [1, 2.0, 10e3], "number");
});

it("should return numbers when a string is parsable", () => {
const obj = {
a: "1",
b: "2.0",
c: "10e3",
d: "0x1",
};
expectObjectToContain(obj, [1, 2.0, 10e3, 0x1], "number");
});

it("should return the label when the queried object has a 'label' key", () => {
const object = {
data: {
label: "data label",
value1: 123,
value2: "hello",
},
};
const accessed = entityListSortingAccessor(object, "data");
expect(typeof accessed).toBe("string");
expect(accessed).toBe("data label");
});

it("should return the object itself if it does not contain a label", () => {
const object = {
data: {
value1: 123,
value2: "hello",
},
};
const accessed = entityListSortingAccessor(object, "data");
expect(typeof accessed).toBe("object");
expect(accessed).toEqual({
value1: 123,
value2: "hello",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export function entityListSortingAccessor(data: Object, sortingHeader: string) {
) {
return data[sortingHeader].label;
} else {
return data[sortingHeader];
return tryNumber(data[sortingHeader]);
}
}

function tryNumber(input?: any): any {
return Number(input) || input;
}

0 comments on commit aac4129

Please sign in to comment.