| 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. 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 return completer.future; | 111 return completer.future; |
| 112 } | 112 } |
| 113 | 113 |
| 114 /// 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 |
| 115 /// to [completer]. | 115 /// to [completer]. |
| 116 void chainToCompleter(Future future, Completer completer) { | 116 void chainToCompleter(Future future, Completer completer) { |
| 117 future.then((value) => completer.complete(value), | 117 future.then((value) => completer.complete(value), |
| 118 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 118 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the |
| 122 /// results of those [Future]s as the test. |
| 123 Future<Iterable> futureWhere(Iterable iter, test(value)) { |
| 124 return Future.wait(iter.mappedBy((e) { |
| 125 var result = test(e); |
| 126 if (result is! Future) result = new Future.immediate(result); |
| 127 return result.then((result) => new Pair(e, result)); |
| 128 })) |
| 129 .then((pairs) => pairs.where((pair) => pair.last)) |
| 130 .then((pairs) => pairs.mappedBy((pair) => pair.first)); |
| 131 } |
| 132 |
| 121 // TODO(nweiz): unify the following functions with the utility functions in | 133 // TODO(nweiz): unify the following functions with the utility functions in |
| 122 // pkg/http. | 134 // pkg/http. |
| 123 | 135 |
| 124 /// Like [String.split], but only splits on the first occurrence of the pattern. | 136 /// Like [String.split], but only splits on the first occurrence of the pattern. |
| 125 /// This will always return an array of two elements or fewer. | 137 /// This will always return an array of two elements or fewer. |
| 126 List<String> split1(String toSplit, String pattern) { | 138 List<String> split1(String toSplit, String pattern) { |
| 127 if (toSplit.isEmpty) return <String>[]; | 139 if (toSplit.isEmpty) return <String>[]; |
| 128 | 140 |
| 129 var index = toSplit.indexOf(pattern); | 141 var index = toSplit.indexOf(pattern); |
| 130 if (index == -1) return [toSplit]; | 142 if (index == -1) return [toSplit]; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 182 |
| 171 /// Add all key/value pairs from [source] to [destination], overwriting any | 183 /// Add all key/value pairs from [source] to [destination], overwriting any |
| 172 /// pre-existing values. | 184 /// pre-existing values. |
| 173 void mapAddAll(Map destination, Map source) => | 185 void mapAddAll(Map destination, Map source) => |
| 174 source.forEach((key, value) => destination[key] = value); | 186 source.forEach((key, value) => destination[key] = value); |
| 175 | 187 |
| 176 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 188 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
| 177 /// replacing `+` with ` `. | 189 /// replacing `+` with ` `. |
| 178 String urlDecode(String encoded) => | 190 String urlDecode(String encoded) => |
| 179 decodeUriComponent(encoded.replaceAll("+", " ")); | 191 decodeUriComponent(encoded.replaceAll("+", " ")); |
| OLD | NEW |