tp note fini

This commit is contained in:
Pauline Srifi 2024-04-18 15:17:12 +02:00
parent 4df48587df
commit 5ca2e7a358
5 changed files with 117 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import { divide } from "./index"; import { divide } from "./index";
describe("Test divide", () => { describe("Test hexToDecimal", () => {
it.each([ it.each([
{ a: 2, b: 1, expected: 2 }, { a: 2, b: 1, expected: 2 },
{ a: 10, b: 2, expected: 5 }, { a: 10, b: 2, expected: 5 },

View File

@ -0,0 +1,31 @@
import {hexToRgb} from "./index"
describe("hexToRgb", () => {
it.each([
{ hex: '#BD3F0A', expected: 'rgb(189,63,10)' },
{ hex: '#A3BF7E', expected: 'rgb(163,191,126)' },
])(
"should convert $hex to $expected",
({ hex, expected }: { hex: string, expected: string }) => {
// act
const result = hexToRgb(hex);
// assert
expect(result).toEqual(expected);
console.log(`Test with hex = ${hex}, expected = ${expected}, result = ${result}`);
}
);
it.each([
{ hex: '', expectedErrorMsg: "Couleur hexadécimale invalide, ne doit pas être vide" },
{ hex: 'BD3F0A', expectedErrorMsg: "Couleur hexadécimale invalide, doit commencer par #" },
{ hex: '#', expectedErrorMsg: "Couleur hexadécimale invalide, doit contenir 6 caractères" },
{ hex: '#12345', expectedErrorMsg: "Couleur hexadécimale invalide, doit contenir 6 caractères" },
{ hex: '#GHIJKL', expectedErrorMsg: "Couleur hexadécimale invalide, caractères invalides" },
])(
"should throw an error for invalid hex $hex",
({ hex, expectedErrorMsg }: { hex: string, expectedErrorMsg: string }) => {
// assert
expect(() => hexToRgb(hex)).toThrow(expectedErrorMsg);
}
);
});

View File

@ -0,0 +1,28 @@
import {parse} from "../hexToDecimal"
export function hexToRgb(hex: string): string {
if (hex === "") {
throw new Error("Couleur hexadécimale invalide, ne doit pas être vide");
}
if (!hex.startsWith("#")) {
throw new Error("Couleur hexadécimale invalide, doit commencer par #");
}
hex = hex.slice(1);
if (hex.length !== 6) {
throw new Error("Couleur hexadécimale invalide, doit contenir 6 caractères");
}
if (!/^[0-9A-F]{6}$/.test(hex.toUpperCase())) {
throw new Error("Couleur hexadécimale invalide, caractères invalides");
}
const r: number = parse(hex.slice(0, 2));
const g: number = parse(hex.slice(2, 4));
const b: number = parse(hex.slice(4, 6));
return `rgb(${r},${g},${b})`;
}

View File

@ -0,0 +1,35 @@
import {parse} from "./index"
describe("parse", () => {
it.each([
{a: 'CE', expected: 206},
{a: '9B', expected: 155},
{a: 'E6', expected: 230},
{a: '25', expected: 37},
])(
"should return conversion of $a hex value to decimal value",
({a, expected}: { a: string, expected: number }) => {
// act
const result = parse(a);
// assert
expect(result).toEqual(expected);
console.log(`Test with a = ${a}, expected = ${expected}, result = ${result}`);
}
);
it("should throw an error for empty string", () => {
expect(() => parse("")).toThrow("Teinte hexadécimale invalide, teinte vide");
});
it("should throw an error for invalid length", () => {
expect(() => parse("B")).toThrow("Teinte hexadécimale invalide, la teinte doit contenir 2 caractères");
expect(() => parse("2BC")).toThrow("Teinte hexadécimale invalide, la teinte doit contenir 2 caractères");
});
it("should throw an error for invalid characters", () => {
expect(() => parse("-G")).toThrow("Teinte hexadécimale invalide, caractère non valide");
expect(() => parse("1.")).toThrow("Teinte hexadécimale invalide, caractère non valide");
expect(() => parse("G0")).toThrow("Teinte hexadécimale invalide, caractère non valide");
expect(() => parse("0G")).toThrow("Teinte hexadécimale invalide, caractère non valide");
});
});

View File

@ -0,0 +1,22 @@
export function parse(a: string): number {
const alphabet: string = "0123456789ABCDEF";
if (a === "") {
throw new Error("Teinte hexadécimale invalide, teinte vide");
}
if (a.length !== 2) {
throw new Error("Teinte hexadécimale invalide, la teinte doit contenir 2 caractères");
}
const hexToDecimal = (char: string): number => {
const value = alphabet.indexOf(char.toUpperCase());
if (value == -1) {
throw new Error(" Teinte hexadécimale invalide, caractère non valide");
} else {
return value;
}
};
return hexToDecimal(a[0]) * 16 + hexToDecimal(a[1]);
}