Giter Club home page Giter Club logo

clss.extensionmethods.icomparable.clamptorange's Introduction

CLSS.ExtensionMethods.IComparable.ClampToRange

Problem

The process to clamp a value to a range is somewhat long:

if (value < min) value = min;
else if (value > max) value = max;

Even longer when the type of your value does not implement comparison operators:

// Type of meetingDate is System.DateTime
if (meetingDate.CompareTo(startDate) < 0) meetingDate = startDate;
else if (meetingDate.CompareTo(endDate) > 0) meetingDate = endDate;

The System.Math.Clamp method can do this operation in one line, but it only supports a limited number of primitive types. Types such as System.Version and System.DateTime are not supported.

Solution

ClampToRange does this in a more intuitive, shorter and functional style friendly way:

using CLSS;

value = value.ClampToRange(min, max);

No more short stops in your code reading to decode the meanings of comparison operators. When you read ClampToRange, you know what it's doing.

ClampToRange has a wider range of supported types than System.Math.Clamp. It supports all IComparable<T> types. For non-generic IComparable types, the equivalent method provided by this package is ClampToRangeNonGeneric.

using CLSS;

// System.DateTime implements IComparable<T>
meetingDate = meetingDate.ClampToRange(startDate, endDate);

ClampToRange can also take in CLSS type ValueRange on .NET Standard 2.0 or higher.

using CLSS;

var displayableRange = new ValueRange(0, 9999);
int value = value.ClampToRange(displayableRange);
This package is a part of the C# Language Syntactic Sugar suite.

clss.extensionmethods.icomparable.clamptorange's People

Contributors

tonygiang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.