From a6753474e28aaca2f98c36554dcba07609c99699 Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Fri, 7 Jun 2024 15:25:52 +0200 Subject: [PATCH 1/4] Add more String extensions --- .../lib/src/extensions/string_extensions.dart | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/netglade_utils/lib/src/extensions/string_extensions.dart b/packages/netglade_utils/lib/src/extensions/string_extensions.dart index b6990bf..237f987 100644 --- a/packages/netglade_utils/lib/src/extensions/string_extensions.dart +++ b/packages/netglade_utils/lib/src/extensions/string_extensions.dart @@ -51,6 +51,31 @@ extension StringExtensions on String { return replaceAll(pattern, ''); } + + String capitalize() { + return '${this[0].toUpperCase()}${characters.getRange(1).toLowerCase()}'; + } + + String shorten(int maxLength) { + if (length < maxLength) return this; + + return '${characters.getRange(0, maxLength)}...'; + } + + String stripOuterQuotes() { + var result = this; + if (result.startsWith('"')) result = result.replaceFirst('"', ''); + + if (result.endsWith('"')) result = result.replaceRange(result.length - 1, null, ''); + + return result; + } + + String firstLetterUppercase() => '${this[0].toUpperCase()}${characters.getRange(1)}'; + + String stripNewLines([String placeholder = ' ']) { + return replaceAll(RegExp(r'\s+'), placeholder); + } } extension NullableStringExtensions on String? { From e872c92a2e4b1453fef8bc83c4379883a06f6626 Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Fri, 7 Jun 2024 19:47:16 +0200 Subject: [PATCH 2/4] Add more String extensions --- packages/netglade_utils/CHANGELOG.md | 3 +++ .../netglade_utils/lib/src/extensions/string_extensions.dart | 1 + packages/netglade_utils/pubspec.yaml | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/netglade_utils/CHANGELOG.md b/packages/netglade_utils/CHANGELOG.md index 5d04a9f..d734b30 100644 --- a/packages/netglade_utils/CHANGELOG.md +++ b/packages/netglade_utils/CHANGELOG.md @@ -1,4 +1,7 @@ +## 2.3.0 +- Add more String extensions. + ## 2.2.0 - Add `mapIndexed2` - replacement for built-in mapIndexed which uses sync* - Add `where` extension on Map type - returning new map instance. diff --git a/packages/netglade_utils/lib/src/extensions/string_extensions.dart b/packages/netglade_utils/lib/src/extensions/string_extensions.dart index 237f987..ca93cd2 100644 --- a/packages/netglade_utils/lib/src/extensions/string_extensions.dart +++ b/packages/netglade_utils/lib/src/extensions/string_extensions.dart @@ -78,6 +78,7 @@ extension StringExtensions on String { } } +// ignore: prefer-single-declaration-per-file, ok in single file. extension NullableStringExtensions on String? { /// Returns true when `this` is either `null` or `isBlank`. /// diff --git a/packages/netglade_utils/pubspec.yaml b/packages/netglade_utils/pubspec.yaml index dfc40f8..962f5f8 100644 --- a/packages/netglade_utils/pubspec.yaml +++ b/packages/netglade_utils/pubspec.yaml @@ -1,5 +1,5 @@ name: netglade_utils -version: 2.2.0 +version: 2.3.0 description: Dart utils used internally at netglade. repository: https://github.com/netglade/flutter_core/tree/main/packages/netglade_utils issue_tracker: https://github.com/netglade/flutter_core/issues From 72488281c5eaac58cb3bd9e2997a1c35fc455bfc Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Fri, 7 Jun 2024 19:53:40 +0200 Subject: [PATCH 3/4] DCM Lint --- .../netglade_utils/lib/src/extensions/string_extensions.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/netglade_utils/lib/src/extensions/string_extensions.dart b/packages/netglade_utils/lib/src/extensions/string_extensions.dart index ca93cd2..237f987 100644 --- a/packages/netglade_utils/lib/src/extensions/string_extensions.dart +++ b/packages/netglade_utils/lib/src/extensions/string_extensions.dart @@ -78,7 +78,6 @@ extension StringExtensions on String { } } -// ignore: prefer-single-declaration-per-file, ok in single file. extension NullableStringExtensions on String? { /// Returns true when `this` is either `null` or `isBlank`. /// From c3f21d3d300339fa6a56fad65fe2f9630fa18eac Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Mon, 10 Jun 2024 11:21:39 +0200 Subject: [PATCH 4/4] Add string lenght check for capitalize --- .../netglade_utils/lib/src/extensions/string_extensions.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/netglade_utils/lib/src/extensions/string_extensions.dart b/packages/netglade_utils/lib/src/extensions/string_extensions.dart index 237f987..4c72f57 100644 --- a/packages/netglade_utils/lib/src/extensions/string_extensions.dart +++ b/packages/netglade_utils/lib/src/extensions/string_extensions.dart @@ -53,6 +53,8 @@ extension StringExtensions on String { } String capitalize() { + if (length < 2) return this; + return '${this[0].toUpperCase()}${characters.getRange(1).toLowerCase()}'; }