An open API service indexing awesome lists of open source software.

https://github.com/banane9/compiledfilters

Write reusable filter expressions that get compiled at runtime!
https://github.com/banane9/compiledfilters

Last synced: 5 months ago
JSON representation

Write reusable filter expressions that get compiled at runtime!

Awesome Lists containing this project

README

        

CompiledFilters
===============

[![Build status](https://ci.appveyor.com/api/projects/status/a16du0dg4thgul5u?svg=true)](https://ci.appveyor.com/project/Banane9/compiledfilters)

Write reusable filter expressions that get compiled at runtime!

## How to Use

[Example for Telegram.Bot](https://github.com/TelegramBotExtensions/Telegram.Bot.Extensions.Filters/tree/develop/src/Telegram.Bot.Extensions.Filters)

``` CSharp
///
/// A that checks if a came from a bot.
///
public static readonly Filter FromBot = UserFilters.IsBot.Using((Message msg) => msg.From);

///
/// A that checks if a was forward from a Channel.
///
public static readonly Filter ForwardedFromChannel = Filter.With(msg => msg.ForwardFromChat != null);

///
/// A that checks if a was forwarded at all.
///
public static readonly Filter IsForwarded = ForwardedFromUser | ForwardedFromChannel;

internal sealed class MessageRegexFilter : CustomFilter
{
private readonly Regex regex;
private readonly bool text;
private readonly bool caption;

private MessageRegexFilter(bool checkText, bool checkCaption)
{
text = checkText;
caption = checkCaption;
}

public MessageRegexFilter(Regex regex, bool checkText, bool checkCaption)
: this(checkText, checkCaption)
{
this.regex = regex ?? throw new ArgumentNullException(nameof(regex), "Regex can't be null!");
}

public MessageRegexFilter(string text, bool checkText, bool checkCaption)
:this(checkText, checkCaption)
{
regex = new Regex(Regex.Escape(text ?? throw new ArgumentNullException(nameof(text), "Pattern can't be null!")));
}

protected override bool Matches(Message message)
{
return (text && message.Text != null && regex.IsMatch(message.Text))
|| (caption && message.Caption != null && regex.IsMatch(message.Caption));
}
}
```