OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 library json_rpc_2.utils; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 |
| 11 typedef ZeroArgumentFunction(); |
| 12 |
| 13 /// Like [new Future.sync], but automatically wraps the future in a |
| 14 /// [Chain.track] call. |
| 15 Future syncFuture(callback()) => Chain.track(new Future.sync(callback)); |
| 16 |
| 17 /// Returns a sentence fragment listing the elements of [iter]. |
| 18 /// |
| 19 /// This converts each element of [iter] to a string and separates them with |
| 20 /// commas and/or "and" where appropriate. |
| 21 String toSentence(Iterable iter) { |
| 22 if (iter.length == 1) return iter.first.toString(); |
| 23 return iter.take(iter.length - 1).join(", ") + " and ${iter.last}"; |
| 24 } |
| 25 |
| 26 /// Returns [name] if [number] is 1, or the plural of [name] otherwise. |
| 27 /// |
| 28 /// By default, this just adds "s" to the end of [name] to get the plural. If |
| 29 /// [plural] is passed, that's used instead. |
| 30 String pluralize(String name, int number, {String plural}) { |
| 31 if (number == 1) return name; |
| 32 if (plural != null) return plural; |
| 33 return '${name}s'; |
| 34 } |
| 35 |
| 36 /// A regular expression to match the exception prefix that some exceptions' |
| 37 /// [Object.toString] values contain. |
| 38 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); |
| 39 |
| 40 /// Get a string description of an exception. |
| 41 /// |
| 42 /// Many exceptions include the exception class name at the beginning of their |
| 43 /// [toString], so we remove that if it exists. |
| 44 String getErrorMessage(error) => |
| 45 error.toString().replaceFirst(_exceptionPrefix, ''); |
OLD | NEW |