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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 /// This is also used to wrap synchronous code that may thrown an exception to | 151 /// This is also used to wrap synchronous code that may thrown an exception to |
152 /// ensure that methods that have both sync and async code only report errors | 152 /// ensure that methods that have both sync and async code only report errors |
153 /// asynchronously. | 153 /// asynchronously. |
154 Future defer(callback()) { | 154 Future defer(callback()) { |
155 return new Future.immediate(null).then((_) => callback()); | 155 return new Future.immediate(null).then((_) => callback()); |
156 } | 156 } |
157 | 157 |
158 /// Returns a [Future] that completes in [milliseconds]. | 158 /// Returns a [Future] that completes in [milliseconds]. |
159 Future sleep(int milliseconds) { | 159 Future sleep(int milliseconds) { |
160 var completer = new Completer(); | 160 var completer = new Completer(); |
161 new Timer(new Duration(milliseconds: milliseconds), completer.complete); | 161 new Timer(new Duration(milliseconds: milliseconds), |
| 162 (_) => completer.complete()); |
162 return completer.future; | 163 return completer.future; |
163 } | 164 } |
164 | 165 |
165 /// Configures [future] so that its result (success or exception) is passed on | 166 /// Configures [future] so that its result (success or exception) is passed on |
166 /// to [completer]. | 167 /// to [completer]. |
167 void chainToCompleter(Future future, Completer completer) { | 168 void chainToCompleter(Future future, Completer completer) { |
168 future.then((value) => completer.complete(value), | 169 future.then((value) => completer.complete(value), |
169 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 170 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
170 } | 171 } |
171 | 172 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 } | 320 } |
320 | 321 |
321 /// Convert a [Map] from parameter names to values to a URL query string. | 322 /// Convert a [Map] from parameter names to values to a URL query string. |
322 String mapToQuery(Map<String, String> map) { | 323 String mapToQuery(Map<String, String> map) { |
323 var pairs = <List<String>>[]; | 324 var pairs = <List<String>>[]; |
324 map.forEach((key, value) { | 325 map.forEach((key, value) { |
325 key = encodeUriComponent(key); | 326 key = encodeUriComponent(key); |
326 value = (value == null || value.isEmpty) ? null : encodeUriComponent(value); | 327 value = (value == null || value.isEmpty) ? null : encodeUriComponent(value); |
327 pairs.add([key, value]); | 328 pairs.add([key, value]); |
328 }); | 329 }); |
329 return Strings.join(pairs.map((pair) { | 330 return pairs.map((pair) { |
330 if (pair[1] == null) return pair[0]; | 331 if (pair[1] == null) return pair[0]; |
331 return "${pair[0]}=${pair[1]}"; | 332 return "${pair[0]}=${pair[1]}"; |
332 }), "&"); | 333 }).join("&"); |
333 } | 334 } |
334 | 335 |
335 /// Add all key/value pairs from [source] to [destination], overwriting any | 336 /// Add all key/value pairs from [source] to [destination], overwriting any |
336 /// pre-existing values. | 337 /// pre-existing values. |
337 void mapAddAll(Map destination, Map source) => | 338 void mapAddAll(Map destination, Map source) => |
338 source.forEach((key, value) => destination[key] = value); | 339 source.forEach((key, value) => destination[key] = value); |
339 | 340 |
340 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 341 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
341 /// replacing `+` with ` `. | 342 /// replacing `+` with ` `. |
342 String urlDecode(String encoded) => | 343 String urlDecode(String encoded) => |
343 decodeUriComponent(encoded.replaceAll("+", " ")); | 344 decodeUriComponent(encoded.replaceAll("+", " ")); |
OLD | NEW |