| 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'; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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}) { |
| 37 var pairs = <List<String>>[]; | 37 var pairs = <List<String>>[]; |
| 38 map.forEach((key, value) => | 38 map.forEach((key, value) => |
| 39 pairs.add([urlEncode(key, encoding: encoding), | 39 pairs.add([Uri.encodeQueryComponent(key, encoding: encoding), |
| 40 urlEncode(value, encoding: encoding)])); | 40 Uri.encodeQueryComponent(value, encoding: encoding)])); |
| 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 // TODO(nweiz): get rid of this when issue 12780 is fixed. | |
| 45 /// URL-encodes [source] using [encoding]. | |
| 46 String urlEncode(String source, {Encoding encoding}) { | |
| 47 if (encoding == null) encoding = UTF8; | |
| 48 return encoding.encode(source).map((byte) { | |
| 49 // Convert spaces to +, like encodeQueryComponent. | |
| 50 if (byte == 0x20) return '+'; | |
| 51 // Pass through digits. | |
| 52 if ((byte >= 0x30 && byte < 0x3A) || | |
| 53 // Pass through uppercase letters. | |
| 54 (byte >= 0x41 && byte < 0x5B) || | |
| 55 // Pass through lowercase letters. | |
| 56 (byte >= 0x61 && byte < 0x7B) || | |
| 57 // Pass through `-._~`. | |
| 58 (byte == 0x2D || byte == 0x2E || byte == 0x5F || byte == 0x7E)) { | |
| 59 return new String.fromCharCode(byte); | |
| 60 } | |
| 61 return '%' + byte.toRadixString(16).toUpperCase(); | |
| 62 }).join(); | |
| 63 } | |
| 64 | |
| 65 /// Like [String.split], but only splits on the first occurrence of the pattern. | 44 /// Like [String.split], but only splits on the first occurrence of the pattern. |
| 66 /// This will always return an array of two elements or fewer. | 45 /// This will always return an array of two elements or fewer. |
| 67 /// | 46 /// |
| 68 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"] | 47 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"] |
| 69 /// split1("foo", ","); //=> ["foo"] | 48 /// split1("foo", ","); //=> ["foo"] |
| 70 /// split1("", ","); //=> [] | 49 /// split1("", ","); //=> [] |
| 71 List<String> split1(String toSplit, String pattern) { | 50 List<String> split1(String toSplit, String pattern) { |
| 72 if (toSplit.isEmpty) return <String>[]; | 51 if (toSplit.isEmpty) return <String>[]; |
| 73 | 52 |
| 74 var index = toSplit.indexOf(pattern); | 53 var index = toSplit.indexOf(pattern); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 /// The return values of all [Future]s are discarded. Any errors will cause the | 206 /// The return values of all [Future]s are discarded. Any errors will cause the |
| 228 /// iteration to stop and will be piped through the return value. | 207 /// iteration to stop and will be piped through the return value. |
| 229 Future forEachFuture(Iterable input, Future fn(element)) { | 208 Future forEachFuture(Iterable input, Future fn(element)) { |
| 230 var iterator = input.iterator; | 209 var iterator = input.iterator; |
| 231 Future nextElement(_) { | 210 Future nextElement(_) { |
| 232 if (!iterator.moveNext()) return new Future.value(); | 211 if (!iterator.moveNext()) return new Future.value(); |
| 233 return fn(iterator.current).then(nextElement); | 212 return fn(iterator.current).then(nextElement); |
| 234 } | 213 } |
| 235 return nextElement(null); | 214 return nextElement(null); |
| 236 } | 215 } |
| OLD | NEW |