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 29 matching lines...) Expand all Loading... | |
40 var start; | 40 var start; |
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(List list) { |
Lasse Reichstein Nielsen
2012/07/09 10:39:33
Put a generic type on the List types here. It's pa
Johnni Winther
2012/07/09 14:57:18
Done.
| |
51 List keys = map.getKeys(); | 51 final elements = []; |
52 keys.sort((a, b) { | 52 for (var element in list) { |
53 elements.add(element); | |
54 } | |
Lasse Reichstein Nielsen
2012/07/09 10:39:33
final elements = new List<whateveritis>.from(list)
Johnni Winther
2012/07/09 14:57:18
Done.
| |
55 elements.sort((a, b) { | |
53 // Hack: make sure $dom methods show up last in the list not first. | 56 // Hack: make sure $dom methods show up last in the list not first. |
54 String transformName(String name) => | 57 String transformName(String name) => |
55 name.toUpperCase().replaceFirst(const RegExp('\\\$DOM_'), 'zzzz'); | 58 name.toUpperCase().replaceFirst(const RegExp('\\\$DOM_'), 'zzzz'); |
Lasse Reichstein Nielsen
2012/07/09 10:39:33
Use raw strings for regexp literals: const RegExp(
Johnni Winther
2012/07/09 14:57:18
Done.
| |
56 | 59 |
57 return transformName(a).compareTo(transformName(b)); | 60 return transformName(a.simpleName()).compareTo( |
kasperl
2012/07/06 12:40:41
Maybe do the transform before calling compareTo an
Johnni Winther
2012/07/09 14:57:18
Done.
| |
61 transformName(b.simpleName())); | |
58 }); | 62 }); |
59 final values = []; | 63 return elements; |
60 for (var k in keys) { | |
61 values.add(map[k]); | |
62 } | |
63 return values; | |
64 } | 64 } |
65 | 65 |
66 /** | 66 /** |
67 * Joins [items] into a single, comma-separated string using [conjunction]. | 67 * Joins [items] into a single, comma-separated string using [conjunction]. |
68 * E.g. `['A', 'B', 'C']` becomes `"A, B, and C"`. | 68 * E.g. `['A', 'B', 'C']` becomes `"A, B, and C"`. |
69 */ | 69 */ |
70 String joinWithCommas(List<String> items, [String conjunction = 'and']) { | 70 String joinWithCommas(List<String> items, [String conjunction = 'and']) { |
71 if (items.length == 1) return items[0]; | 71 if (items.length == 1) return items[0]; |
72 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; | 72 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; |
73 return Strings.join(items.getRange(0, items.length - 1), ', ') + | 73 return '${Strings.join(items.getRange(0, items.length - 1), ', ')}' |
74 ', $conjunction ' + items[items.length - 1]; | 74 ', $conjunction ${items[items.length - 1]}'; |
75 } | 75 } |
76 | |
77 void writeString(File file, String text) { | |
78 var randomAccessFile = file.openSync(FileMode.WRITE); | |
79 randomAccessFile.writeStringSync(text); | |
80 randomAccessFile.closeSync(); | |
81 } | |
OLD | NEW |