LCOV - code coverage report
Current view: top level - extensions - extensions.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 34 34 100.0 %
Date: 2022-07-13 09:29:13 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:developer' as devtools;
       2             : 
       3             : import 'package:basf_flutter_components/basf_flutter_components.dart';
       4             : import 'package:flutter/material.dart';
       5             : 
       6             : /// A collection of usefull extensions on [String]
       7             : extension StringCasingExtension on String {
       8             :   /// Converts the first letter of a [String] into uppercase
       9             :   /// or returns '' if null
      10             :   ///
      11             :   /// Example:
      12             :   /// ```dart
      13             :   /// 'carlos gutiérrez'.toCapitalized(); // Carlos gutiérrez
      14             :   /// ```
      15           1 :   String toCapitalized() =>
      16           7 :       length > 0 ? '${this[0].toUpperCase()}${substring(1).toLowerCase()}' : '';
      17             : 
      18             :   /// Converts all the first letters of each words of a given [String] into
      19             :   /// uppercase
      20             :   ///
      21             :   /// Example:
      22             :   /// ```dart
      23             :   /// 'carlos gutiérrez'.toTitleCase(); // Carlos Gutiérrez
      24             :   /// ```
      25             :   /// See also:
      26             :   ///
      27             :   ///  * [toCapitalized]
      28           3 :   String toTitleCase() => replaceAll(RegExp(' +'), ' ')
      29           1 :       .split(' ')
      30           3 :       .map((str) => str.toCapitalized())
      31           1 :       .join(' ');
      32             : }
      33             : 
      34             : /// A collection of usefull extensions on [List<Widget>]
      35             : extension JoinedWidgets on List<Widget> {
      36             :   /// Adds a specific type of [Widget] in between a list of Widgets
      37             :   /// This can be usefull to add some height in between Widgets
      38             :   /// without the need of writing it multiple times
      39             :   ///
      40             :   /// Example:
      41             :   /// ```dart
      42             :   /// [
      43             :   ///  const Text('BASF'),
      44             :   ///  const Text('MOBILE SOLUTIONS'),
      45             :   /// ].joinWithSeparator(const SizedBox(height: 10));
      46             :   /// ```
      47           1 :   List<Widget> joinWithSeparator(Widget separator) {
      48           2 :     return length > 1
      49           3 :         ? (take(length - 1)
      50           3 :             .map((widget) => [widget, separator])
      51           2 :             .expand((widget) => widget)
      52           1 :             .toList()
      53           2 :           ..add(last))
      54             :         : this;
      55             :   }
      56             : 
      57             :   /// Adds the same ammount of padding to a list of Widgets
      58             :   /// This can be usefull if you need to have some of the widgets of a list
      59             :   /// spaced, and some don't
      60             :   ///
      61             :   /// Example:
      62             :   /// ```dart
      63             :   /// [
      64             :   ///   const Text('BASF'),
      65             :   ///   const Text('MOBILE SOLUTIONS'),
      66             :   /// ].spaced();
      67             :   /// // or
      68             :   /// ].spaced(padding: const EdgeInsets.all(10));
      69             :   /// ```
      70           1 :   List<Widget> spaced({EdgeInsetsGeometry? padding, bool excludeFlex = true}) {
      71           1 :     final spacedWidgets = <Widget>[];
      72           2 :     for (final w in this) {
      73           3 :       if (excludeFlex && (w is Expanded || w is Spacer || w is Flexible)) {
      74           1 :         spacedWidgets.add(w);
      75             :       } else {
      76           1 :         spacedWidgets.add(
      77           1 :           Padding(
      78             :             padding: padding ??
      79             :                 const EdgeInsets.fromLTRB(
      80             :                   Dimens.paddingMediumLarge,
      81             :                   0,
      82             :                   Dimens.paddingMediumLarge,
      83             :                   0,
      84             :                 ),
      85             :             child: w,
      86             :           ),
      87             :         );
      88             :       }
      89             :     }
      90             :     return spacedWidgets;
      91             :   }
      92             : }
      93             : 
      94             : /// {@template log_extension}
      95             : /// Emit a log event of the current object
      96             : /// {@endtemplate}
      97             : extension Log on Object {
      98             :   /// {@macro log_extension}
      99           1 :   int log() {
     100           1 :     final str = toString();
     101           1 :     devtools.log(str);
     102             : 
     103             :     // returns the length of the ouput
     104           1 :     return str.length;
     105             :   }
     106             : }
     107             : 
     108             : /// {@template map_extension}
     109             : /// Allows us to find an entry
     110             : /// {@endtemplate}
     111             : extension DetailedWhere<K, V> on Map<K, V> {
     112             :   /// {@macro map_extension}
     113             :   /// based on key and value
     114             :   ///
     115             :   /// Example:
     116             :   /// ```dart
     117             :   /// people.where((key, value) => key.length > 4 && value > 20);
     118             :   /// // {Peter: 22}
     119             :   ///
     120             :   /// const Map<String, int> people = {'John': 20, 'Mary': 21, 'Peter':20};
     121             :   /// ```
     122           2 :   Map<K, V> where(bool Function(K key, V value) f) => Map<K, V>.fromEntries(
     123           6 :         entries.where((entry) => f(entry.key, entry.value)),
     124             :       );
     125             : 
     126             :   /// {@macro map_extension}
     127             :   /// based on key
     128             :   ///
     129             :   /// Example:
     130             :   /// ```dart
     131             :   /// people.whereKey((key) => key.length < 5);
     132             :   /// // {John: 20, Mary: 21}
     133             :   ///
     134             :   /// const Map<String, int> people = {'John': 20, 'Mary': 21, 'Peter':20};
     135             :   /// ```
     136           1 :   Map<K, V> whereKey(bool Function(K key) f) =>
     137           4 :       {...where((key, value) => f(key))};
     138             : 
     139             :   /// {@macro map_extension}
     140             :   /// based on  value
     141             :   ///
     142             :   /// Example:
     143             :   /// ```dart
     144             :   /// people.whereValue((value) => value.isEven);
     145             :   /// // {John: 20, Peter: 22}
     146             :   ///
     147             :   /// const Map<String, int> people = {'John': 20, 'Mary': 21, 'Peter':20};
     148             :   /// ```
     149           1 :   Map<K, V> whereValue(bool Function(V value) f) =>
     150           4 :       {...where((key, value) => f(value))};
     151             : }
     152             : 
     153             : /// {@template show_app_snackbar}
     154             : /// Shows an [AppSnackBar]
     155             : /// {@endtemplate}
     156             : extension SnackbarActions on AppSnackBar {
     157             :   /// {@macro show_app_snackbar}
     158           1 :   void show(BuildContext context) {
     159           2 :     ScaffoldMessenger.of(context).showSnackBar(
     160           2 :       SnackBar(backgroundColor: backgroundColor, content: this),
     161             :     );
     162             :   }
     163             : }
     164             : 
     165             : /// {@template theme_extension}
     166             : /// Allows access to the current theme data
     167             : /// {@endtemplate}
     168             : extension ThemeExtension on BuildContext {
     169             :   /// {@macro theme_extension}
     170             :   /// Example:
     171             :   /// ```dart
     172             :   /// final theme = context.theme; // Access current theme data
     173             :   /// ```
     174           2 :   ThemeData get theme => Theme.of(this);
     175             : }

Generated by: LCOV version 1.16