Skip to content

Commit

Permalink
update string compare method
Browse files Browse the repository at this point in the history
  • Loading branch information
choden-dev committed Aug 18, 2024
1 parent 452d03f commit 21c9abb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
34 changes: 34 additions & 0 deletions client/src/services/Admin/AdminUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,38 @@ describe("compareStrings", () => {
it("should return 0 for equal strings", () => {
expect(compareStrings("abbb", "abbb")).toEqual(0)
})

it("should handle empty strings correctly", () => {
expect(compareStrings("", "a")).toBeLessThan(0)
expect(compareStrings("a", "")).toBeGreaterThan(0)
expect(compareStrings("", "")).toEqual(0)
})

it("should handle strings with different cases", () => {
expect(compareStrings("abc", "ABC")).toBeGreaterThan(0) // Assuming case-sensitive comparison
expect(compareStrings("ABC", "abc")).toBeLessThan(0) // Assuming case-sensitive comparison
})

it("should handle strings with special characters", () => {
expect(compareStrings("abc!", "abc")).toBeGreaterThan(0)
expect(compareStrings("abc", "abc!")).toBeLessThan(0)
expect(compareStrings("abc!", "abc!")).toEqual(0)
})

it("should handle strings with numbers", () => {
expect(compareStrings("abc1", "abc2")).toBeLessThan(0)
expect(compareStrings("abc2", "abc1")).toBeGreaterThan(0)
expect(compareStrings("abc1", "abc1")).toEqual(0)
})

it("should handle strings with spaces", () => {
expect(compareStrings("abc ", "abc")).toBeGreaterThan(0)
expect(compareStrings("abc", "abc ")).toBeLessThan(0)
expect(compareStrings("abc ", "abc ")).toEqual(0)
})

it("should handle strings with different lengths", () => {
expect(compareStrings("abc", "abcd")).toBeLessThan(0)
expect(compareStrings("abcd", "abc")).toBeGreaterThan(0)
})
})
17 changes: 6 additions & 11 deletions client/src/services/Admin/AdminUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,11 @@ export function replaceUserInPage<T extends keyof CombinedUserData>(
* @returns `0` if `a` and `b` are equal strings
*/
export function compareStrings(a: string, b: string) {
for (let i = 0; i < Math.min(a.length, b.length); ++i) {
const charCodeA = a.charCodeAt(i)
const charCodeB = b.charCodeAt(i)

const difference = charCodeA - charCodeB

if (difference !== 0) {
return difference
}
if (a < b) {
return -1
} else if (a > b) {
return 1
} else {
return 0
}

return 0
}

0 comments on commit 21c9abb

Please sign in to comment.