OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
8 | 8 |
9 import 'package:async/async.dart' hide StreamQueue; | 9 import 'package:async/async.dart' hide StreamQueue; |
10 import 'package:crypto/crypto.dart'; | 10 import 'package:crypto/crypto.dart'; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 String getErrorMessage(error) => | 96 String getErrorMessage(error) => |
97 error.toString().replaceFirst(_exceptionPrefix, ''); | 97 error.toString().replaceFirst(_exceptionPrefix, ''); |
98 | 98 |
99 /// Indent each line in [str] by two spaces. | 99 /// Indent each line in [str] by two spaces. |
100 String indent(String str) => | 100 String indent(String str) => |
101 str.replaceAll(new RegExp("^", multiLine: true), " "); | 101 str.replaceAll(new RegExp("^", multiLine: true), " "); |
102 | 102 |
103 /// Returns a sentence fragment listing the elements of [iter]. | 103 /// Returns a sentence fragment listing the elements of [iter]. |
104 /// | 104 /// |
105 /// This converts each element of [iter] to a string and separates them with | 105 /// This converts each element of [iter] to a string and separates them with |
106 /// commas and/or "and" where appropriate. | 106 /// commas and/or [conjunction] where appropriate. The [conjunction] defaults to |
107 String toSentence(Iterable iter) { | 107 /// "and". |
| 108 String toSentence(Iterable iter, {String conjunction}) { |
108 if (iter.length == 1) return iter.first.toString(); | 109 if (iter.length == 1) return iter.first.toString(); |
| 110 |
109 var result = iter.take(iter.length - 1).join(", "); | 111 var result = iter.take(iter.length - 1).join(", "); |
110 if (iter.length > 2) result += ","; | 112 if (iter.length > 2) result += ","; |
111 return "$result and ${iter.last}"; | 113 return "$result ${conjunction ?? 'and'} ${iter.last}"; |
112 } | 114 } |
113 | 115 |
114 /// Returns [name] if [number] is 1, or the plural of [name] otherwise. | 116 /// Returns [name] if [number] is 1, or the plural of [name] otherwise. |
115 /// | 117 /// |
116 /// By default, this just adds "s" to the end of [name] to get the plural. If | 118 /// By default, this just adds "s" to the end of [name] to get the plural. If |
117 /// [plural] is passed, that's used instead. | 119 /// [plural] is passed, that's used instead. |
118 String pluralize(String name, int number, {String plural}) { | 120 String pluralize(String name, int number, {String plural}) { |
119 if (number == 1) return name; | 121 if (number == 1) return name; |
120 if (plural != null) return plural; | 122 if (plural != null) return plural; |
121 return '${name}s'; | 123 return '${name}s'; |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 urlSafe: urlSafe, addLineSeparator: addLineSeparator); | 439 urlSafe: urlSafe, addLineSeparator: addLineSeparator); |
438 } | 440 } |
439 | 441 |
440 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. | 442 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. |
441 shelf.Middleware nestingMiddleware(String beneath) { | 443 shelf.Middleware nestingMiddleware(String beneath) { |
442 return (handler) { | 444 return (handler) { |
443 var pathHandler = new PathHandler()..add(beneath, handler); | 445 var pathHandler = new PathHandler()..add(beneath, handler); |
444 return pathHandler.handler; | 446 return pathHandler.handler; |
445 }; | 447 }; |
446 } | 448 } |
OLD | NEW |