diff --git a/README.md b/README.md index ed2ce9e..f08b29f 100644 --- a/README.md +++ b/README.md @@ -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', @@ -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', @@ -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', @@ -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', @@ -76,8 +83,10 @@ 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) @@ -85,6 +94,7 @@ You may use one of the following date range presets: * last_month Or a custom range like this: + ``` { 'field': 'searchField', @@ -100,7 +110,9 @@ 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', @@ -108,8 +120,11 @@ You can filter for numeric numbers that are higher than a specified one like thi 'value': 1, }, ``` + ### Maximum number filters + You can filter for numeric numbers that are lower than a specified one like this: + ``` { 'field': 'searchField', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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%. diff --git a/package-lock.json b/package-lock.json index 7f269e1..955c460 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "butterfly-data-filters", - "version": "0.2.1", + "version": "0.2.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "butterfly-data-filters", - "version": "0.2.1", + "version": "0.2.2", "license": "MIT", "devDependencies": { "@babel/core": "^7.10", diff --git a/package.json b/package.json index bd799d0..cfe55d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "butterfly-data-filters", - "version": "0.2.1", + "version": "0.2.2", "license": "MIT", "private": false, "main": "dist/filterHelper.js", diff --git a/src/__tests__/filterHelper.jest.js b/src/__tests__/filterHelper.jest.js index 5eca6d6..ba5ca06 100644 --- a/src/__tests__/filterHelper.jest.js +++ b/src/__tests__/filterHelper.jest.js @@ -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); diff --git a/src/filterHelper.js b/src/filterHelper.js index e957f0a..5d66581 100644 --- a/src/filterHelper.js +++ b/src/filterHelper.js @@ -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;