| 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 /** | 5 /** |
| 6 * This contains extra functions and classes useful for implementing | 6 * This contains extra functions and classes useful for implementing |
| 7 * serialiation. Some or all of these will be removed once the functionality is | 7 * serialiation. Some or all of these will be removed once the functionality is |
| 8 * available in the core library. | 8 * available in the core library. |
| 9 */ | 9 */ |
| 10 library serialization_helpers; | 10 library serialization_helpers; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * A named function of one argument that just returns it. Useful for using | 13 * A named function of one argument that just returns it. Useful for using |
| 14 * as a default value for a function parameter or other places where you want | 14 * as a default value for a function parameter or other places where you want |
| 15 * to concisely provide a function that just returns its argument. | 15 * to concisely provide a function that just returns its argument. |
| 16 */ | 16 */ |
| 17 doNothing(x) => x; | 17 doNothing(x) => x; |
| 18 | 18 |
| 19 /** Concatenate two lists. Handle the case where one or both might be null. */ | 19 /** Concatenate two lists. Handle the case where one or both might be null. */ |
| 20 // TODO(alanknight): Remove once issue 5342 is resolved. | 20 // TODO(alanknight): Remove once issue 5342 is resolved. |
| 21 Iterable append(Iterable a, Iterable b) { | 21 Iterable append(Iterable a, Iterable b) { |
| 22 if (a == null) { | 22 if (a == null) { |
| 23 return (b == null) ? [] : new List.from(b, growable: true); | 23 return (b == null) ? [] : new List.from(b); |
| 24 } | 24 } |
| 25 if (b == null) return new List.from(a, growable: true); | 25 if (b == null) return new List.from(a); |
| 26 var result = new List.from(a, growable: true); | 26 var result = new List.from(a); |
| 27 result.addAll(b); | 27 result.addAll(b); |
| 28 return result; | 28 return result; |
| 29 } | 29 } |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * Return a sorted version of [anIterable], using the default sort criterion. | 32 * Return a sorted version of [anIterable], using the default sort criterion. |
| 33 * Always returns a List, regardless of the type of [anIterable]. | 33 * Always returns a List, regardless of the type of [anIterable]. |
| 34 */ | 34 */ |
| 35 List sorted(anIterable) { | 35 List sorted(anIterable) { |
| 36 var result = new List.from(anIterable); | 36 var result = new List.from(anIterable); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 * for the moment. | 125 * for the moment. |
| 126 */ | 126 */ |
| 127 values(x) { | 127 values(x) { |
| 128 if (x is Iterable) return x; | 128 if (x is Iterable) return x; |
| 129 if (x is Map) return new ListLikeIterable(x); | 129 if (x is Map) return new ListLikeIterable(x); |
| 130 throw new ArgumentError("Invalid argument"); | 130 throw new ArgumentError("Invalid argument"); |
| 131 } | 131 } |
| 132 | 132 |
| 133 mapValues(x, f) { | 133 mapValues(x, f) { |
| 134 if (x is Set) return x.map(f).toSet(); | 134 if (x is Set) return x.map(f).toSet(); |
| 135 if (x is Iterable) return x.map(f).toList(growable: true); | 135 if (x is Iterable) return x.map(f).toList(); |
| 136 if (x is Map) return new ListLikeIterable(x).map(f); | 136 if (x is Map) return new ListLikeIterable(x).map(f); |
| 137 throw new ArgumentError("Invalid argument"); | 137 throw new ArgumentError("Invalid argument"); |
| 138 } | 138 } |
| 139 | 139 |
| 140 /** | 140 /** |
| 141 * A class for iterating over things as if they were Lists, which primarily | 141 * A class for iterating over things as if they were Lists, which primarily |
| 142 * means that forEach passes one argument, and map() returns a new Map | 142 * means that forEach passes one argument, and map() returns a new Map |
| 143 * with the same keys as [collection] and values which haev been transformed | 143 * with the same keys as [collection] and values which haev been transformed |
| 144 * by the argument to map(). | 144 * by the argument to map(). |
| 145 */ | 145 */ |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 int get length => keys.length; | 245 int get length => keys.length; |
| 246 void clear() { | 246 void clear() { |
| 247 keys.clear(); | 247 keys.clear(); |
| 248 values.clear(); | 248 values.clear(); |
| 249 } | 249 } |
| 250 bool get isEmpty => keys.isEmpty; | 250 bool get isEmpty => keys.isEmpty; |
| 251 | 251 |
| 252 // Note that this is doing an equality comparison. | 252 // Note that this is doing an equality comparison. |
| 253 bool containsValue(x) => values.contains(x); | 253 bool containsValue(x) => values.contains(x); |
| 254 } | 254 } |
| OLD | NEW |