mirror of
https://github.com/hanskokx/arcane_helper_utils.git
synced 2026-05-14 10:29:07 +02:00
v1.2.2
- Added additional `String` manipulation utilities, including: - `capitalizeWords`: Capitalizes the first letter of each word in the string. - `spacePascalCase`: Adds spaces between words in a PascalCase string. Signed-off-by: Hans Kokx <hans.kokx@hackberry.se>
This commit is contained in:
@@ -328,6 +328,20 @@ objects:
|
|||||||
final String capitalized = text.capitalize; // "Hello"
|
final String capitalized = text.capitalize; // "Hello"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- `capitalizeWords`: Capitalizes the first letter of each word in a given `String`
|
||||||
|
|
||||||
|
```dart
|
||||||
|
String text = "hello world";
|
||||||
|
String capitalizedWords = text.capitalizeWords; // "Hello World"
|
||||||
|
```
|
||||||
|
|
||||||
|
- `spacePascalCase`: Adds spaces between words in a PascalCase `String`
|
||||||
|
|
||||||
|
```dart
|
||||||
|
String text = "ArcaneHelperUtils";
|
||||||
|
String spaced = text.spacePascalCase; // "Arcane Helper Utils"
|
||||||
|
```
|
||||||
|
|
||||||
Additionally, the `CommonString` class provides a quick shortcut to common
|
Additionally, the `CommonString` class provides a quick shortcut to common
|
||||||
strings, such as punctuation marks that are otherwise cumbersome to find or type.
|
strings, such as punctuation marks that are otherwise cumbersome to find or type.
|
||||||
|
|
||||||
|
|||||||
+38
-10
@@ -3,7 +3,7 @@
|
|||||||
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// DateTime
|
// * DateTime
|
||||||
final DateTime dateTime = DateTime(2019, 1, 1, 13, 45);
|
final DateTime dateTime = DateTime(2019, 1, 1, 13, 45);
|
||||||
print(dateTime.toIso8601String()); // 2019-01-01T13:45:00.0
|
print(dateTime.toIso8601String()); // 2019-01-01T13:45:00.0
|
||||||
print(dateTime.startOfHour.toIso8601String()); // 2019-01-01T13:00:00.0
|
print(dateTime.startOfHour.toIso8601String()); // 2019-01-01T13:00:00.0
|
||||||
@@ -19,31 +19,59 @@ void main() {
|
|||||||
print("Yesterday: $yesterday");
|
print("Yesterday: $yesterday");
|
||||||
print("Tomorrow: $tomorrow");
|
print("Tomorrow: $tomorrow");
|
||||||
|
|
||||||
// Strings
|
// * Strings
|
||||||
const String? nullString = null;
|
const String? nullString = null;
|
||||||
const String emptyString = " ";
|
const String emptyString = " ";
|
||||||
const String nonEmptyString = "Hello World!";
|
const String nonEmptyString = "Hello World!";
|
||||||
|
|
||||||
|
// Nullability
|
||||||
print("${nullString.isNullOrEmpty}"); // true
|
print("${nullString.isNullOrEmpty}"); // true
|
||||||
print("${emptyString.isNullOrEmpty}"); // true
|
print("${emptyString.isNullOrEmpty}"); // true
|
||||||
print("${nonEmptyString.isNullOrEmpty}"); // false
|
print("${nonEmptyString.isNullOrEmpty}"); // false
|
||||||
|
|
||||||
|
// Split a string by x characters
|
||||||
const String text = "DartLang";
|
const String text = "DartLang";
|
||||||
final List<String> result = text.splitByLength(3);
|
final List<String> result = text.splitByLength(3);
|
||||||
print("$result"); // ["Dar", "tLa", "ng"]
|
print("$result"); // ["Dar", "tLa", "ng"]
|
||||||
|
|
||||||
|
// Capitalize a string
|
||||||
const String lowercase = "hello";
|
const String lowercase = "hello";
|
||||||
final String capitalized = lowercase.capitalize;
|
final String capitalized = lowercase.capitalize;
|
||||||
print(capitalized); // "Hello"
|
print(capitalized); // "Hello"
|
||||||
|
|
||||||
final list = [1, 2, 2, 3, 4, 4];
|
// Capitalize words in a string
|
||||||
final uniqueList = list.unique();
|
const String lowercaseWords = "hello world";
|
||||||
print(uniqueList); // Output: [1, 2, 3, 4]
|
final String capitalizedWords = lowercaseWords.capitalizeWords;
|
||||||
final people = [
|
print(capitalizedWords); // "Hello World"
|
||||||
Person(id: 1, name: "Alice"),
|
|
||||||
Person(id: 2, name: "Bob"),
|
// Space out PascalCase words
|
||||||
Person(id: 1, name: "Alice Duplicate"),
|
const String pascalCase = "ArcaneHelperUtils";
|
||||||
|
final String spacedOut = pascalCase.spacePascalCase;
|
||||||
|
print(spacedOut); // "Arcane Helper Utils";
|
||||||
|
|
||||||
|
// * Lists
|
||||||
|
final List<Person> people = [
|
||||||
|
const Person(id: 1, name: "Alice"),
|
||||||
|
const Person(id: 2, name: "Bob"),
|
||||||
|
const Person(id: 1, name: "Alice Duplicate"),
|
||||||
];
|
];
|
||||||
final uniquePeople = people.unique((person) => person.id);
|
|
||||||
|
// Process the list into a new list item. Original list is preserved.
|
||||||
|
final List<Person> uniquePeople = people.unique((person) => person.id, false);
|
||||||
|
print(
|
||||||
|
people.map((p) => p.name),
|
||||||
|
// Output: ['Alice', 'Bob', 'Alice Duplicate']
|
||||||
|
);
|
||||||
print(uniquePeople.map((p) => p.name)); // Output: ['Alice', 'Bob']
|
print(uniquePeople.map((p) => p.name)); // Output: ['Alice', 'Bob']
|
||||||
|
|
||||||
|
// Process the existing list, in place
|
||||||
|
people.unique((person) => person.id);
|
||||||
|
print(people.map((p) => p.name)); // Output: ['Alice', 'Bob']
|
||||||
|
}
|
||||||
|
|
||||||
|
class Person {
|
||||||
|
final int id;
|
||||||
|
final String name;
|
||||||
|
|
||||||
|
const Person({required this.id, required this.name});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,4 +91,46 @@ extension TextManipulation on String {
|
|||||||
if (isEmpty) return "";
|
if (isEmpty) return "";
|
||||||
return "${this[0].toUpperCase()}${substring(1)}";
|
return "${this[0].toUpperCase()}${substring(1)}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Capitalizes the first letter of each word in the string.
|
||||||
|
///
|
||||||
|
/// This method returns a new string where the first character of each word
|
||||||
|
/// is converted to uppercase, while the rest of the string remains unchanged.
|
||||||
|
///
|
||||||
|
/// Example:
|
||||||
|
/// ```dart
|
||||||
|
/// String text = "hello world";
|
||||||
|
/// String capitalizedWords = text.capitalizeWords; // "Hello World"
|
||||||
|
/// ```
|
||||||
|
String get capitalizeWords {
|
||||||
|
final strings = split(" ");
|
||||||
|
return strings.map((s) => s.capitalize).join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds spaces between words in a PascalCase string.
|
||||||
|
///
|
||||||
|
/// This method returns a new string where the capitalized words in a
|
||||||
|
/// PascalCase string are separated by spaces. The rest of the string remains
|
||||||
|
/// unchanged, aside from whitespace at the beginning and the end of the
|
||||||
|
/// string being stripped.
|
||||||
|
///
|
||||||
|
/// Example:
|
||||||
|
/// ```dart
|
||||||
|
/// String text = "ArcaneHelperUtils";
|
||||||
|
/// String spaced = text.spacePascalCase; // "Arcane Helper Utils"
|
||||||
|
/// ```
|
||||||
|
String get spacePascalCase {
|
||||||
|
final List<String> strings = split(
|
||||||
|
"",
|
||||||
|
);
|
||||||
|
String output = "";
|
||||||
|
for (final String char in strings) {
|
||||||
|
if (RegExp(r"[A-ZÄÖÅ]").hasMatch(char)) {
|
||||||
|
output += " $char";
|
||||||
|
} else {
|
||||||
|
output += char;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output.trim();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user