Giter Club home page Giter Club logo

filter-anything's Introduction

Filter anything ⚔️

Total Downloads Latest Stable Version

npm i filter-anything

An implementation that filters out object props like the TypeScript "pick" and "omit". In the Laravel world, this is also called "fillables" and "guard".

Motivation

I created this package because I needed:

  • be able to filter out object props based on just what we need - aka "pick" props
  • be able to filter out object props based on what we don't need - aka "omit" props
  • supports for nested properties
  • supports wildcards * for nested properties
  • the return type must be TypeScript supported! (see screenshots below)

Meet the family (more tiny utils with TS support)

Usage

Pick

With pick you pass an object and an array of keys of an object - the props which may stay.

import { pick } from 'filter-anything'

const squirtle = { id: '007', name: 'Squirtle', type: 'water' }

const newObject = pick(squirtle, ['name', 'type'])
// returns { name: 'Squirtle', type: 'water' }

Omit

With omit you pass an object and an array of keys of an object - the props which should be removed.

import { omit } from 'filter-anything'

const squirtle = { id: '007', name: 'Squirtle', type: 'water' }

const withoutId = omit(squirtle, ['id'])
// returns { name: 'Squirtle', type: 'water' }

Aliases

pick() and omit() can also be imported with the names fillable() and guard(). This pays homage to my history with Laravel. 😉

TypeScript

TypeScript users will love this, because, as you can see, the result has the correct type automatically!

typescript example pick typescript example omit

Nested props

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

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

pick(doc, ['items.keep'])
// returns {items: {keep: '📌'}}

omit(doc, ['items.discard'])
// returns {items: {keep: '📌'}}

Please note that TypeScript users will need to cast the result when using nested props.

Wildcards

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

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

Please note that TypeScript users will need to cast the result when using wildcards props.

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

filter-anything's People

Contributors

dependabot[bot] avatar mesqueeb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

filter-anything's Issues

omit ts bug - dont correct type automatically

import { omit } from 'filter-anything'

const togglePropsa = {
  props: {
    modelValue: String,
  },
  emits: {
    'update:modelValue': (pressed: boolean) => String,
    'pressedChange': (pressed: boolean) => String,
    'click': (event: MouseEvent) => String,
  },
}
const test = omit(togglePropsa.emits, ['update:modelValue', 'pressedChange'])

image

ts dont omit see

dont work dynamic array

import { omit } from 'filter-anything'

const aaa = ['hasThumb', 'sizes']

const dd = omit({
      hasThumb: {
        type: Boolean,
        required: true,
      },
      sizes: {
        type: String
        required: true,
      },
    }, aaa)
CleanShot 2023-09-11 at 06 00 09@2x

Feature Request: Ability to filter out fields in an array of objects.

Ability to filter out fields in an array of objects.

    var ash = { 
        name: 'Ash Catch Em', 
        badges: [
            {name: 'Otoshi', year: 2001}
            {name: 'Mismagius', year: 2003}
            {name: 'Nando', year: 1999}
        ]
    }

    const filter = require('filter-anything');
    console.log(filter.omit(ash, ['badges.year']));
    /*
    name: 'Ash Catch Em', 
        badges: [
            {name: 'Otoshi'}
            {name: 'Mismagius'}
            {name: 'Nando'}
        ]
    }
    */

I guess syntax would have to be different, since omitting 'badges.year' would remove {badges: {year: ___} in addition to {badges: [{year: ___}, ... ] }. Maybe ['badges].year' to specify that badges is an array of objects you want to omit the field year from?

Key type safety

@mesqueeb I noticed that omit allows keys that aren't preset in the original record.

if you swap the key and record definition, it'll enforce the record's types

export declare function omit<K extends string, T extends Record<K, any>>(obj: T, keys: F.AutoPath<T, K>[]): U.Merge<O.P.Omit<T, S.Split<K, '.'>>>;
export declare const guard: typeof omit;

how to dynamic omit

import { omit } from 'filter-anything'

const obj1 = {
  a: 1,
  b: 2,
  c: 3,
}

const obj2 = {
  a: 2,
  c: 3,
  d: 4,
}
type CommonKeys = Extract<keyof typeof obj1, keyof typeof obj2> // 'a' | 'c'

const commonKeyss = Object.keys(obj1).filter(key => obj2.hasOwnProperty(key)) as CommonKeys[] // ['a', 'c']

const aa = omit(obj1, commonKeyss)
aa. // dont working dont a see only `b` 
CleanShot 2023-09-11 at 05 42 09@2x

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.