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:io'; | 8 import 'dart:io'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 import 'dart:utf'; | 10 import 'dart:utf'; |
(...skipping 24 matching lines...) Expand all Loading... |
35 /// mapToQuery({"foo": "bar", "baz": "bang"}); | 35 /// mapToQuery({"foo": "bar", "baz": "bang"}); |
36 /// //=> "foo=bar&baz=bang" | 36 /// //=> "foo=bar&baz=bang" |
37 String mapToQuery(Map<String, String> map) { | 37 String mapToQuery(Map<String, String> map) { |
38 var pairs = <List<String>>[]; | 38 var pairs = <List<String>>[]; |
39 map.forEach((key, value) => | 39 map.forEach((key, value) => |
40 pairs.add([Uri.encodeQueryComponent(key), | 40 pairs.add([Uri.encodeQueryComponent(key), |
41 Uri.encodeQueryComponent(value)])); | 41 Uri.encodeQueryComponent(value)])); |
42 return pairs.map((pair) => "${pair[0]}=${pair[1]}").join("&"); | 42 return pairs.map((pair) => "${pair[0]}=${pair[1]}").join("&"); |
43 } | 43 } |
44 | 44 |
45 /// Adds all key/value pairs from [source] to [destination], overwriting any | |
46 /// pre-existing values. | |
47 /// | |
48 /// var a = {"foo": "bar", "baz": "bang"}; | |
49 /// mapAddAll(a, {"baz": "zap", "qux": "quux"}); | |
50 /// a; //=> {"foo": "bar", "baz": "zap", "qux": "quux"} | |
51 void mapAddAll(Map destination, Map source) => | |
52 source.forEach((key, value) => destination[key] = value); | |
53 | |
54 /// Decodes a URL-encoded string. Unlike [Uri.decodeComponent], this includes | 45 /// Decodes a URL-encoded string. Unlike [Uri.decodeComponent], this includes |
55 /// replacing `+` with ` `. | 46 /// replacing `+` with ` `. |
56 String urlDecode(String encoded) => | 47 String urlDecode(String encoded) => |
57 Uri.decodeComponent(encoded.replaceAll("+", " ")); | 48 Uri.decodeComponent(encoded.replaceAll("+", " ")); |
58 | 49 |
59 /// Like [String.split], but only splits on the first occurrence of the pattern. | 50 /// Like [String.split], but only splits on the first occurrence of the pattern. |
60 /// This will always return an array of two elements or fewer. | 51 /// This will always return an array of two elements or fewer. |
61 /// | 52 /// |
62 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"] | 53 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"] |
63 /// split1("foo", ","); //=> ["foo"] | 54 /// split1("foo", ","); //=> ["foo"] |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 /// The return values of all [Future]s are discarded. Any errors will cause the | 227 /// The return values of all [Future]s are discarded. Any errors will cause the |
237 /// iteration to stop and will be piped through the return value. | 228 /// iteration to stop and will be piped through the return value. |
238 Future forEachFuture(Iterable input, Future fn(element)) { | 229 Future forEachFuture(Iterable input, Future fn(element)) { |
239 var iterator = input.iterator; | 230 var iterator = input.iterator; |
240 Future nextElement(_) { | 231 Future nextElement(_) { |
241 if (!iterator.moveNext()) return new Future.value(); | 232 if (!iterator.moveNext()) return new Future.value(); |
242 return fn(iterator.current).then(nextElement); | 233 return fn(iterator.current).then(nextElement); |
243 } | 234 } |
244 return nextElement(null); | 235 return nextElement(null); |
245 } | 236 } |
OLD | NEW |