Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions projects/ui/src/lib/utils/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,22 +846,77 @@ describe('Function sortValues:', () => {
expect(sortValues(leftSide, rightSide, ascending)).toBe(expectedReturn);
});

it('should return `-1` if `leftSide` is numeric value, `rightSide` is a character value and `ascending` is `true`', () => {
it('should convert numeric strings to numbers and sort correctly ascending', () => {
const ascending: boolean = true;
const expectedReturn: number = -1;
const leftSide: string = '123';
const rightSide: string = 'ABC';
const leftSide: string = '10';
const rightSide: string = '100';

expect(sortValues(leftSide, rightSide, ascending)).toBe(expectedReturn);
expect(sortValues(leftSide, rightSide, ascending)).toBe(-1);
});

it('should return `1` if `leftSide` is numeric value, `rightSide` is a character value and `ascending` is `false`', () => {
it('should convert numeric strings to numbers and sort correctly descending', () => {
const ascending: boolean = false;
const expectedReturn: number = 1;
const leftSide: string = '10';
const rightSide: string = '100';

expect(sortValues(leftSide, rightSide, ascending)).toBe(1);
});

it('should handle numeric string comparison correctly (100 > 20 numerically)', () => {
const ascending: boolean = true;
const leftSide: string = '100';
const rightSide: string = '20';

expect(sortValues(leftSide, rightSide, ascending)).toBe(1);
});

it('should not convert empty string to number', () => {
const ascending: boolean = true;
const leftSide: string = '';
const rightSide: string = 'ABC';

expect(sortValues(leftSide, rightSide, ascending)).toBe(-1);
});

it('should not convert string with only spaces to number', () => {
const ascending: boolean = true;
const leftSide: string = ' ';
const rightSide: string = 'ABC';

expect(sortValues(leftSide, rightSide, ascending)).toBe(-1);
});

it('should handle decimal numeric strings', () => {
const ascending: boolean = true;
const leftSide: string = '10.5';
const rightSide: string = '10.9';

expect(sortValues(leftSide, rightSide, ascending)).toBe(-1);
});

it('should handle negative numeric strings', () => {
const ascending: boolean = true;
const leftSide: string = '-10';
const rightSide: string = '10';

expect(sortValues(leftSide, rightSide, ascending)).toBe(-1);
});

it('should compare equal numeric strings', () => {
const ascending: boolean = true;
const leftSide: string = '123';
const rightSide: string = '123';

expect(sortValues(leftSide, rightSide, ascending)).toBe(0);
});

it('should not convert alphanumeric string starting with number', () => {
const ascending: boolean = true;
const leftSide: string = '123abc';
const rightSide: string = 'ABC';

expect(sortValues(leftSide, rightSide, ascending)).toBe(expectedReturn);
// '123abc' permanece string, então comparação alfabética
expect(sortValues(leftSide, rightSide, ascending)).toBe(-1);
});
});

Expand Down
13 changes: 11 additions & 2 deletions projects/ui/src/lib/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,17 @@ export function getFormattedLink(link: string): string {
* @param ascending Determina se será em ordem ascendente ou descendente.
*/
export function sortValues(leftSide: string | Date, rightSide: string | Date, ascending: boolean = true): number {
const left = isTypeof(leftSide, 'string') ? (leftSide as string).toLowerCase() : leftSide;
const right = isTypeof(rightSide, 'string') ? (rightSide as string).toLowerCase() : rightSide;

const leftNumeric = isTypeof(leftSide, 'string') && !isNaN(Number(leftSide)) && leftSide.toString().trim() !== ''
? Number(leftSide)
: leftSide;

const rightNumeric = isTypeof(rightSide, 'string') && !isNaN(Number(rightSide)) && rightSide.toString().trim() !== ''
? Number(rightSide)
: rightSide;

const left = isTypeof(leftNumeric, 'string') ? (leftNumeric as string).toLowerCase() : leftNumeric;
const right = isTypeof(rightNumeric, 'string') ? (rightNumeric as string).toLowerCase() : rightNumeric;

const leftIsInvalid = left === null || left === undefined || Number.isNaN(left);
const rightIsInvalid = right === null || right === undefined || Number.isNaN(right);
Expand Down