| 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:crypto'; | 8 import 'dart:crypto'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
| 11 import 'dart:uri'; | |
| 12 import 'dart:utf'; | 11 import 'dart:utf'; |
| 13 | 12 |
| 14 import 'byte_stream.dart'; | 13 import 'byte_stream.dart'; |
| 15 | 14 |
| 16 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) | 15 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) |
| 17 /// into a [Map] from parameter names to values. | 16 /// into a [Map] from parameter names to values. |
| 18 /// | 17 /// |
| 19 /// queryToMap("foo=bar&baz=bang&qux"); | 18 /// queryToMap("foo=bar&baz=bang&qux"); |
| 20 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} | 19 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} |
| 21 Map<String, String> queryToMap(String queryList) { | 20 Map<String, String> queryToMap(String queryList) { |
| 22 var map = {}; | 21 var map = {}; |
| 23 for (var pair in queryList.split("&")) { | 22 for (var pair in queryList.split("&")) { |
| 24 var split = split1(pair, "="); | 23 var split = split1(pair, "="); |
| 25 if (split.isEmpty) continue; | 24 if (split.isEmpty) continue; |
| 26 var key = urlDecode(split[0]); | 25 var key = urlDecode(split[0]); |
| 27 var value = urlDecode(split.length > 1 ? split[1] : ""); | 26 var value = urlDecode(split.length > 1 ? split[1] : ""); |
| 28 map[key] = value; | 27 map[key] = value; |
| 29 } | 28 } |
| 30 return map; | 29 return map; |
| 31 } | 30 } |
| 32 | 31 |
| 33 /// 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. |
| 34 /// | 33 /// |
| 35 /// mapToQuery({"foo": "bar", "baz": "bang"}); | 34 /// mapToQuery({"foo": "bar", "baz": "bang"}); |
| 36 /// //=> "foo=bar&baz=bang" | 35 /// //=> "foo=bar&baz=bang" |
| 37 String mapToQuery(Map<String, String> map) { | 36 String mapToQuery(Map<String, String> map) { |
| 38 var pairs = <List<String>>[]; | 37 var pairs = <List<String>>[]; |
| 39 map.forEach((key, value) => | 38 map.forEach((key, value) => |
| 40 pairs.add([encodeUriComponent(key), encodeUriComponent(value)])); | 39 pairs.add([Uri.encodeQueryComponent(key), |
| 40 Uri.encodeQueryComponent(value)])); |
| 41 return pairs.map((pair) => "${pair[0]}=${pair[1]}").join("&"); | 41 return pairs.map((pair) => "${pair[0]}=${pair[1]}").join("&"); |
| 42 } | 42 } |
| 43 | 43 |
| 44 /// Adds all key/value pairs from [source] to [destination], overwriting any | 44 /// Adds all key/value pairs from [source] to [destination], overwriting any |
| 45 /// pre-existing values. | 45 /// pre-existing values. |
| 46 /// | 46 /// |
| 47 /// var a = {"foo": "bar", "baz": "bang"}; | 47 /// var a = {"foo": "bar", "baz": "bang"}; |
| 48 /// mapAddAll(a, {"baz": "zap", "qux": "quux"}); | 48 /// mapAddAll(a, {"baz": "zap", "qux": "quux"}); |
| 49 /// a; //=> {"foo": "bar", "baz": "zap", "qux": "quux"} | 49 /// a; //=> {"foo": "bar", "baz": "zap", "qux": "quux"} |
| 50 void mapAddAll(Map destination, Map source) => | 50 void mapAddAll(Map destination, Map source) => |
| 51 source.forEach((key, value) => destination[key] = value); | 51 source.forEach((key, value) => destination[key] = value); |
| 52 | 52 |
| 53 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 53 /// Decodes a URL-encoded string. Unlike [Uri.decodeComponent], this includes |
| 54 /// replacing `+` with ` `. | 54 /// replacing `+` with ` `. |
| 55 String urlDecode(String encoded) => | 55 String urlDecode(String encoded) => |
| 56 decodeUriComponent(encoded.replaceAll("+", " ")); | 56 Uri.decodeComponent(encoded.replaceAll("+", " ")); |
| 57 | 57 |
| 58 /// Like [String.split], but only splits on the first occurrence of the pattern. | 58 /// Like [String.split], but only splits on the first occurrence of the pattern. |
| 59 /// This will always return an array of two elements or fewer. | 59 /// This will always return an array of two elements or fewer. |
| 60 /// | 60 /// |
| 61 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"] | 61 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"] |
| 62 /// split1("foo", ","); //=> ["foo"] | 62 /// split1("foo", ","); //=> ["foo"] |
| 63 /// split1("", ","); //=> [] | 63 /// split1("", ","); //=> [] |
| 64 List<String> split1(String toSplit, String pattern) { | 64 List<String> split1(String toSplit, String pattern) { |
| 65 if (toSplit.isEmpty) return <String>[]; | 65 if (toSplit.isEmpty) return <String>[]; |
| 66 | 66 |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 /// The return values of all [Future]s are discarded. Any errors will cause the | 232 /// The return values of all [Future]s are discarded. Any errors will cause the |
| 233 /// iteration to stop and will be piped through the return value. | 233 /// iteration to stop and will be piped through the return value. |
| 234 Future forEachFuture(Iterable input, Future fn(element)) { | 234 Future forEachFuture(Iterable input, Future fn(element)) { |
| 235 var iterator = input.iterator; | 235 var iterator = input.iterator; |
| 236 Future nextElement(_) { | 236 Future nextElement(_) { |
| 237 if (!iterator.moveNext()) return new Future.value(); | 237 if (!iterator.moveNext()) return new Future.value(); |
| 238 return fn(iterator.current).then(nextElement); | 238 return fn(iterator.current).then(nextElement); |
| 239 } | 239 } |
| 240 return nextElement(null); | 240 return nextElement(null); |
| 241 } | 241 } |
| OLD | NEW |