|
6 | 6 | "title": "Hello, World!", |
7 | 7 | "description": "Prints Hello, World! to the terminal.", |
8 | 8 | "author": "chaitanya-jvnm", |
9 | | - "code": "public class Program {\n public static void Main(string[] args) {\n System.Console.WriteLine(\"Hello, World!\");\n }\n}\n", |
10 | 9 | "tags": [ |
11 | 10 | "printing", |
12 | 11 | "hello-world" |
13 | 12 | ], |
14 | | - "contributors": [] |
| 13 | + "contributors": [], |
| 14 | + "code": "public class Program {\n public static void Main(string[] args) {\n System.Console.WriteLine(\"Hello, World!\");\n }\n}\n" |
15 | 15 | } |
16 | 16 | ] |
17 | 17 | }, |
|
22 | 22 | "title": "Generate GUID", |
23 | 23 | "description": "Generates a new GUID", |
24 | 24 | "author": "chaitanya-jvnm", |
25 | | - "code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n\n// Usage:\nGenerateGuid(); // Returns: 1c4c38d8-64e4-431b-884a-c6eec2ab02cd (Uuid is random)\n", |
26 | 25 | "tags": [ |
27 | 26 | "guid", |
28 | 27 | "generate" |
29 | 28 | ], |
30 | | - "contributors": [] |
| 29 | + "contributors": [], |
| 30 | + "code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n\n// Usage:\nGenerateGuid(); // Returns: 1c4c38d8-64e4-431b-884a-c6eec2ab02cd (Uuid is random)\n" |
31 | 31 | }, |
32 | 32 | { |
33 | 33 | "title": "Validate GUID", |
34 | 34 | "description": "Checks if a string is a valid GUID.", |
35 | 35 | "author": "chaitanya-jvnm", |
36 | | - "code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n\n// Usage:\nIsGuid(\"1c4c38d8-64e4-431b-884a-c6eec2ab02cd\"); // Returns: true\nIsGuid(\"quicksnip\"); // Returns: false\n", |
37 | 36 | "tags": [ |
38 | 37 | "guid", |
39 | 38 | "validate" |
40 | 39 | ], |
41 | | - "contributors": [] |
| 40 | + "contributors": [], |
| 41 | + "code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n\n// Usage:\nIsGuid(\"1c4c38d8-64e4-431b-884a-c6eec2ab02cd\"); // Returns: true\nIsGuid(\"quicksnip\"); // Returns: false\n" |
42 | 42 | } |
43 | 43 | ] |
44 | 44 | }, |
|
49 | 49 | "title": "Decode JWT", |
50 | 50 | "description": "Decodes a JWT.", |
51 | 51 | "author": "chaitanya-jvnm", |
52 | | - "code": "public static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n\n// Usage:\nstring token = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\nDecodeJwt(token); // Returns: \"{\\\"alg\\\":\\\"HS256\\\",\\\"typ\\\":\\\"JWT\\\"}.{\\\"sub\\\":\\\"1234567890\\\",\\\"name\\\":\\\"John Doe\\\",\\\"iat\\\":1516239022}\"\n", |
53 | 52 | "tags": [ |
54 | 53 | "jwt", |
55 | 54 | "decode" |
56 | 55 | ], |
57 | | - "contributors": [] |
| 56 | + "contributors": [], |
| 57 | + "code": "public static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n\n// Usage:\nstring token = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\nDecodeJwt(token); // Returns: \"{\\\"alg\\\":\\\"HS256\\\",\\\"typ\\\":\\\"JWT\\\"}.{\\\"sub\\\":\\\"1234567890\\\",\\\"name\\\":\\\"John Doe\\\",\\\"iat\\\":1516239022}\"\n" |
58 | 58 | }, |
59 | 59 | { |
60 | 60 | "title": "Validate JWT", |
61 | 61 | "description": "Validates a JWT.", |
62 | 62 | "author": "chaitanya-jvnm", |
63 | | - "code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n\n// Usage:\nstring JWT = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\nstring correctSecret = \"your-256-bit-secret\";\nstring wrongSecret = \"this-is-not-the-right-secret\";\n\nValidateJwt(JWT, correctSecret); // Returns: true\nValidateJwt(JWT, wrongSecret); // Returns: false\n", |
64 | 63 | "tags": [ |
65 | 64 | "jwt", |
66 | 65 | "validate" |
67 | 66 | ], |
68 | | - "contributors": [] |
| 67 | + "contributors": [], |
| 68 | + "code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n\n// Usage:\nstring JWT = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\nstring correctSecret = \"your-256-bit-secret\";\nstring wrongSecret = \"this-is-not-the-right-secret\";\n\nValidateJwt(JWT, correctSecret); // Returns: true\nValidateJwt(JWT, wrongSecret); // Returns: false\n" |
69 | 69 | } |
70 | 70 | ] |
71 | 71 | }, |
|
76 | 76 | "title": "Swap items at index", |
77 | 77 | "description": "Swaps two items at determined indexes", |
78 | 78 | "author": "omegaleo", |
79 | | - "code": "public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)\n{\n (list[indexA], list[indexB]) = (list[indexB], list[indexA]);\n return list;\n}\n\nvar list = new List<string>() {\"Test\", \"Test2\"};\n\nlist.Swap(0, 1); // Swaps \"Test\" and \"Test2\" in place\n", |
80 | 79 | "tags": [ |
81 | 80 | "list", |
82 | 81 | "swapping" |
83 | 82 | ], |
84 | | - "contributors": [] |
| 83 | + "contributors": [], |
| 84 | + "code": "public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)\n{\n (list[indexA], list[indexB]) = (list[indexB], list[indexA]);\n return list;\n}\n\nvar list = new List<string>() {\"Test\", \"Test2\"};\n\nlist.Swap(0, 1); // Swaps \"Test\" and \"Test2\" in place\n" |
85 | 85 | } |
86 | 86 | ] |
87 | 87 | }, |
|
92 | 92 | "title": "Capitalize first letter", |
93 | 93 | "description": "Makes the first letter of a string uppercase.", |
94 | 94 | "author": "chaitanya-jvnm", |
95 | | - "code": "public static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n\n// Usage:\n\"quicksnip\".Capitalize(); // Returns: \"Quicksnip\"\n", |
96 | 95 | "tags": [ |
97 | 96 | "string", |
98 | 97 | "capitalize" |
99 | 98 | ], |
100 | | - "contributors": [] |
| 99 | + "contributors": [], |
| 100 | + "code": "public static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n\n// Usage:\n\"quicksnip\".Capitalize(); // Returns: \"Quicksnip\"\n" |
101 | 101 | }, |
102 | 102 | { |
103 | 103 | "title": "Truncate String", |
104 | 104 | "description": "Cut off a string once it reaches a determined amount of characters and add '...' to the end of the string", |
105 | 105 | "author": "omegaleo", |
106 | | - "code": "public static string Truncate(this string value, int maxChars)\n{\n return value.Length <= maxChars ? value : value.Substring(0, maxChars) + \"...\";\n}\n\n// Usage:\n\"Quicksnip\".Truncate(5); // Returns: \"Quick...\"\n", |
107 | 106 | "tags": [ |
108 | 107 | "string", |
109 | 108 | "truncate" |
110 | 109 | ], |
111 | | - "contributors": [] |
| 110 | + "contributors": [], |
| 111 | + "code": "public static string Truncate(this string value, int maxChars)\n{\n return value.Length <= maxChars ? value : value.Substring(0, maxChars) + \"...\";\n}\n\n// Usage:\n\"Quicksnip\".Truncate(5); // Returns: \"Quick...\"\n" |
112 | 112 | } |
113 | 113 | ] |
114 | 114 | } |
|
0 commit comments