Chromium Code Reviews| 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()) { | |
|
Bob Nystrom
2012/05/03 00:15:49
I sketched out something like this once but didn't
nweiz
2012/05/04 01:03:43
I don't follow... what would these arguments do?
Bob Nystrom
2012/05/04 17:02:06
I don't know if this is a good idea, but for examp
nweiz
2012/05/07 18:28:35
I don't think that really adds any clarity.
| |
| 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 e in list) { | |
|
Bob Nystrom
2012/05/03 00:15:49
e -> element or item
Bob Nystrom
2012/05/03 00:15:49
"e" -> element or item
nweiz
2012/05/04 01:03:43
Done.
| |
| 43 if (e is List) { | |
|
Bob Nystrom
2012/05/03 00:15:49
Iterable?
nweiz
2012/05/04 01:03:43
That's how I had it originally, but I changed it t
Bob Nystrom
2012/05/04 17:02:06
I was thinking the same thing. Type tests are alwa
| |
| 44 helper(e); | |
| 45 } else { | |
| 46 result.add(e); | |
| 47 } | |
| 48 } | |
| 49 } | |
| 50 helper(nested); | |
| 51 return result; | |
| 52 } | |
| OLD | NEW |