- Added the `isLeapYear` extension to the `DateTime` and `int` objects.
- Added the `FixedSizeList` class.
This commit is contained in:
2025-05-16 11:15:08 +02:00
parent bfdba4603b
commit 363fb20665
9 changed files with 188 additions and 5 deletions
+29
View File
@@ -0,0 +1,29 @@
/// Represents a `List` with a fixed capacity.
///
/// When a new element is added and the list reaches its maximum capacity,
/// the oldest element in the list is automatically removed to make space.
class FixedSizeList {
final int _capacity;
final List<String> _list;
/// Creates a [FixedSizeList] with the specified [capacity].
///
/// The initial list will be empty.
FixedSizeList(this._capacity) : _list = <String>[];
/// Adds a new [element] to the list.
///
/// If the list is already at its maximum [capacity], the oldest element
/// will be removed before the new [element] is added.
void add(String element) {
_list.add(element);
if (_list.length > _capacity) {
_list.removeAt(0);
}
}
/// Returns an unmodifiable view of the current items in the list.
///
/// This prevents external modification of the internal list.
List<String> get items => List.unmodifiable(_list);
}
+14 -2
View File
@@ -1,5 +1,3 @@
import "package:week_number/iso.dart";
/// An extension on `DateTime` to get the start and end of various time periods.
extension StartAndEndOfPeriod on DateTime {
/// Returns a `DateTime` object representing the start of the hour.
@@ -141,3 +139,17 @@ extension YesterdayAndTomorrow on DateTime {
DateTime get tomorrow =>
DateTime.now().add(const Duration(days: 1)).startOfDay;
}
extension IsLeapYear on DateTime {
/// Returns `true` if the year is a leap year, otherwise returns `false`.
bool get isLeapYear => year.isLeapYear;
}
extension IsIntLeapYear on int {
/// Returns `true` if the given value would be considered a leap year,
/// otherwise returns `false`.
bool get isLeapYear =>
!this.isNegative &&
this > 0 &&
((this * 1073750999) & 3221352463) <= 126976;
}