muesli / beehive

A flexible event/agent & automation system with lots of bees 🐝
GNU Affero General Public License v3.0
6.32k stars 324 forks source link

Add a few helpful template filters #336

Open orsinium opened 4 years ago

orsinium commented 4 years ago

ContainsAny

I patched ContainsAny filter to be able to check if the given string contains any of the given substrings.

For example, this one:

{{test or (Contains .title "dephell") (Contains .title "flakehell") (Contains .title "orsinium.dev")}}

Can now be transformed into much simpler and shorter expression:

{{test ContainsAny .title "dephell" "flakehell" "orsinium.dev"}}

If only 2 strings are passed into the filter, it behaves as it used to, checking every symbol from the second string to be represented in the first one. While I cannot imagine a use case for it in beehive, let's keep the backward compatibility. It should be ok because ContainsAny looking only for one substring would be the same as just Contains.

ContainsAll

If we have ContainsAny, why not to have ContainsAll? I've added a new filter to check if a string contains all substrings.

Array

There is no syntax in the template engine to create an array but some functions accept arrays. It can be emulated with Split but it's not nice looking. So, let's make one.

{{Array "flakehell" "dephell"}}

More tests

I've added more tests for string functions. Since there is no way to edit the wiki from the master branch (where is it, BTW?), let's at least have the code "self-documented".

muesli commented 4 years ago

If only 2 strings are passed into the filter, it behaves as it used to, checking every symbol from the second string to be represented in the first one. While I cannot imagine a use case for it in beehive, let's keep the backward compatibility. It should be ok because ContainsAny looking only for one substring would be the same as just Contains.

I really don't like this kind of ambiguity. Why not introduce a separate function for that behavior?

Array

Nice!

More tests

Awesome!

I've added more tests for string functions. Since there is no way to edit the wiki from the master branch (where is it, BTW?), let's at least have the code "self-documented".

GitHub wikis are in a separate git repository: https://github.com/muesli/beehive.wiki.git

orsinium commented 4 years ago

I really don't like this kind of ambiguity.

Me too. However, the new behavior (check if any of substrings is in the string) is what I would expect from ContainsAny. I'd remove the old behavior because ContainsAny "something" "abc" is the same as ContainsAny "something" "a" "b" "c" from now on but it would be a breaking change.

Why not introduce a separate function for that behavior?

How would you name it?

muesli commented 4 years ago

I can't come up with anything better than ContainsAnyString :disappointed: Still open for suggestions :smiley:

orsinium commented 4 years ago

A few suggestions:

  1. Accept an array of strings as the second argument for the new behavior instead of variadic args.
  2. Screw the backward compatibility, return an error if only 2 arguments were passed like it is done in ContainsAll. The old implementation of ContainsAny rename to ContainsAnyRune.
  3. Don't touch ContainsAny, extend Contains instead.
  4. Don't worry about mixing behaviors (the implementation in this PR).
orsinium commented 1 year ago

Does it make sense for me to keep the PR open any longer or should I just close it? It's ok to not want to merge some random contributions. I'm long enough in open source to understand :)