MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 146 lines 5.4 kB view raw
1// Test String methods functionality 2 3console.log("=== String Methods Tests ===\n"); 4 5// Test 1: indexOf 6console.log("Test 1: indexOf()"); 7let str1 = "hello world"; 8console.log(" '" + str1 + "'.indexOf('world'):", str1.indexOf("world")); 9console.log(" '" + str1 + "'.indexOf('o'):", str1.indexOf("o")); 10console.log(" '" + str1 + "'.indexOf('xyz'):", str1.indexOf("xyz")); 11console.log(" '" + str1 + "'.indexOf(''):", str1.indexOf("")); 12 13// Test 2: substring 14console.log("\nTest 2: substring()"); 15let str2 = "JavaScript"; 16console.log(" '" + str2 + "'.substring(0, 4):", str2.substring(0, 4)); 17console.log(" '" + str2 + "'.substring(4):", str2.substring(4)); 18console.log(" '" + str2 + "'.substring(4, 10):", str2.substring(4, 10)); 19console.log(" '" + str2 + "'.substring(10, 4):", str2.substring(10, 4)); // Should swap 20 21// Test 3: slice 22console.log("\nTest 3: slice()"); 23let str3 = "The quick brown fox"; 24console.log(" '" + str3 + "'.slice(0, 3):", str3.slice(0, 3)); 25console.log(" '" + str3 + "'.slice(4, 9):", str3.slice(4, 9)); 26console.log(" '" + str3 + "'.slice(10):", str3.slice(10)); 27console.log(" '" + str3 + "'.slice(-3):", str3.slice(-3)); 28console.log(" '" + str3 + "'.slice(0, -4):", str3.slice(0, -4)); 29console.log(" '" + str3 + "'.slice(-9, -4):", str3.slice(-9, -4)); 30 31// Test 4: split 32console.log("\nTest 4: split()"); 33let str4 = "apple,banana,cherry"; 34let parts = str4.split(","); 35console.log(" '" + str4 + "'.split(','):"); 36for (let i = 0; i < parts.length; i++) { 37 console.log(" [" + i + "]: " + parts[i]); 38} 39 40let str5 = "hello"; 41let chars = str5.split(""); 42console.log(" '" + str5 + "'.split(''):"); 43for (let i = 0; i < chars.length; i++) { 44 console.log(" [" + i + "]: " + chars[i]); 45} 46 47// Test 5: includes 48console.log("\nTest 5: includes()"); 49let str6 = "The quick brown fox jumps over the lazy dog"; 50console.log(" '" + str6 + "'"); 51console.log(" includes('quick'):", str6.includes("quick")); 52console.log(" includes('cat'):", str6.includes("cat")); 53console.log(" includes('fox'):", str6.includes("fox")); 54console.log(" includes(''):", str6.includes("")); 55console.log(" includes('QUICK'):", str6.includes("QUICK")); 56 57// Test 6: startsWith 58console.log("\nTest 6: startsWith()"); 59let str7 = "Hello, World!"; 60console.log(" '" + str7 + "'"); 61console.log(" startsWith('Hello'):", str7.startsWith("Hello")); 62console.log(" startsWith('World'):", str7.startsWith("World")); 63console.log(" startsWith('H'):", str7.startsWith("H")); 64console.log(" startsWith(''):", str7.startsWith("")); 65 66// Test 7: endsWith 67console.log("\nTest 7: endsWith()"); 68let str8 = "index.html"; 69console.log(" '" + str8 + "'"); 70console.log(" endsWith('.html'):", str8.endsWith(".html")); 71console.log(" endsWith('.js'):", str8.endsWith(".js")); 72console.log(" endsWith('html'):", str8.endsWith("html")); 73console.log(" endsWith(''):", str8.endsWith("")); 74 75// Test 8: Combining methods 76console.log("\nTest 8: Combining methods"); 77let path = "/api/users/123/profile"; 78console.log(" Path:", path); 79let segments = path.split("/"); 80console.log(" Segments:"); 81for (let i = 0; i < segments.length; i++) { 82 if (segments[i] !== "") { 83 console.log(" " + segments[i]); 84 } 85} 86 87// Test 9: URL parsing example 88console.log("\nTest 9: URL parsing"); 89let url = "https://example.com/path/to/resource"; 90if (url.startsWith("https://")) { 91 console.log(" URL uses HTTPS"); 92 let domain = url.slice(8); 93 let domainEnd = domain.indexOf("/"); 94 if (domainEnd !== -1) { 95 let hostname = domain.substring(0, domainEnd); 96 console.log(" Hostname:", hostname); 97 let pathname = domain.slice(domainEnd); 98 console.log(" Pathname:", pathname); 99 } 100} 101 102// Test 10: String validation 103console.log("\nTest 10: String validation"); 104let email = "user@example.com"; 105console.log(" Email:", email); 106console.log(" Contains '@':", email.includes("@")); 107let atIndex = email.indexOf("@"); 108if (atIndex !== -1) { 109 let username = email.substring(0, atIndex); 110 let domain = email.substring(atIndex + 1); 111 console.log(" Username:", username); 112 console.log(" Domain:", domain); 113 console.log(" Domain ends with '.com':", domain.endsWith(".com")); 114} 115 116// Test 11: Edge cases 117console.log("\nTest 11: Edge cases"); 118let empty = ""; 119console.log(" Empty string:"); 120console.log(" length:", empty.length); 121console.log(" indexOf('x'):", empty.indexOf("x")); 122console.log(" slice(0, 5):", "'" + empty.slice(0, 5) + "'"); 123console.log(" includes(''):", empty.includes("")); 124console.log(" startsWith(''):", empty.startsWith("")); 125console.log(" endsWith(''):", empty.endsWith("")); 126 127let single = "a"; 128console.log(" Single char 'a':"); 129console.log(" slice(-1):", single.slice(-1)); 130console.log(" substring(0, 1):", single.substring(0, 1)); 131 132// Test 12: Practical example - template processing 133console.log("\nTest 12: Template processing"); 134let template = "Hello, {{name}}! You have {{count}} messages."; 135console.log(" Template:", template); 136if (template.includes("{{") && template.includes("}}")) { 137 console.log(" Template contains placeholders"); 138 let start = template.indexOf("{{"); 139 let end = template.indexOf("}}"); 140 if (start !== -1 && end !== -1) { 141 let placeholder = template.substring(start + 2, end); 142 console.log(" First placeholder:", placeholder); 143 } 144} 145 146console.log("\n=== All tests completed ===");