-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
almost done, just writing tests for functions
- Loading branch information
1 parent
492dc60
commit 1ff43b9
Showing
21 changed files
with
242 additions
and
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,6 @@ | |
"typedoc": "^0.25.8" | ||
}, | ||
"dependencies": { | ||
"@devlander/utils": "^0.0.63" | ||
"@devlander/utils": "^0.0.66" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,33 @@ | ||
export const isValidRgb = (rgb: string): boolean => { | ||
const rgbRegex = /^rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)$/; | ||
const match = rgb.match(rgbRegex); | ||
if (!match) return false; | ||
const [, r, g, b] = match.map(Number); | ||
return r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255; | ||
}; | ||
import { isValidRgb } from "../isValidRgb"; | ||
|
||
describe("isValidRgb", () => { | ||
test("validates correctly formatted RGB strings", () => { | ||
expect(isValidRgb("rgb(255, 0, 128)")).toBe(true); | ||
expect(isValidRgb("rgb(0, 128, 255)")).toBe(true); | ||
expect(isValidRgb("rgb(100, 100, 100)")).toBe(true); | ||
}); | ||
|
||
test("handles edge cases", () => { | ||
expect(isValidRgb("rgb(0,0,0)")).toBe(true); // No spaces | ||
expect(isValidRgb("rgb(255, 255, 255)")).toBe(true); // Maximum values | ||
expect(isValidRgb("rgb(256, 255, 255)")).toBe(false); // Out of range | ||
expect(isValidRgb("rgb(255, 256, 255)")).toBe(false); // Out of range | ||
expect(isValidRgb("rgb(255, 255, 256)")).toBe(false); // Out of range | ||
expect(isValidRgb("rgb(-1, 0, 128)")).toBe(false); // Negative value | ||
expect(isValidRgb("rgb(255, 0, 128")).toBe(false); // Missing closing parenthesis | ||
expect(isValidRgb("rgba(255, 0, 128)")).toBe(false); // Wrong format | ||
expect(isValidRgb("rgb(255, 0)")).toBe(false); // Missing a component | ||
expect(isValidRgb("rgb(255, 0, 128, 0.5)")).toBe(false); // Extra alpha component | ||
expect(isValidRgb("rgb(255, 0, 128,)")).toBe(false); // Trailing comma | ||
expect(isValidRgb("rgb(255, 0, 128")).toBe(false); // Missing closing parenthesis | ||
}); | ||
|
||
test("handles invalid input gracefully", () => { | ||
expect(isValidRgb("")).toBe(false); // Empty string | ||
expect(isValidRgb("rgb")).toBe(false); // Missing parentheses and numbers | ||
expect(isValidRgb("rgb()")).toBe(false); // Empty parentheses | ||
expect(isValidRgb("rgb(255, 0, 128, extra)")).toBe(false); // Non-numeric component | ||
expect(isValidRgb("rgb(255, 0, 128," + "\n" + ")")).toBe(false); // Newline between components | ||
expect(isValidRgb("rgb(255, 0, 128) extra")).toBe(false); // Extra characters after valid format | ||
}); | ||
}); |
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,35 @@ | ||
import { isValidRgba } from "../isValidRgba"; | ||
|
||
describe("isValidRgba", () => { | ||
test("validates correctly formatted RGBA strings", () => { | ||
expect(isValidRgba("rgba(255, 0, 128, 0.5)")).toBe(true); | ||
expect(isValidRgba("rgba(0, 128, 255, 1)")).toBe(true); | ||
expect(isValidRgba("rgba(100, 100, 100, 0)")).toBe(true); | ||
expect(isValidRgba("rgba(255, 255, 255, 0.75)")).toBe(true); | ||
expect(isValidRgba("rgb(255, 0, 0)")).toBe(true); // Should also validate RGB strings | ||
}); | ||
|
||
test("handles edge cases", () => { | ||
expect(isValidRgba("rgba(0,0,0,0)")).toBe(true); // No spaces | ||
expect(isValidRgba("rgba(255, 255, 255, 1)")).toBe(true); // Maximum values | ||
expect(isValidRgba("rgba(256, 255, 255, 0.5)")).toBe(false); // Out of range RGB | ||
expect(isValidRgba("rgba(255, 256, 255, 0.5)")).toBe(false); // Out of range RGB | ||
expect(isValidRgba("rgba(255, 255, 256, 0.5)")).toBe(false); // Out of range RGB | ||
expect(isValidRgba("rgba(-1, 0, 128, 0.5)")).toBe(false); // Negative value | ||
expect(isValidRgba("rgba(255, 0, 128")).toBe(false); // Missing closing parenthesis | ||
expect(isValidRgba("rgba(255, 0, 128,")).toBe(false); // Trailing comma | ||
expect(isValidRgba("rgba(255, 0, 128, extra)")).toBe(false); // Non-numeric alpha | ||
expect(isValidRgba("rgba(255, 0, 128, 0.5, extra)")).toBe(false); // Extra alpha component | ||
expect(isValidRgba("rgba(255, 0, 128, 0.5")).toBe(false); // Missing closing parenthesis | ||
expect(isValidRgba("rgba(255, 0, 128 extra")).toBe(false); // Extra characters | ||
expect(isValidRgba("rgba(255, 0, 128) extra")).toBe(false); // Extra characters after valid format | ||
}); | ||
|
||
test("handles invalid input gracefully", () => { | ||
expect(isValidRgba("")).toBe(false); // Empty string | ||
expect(isValidRgba("rgba")).toBe(false); // Missing parentheses and numbers | ||
expect(isValidRgba("rgba()")).toBe(false); // Empty parentheses | ||
expect(isValidRgba("rgba(255, 0, 128, extra)")).toBe(false); // Non-numeric component | ||
expect(isValidRgba("rgba(255, 0, 128," + "\n" + ")")).toBe(false); // Newline between components | ||
}); | ||
}); |
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,38 @@ | ||
import { parseRgbString } from "../parseRgbString"; | ||
|
||
describe('parseRgbString', () => { | ||
it('should return parsed RGB object for valid RGB string', () => { | ||
const result = parseRgbString('rgb(255, 255, 255)'); | ||
expect(result).toEqual({ r: 255, g: 255, b: 255 }); | ||
}); | ||
|
||
it('should return parsed RGBA object for valid RGBA string', () => { | ||
const result = parseRgbString('rgba(255, 255, 255, 0.5)'); | ||
expect(result).toEqual({ r: 255, g: 255, b: 255, a: 0.5 }); | ||
}); | ||
|
||
it('should return null for invalid RGB string', () => { | ||
const result = parseRgbString('rgb(255, 255, 255, 1)'); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return null for invalid RGBA string', () => { | ||
const result = parseRgbString('rgba(255, 255, 255)'); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return null for completely invalid string', () => { | ||
const result = parseRgbString('invalid string'); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should handle errors and return null', () => { | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); // Mock console.error | ||
|
||
const result = parseRgbString('rgb(255, 255, not-a-number)'); | ||
expect(result).toBeNull(); | ||
expect(console.error).toHaveBeenCalledWith(expect.stringContaining('Error parsing RGB string')); | ||
|
||
(console.error as jest.Mock).mockRestore(); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,23 @@ | ||
/** | ||
* Checks if the provided string is a valid RGB color format. | ||
* @param rgb The string to validate as RGB (e.g., "rgb(255, 0, 128)"). | ||
* @returns True if the string is a valid RGB format, false otherwise. | ||
*/ | ||
export const isValidRgb = (rgb: string): boolean => { | ||
const rgbRegex = /^rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)$/; | ||
const match = rgb.match(rgbRegex); | ||
if (!match) return false; | ||
const [, r, g, b] = match.map(Number); | ||
return r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255; | ||
try { | ||
const rgbRegex = /^rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)$/; | ||
const match = rgb.match(rgbRegex); | ||
|
||
if (!match) { | ||
return false; | ||
} | ||
|
||
const [, r, g, b] = match.map(Number); | ||
|
||
// Check if components are within valid range | ||
return r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255; | ||
} catch (error) { | ||
console.error(`Error validating RGB string: ${rgb}`, error); | ||
return false; | ||
} | ||
}; |
Oops, something went wrong.