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 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 : new StreamController(); | 267 : new StreamController(); |
268 stream.listen(controller.add, | 268 stream.listen(controller.add, |
269 onError: (e) => controller.signalError(e), | 269 onError: (e) => controller.signalError(e), |
270 onDone: controller.close); | 270 onDone: controller.close); |
271 return controller.stream; | 271 return controller.stream; |
272 } | 272 } |
273 | 273 |
274 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the | 274 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the |
275 /// results of those [Future]s as the test. | 275 /// results of those [Future]s as the test. |
276 Future<Iterable> futureWhere(Iterable iter, test(value)) { | 276 Future<Iterable> futureWhere(Iterable iter, test(value)) { |
277 return Future.wait(iter.mappedBy((e) { | 277 return Future.wait(iter.map((e) { |
278 var result = test(e); | 278 var result = test(e); |
279 if (result is! Future) result = new Future.immediate(result); | 279 if (result is! Future) result = new Future.immediate(result); |
280 return result.then((result) => new Pair(e, result)); | 280 return result.then((result) => new Pair(e, result)); |
281 })) | 281 })) |
282 .then((pairs) => pairs.where((pair) => pair.last)) | 282 .then((pairs) => pairs.where((pair) => pair.last)) |
283 .then((pairs) => pairs.mappedBy((pair) => pair.first)); | 283 .then((pairs) => pairs.map((pair) => pair.first)); |
284 } | 284 } |
285 | 285 |
286 // TODO(nweiz): unify the following functions with the utility functions in | 286 // TODO(nweiz): unify the following functions with the utility functions in |
287 // pkg/http. | 287 // pkg/http. |
288 | 288 |
289 /// Like [String.split], but only splits on the first occurrence of the pattern. | 289 /// Like [String.split], but only splits on the first occurrence of the pattern. |
290 /// This will always return an array of two elements or fewer. | 290 /// This will always return an array of two elements or fewer. |
291 List<String> split1(String toSplit, String pattern) { | 291 List<String> split1(String toSplit, String pattern) { |
292 if (toSplit.isEmpty) return <String>[]; | 292 if (toSplit.isEmpty) return <String>[]; |
293 | 293 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 | 335 |
336 /// Add all key/value pairs from [source] to [destination], overwriting any | 336 /// Add all key/value pairs from [source] to [destination], overwriting any |
337 /// pre-existing values. | 337 /// pre-existing values. |
338 void mapAddAll(Map destination, Map source) => | 338 void mapAddAll(Map destination, Map source) => |
339 source.forEach((key, value) => destination[key] = value); | 339 source.forEach((key, value) => destination[key] = value); |
340 | 340 |
341 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 341 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
342 /// replacing `+` with ` `. | 342 /// replacing `+` with ` `. |
343 String urlDecode(String encoded) => | 343 String urlDecode(String encoded) => |
344 decodeUriComponent(encoded.replaceAll("+", " ")); | 344 decodeUriComponent(encoded.replaceAll("+", " ")); |
OLD | NEW |