Line data Source code
1 : // Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file 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. 4 : 5 : /// Returns [name] if [number] is 1, or the plural of [name] otherwise. 6 : /// 7 : /// By default, this just adds "s" to the end of [name] to get the plural. If 8 : /// [plural] is passed, that's used instead. 9 0 : String pluralize(String name, int number, {String? plural}) { 10 0 : if (number == 1) return name; 11 : if (plural != null) return plural; 12 0 : return '${name}s'; 13 : } 14 : 15 : /// Returns a sentence fragment listing the elements of [iter]. 16 : /// 17 : /// This converts each element of [iter] to a string and separates them with 18 : /// commas and/or [conjunction] where appropriate. The [conjunction] defaults to 19 : /// "and". 20 0 : String toSentence(Iterable iter, {String conjunction = 'and'}) { 21 0 : if (iter.length == 1) return iter.first.toString(); 22 : 23 0 : var result = iter.take(iter.length - 1).join(', '); 24 0 : if (iter.length > 2) result += ','; 25 0 : return '$result $conjunction ${iter.last}'; 26 : } 27 : 28 : /// Returns a human-friendly representation of [duration]. 29 0 : String niceDuration(Duration duration) { 30 0 : var minutes = duration.inMinutes; 31 0 : var seconds = duration.inSeconds % 60; 32 0 : var decaseconds = (duration.inMilliseconds % 1000) ~/ 100; 33 : 34 0 : var buffer = StringBuffer(); 35 0 : if (minutes != 0) buffer.write('$minutes minutes'); 36 : 37 0 : if (minutes == 0 || seconds != 0) { 38 0 : if (minutes != 0) buffer.write(', '); 39 0 : buffer.write(seconds); 40 0 : if (decaseconds != 0) buffer.write('.$decaseconds'); 41 0 : buffer.write(' seconds'); 42 : } 43 : 44 0 : return buffer.toString(); 45 : }