From 5ca2e7a358b9ec5c9936acd2de3a700c73e4c00c Mon Sep 17 00:00:00 2001 From: Pauline Srifi Date: Thu, 18 Apr 2024 15:17:12 +0200 Subject: [PATCH] tp note fini --- src/exercises/divide/index.test.ts | 2 +- src/note/HexColorToDecimal/index.test.ts | 31 +++++++++++++++++++++ src/note/HexColorToDecimal/index.ts | 28 +++++++++++++++++++ src/note/hexToDecimal/index.test.ts | 35 ++++++++++++++++++++++++ src/note/hexToDecimal/index.ts | 22 +++++++++++++++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/note/HexColorToDecimal/index.test.ts create mode 100644 src/note/HexColorToDecimal/index.ts create mode 100644 src/note/hexToDecimal/index.test.ts create mode 100644 src/note/hexToDecimal/index.ts diff --git a/src/exercises/divide/index.test.ts b/src/exercises/divide/index.test.ts index b748fd5..74e2f5d 100644 --- a/src/exercises/divide/index.test.ts +++ b/src/exercises/divide/index.test.ts @@ -1,6 +1,6 @@ import { divide } from "./index"; -describe("Test divide", () => { +describe("Test hexToDecimal", () => { it.each([ { a: 2, b: 1, expected: 2 }, { a: 10, b: 2, expected: 5 }, diff --git a/src/note/HexColorToDecimal/index.test.ts b/src/note/HexColorToDecimal/index.test.ts new file mode 100644 index 0000000..386317a --- /dev/null +++ b/src/note/HexColorToDecimal/index.test.ts @@ -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); + } + ); +}); diff --git a/src/note/HexColorToDecimal/index.ts b/src/note/HexColorToDecimal/index.ts new file mode 100644 index 0000000..04e0132 --- /dev/null +++ b/src/note/HexColorToDecimal/index.ts @@ -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})`; +} \ No newline at end of file diff --git a/src/note/hexToDecimal/index.test.ts b/src/note/hexToDecimal/index.test.ts new file mode 100644 index 0000000..d1dae84 --- /dev/null +++ b/src/note/hexToDecimal/index.test.ts @@ -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"); + }); +}); diff --git a/src/note/hexToDecimal/index.ts b/src/note/hexToDecimal/index.ts new file mode 100644 index 0000000..3d683a6 --- /dev/null +++ b/src/note/hexToDecimal/index.ts @@ -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]); +} \ No newline at end of file