Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds string extension #3

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## 1.0.0

- Initial version.
- Frist release.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024 Warren Rhodes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
68 changes: 50 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,67 @@ Import the package in your Dart code:
import 'package:dart_utils/dart_utils.dart';
```

### Examples
## Features

### List Extensions
- **Operator***: Returns a new list that contains this list repeated [n] times.
- **Operator+**: Concatenate two list.
- **count**: Count the occurrences of an element in the list.
- **sum**: Calculate the sum of elements in the list.
- **average**: Calculate the average of elements in the list.
- **median**: Calculates the median of all elements in the list.
- **min**: Find the minimum value in the list.
- **max**: Find the maximum value in the list.

### String Extensions
- **capitalize**: Capitalize the first character of the string.
- **center**: Center the string within a specified width.
- **count**: Count the number of occurrences of a substring.
- **isnumeric**: Check if all characters in the string are numeric.
- **ljust**: Left-justify the string within a specified width.
- **rjust**: Right-justify the string within a specified width.
- **isValidEmail**: hecks if the current string is a valid email address.
- **isAlphanumeric**: Checks if the current string is alphanumeric.
- **isBlank**: Checks if the current string is blank.
- **reverse**: Reverses the order of the characters in the string.
- **toCamelCase**: Converts a string to camel case.
- **toKebabCase**: Converts a string to kebab case.
- **isLow**: Whether the string is lowercase case or not.
- **isUpper**: Whether the string is uppercase case or not.
- **strip**: Returns a new string with leading and trailing characters removed.
- **title**: Returns a new string with the first letter of each word capitalized and all other letters lowercased.
- **charToUpper**: Returns a new string with the character at the specified [index] capitalized.

#### Repeat data of list
### Examples

```dart
import 'package:dart_utils/dart_utils.dart';

void main() {
List<int> list1 = [1] * 3;
print(list); // Output: [1, 1, 1]
}
```
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

#### Count Occurrences
print(numbers.max); // Output: 10
print(numbers.min); // Output: 1

```dart
void main() {
List<dynamic> listOfList = [[1,2], [1,2], [1,4], [9,4], 2];
print(listOfList.count([1,2])); // Output: 2
}
```
List repeatedList = [54] * 3;
print(repeatedList); // Output: [54, 54, 54]

#### Concat two list
String? email = "[email protected]";
print(email.isValidEmail()); // Output: true

String? number = "12345";
print(number.isNumeric); // Output: true

String? alphanumeric = "abc123";
print(alphanumeric.isAlphanumeric()); // Output: true

String? blankString = " ";
print(blankString.isBlank); // Output: true

```dart
void main() {
List<int> list = [1] + [3];
print(list); // Output: [1,3]
}
```


## Contributing

If you find any issues or have suggestions for new features, please create an issue or submit a pull request on GitHub.
Expand Down
24 changes: 23 additions & 1 deletion example/dart_utils_example.dart
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
void main() {}
import 'package:dart_utils/dart_utils.dart';

void main() {
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

print(numbers.max); // Output: 10
print(numbers.min); // Output: 1

List repeatedList = [54] * 3;
print(repeatedList); // Output: [54, 54, 54]

String? email = "[email protected]";
print(email.isValidEmail()); // Output: true

String? number = "12345";
print(number.isNumeric); // Output: true

String? alphanumeric = "abc123";
print(alphanumeric.isAlphanumeric()); // Output: true

String? blankString = " ";
print(blankString.isBlank); // Output: true
}
1 change: 1 addition & 0 deletions lib/dart_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
library dart_utils;

export 'src/list_extensions.dart';
export 'src/string_extensions.dart';
103 changes: 89 additions & 14 deletions lib/src/list_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ import 'package:collection/collection.dart';

/// List extensions.
extension GenericListExtensions<E> on Iterable<E> {
/// Repeats the list elements.
/// Returns a new list that contains this list repeated [data] times.
///
/// The [data] argument specifies the number of times the list should be repeated.
///
/// Example:
/// ```dart
/// List<int> numbers = [1, 2, 3];
/// List<int> repeatedNumbers = numbers * 3;
/// print(repeatedNumbers); // Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
/// ```
///
/// Returns:
/// A new list that contains this list repeated [data] times.
List<E> operator *(int data) {
List<E> result = [];
for (int i = 0; i < data; i++) {
Expand All @@ -11,36 +23,83 @@ extension GenericListExtensions<E> on Iterable<E> {
return result;
}

/// Concatenates tow list.
/// Returns a new list that contains the elements of this list followed by the elements of [data].
///
/// The [data] argument specifies the list to concatenate with this list.
///
/// Example:
/// ```dart
/// List<int> numbers1 = [1, 2, 3];
/// List<int> numbers2 = [4, 5, 6];
/// List<int> concatenatedNumbers = numbers1 + numbers2;
/// print(concatenatedNumbers); // Output: [1, 2, 3, 4, 5, 6]
/// ```
///
/// Returns:
/// A new list that contains the elements of this list followed by the elements of [data].
List<E> operator +(List<E> data) {
return [...this, ...data];
}

/// Counts the occurrences of an element in the list, including the map, list
/// or set.
///
/// The [element] parameter is the element to count the occurrences of.
///
/// Returns an integer representing the number of occurrences.
///
/// Example:
/// ```dart
/// List<int> numbers = [1, 2, 3, 2, 1];
/// print(numbers.count(2)); // Output: 2
/// ```
int count(E element) {
DeepCollectionEquality deepEquality = DeepCollectionEquality();
return where((e) => deepEquality.equals(e, element)).length;
}
}

extension IntList<T extends num> on Iterable<T> {
/// A sum element of the iterable.
/// Calculates the sum of all elements in the list.
///
/// If the list is empty, a [StateError] is thrown with the message 'No element'.
///
/// The iterable must not be empty.
/// Returns the sum of all elements in the list.
///
/// Example:
/// ```dart
/// List<int> numbers = [1, 2, 3];
/// print(numbers.sum); // Output: 6
/// ```
num get sum => length == 0
? throw StateError('No element')
: fold(0, (current, next) => current + next);

/// A average element of the iterable.
/// Calculates the average of all elements in the list.
///
/// If the list is empty, a [StateError] is thrown with the message 'No element'.
///
/// Returns the average of all elements in the list.
///
/// The iterable must not be empty.
/// Example:
/// ```dart
/// List<int> numbers = [1, 2, 3];
/// print(numbers.average); // Output: 2.0
/// ```
num get average =>
length == 0 ? throw StateError('No element') : sum / length;

/// A media of element in the iterable.
/// Calculates the median of all elements in the list.
///
/// If the list is empty, an [Exception] is thrown with the message 'List is empty'.
///
/// Returns the median of all elements in the list.
///
/// The iterable must not be empty.
/// Example:
/// ```dart
/// List<int> numbers = [1, 2, 3, 4];
/// print(numbers.median); // Output: 2.5
/// ```
num median() {
if (length == 0) {
throw Exception('List is empty');
Expand All @@ -52,17 +111,33 @@ extension IntList<T extends num> on Iterable<T> {
return sorted[length ~/ 2];
}

/// A maximal element of the iterable.
/// Returns the maximum value in the list.
///
/// The iterable must not be empty.
/// If the list is empty, a [StateError] is thrown with the message 'No element'.
///
/// Returns the maximum value in the list.
///
/// Example:
/// ```dart
/// List<int> numbers = [1, 2, 3];
/// print(numbers.max); // Output: 3
/// ```
T get max => length == 0
? throw StateError('No element')
: reduce((max, element) => max > element ? max : element);
: reduce((curr, next) => curr > next ? curr : next);

/// A minimal element of the iterable.
/// Returns the minimum value in the list.
///
/// If the list is empty, a [StateError] is thrown with the message 'No element'.
///
/// Returns the minimum value in the list.
///
/// The iterable must not be empty.
/// Example:
/// ```dart
/// List<int> numbers = [1, 2, 3];
/// print(numbers.min); // Output: 1
/// ```
T get min => length == 0
? throw StateError('No element')
: reduce((min, element) => min < element ? min : element);
: reduce((curr, next) => curr < next ? curr : next);
}
Loading