Skip to content

Commit

Permalink
Merge pull request #73 from passsy/feature/take_last
Browse files Browse the repository at this point in the history
Add KtList.takeLast(n)
  • Loading branch information
passsy authored Feb 14, 2019
2 parents a9d7bed + 1f0e18b commit b327237
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/src/collection/extension/list_extension_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,23 @@ abstract class KtListExtensionsMixin<T>
}
return list;
}

@override
KtList<T> takeLast(int n) {
assert(() {
if (n == null) throw ArgumentError("n can't be null");
return true;
}());
if (n < 0) {
throw ArgumentError("Requested element count $n is less than zero.");
}
if (n == 0) return emptyList();
if (n >= size) return toList();
if (n == 1) return listFrom([last()]);
final list = mutableListOf<T>();
for (var i = size - n; i < size; i++) {
list.add(get(i));
}
return list;
}
}
5 changes: 5 additions & 0 deletions lib/src/collection/kt_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,9 @@ abstract class KtListExtension<T> {
* Returns a list containing elements at specified [indices].
*/
KtList<T> slice(KtIterable<int> indices);

/**
* Returns a list containing last [n] elements.
*/
KtList<T> takeLast(int n);
}
7 changes: 6 additions & 1 deletion test/collection/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,11 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
expect(() => emptyIterable().last((it) => it == "x"),
throwsA(TypeMatcher<NoSuchElementException>()));
});

test("returns null when null is the last element", () {
expect(listFrom([1, 2, null]).last(), null);
expect(listFrom([1, null, 2]).last(), 2);
});
});

group("lastOrNull", () {
Expand Down Expand Up @@ -1985,7 +1990,7 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
expect(iterable.take(0).toList(), emptyList());
});

test("take negative returns empty", () {
test("take negative throws", () {
final iterable = iterableOf([1, 2, 3, 4]);
final e = catchException<ArgumentError>(() => iterable.take(-3));
expect(e.message, allOf(contains("-3"), contains("less than zero")));
Expand Down
35 changes: 35 additions & 0 deletions test/collection/list_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,39 @@ void testList(
expect(e.message, allOf(contains("size: 3"), contains("index: -1")));
});
});

group("takeLast", () {
test("takeLast zero returns empty", () {
final list = listOf(1, 2, 3, 4);
expect(list.takeLast(0).toList(), emptyList());
});

test("take negative throws", () {
final list = listOf(1, 2, 3, 4);
final e = catchException<ArgumentError>(() => list.takeLast(-3));
expect(e.message, allOf(contains("-3"), contains("less than zero")));
});

test("take more than size returns full list", () {
final list = listOf(1, 2, 3, 4);
expect(list.takeLast(10).toList(), list.toList());
});

test("take smaller list size returns last elements", () {
final list = listOf(1, 2, 3, 4);
expect(list.takeLast(2).toList(), listOf(3, 4));
});

test("takeLast doesn't allow null as n", () {
final list = emptyList<num>();
var e = catchException<ArgumentError>(() => list.takeLast(null));
expect(e.message, allOf(contains("null"), contains("n")));
});

test("take last element which is null", () {
final list = listFrom([1, null]);
expect(list.takeLast(1).toList(), listFrom<int>([null]));
expect(list.takeLast(2).toList(), listFrom([1, null]));
});
});
}

0 comments on commit b327237

Please sign in to comment.