-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [GSW-2032] SonarQube issue when using Array.sort()
Provide a compare function to avoid sorting elements alphabetically. "Array.prototype.sort()" and "Array.prototype.toSorted()" should use a compare function typescript:S2871 Software qualities impacted: Reliability
- Loading branch information
Showing
12 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { sortTokenPaths } from "./sort-utils"; | ||
import { GNS_TOKEN_PATH, WRAPPED_GNOT_PATH } from "@constants/environment.constant"; | ||
|
||
describe("sortTokenPaths utility function test", () => { | ||
test("Same result as default sort - plain string", () => { | ||
const tokens = ["gns", "GNS", "Gns", "FOO", "BAR", "BAZ"]; | ||
|
||
expect([...tokens].sort()).toEqual([...tokens].sort(sortTokenPaths)); | ||
}); | ||
|
||
test("Same result as default sort - includes undefined", () => { | ||
const tokens = ["gns", "GNS", undefined, "Gns", "FOO", "BAR", "BAZ"]; | ||
|
||
expect([...tokens].sort()).toEqual([...tokens].sort(sortTokenPaths)); | ||
}); | ||
|
||
test("Same result as default sort - includes number", () => { | ||
const tokens = ["GNOT1", "GNOT2", "GNOT10", "GNS1", "GNS10", "GNS2", "wrapped.GNOT1", "wrapped.GNS1", undefined]; | ||
|
||
expect([...tokens].sort()).toEqual([...tokens].sort(sortTokenPaths)); | ||
}); | ||
|
||
test("Same result as default sort - includes special characters", () => { | ||
const tokens = ["gns-1", "gns_1", "gns.1", "gns/1", undefined, "GNS"]; | ||
|
||
expect([...tokens].sort()).toEqual([...tokens].sort(sortTokenPaths)); | ||
}); | ||
|
||
test("Testing real-world use cases", () => { | ||
const tokens = [WRAPPED_GNOT_PATH, GNS_TOKEN_PATH]; | ||
|
||
expect([...tokens].sort()).toEqual([...tokens].sort(sortTokenPaths)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* A comparison function for token path strings | ||
* | ||
* to comply with SonarQube's "Provide a compare function when using 'Array.prototype.sort()'" rule. | ||
* Used as a comparison function for Array.prototype.sort() to determine the order of token paths. | ||
* | ||
* Performs lexicographical sorting based on UTF-16 code unit values, identical to default .sort() | ||
* Handles undefined values by treating them as empty strings | ||
* | ||
* @param tokenA - First token path (possibly undefined) | ||
* @param tokenB - Second token path (possibly undefined) | ||
* @returns | ||
* -1 if tokenA < tokenB | ||
* 1 if tokenA > tokenB | ||
* 0 if tokenA === tokenB | ||
* | ||
* @example | ||
* Same result as default .sort() | ||
* ['gns', undefined, 'GNS'].sort() // ['GNS', 'gns', undefined] | ||
* ['gns', undefined, 'GNS'].sort(compareTokenPaths) // ['GNS', 'gns', undefined] | ||
* | ||
* Actual usage examples | ||
* const tokenPair = [tokenAPath, tokenBPath].sort(compareTokenPaths); | ||
* const poolPath = [...[WRAPPED_GNOT_PATH, GNS_TOKEN_PATH].sort(compareTokenPaths), "3000"].join(":"); | ||
*/ | ||
export const sortTokenPaths = (tokenA: string | undefined, tokenB: string | undefined): number => { | ||
const tokenAString = tokenA ?? ""; | ||
const tokenBString = tokenB ?? ""; | ||
return tokenAString === tokenBString ? 0 : tokenAString > tokenBString ? 1 : -1; | ||
}; |