first extension packages
This commit is contained in:
parent
4c0848d1d9
commit
b982cfe7fd
7 changed files with 120 additions and 18 deletions
|
@ -1,7 +1,6 @@
|
||||||
library open_exts;
|
library open_exts;
|
||||||
|
|
||||||
/// A Calculator.
|
export 'src/color_extensions.dart';
|
||||||
class Calculator {
|
export 'src/datetime_extensions.dart';
|
||||||
/// Returns [value] plus 1.
|
export 'src/duration_extensions.dart';
|
||||||
int addOne(int value) => value + 1;
|
export 'src/string_extensions.dart';
|
||||||
}
|
|
||||||
|
|
16
lib/src/color_extensions.dart
Normal file
16
lib/src/color_extensions.dart
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
extension OpenColorExtensions on Color {
|
||||||
|
Color fromHex(String hexString) {
|
||||||
|
final buffer = StringBuffer();
|
||||||
|
if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
|
||||||
|
buffer.write(hexString.replaceFirst('#', ''));
|
||||||
|
return Color(int.parse(buffer.toString(), radix: 16));
|
||||||
|
}
|
||||||
|
|
||||||
|
String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
|
||||||
|
'${alpha.toRadixString(16).padLeft(2, '0')}'
|
||||||
|
'${red.toRadixString(16).padLeft(2, '0')}'
|
||||||
|
'${green.toRadixString(16).padLeft(2, '0')}'
|
||||||
|
'${blue.toRadixString(16).padLeft(2, '0')}';
|
||||||
|
}
|
50
lib/src/datetime_extensions.dart
Normal file
50
lib/src/datetime_extensions.dart
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
extension OpenDatetimeExtensions on DateTime {
|
||||||
|
static DateTime get _now => DateTime.now();
|
||||||
|
|
||||||
|
String nowString() => _now.toUtc().toIso8601String();
|
||||||
|
|
||||||
|
bool isToday() => _isSameDay(this, _now);
|
||||||
|
|
||||||
|
bool isYesterday() =>
|
||||||
|
_isSameDay(this, _now.subtract(const Duration(days: 1)));
|
||||||
|
|
||||||
|
bool isTomorrow() => _isSameDay(this, _now.add(const Duration(days: 1)));
|
||||||
|
|
||||||
|
bool isMorning() => hour >= 4 && hour < 12;
|
||||||
|
bool isAfternoon() => hour >= 12 && hour < 18;
|
||||||
|
bool isEvening() => hour < 4 || hour >= 18;
|
||||||
|
|
||||||
|
String getTimeOfDay() {
|
||||||
|
if (isMorning()) return 'morning';
|
||||||
|
if (isAfternoon()) return 'afternoon';
|
||||||
|
return 'evening';
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime findFirstDateOfTheWeek() => subtract(Duration(days: weekday - 1));
|
||||||
|
|
||||||
|
DateTime findLastDateOfTheWeek() =>
|
||||||
|
add(Duration(days: DateTime.daysPerWeek - weekday));
|
||||||
|
|
||||||
|
String formatSeconds(int value) {
|
||||||
|
final hours = (value ~/ 3600).toString().padLeft(2, '0');
|
||||||
|
final minutes = ((value % 3600) ~/ 60).toString().padLeft(2, '0');
|
||||||
|
final seconds = (value % 60).toString().padLeft(2, '0');
|
||||||
|
return "$hours:$minutes:$seconds";
|
||||||
|
}
|
||||||
|
|
||||||
|
String formatMinutes(int value, {bool allowZero = false}) {
|
||||||
|
final hours = (value ~/ 60).toString().padLeft(2, '0');
|
||||||
|
var minutes = (value % 60).toString().padLeft(2, '0');
|
||||||
|
if (minutes == '00' && !allowZero) minutes = '01';
|
||||||
|
return "$hours:$minutes";
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime get lastDayOfMonth =>
|
||||||
|
month < 12 ? DateTime(year, month + 1, 0) : DateTime(year + 1, 1, 0);
|
||||||
|
|
||||||
|
static bool _isSameDay(DateTime date1, DateTime date2) {
|
||||||
|
return date1.year == date2.year &&
|
||||||
|
date1.month == date2.month &&
|
||||||
|
date1.day == date2.day;
|
||||||
|
}
|
||||||
|
}
|
11
lib/src/duration_extensions.dart
Normal file
11
lib/src/duration_extensions.dart
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
extension OpenDurationExtensions on Duration {
|
||||||
|
/// Converts the duration into a readable string in the format "HH:MM"
|
||||||
|
String toHoursMinutes() {
|
||||||
|
return "${inHours.toString().padLeft(2, '0')}:${inMinutes.remainder(60).toString().padLeft(2, '0')}";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts the duration into a readable string in the format "HH:MM:SS"
|
||||||
|
String toHoursMinutesSeconds() {
|
||||||
|
return "${toHoursMinutes()}:${inSeconds.remainder(60).toString().padLeft(2, '0')}";
|
||||||
|
}
|
||||||
|
}
|
36
lib/src/string_extensions.dart
Normal file
36
lib/src/string_extensions.dart
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
extension StringExtension on String {
|
||||||
|
String operator -(String rhs) => replaceAll(rhs, '');
|
||||||
|
|
||||||
|
String truncateTo(int maxLength) =>
|
||||||
|
(length <= maxLength) ? this : '${substring(0, maxLength - 3)}...';
|
||||||
|
|
||||||
|
String shortenByWords(int maxLength) =>
|
||||||
|
shortenByDelimiterWithCount(maxLength);
|
||||||
|
|
||||||
|
String shortenByDelimiterWithCount(int maxLength, [String delimiter = ' ']) {
|
||||||
|
if (length <= maxLength) return this;
|
||||||
|
|
||||||
|
int endIndex = substring(0, maxLength).lastIndexOf(delimiter);
|
||||||
|
if (endIndex == -1) return substring(0, maxLength);
|
||||||
|
|
||||||
|
String shortened = substring(0, endIndex);
|
||||||
|
int remainingWordsCount =
|
||||||
|
split(delimiter).length - shortened.split(delimiter).length;
|
||||||
|
return remainingWordsCount > 0
|
||||||
|
? '$shortened +$remainingWordsCount'
|
||||||
|
: shortened;
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterable<String> splitByLength(int len, {String filler = '0'}) sync* {
|
||||||
|
String paddedString = padRight((length + len - 1) ~/ len * len, filler);
|
||||||
|
for (var i = 0; i < paddedString.length; i += len) {
|
||||||
|
yield paddedString.substring(i, i + len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String capitalize() =>
|
||||||
|
length > 0 ? substring(0, 1).toUpperCase() + substring(1) : this;
|
||||||
|
|
||||||
|
String decapitalize() =>
|
||||||
|
length > 0 ? substring(0, 1).toLowerCase() + substring(1) : this;
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
name: open_exts
|
name: open_exts
|
||||||
description: "Extensions. Its that simple..."
|
description: "Extensions. Its that simple..."
|
||||||
version: 0.0.1
|
version: 0.0.1
|
||||||
homepage:
|
homepage: http://reinemuth.rocks:3000/jens/open_exts
|
||||||
|
publish_to:
|
||||||
|
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.5.4
|
sdk: ^3.5.4
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
|
|
||||||
import 'package:open_exts/open_exts.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
test('adds one to input values', () {
|
|
||||||
final calculator = Calculator();
|
|
||||||
expect(calculator.addOne(2), 3);
|
|
||||||
expect(calculator.addOne(-7), -6);
|
|
||||||
expect(calculator.addOne(0), 1);
|
|
||||||
});
|
|
||||||
}
|
|
Loading…
Reference in a new issue