A dart port of the excellent Guard Clauses C# package by Ardalis

Overview


coverage   build

Guard Clauses

A dart port of the excellent C# Guard Clauses package by Ardalis.

A simple extensible package with guard clause extensions.

A guard clause is a software pattern that simplifies complex functions by "failing fast", checking for invalid inputs up front and immediately failing if any are found.

Usage

void processOrder(Order? order)
{
    Guard.against.nullValue(order, 'order');

    // process order here
}

// OR

class Order
{
    String _name;
    int _quantity;
    double _max;
    double _unitPrice;

    Order(String name, int quantity, double max, double unitPrice) {
        _name = Guard.against.nullOrWhiteSpace(name);
        _quantity = Guard.against.negativeOrZero(quantity);
        _max = Guard.against.zero(max);
        _unitPrice = Guard.against.negative(unitPrice);
    }
}

Supported Guard Clauses

  • Guard.against.invalidInput (throws if predicate expression returns false)
  • Guard.against.invalidFormat (throws if the input string doesn't match the provided regex)
  • Guard.against.negative (throws if the input is negative)
  • Guard.against.negativeOrZero (throws if the input is negative or zero)
  • Guard.against.nullValue (throws if input is null)
  • Guard.against.nullOrEmpty (throws if string input is null or empty)
  • Guard.against.nullOrEmptyCollection (throws if the input collection is null or empty)
  • Guard.against.nullOrWhiteSpace (throws if string input is null, empty or whitespace)
  • Guard.against.nullOrInvalidInput (throws if input is null, or predicate expression returns false)
  • Guard.against.indexOutOfRange (throws if input is not a valid index)
  • Guard.against.outOfRangeItems (throws if any values in the input collection are outside the provided range)
  • Guard.against.outOfRange (throws if the input collection is outside the provided range)
  • Guard.against.zero (throws if number input is zero)

Extending with your own Guard Clauses

To extend by creating your own guard clauses, create a new extension class for Guard.

extension GuardExtensions on Guard {
  /// Throws an ArgumentError if [input] is 'foo'.
  /// Returns [input] otherwise.
  String foo(String input, {String? name}) {
    if (input.toLowerCase()) {
      throw ArgumentError.value(input, name);
    }

    return input;
  }
}

// Usage
void sumpin(String otherSumpin) {
    Guard.against.foo(otherSumpin);
    ...
}

References

The references are C#-centric but the concepts apply well.

You might also like...

Mysql.dart - MySQL client for Dart written in Dart

Native MySQL client written in Dart for Dart See example directory for examples

Dec 29, 2022

A GUI package manager and package installer for Windows Subsystem for Android (WSA)

A GUI package manager and package installer for Windows Subsystem for Android (WSA)

wsa_pacman A GUI package manager and package installer for Windows Subsystem for Android (WSA). Currently provides a double-click GUI installer for .a

Jan 1, 2023

Lottie-package-example-Flutter - A simple example about lottie package in Flutter

Lottie-package-example-Flutter - A simple example about lottie package in Flutter

Lottie Package example - Flutter ScreenShot ⚠️ Essential Packages lottie: ^1.2.1

Dec 7, 2022

A GUI package manager and package installer for Windows Subsystem for Android (WSA)

A GUI package manager and package installer for Windows Subsystem for Android (WSA)

wsa_pacman A GUI package manager and package installer for Windows Subsystem for Android (WSA). Currently provides a double-click GUI installer for .a

Nov 14, 2022

Dart / Flutter package that allows discovering network devices in local network (LAN).

lan_scanner Dart / Flutter package that allows discovering network devices in local network (LAN). Note: This library is intended to be used on Class

Dec 9, 2022

:end: A dart package to append either `&` or `and` at the end of a List.

And Game A dart package to append either & or and at the end of a List. Features Appends & or and at the end of the list. Available as method or exten

Sep 24, 2021

A Dart package that constantly writes a string to an IOSink, simillarly to the UNIX yes utility.

yes A Dart package that constantly writes a string to an IOSink, simillarly to the UNIX yes utility. Usage // Write to stdout for 5 seconds. final con

Sep 22, 2022

Dynamic Text Highlighting (DTH) package for Dart & Flutter.

Dynamic Text Highlighting (DTH) package for Dart & Flutter.

Dynamic Text Highlighting (DTH) This package is used to highlight, in a completely dynamic way, keywords, or phrases, wherever they are present in a s

Oct 3, 2022

A simple dart package to convert large numbers to a human readable format. 1278 to 1.2K instead, for example.

A simple dart package to convert large numbers to a human readable format. 1278 to 1.2K instead, for example. Features Represents large numbers in ter

Oct 8, 2022
Owner
Chris Hibler
Chris Hibler
Dart port of FormCoreJS: A minimal pure functional language based on self dependent types.

FormCore.js port to Dart. So far only the parser and typechecker have been ported i.e. the FormCore.js file in the original repo. (Original readme fro

Modestas Valauskas 2 Jan 28, 2022
Radiance - Dart port of gdx-ai

⚠️ This package is currently under development. This library is a collection of AI algorithms used in games. This includes: Steering behaviors (NYI) F

Flame Engine 20 Oct 30, 2022
A pure Dart utility library that checks for an internet connection by opening a socket to a list of specified addresses, each with individual port and timeout. Defaults are provided for convenience.

data_connection_checker A pure Dart utility library that checks for an internet connection by opening a socket to a list of specified addresses, each

Kristiyan Mitev 103 Nov 29, 2022
A port of kotlin-stdlib for Dart/Flutter including immutable collections (KtList, KtMap, KtSet) and other packages

kt.dart This project is a port of Kotlin's Kotlin Standard library for Dart/Flutter projects. It's a useful addition to dart:core and includes collect

Pascal Welsch 460 Jan 9, 2023
Flutter package for displaying and animating Scalable Vector Graphics 1.1 files. The package has been written solely in Dart Language.

Animated SVG | Flutter Package Flutter package for displaying and animating Scalable Vector Graphics 1.1 files. The package has been written solely in

Bulent Baris Kilic 5 Jul 19, 2022
Dart package responsible to provide the basic resources to Lambda Functions with Clean Dart

AWS Lambda Core This package is responsible to provide the basic resources to all services; Usage pubspec.yaml dependencies: aws_lambda_core: <laste

David Araujo 1 Dec 2, 2021
GitHub Action that uses the Dart Package Analyzer to compute the Pub score of Dart/Flutter packages

Dart/Flutter package analyzer This action uses the pana (Package ANAlysis) package to compute the score that your Dart or Flutter package will have on

Axel Ogereau-Peltier 45 Dec 29, 2022
This is a dart package that converts words to numbers. It can be used in Flutter and normal Dart programs

Wordstonumbers.dart Wordstonumbers.dart is a simple dart package that converts a string of simple worded numbers into digits (e.g one hundred -> 100).

Michael Essiet 3 Oct 17, 2022
Simple Dart package with build-in code generation. It simplifies and speedup creation of cache mechanism for dart classes.

Simple Dart package with build-in code generation. It simplifies and speedup creation of cache mechanism for dart classes.

iteo 37 Jan 2, 2023
A Dart/Flutter package to perform network calls. It uses Isolates to perform network calls on Dart VM environments and WebWorkers on Web.

ArDriveHTTP ArDriveHTTP is a package to perform network calls for ArDrive Web. It uses Isolates to perform network calls on Dart VM environments and W

AR.IO 2 Dec 15, 2022