Skip to content

Commit

Permalink
Merge pull request #23 from albertodev01/develop
Browse files Browse the repository at this point in the history
feat: Support for Dart 3
  • Loading branch information
albertodev01 authored May 11, 2023
2 parents bb94692 + e994c00 commit 374cf34
Show file tree
Hide file tree
Showing 27 changed files with 456 additions and 417 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @albertodev01
21 changes: 9 additions & 12 deletions .github/workflows/fraction_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ on:
branches:
- master
- develop
pull_request:
types:
- opened


jobs:
verify_fraction_package:
name: fraction pub package
runs-on: ubuntu-latest
runs-on: macos-latest
steps:
- uses: actions/checkout@v3.0.2
- uses: dart-lang/setup-dart@v1.3
- uses: actions/checkout@v3
- uses: dart-lang/setup-dart@v1

- name: Install dependencies
run: dart pub get
Expand All @@ -30,25 +27,25 @@ jobs:
run: dart pub global activate coverage && dart pub global run coverage:test_with_coverage

- name: Check Code Coverage
uses: VeryGoodOpenSource/very_good_coverage@v1.2.1
uses: VeryGoodOpenSource/very_good_coverage@v2
with:
min_coverage: 100

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}

verify_fraction_example:
name: fraction package - Dart example app
needs: [verify_fraction_package]
runs-on: ubuntu-latest
runs-on: macos-latest
defaults:
run:
working-directory: example
steps:
- uses: actions/checkout@v3.0.2
- uses: dart-lang/setup-dart@v1.3
- uses: actions/checkout@v3
- uses: dart-lang/setup-dart@v1

- name: Install dependencies
run: dart pub get
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 5.0.0
- **BREAKING**: `Rational`, `Fraction` and `MixedFraction` types have the `base` modifier.
- Updated Dart SDK constraints to `^3.0.0`
- Improved documentation and added more linter rules
- Dependencies update

## 4.1.4
- Increased pubspec description length to raise the score at `pub.dev`

Expand Down
60 changes: 29 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<p align="center"><img src="https://raw.githubusercontent.com/albertodev01/fraction/master/assets/package_logo.png" alt="fraction package logo" /></p>
<p align="center">A package that helps you working with <b>fractions</b> and <b>mixed fractions</b>.</p>
<p align="center">A package that helps you work with <b>fractions</b> and <b>mixed fractions</b>.</p>
<p align="center">
<a href="https://codecov.io/gh/albertodev01/fraction"><img src="https://codecov.io/gh/albertodev01/fraction/branch/master/graph/badge.svg?token=YKA1ZYUROR"/></a>
<a href="https://github.com/albertodev01/fraction/actions"><img src="https://github.com/albertodev01/fraction/workflows/fractions_ci/badge.svg" alt="CI status" /></a>
<a href=""><img src="https://img.shields.io/github/stars/albertodev01/fraction.svg?style=flat&logo=github&colorB=blue&label=stars" alt="Stars count on GitHub" /></a>
<a href="https://pub.dev/packages/fraction"><img src="https://img.shields.io/pub/v/fraction.svg?style=flat&logo=github&colorB=blue" alt="Stars count on GitHub" /></a>
</p>
<p align="center"><a href="https://pub.dev/packages/fraction">https://pub.dev/packages/fraction</a></p>
<p align="center">🌎 <a href="https://pub.dev/packages/fraction">https://pub.dev/packages/fraction</a></p>

---

## Working with fractions

You can create an instance of `Fraction` using one of its constructors:
You can create new `Fraction` objects using one of its constructors:

- **Default**: it just requires the numerator and/or the denominator.
- **Default**: requires the numerator and/or the denominator.

```dart
Fraction(3, 5); // 3/5
Fraction(3, 1); // 3
```

- **fromString**: requires a `String` representing a fraction.
- **fromString**: requires a `String` that represents a fraction.

```dart
Fraction.fromString("2/4"); // 2/4
Expand All @@ -31,7 +31,7 @@ You can create an instance of `Fraction` using one of its constructors:
Fraction.fromString("/3"); // Error
```

- **fromDouble**: converts a `double` into a fraction. Note that irrational numbers cannot be converted into fractions by definition; the constructor has the `precision` parameter which decides how precise the representation has to be.
- **fromDouble**: creates a `Fraction` from a `double` value. Note that irrational numbers cannot be converted into fractions by definition. The constructor has the `precision` parameter which decides how precise the representation has to be.

```dart
Fraction.fromDouble(1.5); // 3/2
Expand All @@ -40,40 +40,40 @@ You can create an instance of `Fraction` using one of its constructors:
Fraction.fromDouble(math.pi, precision: 1.0e-4); // 333/106
```

The constant `pi` cannot be represented as a fraction because it's an irrational number. The constructor considers only `precison` decimal digits to create a fraction.
For example, the constant `pi` cannot be represented as a fraction because it's an irrational number. The constructor considers only `precison` decimal digits to create the fraction.

Thanks to extension methods you can also create a `Fraction` object "on the fly" by calling the `toFraction()` method on a number or a string.
Thanks to extension methods, you can create a `Fraction` object "on the fly" by calling the `toFraction()` method on a number or a string.

```dart
5.toFraction(); // 5/1
1.5.toFraction(); // 3/2
"6/5".toFraction(); // 6/5
```

Note that a `Fraction` object is **immutable** so methods that require changing the internal state of the object return a new instance. For example, the `reduce()` method reduces the fraction to the lowest terms and returns a **new** instance:
The `Fraction` type is **immutable**, so methods that require changing the object's internal state return a new instance. For example, the `reduce()` method reduces the fraction to the lowest terms and returns a **new** object:

```dart
final fraction = Fraction.fromString("12/20"); // 12/20
final reduced = fraction.reduce(); // 3/5
```

Fraction strings can be converted from and to unicode glyphs when possible.
Fraction strings can be converted from and to Unicode glyphs when possible. For example:

```dart
Fraction.fromGlyph("¼"); // Fraction(1, 4)
Fraction(1, 2).toStringAsGlyph(); // "½"
```

You can easily sum, subtract, multiply and divide fractions thanks to arithmetic operators:
You can easily sum, subtract, multiply and divide fractions using arithmetic operators:

```dart
final f1 = Fraction(5, 7);
final f2 = Fraction(1, 5);
final f1 = Fraction(5, 7); // 5/7
final f2 = Fraction(1, 5); // 1/5
final sum = f1 + f2; // -> 5/7 + 1/5
final sub = f1 - f2; // -> 5/7 - 1/5
final mul = f1 * f2; // -> 5/7 * 1/5
final div = f1 / f2; // -> 5/7 / 1/5
final sum = f1 + f2; // 32/35
final sub = f1 - f2; // 18/35
final mul = f1 * f2; // 1/7
final div = f1 / f2; // 25/7
```

The `Fraction` type has a wide API with the most common operations you'd expect to make on a fraction:
Expand All @@ -93,7 +93,7 @@ print('${fraction[0]}'); // -7
print('${fraction[1]}'); // 12
```

Any other index value different from `0` and `1` throws a `FractionException` exception. Two fractions are equal if their "cross product" is equal. For example `1/2` and `3/6` are said to be equivalent because `1*6 = 3*2` (and in fact `3/6` is the same as `1/2`).
In the last example, any other value different from `0` and `1` throws a `FractionException` exception. Two fractions are equal if their "cross product" is equal. For example `1/2` and `3/6` are said to be equivalent because `1*6 = 3*2` (and in fact `3/6` is the same as `1/2`).

## Working with mixed fractions

Expand All @@ -107,28 +107,28 @@ MixedFraction(
);
```

As it happens with fractions, you can use various named constructors as well:
As it happens with the `Fraction` type, you can use various named constructors:

```dart
MixedFraction.fromDouble(1.5);
MixedFraction.fromString("1 1/2");
```

There also is the possibility to initialize a `MixedFraction` using extension methods:
You can create new `MixedFraction` objects using extension methods:

```dart
final mixed = "1 1/2".toMixedFraction();
```

Note that `MixedFraction` objects are **immutable** exactly like `Fraction` objects so you're guaranteed that the internal state of the instance won't change. Make sure to check the official documentation at [pub.dev](https://pub.dev/documentation/fraction/latest/fraction/MixedFraction-class.html) for a complete overview of the API.
The `MixedFraction` type is **immutable**, as it happens with `Fraction`. As such, you're guaranteed that the internal object state will never change. Make sure to check the official documentation at [pub.dev](https://pub.dev/documentation/fraction/latest/fraction/MixedFraction-class.html) for a complete overview of the API.

## Egyptian fractions

An Egyptian fraction is a finite sum of distinct fractions where the numerator is always 1 and, the denominator is a positive number and all the denominators differ from each other. For example:
An Egyptian fraction is a finite sum of distinct fractions where the numerator is always 1, the denominator is a positive number, and all the denominators differ. For example:

- 5/8 = 1/2 + 1/8 (where "1/2 + 1/8" is the egyptian fraction)

In other words, egyptian fractions are a sum of fractions in the form 1/x that represent a proper or an improper fraction. Here's how they can be computed:
In other words, Egyptian fractions are a sum of fractions in the form 1/x that represent a proper or an improper fraction. Here's how they can be computed:

```dart
final egyptianFraction1 = Fraction(5, 8).toEgyptianFraction();
Expand All @@ -138,23 +138,21 @@ final egyptianFraction2 = MixedFraction(2, 4, 5).toEgyptianFraction();
print("$egyptianFraction2"); // prints "1 + 1 + 1/2 + 1/4 + 1/20"
```

The `compute()` method returns an iterable.
The `toEgyptianFraction()` method returns an `Iterable`.

## Notes

Both `Fraction` and `MixedFraction` descend from the `Rational` type which allows parsing both kind
of fractions with a single method call:
Note that `Fraction` and `MixedFraction` are subtypes of `Rational`, which can be used for parsing both of kinds of fractions with a single method call. For example:

```dart
// This is a 'Fraction' object
// Returns a 'Fraction' object
Rational.tryParse('1/5'); // 1/5
// This is a 'MixedFraction' object
// Returns a 'MixedFraction' object
Rational.tryParse('2 4/7'); // 2 4/7
// This is 'null' because the string doesn't represent a fraction or a mixed fraction
// This is 'null' because the string doesn't represent a valid fraction or mixed fraction
Rational.tryParse(''); // null
```

Parsing integer values like `Rational.tryParse('3')` always returns a `Fraction` type but
it can easily be converted into a mixed fraction using the `Fraction.toMixedFraction` method.
Parsing integer values such as `Rational.tryParse('3')` always returns a `Fraction` type.
27 changes: 19 additions & 8 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
analyzer:
exclude:
- example/**
language:
strict-casts: true
strict-inference: true
Expand All @@ -19,7 +17,9 @@ dart_code_metrics:
- example/test/**
- test/**
rules:
- avoid-cascade-after-if-null
- avoid-collection-methods-with-unrelated-types
- avoid-double-slash-imports
- avoid-duplicate-exports
- avoid-dynamic
- avoid-global-state
Expand All @@ -29,10 +29,12 @@ dart_code_metrics:
- avoid-throw-in-catch-block
- avoid-unnecessary-type-assertions
- avoid-unnecessary-type-casts
- avoid-unnecessary-conditionals
- avoid-unrelated-type-assertions
- avoid-unused-parameters
- binary-expression-operand-order
- double-literal-format
- missing-test-assertion
- newline-before-return
- no-boolean-literal-compare
- no-equal-then-else
Expand All @@ -55,7 +57,6 @@ linter:
- always_declare_return_types
- always_put_control_body_on_new_line
- always_put_required_named_parameters_first
- always_require_non_null_named_parameters
- always_use_package_imports
- annotate_overrides
- avoid_annotating_with_dynamic
Expand All @@ -82,8 +83,6 @@ linter:
- avoid_relative_lib_imports
- avoid_renaming_method_parameters
- avoid_return_types_on_setters
- avoid_returning_null
- avoid_returning_null_for_future
- avoid_returning_null_for_void
- avoid_returning_this
- avoid_setters_without_getters
Expand All @@ -104,14 +103,17 @@ linter:
- cascade_invocations
- cast_nullable_to_non_nullable
- close_sinks
- collection_methods_unrelated_type
- combinators_ordering
- comment_references
- conditional_uri_does_not_exist
- constant_identifier_names
- control_flow_in_finally
- curly_braces_in_flow_control_structures
- dangling_library_doc_comments
- depend_on_referenced_packages
- deprecated_consistency
- diagnostic_describe_all_properties
- deprecated_member_use_from_same_package
- directives_ordering
- discarded_futures
- do_not_use_environment
Expand All @@ -124,22 +126,27 @@ linter:
- flutter_style_todos
- hash_and_equals
- implementation_imports
- invariant_booleans
- implicit_call_tearoffs
- implicit_reopen
- invalid_case_patterns
- iterable_contains_unrelated_type
- join_return_with_assignment
- leading_newlines_in_multiline_strings
- library_annotations
- library_names
- library_prefixes
- library_private_types_in_public_api
- lines_longer_than_80_chars
- list_remove_unrelated_type
- literal_only_boolean_expressions
- matching_super_parameters
- missing_whitespace_between_adjacent_strings
- no_adjacent_strings_in_list
- no_default_cases
- no_duplicate_case_values
- no_leading_underscores_for_library_prefixes
- no_leading_underscores_for_local_identifiers
- no_literal_bool_comparisons
- no_logic_in_create_state
- no_runtimeType_toString
- non_constant_identifier_names
Expand All @@ -165,7 +172,6 @@ linter:
- prefer_const_literals_to_create_immutables
- prefer_constructors_over_static_methods
- prefer_contains
- prefer_equal_for_default_values
- prefer_expression_function_bodies
- prefer_final_fields
- prefer_final_in_for_each
Expand Down Expand Up @@ -207,14 +213,17 @@ linter:
- tighten_type_of_initializing_formals
- type_annotate_public_apis
- type_init_formals
- type_literal_in_constant_pattern
- unawaited_futures
- unnecessary_await_in_return
- unnecessary_brace_in_string_interps
- unnecessary_breaks
- unnecessary_const
- unnecessary_constructor_name
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_late
- unnecessary_library_directive
- unnecessary_new
- unnecessary_null_aware_assignments
- unnecessary_null_aware_operator_on_extension_on_nullable
Expand All @@ -229,6 +238,7 @@ linter:
- unnecessary_string_interpolations
- unnecessary_this
- unnecessary_to_list_in_spreads
- unreachable_from_main
- unrelated_type_equality_checks
- unsafe_html
- use_build_context_synchronously
Expand All @@ -246,6 +256,7 @@ linter:
- use_rethrow_when_possible
- use_setters_to_change_properties
- use_string_buffers
- use_string_in_part_of_directives
- use_super_parameters
- use_test_throws_matchers
- use_to_and_as_if_applicable
Expand Down
5 changes: 5 additions & 0 deletions example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.6
- Updated Dart SDK constraints to `^3.0.0`
- Updated `analysis_options.yaml` file
- Updated dependencies

## 1.0.5
- Updated Dart SDK constraints to `">=2.18.0 <3.0.0"`
- Updated `analysis_options.yaml` file
Expand Down
6 changes: 4 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
This is a simple Dart command line app that analyzes fractions and mixed fractions. You can run it using
## Example

This is a simple Dart CLI applications that analyzes fractions and mixed fractions. You can run it using

```bash
$ dart run
```

inside the `example/` folder. If you want to create a portable binary file of this example (like an `.exe` file for Windows), see the [dart compile](https://dart.dev/tools/dart-compile) documentation for more info.
from the `example/` folder. If you want to create a portable binary file of this example (like an `.exe` file for Windows), see the [dart compile](https://dart.dev/tools/dart-compile) documentation for more info.
Loading

0 comments on commit 374cf34

Please sign in to comment.