| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library utils; | 5 library utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 /// A pair of values. | 9 /// A pair of values. |
| 10 class Pair<E, F> { | 10 class Pair<E, F> { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 int get hashCode => first.hashCode ^ last.hashCode; | 23 int get hashCode => first.hashCode ^ last.hashCode; |
| 24 } | 24 } |
| 25 | 25 |
| 26 /// Configures [future] so that its result (success or exception) is passed on | 26 /// Configures [future] so that its result (success or exception) is passed on |
| 27 /// to [completer]. | 27 /// to [completer]. |
| 28 void chainToCompleter(Future future, Completer completer) { | 28 void chainToCompleter(Future future, Completer completer) { |
| 29 future.then((value) => completer.complete(value), | 29 future.then((value) => completer.complete(value), |
| 30 onError: (e) => completer.completeError(e)); | 30 onError: (e) => completer.completeError(e)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 /// Prepends each line in [text] with [prefix]. | 33 /// Prepends each line in [text] with [prefix]. If [firstPrefix] is passed, the |
| 34 String prefixLines(String text, {String prefix: '| '}) => | 34 /// first line is prefixed with that instead. |
| 35 text.split('\n').map((line) => '$prefix$line').join('\n'); | 35 String prefixLines(String text, {String prefix: '| ', String firstPrefix}) { |
| 36 var lines = text.split('\n'); |
| 37 if (firstPrefix == null) { |
| 38 return lines.map((line) => '$prefix$line').join('\n'); |
| 39 } |
| 40 |
| 41 var firstLine = "$firstPrefix${lines.first}"; |
| 42 lines = lines.skip(1).map((line) => '$prefix$line').toList(); |
| 43 lines.insertRange(0, 1, firstLine); |
| 44 return lines.join('\n'); |
| 45 } |
| 36 | 46 |
| 37 /// Returns a [Future] that completes after pumping the event queue [times] | 47 /// Returns a [Future] that completes after pumping the event queue [times] |
| 38 /// times. By default, this should pump the event queue enough times to allow | 48 /// times. By default, this should pump the event queue enough times to allow |
| 39 /// any code to run, as long as it's not waiting on some external event. | 49 /// any code to run, as long as it's not waiting on some external event. |
| 40 Future pumpEventQueue([int times=20]) { | 50 Future pumpEventQueue([int times=20]) { |
| 41 if (times == 0) return new Future.immediate(null); | 51 if (times == 0) return new Future.immediate(null); |
| 42 return new Future.immediate(null).then((_) => pumpEventQueue(times - 1)); | 52 return new Future.immediate(null).then((_) => pumpEventQueue(times - 1)); |
| 43 } | 53 } |
| 44 | 54 |
| 45 /// Returns whether [iterable1] has the same elements in the same order as | 55 /// Returns whether [iterable1] has the same elements in the same order as |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 .then((resolved) => new Pair(key, resolved))); | 170 .then((resolved) => new Pair(key, resolved))); |
| 161 }); | 171 }); |
| 162 return Future.wait(pairs).then((resolvedPairs) { | 172 return Future.wait(pairs).then((resolvedPairs) { |
| 163 var map = {}; | 173 var map = {}; |
| 164 for (var pair in resolvedPairs) { | 174 for (var pair in resolvedPairs) { |
| 165 map[pair.first] = pair.last; | 175 map[pair.first] = pair.last; |
| 166 } | 176 } |
| 167 return map; | 177 return map; |
| 168 }); | 178 }); |
| 169 } | 179 } |
| 180 |
| 181 /// Returns whether [pattern] matches all of [string]. |
| 182 bool fullMatch(String string, Pattern pattern) { |
| 183 var matches = pattern.allMatches(string); |
| 184 if (matches.isEmpty) return false; |
| 185 return matches.first.start == 0 && matches.first.end == string.length; |
| 186 } |
| OLD | NEW |