Created
August 3, 2023 12:26
-
-
Save TheShinriel/0db2447a940919f7f1386c4f6d64782c to your computer and use it in GitHub Desktop.
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
// 🔍 Fonctions de Parsing en TypeScript | |
// 🔍 parseInt() : Convertir une chaîne de caractères en un entier | |
const numString: string = '42'; | |
const parsedInt: number = parseInt(numString); | |
// 👉 Résultat : 42 (typeof parsedInt est number) | |
// 🔍 parseFloat() : Convertir une chaîne de caractères en un nombre à virgule flottante | |
const floatString: string = '3.14'; | |
const parsedFloat: number = parseFloat(floatString); | |
// 👉 Résultat : 3.14 (typeof parsedFloat est number) | |
// 🔍 Number() : Convertir une chaîne de caractères en un nombre | |
const numericString: string = '123'; | |
const parsedNumber: number = Number(numericString); | |
// 👉 Résultat : 123 (typeof parsedNumber est number) | |
// 🔍 JSON.parse() : Analyser une chaîne JSON en un objet JavaScript | |
const jsonString: string = '{"name": "John", "age": 30}'; | |
const parsedObject: object = JSON.parse(jsonString); | |
// 👉 Résultat : { name: 'John', age: 30 } (typeof parsedObject est object) | |
// 🔍 Boolean() : Convertir une valeur en un booléen | |
const trueAsString: string = 'true'; | |
const falseAsString: string = 'false'; | |
const trueBool: boolean = Boolean(trueAsString); | |
const falseBool: boolean = Boolean(falseAsString); | |
// 👉 Résultat : true (typeof trueBool est boolean) | |
// et false (typeof falseBool est boolean) | |
// 🔍 !! : Convertir une valeur en booléen de manière concise | |
const truthyString: string = 'true'; | |
const falsyString: string = ''; | |
const isTruthy: boolean = !!truthyString; | |
const isFalsy: boolean = !!falsyString; | |
// 👉 Résultat : true (isTruthy est true) et false (isFalsy est false) | |
// 🔍 Array.from() : Convertir un objet itérable en un tableau | |
const iterableObject: string = 'hello'; | |
const charArray: string[] = Array.from(iterableObject); | |
// 👉 Résultat : ['h', 'e', 'l', 'l', 'o'] (charArray est un tableau) | |
// 🔍 [...set] : Convertir un Set en tableau | |
const mySet: Set<string> = new Set(['🍎', '🍌', '🍊']); | |
const fruitArray: string[] = [...mySet]; | |
// 👉 Résultat : ['🍎', '🍌', '🍊'] (fruitArray est un tableau) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment