GithubHelp home page GithubHelp logo

filter-shapes's Introduction

filter shapes

github | npm

filter-shapes filter function lets you filter an object using an example of that "shape" (these examples are type values, see type-by-example). The way type-by-example type values work is that an example of what you want will usually do the trick.

Filtering Simple Values

Simple values will be filtered using examples in an obvious way, and more specifically using the Specific Type strings implemented in the type system.

filter(1, -1)                       // -1
filter("1", -1)                     // undefined
filter('#int', -1)                  // -1
filter('#int [0,∞)', -1)            // undefined
filter('#int [0,∞)', 17)            // 17
filter('#regex ^\\d{5}$', '1234')   // undefined
filter('#regex ^\\d{5}$', '90210')  // '90210'

Filtering Objects

You can filter objects down to just the desired "shape".

If the provided object simply doesn't match the type of the filter then nothing will pass through.

const orig = {
  foo: 'bar',
  baz: 17
}
filter({ foo: 'whatever' }, orig)    // { foo: 'bar' }
filter({ baz: 100 }, orig)           // { baz: 17 }
filter({ foo: 100 }, orig)           // undefined

filter({x: 0, y: 0}, {x: 100, y: 120, z: 17}) // returns {x: 100, y: 120}
filter({x: 0, y: 0, z: 0}, {x: 100, y: 120})  // returns undefined

Filtering Arrays

Filter will filter arrays based on the elements in the corresponding type's array, e.g.

const orig = [true, false, 'hello', 17]

filter([true], orig) // returns [true, false]
filter([0], orig) // returns [17]
filter(['test', false], orig) // returns [true, false, 'hello']
filter([], orig) // returns [...orig]

History

This library originally started out as part of xinjs and is now being broken out as a standalone library.

Development

Aside from the usual nodejs toolchain, this project uses bun for speed and as a test runner.

filter-shapes's People

Contributors

tonioloewald avatar

Watchers

James Cloos avatar  avatar  avatar

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.