| 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 * Generic utility functions. Stuff that should possibly be in core. | 6 * Generic utility functions. Stuff that should possibly be in core. |
| 7 */ | 7 */ |
| 8 #library('pub_utils'); | 8 #library('pub_utils'); |
| 9 | 9 |
| 10 // TODO(rnystrom): Move into String? | 10 // TODO(rnystrom): Move into String? |
| 11 /** Pads [source] to [length] by adding spaces at the end. */ | 11 /** Pads [source] to [length] by adding spaces at the end. */ |
| 12 String padRight(String source, int length) { | 12 String padRight(String source, int length) { |
| 13 final result = new StringBuffer(); | 13 final result = new StringBuffer(); |
| 14 result.add(source); | 14 result.add(source); |
| 15 | 15 |
| 16 while (result.length < length) { | 16 while (result.length < length) { |
| 17 result.add(' '); | 17 result.add(' '); |
| 18 } | 18 } |
| 19 | 19 |
| 20 return result.toString(); | 20 return result.toString(); |
| 21 } | 21 } |
| 22 |
| 23 /** |
| 24 * Runs [fn] after [future] completes, whether it completes successfully or not. |
| 25 * Essentially an asynchronous `finally` block. |
| 26 */ |
| 27 always(Future future, fn()) { |
| 28 var completer = new Completer(); |
| 29 future.then((_) => fn()); |
| 30 future.handleException((_) { |
| 31 fn(); |
| 32 return false; |
| 33 }); |
| 34 } |
| 35 |
| 36 /** |
| 37 * Flattens nested lists into a single list containing only non-list elements. |
| 38 */ |
| 39 List flatten(List nested) { |
| 40 var result = []; |
| 41 helper(list) { |
| 42 for (var element in list) { |
| 43 if (element is List) { |
| 44 helper(element); |
| 45 } else { |
| 46 result.add(element); |
| 47 } |
| 48 } |
| 49 } |
| 50 helper(nested); |
| 51 return result; |
| 52 } |
| OLD | NEW |