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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library pub.utils; | 6 library pub.utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import "dart:convert"; | 9 import "dart:convert"; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 final result = new StringBuffer(); | 160 final result = new StringBuffer(); |
161 result.write(source); | 161 result.write(source); |
162 | 162 |
163 while (result.length < length) { | 163 while (result.length < length) { |
164 result.write(' '); | 164 result.write(' '); |
165 } | 165 } |
166 | 166 |
167 return result.toString(); | 167 return result.toString(); |
168 } | 168 } |
169 | 169 |
170 /// Returns a labelled sentence fragment starting with [name] listing the | |
171 /// elements [iter]. | |
172 /// | |
173 /// If [iter] does not have one item, name will be pluralized by adding "s" or | |
174 /// using [plural], if given. | |
175 String namedSequence(String name, Iterable iter, [String plural]) { | |
176 if (iter.length == 1) return "$name ${iter.first}"; | |
nweiz
2014/09/15 22:57:18
Nit: I like [iter.single] here.
Bob Nystrom
2014/09/17 21:34:35
Done.
| |
177 | |
178 var sequence = iter.take(iter.length - 1).join(", ") + " and ${iter.last}"; | |
nweiz
2014/09/15 22:57:18
Use [toSentence] here.
Bob Nystrom
2014/09/17 21:34:35
Done.
| |
179 if (plural == null) plural = "${name}s"; | |
180 return "$plural $sequence"; | |
181 } | |
182 | |
170 /// Returns a sentence fragment listing the elements of [iter]. | 183 /// Returns a sentence fragment listing the elements of [iter]. |
171 /// | 184 /// |
172 /// This converts each element of [iter] to a string and separates them with | 185 /// This converts each element of [iter] to a string and separates them with |
173 /// commas and/or "and" where appropriate. | 186 /// commas and/or "and" where appropriate. |
174 String toSentence(Iterable iter) { | 187 String toSentence(Iterable iter) { |
175 if (iter.length == 1) return iter.first.toString(); | 188 if (iter.length == 1) return iter.first.toString(); |
176 return iter.take(iter.length - 1).join(", ") + " and ${iter.last}"; | 189 return iter.take(iter.length - 1).join(", ") + " and ${iter.last}"; |
177 } | 190 } |
178 | 191 |
179 /// Returns [name] if [number] is 1, or the plural of [name] otherwise. | 192 /// Returns [name] if [number] is 1, or the plural of [name] otherwise. |
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
877 } else { | 890 } else { |
878 throw new ApplicationException(message); | 891 throw new ApplicationException(message); |
879 } | 892 } |
880 } | 893 } |
881 | 894 |
882 /// Throw a [DataException] with [message] to indicate that the command has | 895 /// Throw a [DataException] with [message] to indicate that the command has |
883 /// failed because of invalid input data. | 896 /// failed because of invalid input data. |
884 /// | 897 /// |
885 /// This will report the error and cause pub to exit with [exit_codes.DATA]. | 898 /// This will report the error and cause pub to exit with [exit_codes.DATA]. |
886 void dataError(String message) => throw new DataException(message); | 899 void dataError(String message) => throw new DataException(message); |
OLD | NEW |