Skip to content

Commit

Permalink
Using fold instead of reduce function to avoid error on empty sales
Browse files Browse the repository at this point in the history
  • Loading branch information
zp1ke committed Aug 20, 2024
1 parent 71c566e commit 8fa0279
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 37 deletions.
43 changes: 15 additions & 28 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@

{
"name": "Ubuntu",

"build": {
"dockerfile": "Dockerfile"
},

"customizations": {
"vscode": {
"extensions": [
"DavidAnson.vscode-markdownlint",
"Dart-Code.dart-code",
"EditorConfig.EditorConfig",
"name": "Salert Dart",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"extensions": [
"DavidAnson.vscode-markdownlint",
"Dart-Code.dart-code",
"EditorConfig.EditorConfig",
"GitHub.vscode-github-actions",
"redhat.vscode-yaml"
]
}
},

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

"postCreateCommand": "dart --version"

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
"redhat.vscode-yaml"
]
}
},
"postCreateCommand": "sh ./.devcontainer/postCreateCommand.sh"
}
6 changes: 6 additions & 0 deletions .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

echo ""
dart --version

git config --global --add safe.directory /workspaces/*
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.8]

### Fixed

- Using `fold` instead of `reduce` function to avoid error on empty sales.

## [0.0.7]

### Added
Expand Down
2 changes: 1 addition & 1 deletion lib/src/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Item implements Sellable {
double taxOfCodes(List<String> taxCodes) {
return taxCodes
.map((taxCode) => taxOf(taxCode))
.reduce((value1, value2) => value1 + value2);
.fold(.0, (value1, value2) => value1 + value2);
}

// Calculates discount amount before taxes.
Expand Down
16 changes: 8 additions & 8 deletions lib/src/sale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,45 @@ class Sale implements Sellable {
double get subtotal {
return _items
.map((item) => item.subtotal)
.reduce((value1, value2) => value1 + value2);
.fold(.0, (value1, value2) => value1 + value2);
}

/// Calculates subtotal amount including [discount] for given [taxCode] (before taxes).
@override
double subtotalOf(String taxCode) {
return _items
.map((item) => item.subtotalOf(taxCode))
.reduce((value1, value2) => value1 + value2);
.fold(.0, (value1, value2) => value1 + value2);
}

/// Calculates tax amount.
@override
double get tax {
return _items.map((item) => item.tax).reduce((s1, s2) => s1 + s2);
return _items.map((item) => item.tax).fold(.0, (s1, s2) => s1 + s2);
}

/// Calculates tax amount including [discount] for given [taxCode].
@override
double taxOf(String taxCode) {
return _items
.map((item) => item.taxOf(taxCode))
.reduce((value1, value2) => value1 + value2);
.fold(.0, (value1, value2) => value1 + value2);
}

/// Calculates tax amount including [discount] for given [taxCodes].
@override
double taxOfCodes(List<String> taxCodes) {
return _items
.map((item) => item.taxOfCodes(taxCodes))
.reduce((value1, value2) => value1 + value2);
.fold(.0, (value1, value2) => value1 + value2);
}

// Calculates discount amount before taxes.
@override
double get discountAmount {
return _items
.map((item) => item.discountAmount)
.reduce((value1, value2) => value1 + value2);
.fold(.0, (value1, value2) => value1 + value2);
}

/// Calculates total amount.
Expand Down Expand Up @@ -99,10 +99,10 @@ class Sale implements Sellable {
if (discount?.affectsTotal ?? false) {
final baseSubtotal = items
.map((item) => item.subtotal)
.reduce((value1, value2) => value1 + value2);
.fold(.0, (value1, value2) => value1 + value2);
final baseTax = items
.map((item) => item.tax)
.reduce((value1, value2) => value1 + value2);
.fold(.0, (value1, value2) => value1 + value2);
final baseTotal = baseSubtotal + baseTax + tip;
var discountValue = discount!.discountOf(baseTotal);
final discountedSubtotal = _inverseSubtotalOf(baseTotal - discountValue);
Expand Down
8 changes: 8 additions & 0 deletions test/sale_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,13 @@ void main() {
expect(sale.discountAmountOfSaleAffectingSubtotal, closeTo(1.3, 0.01));
expect(sale.total, closeTo(10.0, 0.01));
});

test('Empty sale calculation', () {
var sale = Sale(items: []);
expect(sale.subtotal, .0);
expect(sale.tax, .0);
expect(sale.discountAmount, .0);
expect(sale.total, .0);
});
});
}

0 comments on commit 8fa0279

Please sign in to comment.