Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: utils/pub/utils.dart

Issue 11830017: Fix ALL the pub tests. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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. Stuff that should possibly be in core. 5 /// Generic utility functions. Stuff that should possibly be in core.
6 library utils; 6 library utils;
7 7
8 import 'dart:async'; 8 import 'dart:async';
9 import 'dart:crypto'; 9 import 'dart:crypto';
10 import 'dart:isolate'; 10 import 'dart:isolate';
(...skipping 22 matching lines...) Expand all
33 final result = new StringBuffer(); 33 final result = new StringBuffer();
34 result.add(source); 34 result.add(source);
35 35
36 while (result.length < length) { 36 while (result.length < length) {
37 result.add(' '); 37 result.add(' ');
38 } 38 }
39 39
40 return result.toString(); 40 return result.toString();
41 } 41 }
42 42
43 /// Runs [fn] after [future] completes, whether it completes successfully or
44 /// not. Essentially an asynchronous `finally` block.
45 always(Future future, fn()) {
46 future.catchError((_) {}).then((_) => fn());
47 }
48
Bob Nystrom 2013/01/09 16:49:23 Yay!
49 /// Flattens nested lists inside an iterable into a single list containing only 43 /// Flattens nested lists inside an iterable into a single list containing only
50 /// non-list elements. 44 /// non-list elements.
51 List flatten(Iterable nested) { 45 List flatten(Iterable nested) {
52 var result = []; 46 var result = [];
53 helper(list) { 47 helper(list) {
54 for (var element in list) { 48 for (var element in list) {
55 if (element is List) { 49 if (element is List) {
56 helper(element); 50 helper(element);
57 } else { 51 } else {
58 result.add(element); 52 result.add(element);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 return completer.future; 111 return completer.future;
118 } 112 }
119 113
120 /// Configures [future] so that its result (success or exception) is passed on 114 /// Configures [future] so that its result (success or exception) is passed on
121 /// to [completer]. 115 /// to [completer].
122 void chainToCompleter(Future future, Completer completer) { 116 void chainToCompleter(Future future, Completer completer) {
123 future.then((value) => completer.complete(value), 117 future.then((value) => completer.complete(value),
124 onError: (e) => completer.completeError(e.error, e.stackTrace)); 118 onError: (e) => completer.completeError(e.error, e.stackTrace));
125 } 119 }
126 120
121 /// Like [Future.whenComplete], except that [action] may return a [Future]. If
122 /// it does, the returned [Future] won't complete until [action]'s [Future]
123 /// completes.
124 ///
125 /// The returned [Future] always has the same value as [future].
Bob Nystrom 2013/01/09 16:49:23 Since this is how then() and catchError() work, it
126 Future asyncWhenComplete(Future future, Future action()) {
127 Future futurify(Future future) {
128 if (future != null) return future;
129 return new Future.immediate(null);
130 }
131
132 return future.then((result) => futurify(action()).then((_) => result),
133 onError: (e) {
134 return futurify(action()).then((_) {
135 throw e;
136 });
137 });
138 }
139
127 // TODO(nweiz): unify the following functions with the utility functions in 140 // TODO(nweiz): unify the following functions with the utility functions in
128 // pkg/http. 141 // pkg/http.
129 142
130 /// Like [String.split], but only splits on the first occurrence of the pattern. 143 /// Like [String.split], but only splits on the first occurrence of the pattern.
131 /// This will always return an array of two elements or fewer. 144 /// This will always return an array of two elements or fewer.
132 List<String> split1(String toSplit, String pattern) { 145 List<String> split1(String toSplit, String pattern) {
133 if (toSplit.isEmpty) return <String>[]; 146 if (toSplit.isEmpty) return <String>[];
134 147
135 var index = toSplit.indexOf(pattern); 148 var index = toSplit.indexOf(pattern);
136 if (index == -1) return [toSplit]; 149 if (index == -1) return [toSplit];
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 // TODO(rnystrom): Remove this when #7781 is fixed. 200 // TODO(rnystrom): Remove this when #7781 is fixed.
188 /// When an error is rethrown in an async callback, you can end up with nested 201 /// When an error is rethrown in an async callback, you can end up with nested
189 /// AsyncErrors. This unwraps them to find the real originating error. 202 /// AsyncErrors. This unwraps them to find the real originating error.
190 getRealError(error) { 203 getRealError(error) {
191 while (error is AsyncError) { 204 while (error is AsyncError) {
192 error = error.error; 205 error = error.error;
193 } 206 }
194 207
195 return error; 208 return error;
196 } 209 }
210
211 // TODO(rnystrom): Remove this when #7781 is fixed.
Bob Nystrom 2013/01/09 16:49:23 "nweiz", not that it really matters. :)
212 /// When an error is rethrown in an async callback, you can end up with nested
213 /// AsyncErrors. This unwraps them to find the real originating stack trace.
214 getRealStackTrace(error) {
215 var trace = null;
216 while (error is AsyncError) {
217 trace = error.stackTrace;
218 error = error.error;
219 }
220
221 return trace;
222 }
OLDNEW
« utils/pub/io.dart ('K') | « utils/pub/system_cache.dart ('k') | utils/pub/validator.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698