Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Generic utility functions. | 5 // Generic utility functions. |
| 6 | 6 |
| 7 /** Turns [name] into something that's safe to use as a file name. */ | 7 /** Turns [name] into something that's safe to use as a file name. */ |
| 8 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); | 8 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); |
| 9 | 9 |
| 10 /** Returns the number of times [search] occurs in [text]. */ | 10 /** Returns the number of times [search] occurs in [text]. */ |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 for (start = 0; start < Math.min(indentation, text.length); start++) { | 41 for (start = 0; start < Math.min(indentation, text.length); start++) { |
| 42 // Stop if we hit a non-whitespace character. | 42 // Stop if we hit a non-whitespace character. |
| 43 if (text[start] != ' ') break; | 43 if (text[start] != ' ') break; |
| 44 } | 44 } |
| 45 | 45 |
| 46 return text.substring(start); | 46 return text.substring(start); |
| 47 } | 47 } |
| 48 | 48 |
| 49 /** Sorts the map by the key, doing a case-insensitive comparison. */ | 49 /** Sorts the map by the key, doing a case-insensitive comparison. */ |
| 50 List orderByName(Map<String, Dynamic> map) { | 50 List orderByName(Map<String, Dynamic> map) { |
| 51 // TODO(rnystrom): it'd be nice to have this in corelib. | |
| 52 List keys = map.getKeys(); | 51 List keys = map.getKeys(); |
| 53 keys.sort((x, y) => x.toUpperCase().compareTo(y.toUpperCase())); | 52 keys.sort((a, b) { |
| 53 // Hack: make sure $dom methods show up last in the list not first. | |
|
Bob Nystrom
2012/04/12 17:54:06
Take credit for it. :)
// TODO(jacobr): ...
| |
| 54 String transformName(String name) => | |
| 55 name.toUpperCase().replaceFirst(const RegExp('\\\$DOM_'), 'zzzz'); | |
| 56 | |
| 57 return transformName(a).compareTo(transformName(b)); | |
| 58 }); | |
| 54 final values = []; | 59 final values = []; |
| 55 for (var k in keys) { | 60 for (var k in keys) { |
| 56 values.add(map[k]); | 61 values.add(map[k]); |
| 57 } | 62 } |
| 58 return values; | 63 return values; |
| 59 } | 64 } |
| 60 | 65 |
| 61 /** | 66 /** |
| 62 * Joins [items] into a single, comma-separated string using [conjunction]. | 67 * Joins [items] into a single, comma-separated string using [conjunction]. |
| 63 * E.g. `['A', 'B', 'C']` becomes `"A, B, and C"`. | 68 * E.g. `['A', 'B', 'C']` becomes `"A, B, and C"`. |
| 64 */ | 69 */ |
| 65 String joinWithCommas(List<String> items, [String conjunction = 'and']) { | 70 String joinWithCommas(List<String> items, [String conjunction = 'and']) { |
| 66 if (items.length == 1) return items[0]; | 71 if (items.length == 1) return items[0]; |
| 67 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; | 72 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; |
| 68 return Strings.join(items.getRange(0, items.length - 1), ', ') + | 73 return Strings.join(items.getRange(0, items.length - 1), ', ') + |
| 69 ', $conjunction ' + items[items.length - 1]; | 74 ', $conjunction ' + items[items.length - 1]; |
| 70 } | 75 } |
| OLD | NEW |