Skip to content

Contains as a new Test (flipped operation of in) #1766

Open
@maweki

Description

Since 2.10 there is the in test where I can filter all elements of a list on whether they occur in another container like so: [1,2,2,4,5,6,7] | select('in', [2,6]) | list returns [2,2,6]. The reverse operation is also useful.

In my example I had a list of TV episode names and wanted only to show those containing a specific word. I would want the following: ['Mike','Joe','Michael'] | select('contains', 'Mi') | list returns ['Mike','Michael']

Writing the for-loop was no option for me, as I needed to select a single entry using | random in the same expression (home automation is a bit finicky).

Other solutions I've thought about were a test that allows to regex-check a string (this would also cover the startswith and endswith cases, and is in general very versatile, but restricted to strings), or the higher-order function flip (rejected in #906) to just flip the arguments of the already existing in filter. I think the contains filter is useful, as it seems to be the only binary operator that's missing. There are tests for a < b and if I want the flipped version, there is a test for a >= b. There is a test for a in b but the b in a test is missing.

I am happy to write the PR myself. That's not too too much work. The contributing guideline suggested though, that an issue is opened before any time is wasted for unwelcome features. :)

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions