Filter anything ⚔️
npm i filter-anything
Filter out object props based on "fillables" and "guard". A simple & small integration.
Motivation
I created this package because I needed to filter object props on fillables or guard and have support for sub-properties.
Meet the family
Usage
You can filter out any prop in an object based on fillables or guard.
- Fillables: Array of keys - the props which may stay
- 0 fillables = all props stay
- 1 or more fillables = only those props stay (any prop not in fillables is removed)
- Guard: Array of keys - the props which should be removed
- adding any prop here will make sure it's removed
You can use it by doing filter(object, fillables, guard)
and it will return the object
with just the props as per the fillables and/or guard!
Simple example
In the example below we want to get rid of the properties called "discard1" and "discard2".
import filter from 'filter-anything'
const doc = {keep1: '📌', keep2: '🧷', keep3: '📎', discard1: '✂️', discard2: '🖍'}
const fillables = ['keep1', 'keep2', 'keep3']
filter(doc, fillables)
const guard = ['discard1', 'discard2']
filter(doc, [], guard)
Nested example
In the example below we want to get rid of the nested property called "discard".
const doc = {items: {keep: '📌', discard: '✂️'}}
const fillables = ['items.keep']
filter(doc, fillables)
const guard = ['items.discard']
filter(doc, [], guard)
Wildcards
Yes! You can also work with wildcards by using *
in the path.
const doc = {
'123': {keep: '📌', discard: '✂️'},
'456': {keep: '📌', discard: '✂️'}
}
const guard = ['*.discard']
filter(doc, [], guard)
Feel free to open issues for any requests, questions or bugs!