diff --git a/lib/open_exts.dart b/lib/open_exts.dart index 7eb4c4c..8d5c283 100644 --- a/lib/open_exts.dart +++ b/lib/open_exts.dart @@ -1,7 +1,6 @@ library open_exts; -/// A Calculator. -class Calculator { - /// Returns [value] plus 1. - int addOne(int value) => value + 1; -} +export 'src/color_extensions.dart'; +export 'src/datetime_extensions.dart'; +export 'src/duration_extensions.dart'; +export 'src/string_extensions.dart'; diff --git a/lib/src/color_extensions.dart b/lib/src/color_extensions.dart new file mode 100644 index 0000000..ceaa6c2 --- /dev/null +++ b/lib/src/color_extensions.dart @@ -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')}'; +} diff --git a/lib/src/datetime_extensions.dart b/lib/src/datetime_extensions.dart new file mode 100644 index 0000000..cffdb91 --- /dev/null +++ b/lib/src/datetime_extensions.dart @@ -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; + } +} diff --git a/lib/src/duration_extensions.dart b/lib/src/duration_extensions.dart new file mode 100644 index 0000000..a85dfc9 --- /dev/null +++ b/lib/src/duration_extensions.dart @@ -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')}"; + } +} diff --git a/lib/src/string_extensions.dart b/lib/src/string_extensions.dart new file mode 100644 index 0000000..f6ffd3f --- /dev/null +++ b/lib/src/string_extensions.dart @@ -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 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; +} diff --git a/pubspec.yaml b/pubspec.yaml index 85e0a8b..6905778 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,9 @@ name: open_exts description: "Extensions. Its that simple..." version: 0.0.1 -homepage: +homepage: http://reinemuth.rocks:3000/jens/open_exts +publish_to: + environment: sdk: ^3.5.4 diff --git a/test/open_exts_test.dart b/test/open_exts_test.dart deleted file mode 100644 index d960d9e..0000000 --- a/test/open_exts_test.dart +++ /dev/null @@ -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); - }); -}