From 1f0e18b5555b74de4f8c1a5252cfa209d56a2d9d Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Fri, 15 Feb 2019 00:11:42 +0100 Subject: [PATCH] Add KtList.takeLast(n) --- .../extension/list_extension_mixin.dart | 19 ++++++++++ lib/src/collection/kt_list.dart | 5 +++ test/collection/iterable_extensions_test.dart | 7 +++- test/collection/list_extensions_test.dart | 35 +++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/src/collection/extension/list_extension_mixin.dart b/lib/src/collection/extension/list_extension_mixin.dart index 51e5251e..9ff482af 100644 --- a/lib/src/collection/extension/list_extension_mixin.dart +++ b/lib/src/collection/extension/list_extension_mixin.dart @@ -244,4 +244,23 @@ abstract class KtListExtensionsMixin } return list; } + + @override + KtList 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(); + for (var i = size - n; i < size; i++) { + list.add(get(i)); + } + return list; + } } diff --git a/lib/src/collection/kt_list.dart b/lib/src/collection/kt_list.dart index 3d0bf9b3..efc82a0e 100644 --- a/lib/src/collection/kt_list.dart +++ b/lib/src/collection/kt_list.dart @@ -202,4 +202,9 @@ abstract class KtListExtension { * Returns a list containing elements at specified [indices]. */ KtList slice(KtIterable indices); + + /** + * Returns a list containing last [n] elements. + */ + KtList takeLast(int n); } diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart index 0d0a67ee..1e9cb666 100644 --- a/test/collection/iterable_extensions_test.dart +++ b/test/collection/iterable_extensions_test.dart @@ -1312,6 +1312,11 @@ void testIterable(KtIterable Function() emptyIterable, expect(() => emptyIterable().last((it) => it == "x"), throwsA(TypeMatcher())); }); + + 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", () { @@ -1985,7 +1990,7 @@ void testIterable(KtIterable Function() 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(() => iterable.take(-3)); expect(e.message, allOf(contains("-3"), contains("less than zero"))); diff --git a/test/collection/list_extensions_test.dart b/test/collection/list_extensions_test.dart index 2060ae45..5f10fb1d 100644 --- a/test/collection/list_extensions_test.dart +++ b/test/collection/list_extensions_test.dart @@ -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(() => 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(); + var e = catchException(() => 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([null])); + expect(list.takeLast(2).toList(), listFrom([1, null])); + }); + }); }