Skip to content

Commit

Permalink
Merge pull request #8 from binary-butterfly/allow-string-any-in-array…
Browse files Browse the repository at this point in the history
…-filters

Allow string _any to skip array filters
  • Loading branch information
DysphoricUnicorn authored Apr 28, 2022
2 parents c96c9b4 + 282738b commit 1c06068
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ For direct examples, check out src/__tests__/filterHelper.jest.js since it shows

* Install the library using `npm save --dev butterfly-data-filters`
* Add `import {applyFilters} from 'butterfly-data-filters';` where you want to use the filters
* You can also import `availableFilterTypes`, which is a list of all types supported.
* This list may be useful for validation before calling the filters or within React PropTypes
* You can also import `availableFilterTypes`, which is a list of all types supported.
* This list may be useful for validation before calling the filters or within React PropTypes
* Build your filters like in the examples below and combine them into a single array
* Call `applyFilters(filters, values, skipUndefined)`
* If `skipUndefined` is false, only datasets that contain all fields that your filter apply to will be returned
* If `skipUndefined` is false, only datasets that contain all fields that your filter apply to will be returned

### Substring filters

In order to filter your dataset for values that contain a certain substring, you will have to format your
In order to filter your dataset for values that contain a certain substring, you will have to format your
filters like so:

```
{
'field': 'searchField',
Expand All @@ -38,6 +39,7 @@ filters like so:
### Array filters

In order to filter your dataset for a list of values, you can use array filters:

```
{
'field': 'searchField',
Expand All @@ -47,16 +49,19 @@ In order to filter your dataset for a list of values, you can use array filters:
```

This filter will return all datasets where `searchField` is either `honk` or `goose`.
You can also pass `['_any']` as value to get all datasets and effectively skip this filter.
You can also pass `['_any']` or just the string `_any` as value to get all datasets and effectively skip this filter.
This is useful as a default value when you don't know what the possible filter options are in advance.

### Date filters

There are a number of filters that allow you to search for datasets with specific date values.
In general, these can be called with either a properly formatted string (a string that `new Date()` understands) or a
In general, these can be called with either a properly formatted string (a string that `new Date()` understands) or a
vanillaJS `Data` object as value.

#### minDate

This filter will return any datasets where the search field date is after the specified one.

```
{
'field': 'searchField',
Expand All @@ -66,7 +71,9 @@ This filter will return any datasets where the search field date is after the sp
```

#### maxDate

This filter will return any datasets where the search field date is before the specified one.

```
{
'field': 'searchField',
Expand All @@ -76,15 +83,18 @@ This filter will return any datasets where the search field date is before the s
```

#### dateRange

This filter lets you specify different date ranges to search for.
You may use one of the following date range presets:

* today
* yesterday
* 7days (the last week)
* month (the current month)
* last_month

Or a custom range like this:

```
{
'field': 'searchField',
Expand All @@ -100,16 +110,21 @@ Or a custom range like this:
You can also skip dateRange checks by setting the value to `_any`.

### Minimum number filters

You can filter for numeric numbers that are higher than a specified one like this:

```
{
'field': 'searchField',
'type': 'minNum',
'value': 1,
},
```

### Maximum number filters

You can filter for numeric numbers that are lower than a specified one like this:

```
{
'field': 'searchField',
Expand All @@ -119,7 +134,9 @@ You can filter for numeric numbers that are lower than a specified one like this
```

### Strict equality filters

Use this filter to search for strict equality:

```
{
'field': 'searchField',
Expand All @@ -129,7 +146,9 @@ Use this filter to search for strict equality:
```

### Lax truthiness filters

Use this filter to search for datasets that have a truthy value as searchField

```
{
'field': 'bool',
Expand All @@ -138,7 +157,9 @@ Use this filter to search for datasets that have a truthy value as searchField
```

### Lax falsiness filters

Use this filter to search for datasets that have a falsy value as searchField

```
{
'field': 'bool',
Expand All @@ -147,8 +168,10 @@ Use this filter to search for datasets that have a falsy value as searchField
```

### Existence check filters

Use this filter to search for datasets that have any value set (or no value at all) for searchField.
Note that this filter overrides the `skipUnset` param.

```
{
'field': 'test',
Expand All @@ -161,7 +184,9 @@ You can also pass the array `[true, false]` or `[_any]` to this filter to effect
This is useful when using material-ui multi selects.

### Child attribute filters

With the childAttr filter type, you can search child fields recursively like this:

```
{
'field': 'child',
Expand All @@ -178,9 +203,11 @@ With the childAttr filter type, you can search child fields recursively like thi
You can use any filter type (including `childAttr` and `childArrayAttr`) in your child filter.

### Array child attribute filters

This filter functions just like the `childAttr` type but instead of querying a single child field.
It can be used to go through an array of child objects and return all datasets where at least one child
It can be used to go through an array of child objects and return all datasets where at least one child
fits the filter.

```
{
field: 'children',
Expand Down Expand Up @@ -209,7 +236,8 @@ This library is expected to grow over time and more features will be added.
## Contributing

Contributions are very welcome, even if they are not related to code.
If you found any bugs or are missing some specific feature, [please file an issue](https://github.com/binary-butterfly/butterflyDataFilters/issues/new).
If you found any bugs or are missing some specific
feature, [please file an issue](https://github.com/binary-butterfly/butterflyDataFilters/issues/new).

Pull requests are obviously also very welcome.
However, please note that we can only accept PRs that do not lower our test coverage of 100%.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "butterfly-data-filters",
"version": "0.2.1",
"version": "0.2.2",
"license": "MIT",
"private": false,
"main": "dist/filterHelper.js",
Expand Down
18 changes: 17 additions & 1 deletion src/__tests__/filterHelper.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,24 @@ describe.each([
[
{'test': true}, {'banana': false},
],
'array value _any all allowed',
'array value [_any] all allowed',
],
[
[
{
'field': 'test',
'type': 'existence',
'value': '_any',
},
],
[
{'test': true}, {'banana': false},
],
[
{'test': true}, {'banana': false},
],
'array value _any all allowed',
]
])('Test existence filters', (filters, values, expected, name) => {
test(name, () => {
expect(applyFilters(filters, values, false)).toStrictEqual(expected);
Expand Down
2 changes: 1 addition & 1 deletion src/filterHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const checkFilter = (filter, valueRow, skipUndefined) => {
}
break;
case ('array'):
if (filter.value[0] !== '_any' && filter.value.indexOf(value) === -1) {
if (filter.value !== '_any' && filter.value[0] !== '_any' && filter.value.indexOf(value) === -1) {
return false;
}
break;
Expand Down

0 comments on commit 1c06068

Please sign in to comment.