Skip to content

Commit

Permalink
Crested mq either
Browse files Browse the repository at this point in the history
  • Loading branch information
Eldar2021 committed Jul 27, 2024
1 parent 18c7881 commit 865313d
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 87 deletions.
48 changes: 48 additions & 0 deletions packages/mq_either/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# MyQuran Dart Either

## Either Class Implementation

This Dart code provides an implementation of the `Either` type, a common functional programming pattern. The `Either` type represents a value that can be one of two types: `Right` or `Left`. This is useful for handling operations that can return two types of results, such as a successful value or an error.

### Either<R, L>

An abstract class for values that can be either `Right` or `Left`.

- `fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight)`: Executes one of the given functions based on the type of value.

### Right<R, L>

Represents a `Right` value.

- `Right(this.value)`: Holds the `Right` value.
- `fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight)`: Executes the `ifRight` function with the `Right` value.

### Left<R, L>

Represents a `Left` value.

- `Left(this.value)`: Holds the Left value.
- `fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight)`: Executes the `ifLeft` function with the `Left` value.

### Usage

Example usage of `Either`, `Right`, and `Left` classes:

```dart
void main() {
final rightValue = Right<int, String>(42);
final leftValue = Left<int, String>('Error');
final rightResult = rightValue.fold(
(error) => 'Left: $error',
(value) => 'Right: $value',
);
print(rightResult); // Output: "Right: 42"
final leftResult = leftValue.fold(
(error) => 'Left: $error',
(value) => 'Right: $value',
);
print(leftResult); // Output: "Left: Error"
}
```
6 changes: 6 additions & 0 deletions packages/mq_either/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include: package:very_good_analysis/analysis_options.6.0.0.yaml

linter:
rules:
public_member_api_docs: false
lines_longer_than_80_chars: false
4 changes: 4 additions & 0 deletions packages/mq_either/lib/mq_either.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// MyQuran Dart Either
library;

export 'src/mq_either.dart';
50 changes: 50 additions & 0 deletions packages/mq_either/lib/src/mq_either.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:meta/meta.dart';

/// An abstract class representing a value that can be either Right or Left.
@immutable
sealed class Either<R, L> {
const Either();

/// Performs operations based on Right and Left values.
B fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight);
}

/// Represents the Right value of an Either.
@immutable
final class Right<R, L> extends Either<R, L> {
const Right(this.value);

/// Holds the Right value.
final R value;

/// Invokes the ifRight function using the Right value.
@override
B fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight) => ifRight(value);

/// Equality and hashCode overrides.
@override
bool operator ==(Object other) => other is Right && other.value == value;

@override
int get hashCode => value.hashCode;
}

/// Represents the Left value of an Either.
@immutable
final class Left<R, L> extends Either<R, L> {
const Left(this.value);

/// Holds the Left value.
final L value;

/// Invokes the ifLeft function using the Left value.
@override
B fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight) => ifLeft(value);

/// Equality and hashCode overrides.
@override
bool operator ==(Object other) => other is Left && other.value == value;

@override
int get hashCode => value.hashCode;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
name: mq_remote_client
description: MyQuran Remote Client
name: mq_either
description: MyQuran Dart Either
version: 0.1.0+1
publish_to: none

environment:
sdk: ^3.4.0

dependencies:
meta: ^1.15.0

dev_dependencies:
mocktail: ^1.0.4
test: ^1.25.7
Expand Down
49 changes: 49 additions & 0 deletions packages/mq_either/test/mq_either_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:mq_either/mq_either.dart';
import 'package:test/test.dart';

void main() {
group('Either', () {
test('Return type is correctly', () {
final rightResult = _successMethod();
final failedResult = _failedMethod();

final rightResultValue = switch (rightResult) {
Right() => 'Test Value ${rightResult.value}',
Left() => 'Test Value ${rightResult.value}',
};

final failedResultValue = switch (failedResult) {
Right() => 'Test Value ${failedResult.value}',
Left() => 'Test Value ${failedResult.value}',
};

expect(rightResultValue, 'Test Value 17');
expect(failedResultValue, 'Test Value Error');
});

test('Right value should fold correctly', () {
const rightValue = Right<int, String>(42);

final result = rightValue.fold(
(error) => 'Left: $error',
(value) => 'Right: $value',
);

expect(result, 'Right: 42');
});

test('Left value should fold correctly', () {
const leftValue = Left<int, String>('Error');

final result = leftValue.fold(
(error) => 'Left: $error',
(value) => 'Right: $value',
);

expect(result, 'Left: Error');
});
});
}

Either<int, String> _successMethod() => const Right(17);
Either<int, String> _failedMethod() => const Left('Error');
62 changes: 0 additions & 62 deletions packages/mq_remote_client/README.md

This file was deleted.

1 change: 0 additions & 1 deletion packages/mq_remote_client/analysis_options.yaml

This file was deleted.

4 changes: 0 additions & 4 deletions packages/mq_remote_client/lib/mq_remote_client.dart

This file was deleted.

7 changes: 0 additions & 7 deletions packages/mq_remote_client/lib/src/mq_remote_client.dart

This file was deleted.

11 changes: 0 additions & 11 deletions packages/mq_remote_client/test/mq_remote_client_test.dart

This file was deleted.

0 comments on commit 865313d

Please sign in to comment.