From b066eace19c4540f8a6cae7dc9dfd4b85e15ddb5 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Thu, 16 Jan 2025 11:07:19 +0100 Subject: [PATCH] v1.2.6 - Added `printValue()` method as an extension to `dynamic` Signed-off-by: Hans Kokx --- CHANGELOG.md | 16 +++++++++++++ example/lib/main.dart | 15 ++++++++++--- lib/arcane_helper_utils.dart | 1 + lib/src/extensions/dynamic.dart | 40 +++++++++++++++++++++++++++++++++ pubspec.yaml | 2 +- 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 lib/src/extensions/dynamic.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 65f12fc..ee8fa62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +## 1.2.6 + +- Added the `printValue()` extension. + The `printValue()` extension can be used to print a value to the console + before returning that same value. + + Example: + + ```dart + // Print the `textTheme` object to the console before returning it + Text( + 'Hello, world', + style: Theme.of(context).textTheme.printValue().headlineMedium, + ), + ``` + ## 1.2.5 - Null `String`s being manipulated should return `null` instead of an empty `String` diff --git a/example/lib/main.dart b/example/lib/main.dart index 552d940..7c994ee 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -36,17 +36,17 @@ void main() { // Capitalize a string const String lowercase = "hello"; - final String capitalized = lowercase.capitalize; + final String capitalized = lowercase.capitalize!; print(capitalized); // "Hello" // Capitalize words in a string const String lowercaseWords = "hello world"; - final String capitalizedWords = lowercaseWords.capitalizeWords; + final String capitalizedWords = lowercaseWords.capitalizeWords!; print(capitalizedWords); // "Hello World" // Space out PascalCase words const String pascalCase = "ArcaneHelperUtils"; - final String spacedOut = pascalCase.spacePascalCase; + final String spacedOut = pascalCase.spacePascalCase!; print(spacedOut); // "Arcane Helper Utils"; // * Lists @@ -67,6 +67,15 @@ void main() { // Process the existing list, in place people.unique((person) => person.id); print(people.map((p) => p.name)); // Output: ['Alice', 'Bob'] + + // * Dynamic debug printing + // Debug print the `Person` object before returning the name + final String alice = const Person(id: 0, name: "Alice").printValue().name; + print(alice); // Output: 'Alice' + + // Debug print the `Person` object with a label before returning the name + final String bob = const Person(id: 1, name: "Bob").printValue("Person").name; + print(bob); // Output: 'Bob' } class Person { diff --git a/lib/arcane_helper_utils.dart b/lib/arcane_helper_utils.dart index 19110ad..1ea013b 100644 --- a/lib/arcane_helper_utils.dart +++ b/lib/arcane_helper_utils.dart @@ -1,6 +1,7 @@ library arcane_helper_utils; export "package:arcane_helper_utils/src/extensions/date_time.dart"; +export "package:arcane_helper_utils/src/extensions/dynamic.dart"; export "package:arcane_helper_utils/src/extensions/list.dart"; export "package:arcane_helper_utils/src/extensions/string.dart"; export "package:arcane_helper_utils/src/extensions/string_jwt.dart"; diff --git a/lib/src/extensions/dynamic.dart b/lib/src/extensions/dynamic.dart new file mode 100644 index 0000000..3f0449a --- /dev/null +++ b/lib/src/extensions/dynamic.dart @@ -0,0 +1,40 @@ +// ignore_for_file: avoid_print + +extension DynamicPrintExtension on dynamic { + /// The `printValue()` extension can be used to print a value to the + /// console before returning that same value. + /// + /// Example: + /// ```dart + /// Text( + /// 'Hello, world', + /// style: Theme.of(context).textTheme.printValue().headlineMedium, + /// ), + /// ``` + /// + /// This will print the text style to the console before returning it, which + /// can be useful for debugging. + /// + /// Additionally, an optional label can be specified for the printed value, which + /// will be prepended to the output. + /// ```dart + /// Text( + /// 'Hello, world', + /// style: Theme.of(context).textTheme.printValue('headlineMedium'), + /// ), + /// ``` + @Deprecated( + "WARNING: The printValue() extension can potentially leak sensitive " + "information.\n" + "It is recommended to use only during debugging and to remove before " + "releasing to production.", + ) + T printValue([String label = ""]) { + if (label.isNotEmpty) { + print("$label: ${toString()}"); + } else { + print("${toString()}"); + } + return this as T; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 619e845..ceebb86 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: arcane_helper_utils description: Provides a variety of helpful utilities and extensions for Flutter and Dart. -version: 1.2.5 +version: 1.2.6 repository: https://github.com/hanskokx/arcane_helper_utils issue_tracker: https://github.com/hanskokx/arcane_helper_utils/issues