All is explained in this video:
To install the template, type:
dotnet new -i LogicMonitor.Ali
To use the template, type:
dotnet new logicmonitor
Update appsettings.json and run using:
dotnet run
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static string ToHumanReadableString(this IEnumerable<object> enumerable, string quoteWith = "'", string delimitWith = ", ", string delimitLastWith = " or ") { if (enumerable == null) { return string.Empty; } var list = enumerable.ToList(); return !list.Any() ? string.Empty : list.Count == 1 ? $"{quoteWith}{list.Single()}{quoteWith}" : $"{string.Join(delimitWith ?? string.Empty, list.Take(list.Count - 1).Select(item => $"{quoteWith}{item}{quoteWith}"))}{delimitLastWith}{quoteWith}{list.Last()}{quoteWith}"; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [Fact] public void JoinForHumansTest() { Assert.Equal("'a'", new List<string> {"a"}.ToHumanReadableString()); Assert.Equal("''", new string[] { null }.ToHumanReadableString()); Assert.Equal("'a'", new[] { "a" }.ToHumanReadableString()); Assert.Equal("'a' or 'b'", new[] { "a", "b" }.ToHumanReadableString()); Assert.Equal("'a' or ''", new[] { "a", null }.ToHumanReadableString()); Assert.Equal("'a', 'b' or 'c'", new[] {"a", "b", "c"}.ToHumanReadableString()); Assert.Equal("'a', 'b', 'c' or 'd'", new[] {"a", "b", "c", "d"}.ToHumanReadableString()); Assert.Equal("'a';'b';'c' or 'd'", new[] {"a", "b", "c", "d"}.ToHumanReadableString(delimitWith: ";")); Assert.Equal("'a', 'b', 'c' and 'd'", new[] {"a", "b", "c", "d"}.ToHumanReadableString(delimitLastWith: " and ")); Assert.Equal("a, b, c or d", new[] {"a", "b", "c", "d"}.ToHumanReadableString(string.Empty)); Assert.Equal("abcd", new[] {"a", "b", "c", "d"}.ToHumanReadableString(null, null, null)); Assert.Equal("", ((string[]) null).ToHumanReadableString(null, null, null)); } |