Skip to content

Commit b5c4d8b

Browse files
committed
feat(facade): add maximum method for ListWrapper
1 parent 5c95b37 commit b5c4d8b

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

modules/angular2/src/facade/collection.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,27 @@ class ListWrapper {
217217
if (end == null) return len;
218218
return end < 0 ? max(len + end, 0) : min(end, len);
219219
}
220+
221+
222+
static maximum(List l, fn(item)) {
223+
if (l.length == 0) {
224+
return null;
225+
}
226+
var solution = null;
227+
var maxValue = double.NEGATIVE_INFINITY;
228+
for (var index = 0; index < l.length; index++) {
229+
var candidate = l[index];
230+
if (candidate == null) {
231+
continue;
232+
}
233+
var candidateValue = fn(candidate);
234+
if (candidateValue > maxValue) {
235+
solution = candidate;
236+
maxValue = candidateValue;
237+
}
238+
}
239+
return solution;
240+
}
220241
}
221242

222243
bool isListLikeIterable(obj) => obj is Iterable;

modules/angular2/src/facade/collection.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isJsObject, global, isPresent, isArray} from 'angular2/src/facade/lang';
1+
import {isJsObject, global, isPresent, isBlank, isArray} from 'angular2/src/facade/lang';
22

33
export var List = global.Array;
44
export var Map = global.Map;
@@ -266,6 +266,26 @@ export class ListWrapper {
266266
}
267267
static toString<T>(l: List<T>): string { return l.toString(); }
268268
static toJSON<T>(l: List<T>): string { return JSON.stringify(l); }
269+
270+
static maximum<T>(list: List<T>, predicate: (T) => number): T {
271+
if (list.length == 0) {
272+
return null;
273+
}
274+
var solution = null;
275+
var maxValue = -Infinity;
276+
for (var index = 0; index < list.length; index++) {
277+
var candidate = list[index];
278+
if (isBlank(candidate)) {
279+
continue;
280+
}
281+
var candidateValue = predicate(candidate);
282+
if (candidateValue > maxValue) {
283+
solution = candidate;
284+
maxValue = candidateValue;
285+
}
286+
}
287+
return solution;
288+
}
269289
}
270290

271291
export function isListLikeIterable(obj: any): boolean {

modules/angular2/test/facade/collection_spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ export function main() {
7676
it('should respect the startIndex parameter',
7777
() => { expect(ListWrapper.indexOf(l, 1, 1)).toEqual(-1); });
7878
});
79+
80+
describe('maximum', () => {
81+
it('should return the maximal element',
82+
() => { expect(ListWrapper.maximum([1, 2, 3, 4], x => x)).toEqual(4); });
83+
84+
it('should ignore null values',
85+
() => { expect(ListWrapper.maximum([null, 2, 3, null], x => x)).toEqual(3); });
86+
87+
it('should use the provided function to determine maximum',
88+
() => { expect(ListWrapper.maximum([1, 2, 3, 4], x => -x)).toEqual(1); });
89+
90+
it('should return null for an empty list',
91+
() => { expect(ListWrapper.maximum([], x => x)).toEqual(null); });
92+
});
7993
});
8094

8195
describe('StringMapWrapper', () => {

0 commit comments

Comments
 (0)