Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(facade): add maximum method for ListWrapper
  • Loading branch information
btford committed Aug 17, 2015
commit 5c89ef4980378f9fd1cd5880cc08e4ae961f36b1
21 changes: 21 additions & 0 deletions modules/angular2/src/facade/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 21 additions & 1 deletion modules/angular2/src/facade/collection.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions modules/angular2/test/facade/collection_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ export function main() {
it('should respect the startIndex parameter',
() => { expect(ListWrapper.indexOf(l, 1, 1)).toEqual(-1); });
});

describe('maximum', () => {
it('should return the maximal element',
() => { expect(ListWrapper.maximum([1, 2, 3, 4], x => x)).toEqual(4); });

it('should ignore null values',
() => { expect(ListWrapper.maximum([null, 2, 3, null], x => x)).toEqual(3); });

it('should use the provided function to determine maximum',
() => { expect(ListWrapper.maximum([1, 2, 3, 4], x => -x)).toEqual(1); });

it('should return null for an empty list',
() => { expect(ListWrapper.maximum([], x => x)).toEqual(null); });
});
});

describe('StringMapWrapper', () => {
Expand Down