|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | + |
| 6 | +namespace GraphQL.Client.Abstractions.Utilities |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Copied from https://github.com/jquense/StringUtils |
| 10 | + /// </summary> |
| 11 | + public static class StringUtils |
| 12 | + { |
| 13 | + private static readonly Regex _reWords = new Regex(@"[A-Z\xc0-\xd6\xd8-\xde]?[a-z\xdf-\xf6\xf8-\xff]+(?:['’](?:d|ll|m|re|s|t|ve))?(?=[\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000]|[A-Z\xc0-\xd6\xd8-\xde]|$)|(?:[A-Z\xc0-\xd6\xd8-\xde]|[^\ud800-\udfff\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\d+\u2700-\u27bfa-z\xdf-\xf6\xf8-\xffA-Z\xc0-\xd6\xd8-\xde])+(?:['’](?:D|LL|M|RE|S|T|VE))?(?=[\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000]|[A-Z\xc0-\xd6\xd8-\xde](?:[a-z\xdf-\xf6\xf8-\xff]|[^\ud800-\udfff\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\d+\u2700-\u27bfa-z\xdf-\xf6\xf8-\xffA-Z\xc0-\xd6\xd8-\xde])|$)|[A-Z\xc0-\xd6\xd8-\xde]?(?:[a-z\xdf-\xf6\xf8-\xff]|[^\ud800-\udfff\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\d+\u2700-\u27bfa-z\xdf-\xf6\xf8-\xffA-Z\xc0-\xd6\xd8-\xde])+(?:['’](?:d|ll|m|re|s|t|ve))?|[A-Z\xc0-\xd6\xd8-\xde]+(?:['’](?:D|LL|M|RE|S|T|VE))?|\d+|(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*"); |
| 14 | + private static readonly Regex _reIndent = new Regex(@"^[ \t]*(?=\S)", RegexOptions.Multiline); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Removes the leading indent from a multi-line string |
| 18 | + /// </summary> |
| 19 | + /// <param name="str">String</param> |
| 20 | + /// <returns></returns> |
| 21 | + public static string StripIndent(string str) |
| 22 | + { |
| 23 | + int indent = _reIndent.Matches(str).Cast<Match>().Select(m => m.Value.Length).Min(); |
| 24 | + return new Regex(@"^[ \t]{" + indent + "}", RegexOptions.Multiline).Replace(str, ""); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Split a cased string into a series of "words" excluding the seperator. |
| 29 | + /// </summary> |
| 30 | + /// <param name="str"></param> |
| 31 | + /// <returns></returns> |
| 32 | + public static IEnumerable<string> ToWords(string str) |
| 33 | + { |
| 34 | + foreach (Match match in _reWords.Matches(str)) |
| 35 | + { |
| 36 | + yield return match.Value; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Uppercase the first character in a string, leaving the rest of the string as is |
| 42 | + /// </summary> |
| 43 | + /// <param name="str"></param> |
| 44 | + /// <returns>a string with the first character uppercased</returns> |
| 45 | + public static string ToUpperFirst(string str) => ChangeCaseFirst(str, c => c.ToUpperInvariant()); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Lowercase the first character in a string, leaving the rest of the string as is |
| 49 | + /// </summary> |
| 50 | + /// <param name="str"></param> |
| 51 | + /// <returns>a string with the first character lowercased</returns> |
| 52 | + public static string ToLowerFirst(string str) => ChangeCaseFirst(str, c => c.ToLowerInvariant()); |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Capitalizes a string, lowercasing the entire string and uppercasing the first character |
| 56 | + /// </summary> |
| 57 | + /// <param name="str"></param> |
| 58 | + /// <returns>a capitalized string</returns> |
| 59 | + public static string Capitalize(string str) => ToUpperFirst(str.ToLowerInvariant()); |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Converts a string to camelCase. |
| 63 | + /// </summary> |
| 64 | + /// <example> |
| 65 | + /// <code>StringUtils.ToCamelCase("FOOBAR") // "foobar"</code> |
| 66 | + /// <code>StringUtils.ToCamelCase("FOO_BAR") // "fooBar"</code> |
| 67 | + /// <code>StringUtils.ToCamelCase("FooBar") // "fooBar"</code> |
| 68 | + /// <code>StringUtils.ToCamelCase("foo bar") // "fooBar"</code> |
| 69 | + /// </example> |
| 70 | + /// <param name="str"></param> |
| 71 | + /// <returns></returns> |
| 72 | + public static string ToCamelCase(string str) => |
| 73 | + ChangeCase(str, (word, index) => |
| 74 | + (index == 0 ? word.ToLowerInvariant() : Capitalize(word))); |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Convert a string to CONSTANT_CASE |
| 78 | + /// </summary> |
| 79 | + /// <example> |
| 80 | + /// <code>StringUtils.ToConstantCase("fOo BaR") // "FOO_BAR"</code> |
| 81 | + /// <code>StringUtils.ToConstantCase("FooBar") // "FOO_BAR"</code> |
| 82 | + /// <code>StringUtils.ToConstantCase("Foo Bar") // "FOO_BAR"</code> |
| 83 | + /// </example> |
| 84 | + /// <param name="str"></param> |
| 85 | + /// <returns></returns> |
| 86 | + public static string ToConstantCase(string str) => ChangeCase(str, "_", w => w.ToUpperInvariant()); |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Convert a string to UPPERCASE |
| 90 | + /// </summary> |
| 91 | + /// <example> |
| 92 | + /// <code>StringUtils.ToUpperCase("foobar") // "FOOBAR"</code> |
| 93 | + /// <code>StringUtils.ToUpperCase("FOO_BAR") // "FOO BAR"</code> |
| 94 | + /// <code>StringUtils.ToUpperCase("FooBar") // "FOO BAR"</code> |
| 95 | + /// <code>StringUtils.ToUpperCase("Foo Bar") // "FOO BAR"</code> |
| 96 | + /// </example> |
| 97 | + /// <param name="str"></param> |
| 98 | + /// <returns></returns> |
| 99 | + public static string ToUpperCase(string str) => ChangeCase(str, " ", (word) => word.ToUpperInvariant()); |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Convert a string to lowercase |
| 103 | + /// </summary> |
| 104 | + /// <example> |
| 105 | + /// <code>StringUtils.ToLowerCase("FOOBAR") // "foobar"</code> |
| 106 | + /// <code>StringUtils.ToLowerCase("FOO_BAR") // "foo bar"</code> |
| 107 | + /// <code>StringUtils.ToLowerCase("FooBar") // "foo bar"</code> |
| 108 | + /// <code>StringUtils.ToLowerCase("Foo Bar") // "foo bar"</code> |
| 109 | + /// </example> |
| 110 | + /// <param name="str"></param> |
| 111 | + /// <returns></returns> |
| 112 | + public static string ToLowerCase(string str) => ChangeCase(str, " ", word => word.ToLowerInvariant()); |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// convert a string to PascalCase |
| 116 | + /// </summary> |
| 117 | + /// <example> |
| 118 | + /// <code>StringUtils.ToPascalCase("FOOBAR") // "FooBar"</code> |
| 119 | + /// <code>StringUtils.ToPascalCase("FOO_BAR") // "FooBar"</code> |
| 120 | + /// <code>StringUtils.ToPascalCase("fooBar") // "FooBar"</code> |
| 121 | + /// <code>StringUtils.ToPascalCase("Foo Bar") // "FooBar"</code> |
| 122 | + /// </example> |
| 123 | + /// <param name="str"></param> |
| 124 | + /// <returns></returns> |
| 125 | + public static string ToPascalCase(string str) => ChangeCase(str, Capitalize); |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// convert a string to kebab-case |
| 129 | + /// </summary> |
| 130 | + /// <example> |
| 131 | + /// <code>StringUtils.ToKebabCase("FOOBAR") // "foo-bar"</code> |
| 132 | + /// <code>StringUtils.ToKebabCase("FOO_BAR") // "foo-bar"</code> |
| 133 | + /// <code>StringUtils.ToKebabCase("fooBar") // "foo-bar"</code> |
| 134 | + /// <code>StringUtils.ToKebabCase("Foo Bar") // "foo-bar"</code> |
| 135 | + /// </example> |
| 136 | + /// <param name="str"></param> |
| 137 | + /// <returns></returns> |
| 138 | + public static string ToKebabCase(string str) => ChangeCase(str, "-", word => word.ToLowerInvariant()); |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// convert a string to snake_case |
| 142 | + /// </summary> |
| 143 | + /// <example> |
| 144 | + /// <code>StringUtils.ToSnakeCase("FOOBAR") // "foo_bar"</code> |
| 145 | + /// <code>StringUtils.ToSnakeCase("FOO_BAR") // "foo_bar"</code> |
| 146 | + /// <code>StringUtils.ToSnakeCase("fooBar") // "foo_bar"</code> |
| 147 | + /// <code>StringUtils.ToSnakeCase("Foo Bar") // "foo_bar"</code> |
| 148 | + /// </example> |
| 149 | + /// <param name="str"></param> |
| 150 | + /// <returns></returns> |
| 151 | + public static string ToSnakeCase(string str) => ChangeCase(str, "_", word => word.ToLowerInvariant()); |
| 152 | + |
| 153 | + public static string ChangeCase(string str, Func<string, string> composer) => ChangeCase(str, "", composer); |
| 154 | + |
| 155 | + public static string ChangeCase(string str, string sep, Func<string, string> composer) => ChangeCase(str, sep, (w, i) => composer(w)); |
| 156 | + |
| 157 | + public static string ChangeCase(string str, Func<string, int, string> composer) => ChangeCase(str, "", composer); |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Convert a string to a new case |
| 161 | + /// </summary> |
| 162 | + /// <example> |
| 163 | + /// Convert a string to inverse camelCase: CAMELcASE |
| 164 | + /// <code> |
| 165 | + /// StringUtils.ChangeCase("my string", "", (word, index) => { |
| 166 | + /// word = word.ToUpperInvariant(); |
| 167 | + /// if (index > 0) |
| 168 | + /// word = StringUtils.toLowerFirst(word); |
| 169 | + /// return word |
| 170 | + /// }); |
| 171 | + /// // "MYsTRING" |
| 172 | + /// </code> |
| 173 | + /// </example> |
| 174 | + /// <param name="str">an input string </param> |
| 175 | + /// <param name="sep">a seperator string used between "words" in the string</param> |
| 176 | + /// <param name="composer">a function that converts individual words to a new case</param> |
| 177 | + /// <returns></returns> |
| 178 | + public static string ChangeCase(string str, string sep, Func<string, int, string> composer) |
| 179 | + { |
| 180 | + string result = ""; |
| 181 | + int index = 0; |
| 182 | + |
| 183 | + foreach (string word in ToWords(str)) |
| 184 | + { |
| 185 | + result += ((index == 0 ? "" : sep) + composer(word, index++)); |
| 186 | + } |
| 187 | + |
| 188 | + return result; |
| 189 | + } |
| 190 | + |
| 191 | + private static string ChangeCaseFirst(string str, Func<string, string> change) => change(str.Substring(0, 1)) + str.Substring(1); |
| 192 | + } |
| 193 | +} |
0 commit comments