| 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 library utils; | 5 library utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
| 11 | 11 |
| 12 import 'byte_stream.dart'; | 12 import 'byte_stream.dart'; |
| 13 | 13 |
| 14 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) | 14 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) |
| 15 /// into a [Map] from parameter names to values. | 15 /// into a [Map] from parameter names to values. |
| 16 /// | 16 /// |
| 17 /// queryToMap("foo=bar&baz=bang&qux"); | 17 /// queryToMap("foo=bar&baz=bang&qux"); |
| 18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} | 18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} |
| 19 Map<String, String> queryToMap(String queryList, {Encoding encoding}) { | 19 Map<String, String> queryToMap(String queryList, {Encoding encoding}) { |
| 20 var map = {}; | 20 var map = {}; |
| 21 for (var pair in queryList.split("&")) { | 21 for (var pair in queryList.split("&")) { |
| 22 var split = split1(pair, "="); | 22 var split = split1(pair, "="); |
| 23 if (split.isEmpty) continue; | 23 if (split.isEmpty) continue; |
| 24 var key = Uri.decodeQueryComponent(split[0], decode: encoding.decode); | 24 var key = Uri.decodeQueryComponent(split[0], encoding: encoding); |
| 25 var value = Uri.decodeQueryComponent(split.length > 1 ? split[1] : "", | 25 var value = Uri.decodeQueryComponent(split.length > 1 ? split[1] : "", |
| 26 decode: encoding.decode); | 26 encoding: encoding); |
| 27 map[key] = value; | 27 map[key] = value; |
| 28 } | 28 } |
| 29 return map; | 29 return map; |
| 30 } | 30 } |
| 31 | 31 |
| 32 /// Converts a [Map] from parameter names to values to a URL query string. | 32 /// Converts a [Map] from parameter names to values to a URL query string. |
| 33 /// | 33 /// |
| 34 /// mapToQuery({"foo": "bar", "baz": "bang"}); | 34 /// mapToQuery({"foo": "bar", "baz": "bang"}); |
| 35 /// //=> "foo=bar&baz=bang" | 35 /// //=> "foo=bar&baz=bang" |
| 36 String mapToQuery(Map<String, String> map, {Encoding encoding}) { | 36 String mapToQuery(Map<String, String> map, {Encoding encoding}) { |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 /// The return values of all [Future]s are discarded. Any errors will cause the | 230 /// The return values of all [Future]s are discarded. Any errors will cause the |
| 231 /// iteration to stop and will be piped through the return value. | 231 /// iteration to stop and will be piped through the return value. |
| 232 Future forEachFuture(Iterable input, Future fn(element)) { | 232 Future forEachFuture(Iterable input, Future fn(element)) { |
| 233 var iterator = input.iterator; | 233 var iterator = input.iterator; |
| 234 Future nextElement(_) { | 234 Future nextElement(_) { |
| 235 if (!iterator.moveNext()) return new Future.value(); | 235 if (!iterator.moveNext()) return new Future.value(); |
| 236 return fn(iterator.current).then(nextElement); | 236 return fn(iterator.current).then(nextElement); |
| 237 } | 237 } |
| 238 return nextElement(null); | 238 return nextElement(null); |
| 239 } | 239 } |
| OLD | NEW |