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 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 | 330 |
331 /// Add all key/value pairs from [source] to [destination], overwriting any | 331 /// Add all key/value pairs from [source] to [destination], overwriting any |
332 /// pre-existing values. | 332 /// pre-existing values. |
333 void mapAddAll(Map destination, Map source) => | 333 void mapAddAll(Map destination, Map source) => |
334 source.forEach((key, value) => destination[key] = value); | 334 source.forEach((key, value) => destination[key] = value); |
335 | 335 |
336 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 336 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
337 /// replacing `+` with ` `. | 337 /// replacing `+` with ` `. |
338 String urlDecode(String encoded) => | 338 String urlDecode(String encoded) => |
339 decodeUriComponent(encoded.replaceAll("+", " ")); | 339 decodeUriComponent(encoded.replaceAll("+", " ")); |
340 | |
341 /// Takes a simple data structure (composed of [Map]s, [List]s, scalar objects, | |
342 /// and [Future]s) and recursively resolves all the [Future]s contained within. | |
343 /// Completes with the fully resolved structure. | |
344 Future awaitObject(object) { | |
345 // Unroll nested futures. | |
346 if (object is Future) return object.then(awaitObject); | |
347 if (object is Collection) { | |
348 return Future.wait(object.map(awaitObject).toList()); | |
349 } | |
350 if (object is! Map) return new Future.immediate(object); | |
351 | |
352 var pairs = <Future<Pair>>[]; | |
353 object.forEach((key, value) { | |
354 pairs.add(awaitObject(value) | |
355 .then((resolved) => new Pair(key, resolved))); | |
356 }); | |
357 return Future.wait(pairs).then((resolvedPairs) { | |
358 var map = {}; | |
359 for (var pair in resolvedPairs) { | |
360 map[pair.first] = pair.last; | |
361 } | |
362 return map; | |
363 }); | |
364 } | |
OLD | NEW |