From f792f15caa6ccd959b3864c70803de9a1bd4e214 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Fri, 22 Dec 2023 19:41:58 -0500 Subject: [PATCH] added example --- Classes_exemples.ipynb | 246 +++++++++++++++++++++++++++++++++++++++++ classes/index.html | 14 +++ classes/main.js | 36 ++++++ 3 files changed, 296 insertions(+) create mode 100644 Classes_exemples.ipynb create mode 100644 classes/index.html create mode 100644 classes/main.js diff --git a/Classes_exemples.ipynb b/Classes_exemples.ipynb new file mode 100644 index 0000000..960ed0c --- /dev/null +++ b/Classes_exemples.ipynb @@ -0,0 +1,246 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f003b793-ddf5-43b7-b557-3c6961a12483", + "metadata": {}, + "source": [ + "### Ce que nous voulons, une classe qui définit un match de dodgeball!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a72f5733-4c3f-4a81-95a7-4ff0f486c292", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "// partie de dodgeball\n", + "\n", + "let players = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"];\n", + "\n", + "function listeDeJoueurMorts(){}\n", + "\n", + "function listDeJoueur(){}\n", + "\n", + "function kill(nomDuJoueur){}\n", + "\n", + "function attrape(){}" + ] + }, + { + "cell_type": "markdown", + "id": "84e2ee1c-d8f5-4145-be1c-2e3775e483ef", + "metadata": {}, + "source": [ + "### Définission premiere classe" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "b1a24c1a-f1f0-434d-b2d4-a8ddcf668047", + "metadata": {}, + "outputs": [], + "source": [ + "class Politicien {\n", + "\n", + "}\n", + "\n", + "const politicien = new Politicien();" + ] + }, + { + "cell_type": "markdown", + "id": "9b2393ec-6ddd-4747-b47b-2a714cb2b7b1", + "metadata": {}, + "source": [ + "### Classe avec méthode simple" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "72649f7e-c5da-48d9-9e9a-846f084e9cf4", + "metadata": {}, + "outputs": [], + "source": [ + "class JackOfAllTrade{\n", + " add(a, b){\n", + " return a + b;\n", + " }\n", + "\n", + " multiply(a, b){\n", + " return a * b;\n", + " }\n", + "\n", + " add1(x){\n", + " return this.add(x, 1);\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "a8c0005e-2fff-4f50-be35-4dd56bf669e2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[33m199\u001b[39m" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "let joat = new JackOfAllTrade()\n", + "joat.add(109, 90)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "f9c756be-ca43-458a-993d-67652cd5986a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[33m101\u001b[39m" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "joat.add1(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "dda5b8a5-3f3d-40dc-9a02-ca38d970f2ec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[32m\"hello_world foifoi\"\u001b[39m" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "class ChangeCase{\n", + " \n", + " toSnake(str){\n", + " str = str.replace(\" \", \"_\");\n", + " return str;\n", + " }\n", + "\n", + "}\n", + "let case1 = new ChangeCase();\n", + "case1.toSnake(\"hello world foifoi\");" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "29783c57-bcd6-4522-97d7-6fda3bde0d19", + "metadata": {}, + "outputs": [], + "source": [ + "let s = \"bonjour monde\"" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6fc63875-819c-4d09-b661-90bbdd273f2c", + "metadata": {}, + "outputs": [], + "source": [ + "let idx = s.indexOf(\" \")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "22adb79c-4c67-45fd-929f-8be1ea3b1c83", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[32m\"bonjourmonde\"\u001b[39m" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = s.replace(\" \", \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "a7bf2d08-6a65-44de-8091-095135b57908", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[32m\"M\"\u001b[39m" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.substring(idx, idx + 1).toUpperCase()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8f64482f-2d87-4f0a-b579-050c0bbe477a", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Deno", + "language": "typescript", + "name": "deno" + }, + "language_info": { + "file_extension": ".ts", + "mimetype": "text/x.typescript", + "name": "typescript", + "nb_converter": "script", + "pygments_lexer": "typescript", + "version": "5.2.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/classes/index.html b/classes/index.html new file mode 100644 index 0000000..f6a0f5e --- /dev/null +++ b/classes/index.html @@ -0,0 +1,14 @@ + + + + + + + Document + + + + + + \ No newline at end of file diff --git a/classes/main.js b/classes/main.js new file mode 100644 index 0000000..c50cb42 --- /dev/null +++ b/classes/main.js @@ -0,0 +1,36 @@ +class CaseUtil{ + toSnakeCase(s){ + return s.replaceAll(" ", "_"); + } + + toKebabCase(s){ + return s.replaceAll(" ", "-"); + } + + + toCamelCase(s){ + while(s.indexOf(" ") !== -1){ + const idx = s.indexOf(" "); + s = s.replace(" ", ""); + const capital = s.substring(idx, idx + 1).toUpperCase(); + s = s.substring(0, idx) + capital + s.substring(idx + 1, s.length); + } + return s; + } + + // bonjour le monde --> bonjouRlEmondE + toAlexandreCase(s){ + const ret = []; + for(const word of s.split(" ")){ + const lastLetterCapitalized = word.substring(word.length - 1, word.length).toUpperCase(); + const wordWithLastLetterCapitalized = word.substring(0, word.length - 1) + lastLetterCapitalized; + ret.push(wordWithLastLetterCapitalized) + } + return ret.join(""); + } + +} + +const caseUtil = new CaseUtil(); + +alert(caseUtil.toAlexandreCase("bonjour le monde")) \ No newline at end of file