| OLD | NEW |
| (Empty) |
| 1 part of angular.formatter_internal; | |
| 2 | |
| 3 /** | |
| 4 * Creates a new List or String containing only a prefix/suffix of the | |
| 5 * elements as specified by the `limit` parameter. | |
| 6 * | |
| 7 * When operating on a List, the returned list is always a copy even when all | |
| 8 * the elements are being returned. | |
| 9 * | |
| 10 * When the `limit` expression evaluates to a positive integer, `limit` items | |
| 11 * from the beginning of the List/String are returned. When `limit` evaluates | |
| 12 * to a negative integer, `|limit|` items from the end of the List/String are | |
| 13 * returned. If `|limit|` is larger than the size of the List/String, then the | |
| 14 * entire List/String is returned. In the case of a List, a copy of the list is | |
| 15 * returned. | |
| 16 * | |
| 17 * If the `limit` expression evaluates to a null or non-integer, then an empty | |
| 18 * list is returned. If the input is a null List/String, a null is returned. | |
| 19 * | |
| 20 * Example: | |
| 21 * | |
| 22 * - `{{ 'abcdefghij' | limitTo: 4 }}` → `'abcd'` | |
| 23 * - `{{ 'abcdefghij' | limitTo: -4 }}` → `'ghij'` | |
| 24 * - `{{ 'abcdefghij' | limitTo: -100 }}` → `'abcdefghij'` | |
| 25 * | |
| 26 * <br> | |
| 27 * | |
| 28 * This [ng-repeat] directive: | |
| 29 * | |
| 30 * <li ng-repeat="i in 'abcdefghij' | limitTo:-2">{{i}}</li> | |
| 31 * | |
| 32 * results in | |
| 33 * | |
| 34 * <li>i</li> | |
| 35 * <li>j</li> | |
| 36 */ | |
| 37 @Formatter(name:'limitTo') | |
| 38 class LimitTo implements Function { | |
| 39 Injector _injector; | |
| 40 | |
| 41 LimitTo(this._injector); | |
| 42 | |
| 43 dynamic call(dynamic items, [int limit]) { | |
| 44 if (items == null) return null; | |
| 45 if (limit == null) return const[]; | |
| 46 if (items is! List && items is! String) return items; | |
| 47 int i = 0, j = items.length; | |
| 48 if (limit > -1) { | |
| 49 j = (limit > j) ? j : limit; | |
| 50 } else { | |
| 51 i = j + limit; | |
| 52 if (i < 0) i = 0; | |
| 53 } | |
| 54 return items is String ? | |
| 55 (items as String).substring(i, j) : | |
| 56 (items as List).getRange(i, j).toList(growable: false); | |
| 57 } | |
| 58 } | |
| OLD | NEW |