initial commit

This commit is contained in:
2026-01-17 08:33:44 +01:00
commit 7920e8c4e5
9 changed files with 251 additions and 0 deletions

18
LICENSE Normal file
View File

@@ -0,0 +1,18 @@
MIT License
Copyright (c) 2026 courses
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# m1-sor-exercises
quick exercises we do during classes: bits of typescript, http, and more

7
exercice1.js Normal file
View File

@@ -0,0 +1,7 @@
function addition(a, b) {
return a + b;
}
console.log(addition(2, 3)); // attendu : 5
console.log(addition("2", 3)); // attendu : ?
console.log(addition(true, 3)); // attendu : ?

19
exercice1.ts Normal file
View File

@@ -0,0 +1,19 @@
function addition(a, b) {
return a + b;
}
console.log(addition(2, 3)); // attendu : 5
console.log(addition("2", 3)); // attendu : ?
console.log(addition(true, 3)); // attendu : ?
namespace Typed {
function addition(a: number, b: number): number {
return a + b;
}
console.log(addition(2, 3));
// L'argument de type 'string' n'est pas attribuable au paramètre de type 'number'.ts(2345)
console.log(addition("2", 3));
// L'argument de type 'boolean' n'est pas attribuable au paramètre de type 'number'.ts(2345)
console.log(addition(true, 3));
}

6
exercice2.js Normal file
View File

@@ -0,0 +1,6 @@
function afficherUtilisateur(user) {
console.log(`${user.nom} a ${user.age} ans`);
}
afficherUtilisateur({ nom: "Alice", age: 25 });
afficherUtilisateur({ nom: "Bob" }); // manque age

15
exercice2.ts Normal file
View File

@@ -0,0 +1,15 @@
interface Utilisateur {
nom: string;
age: number;
}
function afficherUtilisateur(u: Utilisateur): void {
console.log(`${u.nom} a ${u.age} ans`);
}
afficherUtilisateur({ nom: "Alice", age: 25 });
// L'argument de type '{ nom: string; }' n'est pas attribuable
// au paramètre de type 'Utilisateur'.
// La propriété 'age' est absente du type '{ nom: string; }'
// mais obligatoire dans le type 'Utilisateur'.ts(2345)
afficherUtilisateur({ nom: "Bob" });

52
exercice3.ts Normal file
View File

@@ -0,0 +1,52 @@
interface Animal {
name: string;
}
interface Dog extends Animal {
bark: () => void;
}
interface Cat extends Animal {
meow: () => void;
}
const makeNoise = (animal: Animal) => {
if (animal instanceof Dog) {
// 'Dog' fait uniquement référence à un type mais s'utilise en tant que valeur ici.ts(2693)
animal.bark();
}
};
namespace TaggedUnion {
interface Dog {
kind: "dog"; // Tagged union
bark: () => void;
}
interface Cat {
kind: "cat"; // Tagged union
meow: () => void;
}
type Animal = Dog | Cat;
const makeNoise = (animal: Animal) => {
if (animal.kind === "dog") animal.bark();
else animal.meow();
};
const dog: Dog = {
kind: "dog",
bark: () => console.log("bark"),
};
makeNoise(dog);
}
namespace Classes {
class Dog {
constructor(public name: string, public bark: () => void) {}
}
class Cat {
constructor(public name: string, public meow: () => void) {}
}
type Animal = Dog | Cat;
const makeNoise = (animal: Animal) => {
if (animal instanceof Dog) animal.bark();
else animal.meow();
};
const dog = new Dog("Fido", () => console.log("bark"));
makeNoise(dog);
}

69
exercice4.js Normal file
View File

@@ -0,0 +1,69 @@
function readFile(path, charset) {
throw new Error("Function not implemented.");
}
function readTextFileIfExists(path) {
return new Promise((resolve, reject) => {
if (!path) {
reject(new Error("No file path provided"));
return;
}
readFile(path, "utf8")
.then(data => {
if (data.length === 0) {
reject(new Error("File is empty")); // explicit failure
} else {
resolve(data); // successful read
}
})
.catch(err => reject(err)); // I/O error
});
}
readTextFileIfExists("data.txt")
.then(text => console.log("File contents:", text))
.catch(err => console.error("I/O error:", err.message));
try {
const text = await readTextFileIfExists("data.txt");
console.log("File contents:", text);
} catch (err) {
console.error("I/O error:", err.message);
}
const files = ["file1.txt", "file2.txt", "file3.txt"];
for (const file of files) {
try {
const text = await readTextFileIfExists(file);
console.log("File contents:", text);
} catch (err) {
console.error("I/O error:", err.message);
continue;
}
}
const otherFiles = ["does_not_exist.txt", "file2.txt", "file3.txt"];
const failContents = await Promise.all(
otherFiles.map(readTextFileIfExists)
);
try {
failContents.forEach((text, i) => {
console.log("File contents:", text);
});
} catch (err) {
console.error("I/O error:", err.message);
}
const successContents = await Promise.allSettled(
files.map(readTextFileIfExists)
);
successContents.forEach((result, i) => {
if (result.status === "fulfilled") {
console.log("File contents:", result.value);
} else if (result.status === "rejected") {
console.error("I/O error:", result.reason);
}
});

62
exercice5.ts Normal file
View File

@@ -0,0 +1,62 @@
const listener = Deno.listen({ port: 8080 });
const decoder = new TextDecoder();
const encoder = new TextEncoder();
for await (const conn of listener) {
handleConn(conn);
}
async function handleConn(conn: Deno.Conn) {
const buf = new Uint8Array(1024);
const bytes = await conn.read(buf);
if (bytes === null) {
conn.close();
return;
}
const rawRequest = decoder.decode(buf.subarray(0, bytes));
console.log(rawRequest);
const [
requestMethod,
requestHost,
requestUserAgent,
requestAccept,
requestContentType,
requestContentLength,
_,
requestBody,
] = rawRequest.split("\r\n");
const [method, path, protocol] = requestMethod.split(" ");
const [, host] = requestHost.split(" ");
const [, userAgent] = requestUserAgent.split(" ");
const [, accept] = requestAccept.split(" ");
const [, contentType] = requestContentType.split(" ");
const [, contentLength] = requestContentLength.split(" ");
const responseBody = `{` +
`"method": "${method}",` +
`"path": "${path}",` +
`"protocol": "${protocol}",` +
`"host": "${host}",` +
`"user-agent": "${userAgent}",` +
`"accept": "${accept}",` +
`"content-type": "${contentType}",` +
`"content-length": "${contentLength}",` +
`"body": "${requestBody}"` +
`}\n`;
const response =
"HTTP/1.1 200 OK\r\n" +
"Content-Type: text/json\r\n" +
`Content-Length: ${responseBody.length}\r\n` +
"Connection: close\r\n" +
"\r\n" +
responseBody;
await conn.write(encoder.encode(response));
conn.close();
}