|
| 1 | +import 'package:characters/characters.dart'; |
| 2 | + |
| 3 | +const int _DIGIT = 1; |
| 4 | +const int _LOWER = 2; |
| 5 | +const int _UNDERSCORE = 4; |
| 6 | +const int _UPPER = 8; |
| 7 | +const int _ALPHA = _LOWER | _UPPER; |
| 8 | +const int _ALPHA_NUM = _ALPHA | _DIGIT; |
| 9 | +const int _ASCII_END = 127; // _ascii list length |
| 10 | + |
| 11 | +/// "The app is running" => "the app is running" |
| 12 | +/// "theAppIsRunning" => "the_app_is_running" |
| 13 | +String underscore(String input) { |
| 14 | + final sb = StringBuffer(); |
| 15 | + var separate = false; |
| 16 | + Characters(input).forEach((s) { |
| 17 | + final type = _getAsciiType(s.runes); |
| 18 | + |
| 19 | + if (separate && type & _UPPER != 0) { |
| 20 | + sb.write('_'); |
| 21 | + sb.write(s.toLowerCase()); |
| 22 | + separate = true; |
| 23 | + } else { |
| 24 | + sb.write(s.toLowerCase()); |
| 25 | + separate = type & _ALPHA_NUM != 0 || type & _UNDERSCORE != 0 && separate; |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + return sb.toString(); |
| 30 | +} |
| 31 | + |
| 32 | +/// "The_app_is_running" => "theAppIsRunning" |
| 33 | +String camelize(String input) { |
| 34 | + final string = input.toLowerCase(); |
| 35 | + final sb = StringBuffer(); |
| 36 | + |
| 37 | + var capitalize = true; |
| 38 | + var isBeginning = true; |
| 39 | + var remove = false; |
| 40 | + |
| 41 | + Characters(string).forEach((s) { |
| 42 | + final type = _getAsciiType(s.runes); |
| 43 | + |
| 44 | + if (capitalize && type & _ALPHA != 0) { |
| 45 | + sb.write(isBeginning ? s : s.toUpperCase()); |
| 46 | + |
| 47 | + capitalize = false; |
| 48 | + remove = true; |
| 49 | + isBeginning = false; |
| 50 | + } else { |
| 51 | + if (type & _UNDERSCORE != 0) { |
| 52 | + if (!remove) { |
| 53 | + sb.write(s); |
| 54 | + remove = true; |
| 55 | + } |
| 56 | + |
| 57 | + capitalize = true; |
| 58 | + } else { |
| 59 | + if (type & _ALPHA_NUM != 0) { |
| 60 | + capitalize = false; |
| 61 | + remove = true; |
| 62 | + } else { |
| 63 | + capitalize = true; |
| 64 | + remove = false; |
| 65 | + isBeginning = true; |
| 66 | + } |
| 67 | + |
| 68 | + sb.write(s); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + return sb.toString(); |
| 74 | +} |
| 75 | + |
| 76 | +int _getAsciiType(Runes runes) { |
| 77 | + if (runes.length == 1) { |
| 78 | + final c = runes.first; |
| 79 | + if (c <= _ASCII_END) { |
| 80 | + return _ascii[c]; |
| 81 | + } |
| 82 | + } |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +final List<int> _ascii = <int>[ |
| 87 | + 0, |
| 88 | + 0, |
| 89 | + 0, |
| 90 | + 0, |
| 91 | + 0, |
| 92 | + 0, |
| 93 | + 0, |
| 94 | + 0, |
| 95 | + 0, |
| 96 | + 0, |
| 97 | + 0, |
| 98 | + 0, |
| 99 | + 0, |
| 100 | + 0, |
| 101 | + 0, |
| 102 | + 0, |
| 103 | + 0, |
| 104 | + 0, |
| 105 | + 0, |
| 106 | + 0, |
| 107 | + 0, |
| 108 | + 0, |
| 109 | + 0, |
| 110 | + 0, |
| 111 | + 0, |
| 112 | + 0, |
| 113 | + 0, |
| 114 | + 0, |
| 115 | + 0, |
| 116 | + 0, |
| 117 | + 0, |
| 118 | + 0, |
| 119 | + 0, |
| 120 | + 0, |
| 121 | + 0, |
| 122 | + 0, |
| 123 | + 0, |
| 124 | + 0, |
| 125 | + 0, |
| 126 | + 0, |
| 127 | + 0, |
| 128 | + 0, |
| 129 | + 0, |
| 130 | + 0, |
| 131 | + 0, |
| 132 | + 0, |
| 133 | + 0, |
| 134 | + 0, |
| 135 | + 1, |
| 136 | + 1, |
| 137 | + 1, |
| 138 | + 1, |
| 139 | + 1, |
| 140 | + 1, |
| 141 | + 1, |
| 142 | + 1, |
| 143 | + 1, |
| 144 | + 1, |
| 145 | + 0, |
| 146 | + 0, |
| 147 | + 0, |
| 148 | + 0, |
| 149 | + 0, |
| 150 | + 0, |
| 151 | + 0, |
| 152 | + 8, |
| 153 | + 8, |
| 154 | + 8, |
| 155 | + 8, |
| 156 | + 8, |
| 157 | + 8, |
| 158 | + 8, |
| 159 | + 8, |
| 160 | + 8, |
| 161 | + 8, |
| 162 | + 8, |
| 163 | + 8, |
| 164 | + 8, |
| 165 | + 8, |
| 166 | + 8, |
| 167 | + 8, |
| 168 | + 8, |
| 169 | + 8, |
| 170 | + 8, |
| 171 | + 8, |
| 172 | + 8, |
| 173 | + 8, |
| 174 | + 8, |
| 175 | + 8, |
| 176 | + 8, |
| 177 | + 8, |
| 178 | + 0, |
| 179 | + 0, |
| 180 | + 0, |
| 181 | + 0, |
| 182 | + 4, |
| 183 | + 0, |
| 184 | + 2, |
| 185 | + 2, |
| 186 | + 2, |
| 187 | + 2, |
| 188 | + 2, |
| 189 | + 2, |
| 190 | + 2, |
| 191 | + 2, |
| 192 | + 2, |
| 193 | + 2, |
| 194 | + 2, |
| 195 | + 2, |
| 196 | + 2, |
| 197 | + 2, |
| 198 | + 2, |
| 199 | + 2, |
| 200 | + 2, |
| 201 | + 2, |
| 202 | + 2, |
| 203 | + 2, |
| 204 | + 2, |
| 205 | + 2, |
| 206 | + 2, |
| 207 | + 2, |
| 208 | + 2, |
| 209 | + 2, |
| 210 | + 0, |
| 211 | + 0, |
| 212 | + 0, |
| 213 | + 0, |
| 214 | + 0 |
| 215 | +]; |
0 commit comments