Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

filter-anything

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter-anything

A simple integration of "fillables" and "guard" filtering on the props of an object

  • 1.1.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
decreased by-18.63%
Maintainers
1
Weekly downloads
 
Created
Source

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

  • filter-anything ⚔️
  • merge-anything 🥡
  • find-and-replace-anything 🎣
  • compare-anything 🛰
  • copy-anything 🎭
  • flatten-anything 🏏
  • is-what 🙉

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: '🖍'}

// via fillables
const fillables = ['keep1', 'keep2', 'keep3']
filter(doc, fillables)
// returns {keep1: 1, keep2: 1, keep3: 1}

// OR via guard
const guard = ['discard1', 'discard2']
filter(doc, [], guard)
// returns {keep1: 1, keep2: 1, keep3: 1}

Nested example

In the example below we want to get rid of the nested property called "discard".

const doc = {items: {keep: '📌', discard: '✂️'}}

// via fillables:
const fillables = ['items.keep']
filter(doc, fillables)
// returns {items: {keep: '📌'}}

// OR via guard:
const guard = ['items.discard']
filter(doc, [], guard)
// returns {items: {keep: '📌'}}

Wildcards

Yes! You can also work with wildcards by using * in the path.

const doc = {
  '123': {keep: '📌', discard: '✂️'},
  '456': {keep: '📌', discard: '✂️'}
}
// use wildcard *
const guard = ['*.discard']
filter(doc, [], guard)
// returns {
//   '123': {keep: '📌'},
//   '456': {keep: '📌'}
// }

Feel free to open issues for any requests, questions or bugs!

Keywords

FAQs

Package last updated on 07 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc