2023-06-10 20:13:59 +02:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
|
2023-02-04 22:42:42 +02:00
|
|
|
extension DurationExtension on String {
|
2023-03-06 17:27:01 +02:00
|
|
|
Duration? toDuration() {
|
|
|
|
try {
|
|
|
|
final parts = split(':')
|
|
|
|
.map((e) => double.parse(e).toInt())
|
|
|
|
.toList(growable: false);
|
|
|
|
return Duration(hours: parts[0], minutes: parts[1], seconds: parts[2]);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-02-04 22:42:42 +02:00
|
|
|
}
|
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
double toDouble() {
|
|
|
|
return double.parse(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
int toInt() {
|
|
|
|
return int.parse(this);
|
2023-02-04 22:42:42 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 03:50:58 +02:00
|
|
|
|
|
|
|
extension ListExtension<E> on List<E> {
|
2023-06-10 20:13:59 +02:00
|
|
|
List<E> uniqueConsecutive({
|
|
|
|
int Function(E a, E b)? compare,
|
|
|
|
void Function(E a, E b)? onDuplicate,
|
|
|
|
}) {
|
|
|
|
compare ??= (E a, E b) => a == b ? 0 : 1;
|
2023-04-15 03:50:58 +02:00
|
|
|
int i = 1, j = 1;
|
|
|
|
for (; i < length; i++) {
|
2023-06-10 20:13:59 +02:00
|
|
|
if (compare(this[i - 1], this[i]) != 0) {
|
2023-04-15 03:50:58 +02:00
|
|
|
if (i != j) {
|
|
|
|
this[j] = this[i];
|
|
|
|
}
|
|
|
|
j++;
|
2023-06-10 20:13:59 +02:00
|
|
|
} else if (onDuplicate != null) {
|
|
|
|
onDuplicate(this[i - 1], this[i]);
|
2023-04-15 03:50:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
length = length == 0 ? 0 : j;
|
|
|
|
return this;
|
|
|
|
}
|
2023-05-17 19:36:02 +02:00
|
|
|
|
|
|
|
ListSlice<E> nestedSlice(int start, int end) {
|
|
|
|
if (this is ListSlice) {
|
|
|
|
final ListSlice<E> self = this as ListSlice<E>;
|
|
|
|
return ListSlice<E>(self.source, self.start + start, self.start + end);
|
|
|
|
}
|
|
|
|
return ListSlice<E>(this, start, end);
|
|
|
|
}
|
2023-04-15 03:50:58 +02:00
|
|
|
}
|
2023-06-10 20:13:59 +02:00
|
|
|
|
|
|
|
extension IntListExtension on Iterable<int> {
|
|
|
|
Int64List toInt64List() {
|
|
|
|
final list = Int64List(length);
|
|
|
|
list.setAll(0, this);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|