Git Product home page Git Product logo

cullender's Introduction

Cullender

Build Status

A simple and composable way to filter data.

Installation

Cullender is published on NPM registry. It's easy to integrate into your's current project environment, you have just to install like the example below and import/require cullender functions to filter something.

npm install cullender

This is a pretty module to convince you to use cullender to filter your stuff.

import { cull, filters } from 'cullender'

// ...

const latest = cull(
  [ ...users ],
  filters.truthy(),
  (user) => getTime(user.created) > getTime() - 7 * DAY
)

API

cull: (Iterable<T>, ...filters) => Array<T> - Filter data with filter functions

Example

import { cull } from 'cullender'

const isAdmin = (user) => user.role === 'admin'

cull(
  [ ...users ],
  (user) => user.isActive,
  isAdmin
)

create: ('AND'|'OR', ...filters) => boolean - Creates a filter function

Example

import { create } from 'cullender'

const isAdmin = create(
  'AND',
  (user) => user.isActive,
  (user) => user.role === 'admin'
)

[ ...users ].filter(isAdmin)

// You could also use *cull* function.
cull(users, isAdmin)

filters: Object.<string, F => filter> - Some useful filter functions

filters.truthy: <T>((T, number, Array<T>) => *) => filter - Filter truthy

Check if value, or function returned value is truthy.

Example

import { cull, filters } from 'cullender'

cull(
  users,
  filters.truthy(user => user.id)
)

filters.into: <L, T>(Iterable.<L>, (T, number, Array<T>) => *): filter - Filter into

Check if value, or function returned value is included on List.

Example

import { cull, filters } from 'cullender'

const isAuthorized = filters.into(['admin', 'manager', 'executive'], user => user.role)

const authorized = cull(users, isAuthorized)

filters.search: <T>(string, (T, number, Array<T>) => string): filter - Search terms

Check if value, or function returned value matches search terms.

Example

import { cull, filters } from 'cullender'

const terms = document.querySelector('input[type="search"]').value

const results = cull(
  users,
  (terms, user => [user.name, user.email]) // search into multiple values with
                                           // an array you can use an plan
                                           // string value either
)

filters.not: (filter) => filter - Not filter

Check if value, or function returned value matches search terms.

Example

import { cull, filters } from 'cullender'

const withoutEmailUsers = cull(
  users,
  filters.not(filters.truthy(user => user.email))
)

License

Licensed under MIT License. You can see it here.

cullender's People

Contributors

vitorluizc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

danetheory

cullender's Issues

Add not() filter

There's no way to negate filter results.

This function is useful to decorate a function negating it's results.

const not = (filter) => (...args) => !filter(...args)

Add CI

Add travis or another CI to show tests result and keep lib / build safe.

Support for TypeScript

Add type definitions or refactor using TypeScript.

type Map <T, U> = (item: T, index: number, items: ReadonlyArray<T>) => U
type Filter <T> = (item: T, index: number, items: ReadonlyArray<T>) => boolean;

const cull: <T> (items: Iterable<T>, ...λs: Array<Filter<T>>) => Array<T>;
const create: <T> (type: 'AND' | 'OR', ...λs: Array<Filter<T>>) => Filter<T>;

namespace filters {
  export const not: <T> (filter: Filter<T>) => Filter<T>;
  export const into: <T, U> (items: Array<U>, λ?: Map<T, U>) => Filter<T>;
  export const search: <T> (text: string, λ?: Map<T, (string | Array<string>)>) => Filter<T>;
  export const truthy: <T> (λ?: Map<T, boolean>) => Filter<T>;
}

export { cull, create, filters, Filter, Map };

Use String.normalize instead of diacritics

String.prototype.normalize combined with a character set replace do exactly what I'm looking for on diacritics module.

const normalize = (text) => text.normalize('NFKD').replace(/[\u0080-\uF8FF]/g, '')

Add "list includes" to filters

Add a filter to check if value is present in a list.

cull([1,2,3,4,5], filters.in([1,2])) // [1, 2]
cull([1,2,3,4,5], filters.in([7,6])) // []

I thought something like function below would be awesome.

export const in = (list, λ = DEFAULT_Λ) => (...args) => {
  const result = list.includes(λ(...args))
  return result
}

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.