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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 var iterator = iter.iterator; | 103 var iterator = iter.iterator; |
104 var currentIsValid = iterator.moveNext(); | 104 var currentIsValid = iterator.moveNext(); |
105 assert(currentIsValid); | 105 assert(currentIsValid); |
106 var obj = iterator.current; | 106 var obj = iterator.current; |
107 assert(!iterator.moveNext()); | 107 assert(!iterator.moveNext()); |
108 return obj; | 108 return obj; |
109 } | 109 } |
110 | 110 |
111 /// Returns a set containing all elements in [minuend] that are not in | 111 /// Returns a set containing all elements in [minuend] that are not in |
112 /// [subtrahend]. | 112 /// [subtrahend]. |
113 Set setMinus(Collection minuend, Collection subtrahend) { | 113 Set setMinus(Iterable minuend, Iterable subtrahend) { |
114 var minuendSet = new Set.from(minuend); | 114 var minuendSet = new Set.from(minuend); |
115 minuendSet.removeAll(subtrahend); | 115 minuendSet.removeAll(subtrahend); |
116 return minuendSet; | 116 return minuendSet; |
117 } | 117 } |
118 | 118 |
119 /// Replace each instance of [matcher] in [source] with the return value of | 119 /// Replace each instance of [matcher] in [source] with the return value of |
120 /// [fn]. | 120 /// [fn]. |
121 String replace(String source, Pattern matcher, String fn(Match)) { | 121 String replace(String source, Pattern matcher, String fn(Match)) { |
122 var buffer = new StringBuffer(); | 122 var buffer = new StringBuffer(); |
123 var start = 0; | 123 var start = 0; |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
326 /// Add all key/value pairs from [source] to [destination], overwriting any | 326 /// Add all key/value pairs from [source] to [destination], overwriting any |
327 /// pre-existing values. | 327 /// pre-existing values. |
328 void mapAddAll(Map destination, Map source) => | 328 void mapAddAll(Map destination, Map source) => |
329 source.forEach((key, value) => destination[key] = value); | 329 source.forEach((key, value) => destination[key] = value); |
330 | 330 |
331 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 331 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
332 /// replacing `+` with ` `. | 332 /// replacing `+` with ` `. |
333 String urlDecode(String encoded) => | 333 String urlDecode(String encoded) => |
334 decodeUriComponent(encoded.replaceAll("+", " ")); | 334 decodeUriComponent(encoded.replaceAll("+", " ")); |
335 | 335 |
336 /// Takes a simple data structure (composed of [Map]s, [List]s, scalar objects, | 336 /// Takes a simple data structure (composed of [Map]s, [List]s, scalar objects, |
nweiz
2013/04/11 20:36:19
"[List]s" -> "[Iterable]s"
Anders Johnsen
2013/04/12 09:31:14
Done.
| |
337 /// and [Future]s) and recursively resolves all the [Future]s contained within. | 337 /// and [Future]s) and recursively resolves all the [Future]s contained within. |
338 /// Completes with the fully resolved structure. | 338 /// Completes with the fully resolved structure. |
339 Future awaitObject(object) { | 339 Future awaitObject(object) { |
340 // Unroll nested futures. | 340 // Unroll nested futures. |
341 if (object is Future) return object.then(awaitObject); | 341 if (object is Future) return object.then(awaitObject); |
342 if (object is Collection) { | 342 if (object is List || object is Set) { |
nweiz
2013/04/11 20:36:19
I think this should be "object is Iterable".
Anders Johnsen
2013/04/12 09:31:14
Done.
| |
343 return Future.wait(object.map(awaitObject).toList()); | 343 return Future.wait(object.map(awaitObject).toList()); |
344 } | 344 } |
345 if (object is! Map) return new Future.immediate(object); | 345 if (object is! Map) return new Future.immediate(object); |
346 | 346 |
347 var pairs = <Future<Pair>>[]; | 347 var pairs = <Future<Pair>>[]; |
348 object.forEach((key, value) { | 348 object.forEach((key, value) { |
349 pairs.add(awaitObject(value) | 349 pairs.add(awaitObject(value) |
350 .then((resolved) => new Pair(key, resolved))); | 350 .then((resolved) => new Pair(key, resolved))); |
351 }); | 351 }); |
352 return Future.wait(pairs).then((resolvedPairs) { | 352 return Future.wait(pairs).then((resolvedPairs) { |
353 var map = {}; | 353 var map = {}; |
354 for (var pair in resolvedPairs) { | 354 for (var pair in resolvedPairs) { |
355 map[pair.first] = pair.last; | 355 map[pair.first] = pair.last; |
356 } | 356 } |
357 return map; | 357 return map; |
358 }); | 358 }); |
359 } | 359 } |
OLD | NEW |