feat(facade): add maximum method for ListWrapper

This commit is contained in:
Brian Ford
2015-08-13 11:19:37 -07:00
parent 5c95b376b5
commit b5c4d8ba79
3 changed files with 56 additions and 1 deletions
@@ -217,6 +217,27 @@ class ListWrapper {
if (end == null) return len;
return end < 0 ? max(len + end, 0) : min(end, len);
}
static maximum(List l, fn(item)) {
if (l.length == 0) {
return null;
}
var solution = null;
var maxValue = double.NEGATIVE_INFINITY;
for (var index = 0; index < l.length; index++) {
var candidate = l[index];
if (candidate == null) {
continue;
}
var candidateValue = fn(candidate);
if (candidateValue > maxValue) {
solution = candidate;
maxValue = candidateValue;
}
}
return solution;
}
}
bool isListLikeIterable(obj) => obj is Iterable;
+21 -1
View File
@@ -1,4 +1,4 @@
import {isJsObject, global, isPresent, isArray} from 'angular2/src/facade/lang';
import {isJsObject, global, isPresent, isBlank, isArray} from 'angular2/src/facade/lang';
export var List = global.Array;
export var Map = global.Map;
@@ -266,6 +266,26 @@ export class ListWrapper {
}
static toString<T>(l: List<T>): string { return l.toString(); }
static toJSON<T>(l: List<T>): string { return JSON.stringify(l); }
static maximum<T>(list: List<T>, predicate: (T) => number): T {
if (list.length == 0) {
return null;
}
var solution = null;
var maxValue = -Infinity;
for (var index = 0; index < list.length; index++) {
var candidate = list[index];
if (isBlank(candidate)) {
continue;
}
var candidateValue = predicate(candidate);
if (candidateValue > maxValue) {
solution = candidate;
maxValue = candidateValue;
}
}
return solution;
}
}
export function isListLikeIterable(obj: any): boolean {