Giter Site home page Giter Site logo

phenax / pipey Goto Github PK

View Code? Open in Web Editor NEW
24.0 3.0 2.0 511 KB

Create pipeline operator ready functions by converting instance methods's to context-free functions

License: MIT License

JavaScript 100.00%
functional-programming javascript pipe-operator prototype currying

pipey's Introduction

Pipey

Utility functions to convert instance methods's to context-free functions ready for use with esnext pipeline operator and point-free functional programming. Convert any x.whatever(...args) to whatever(...arg)(x).

CircleCI npm bundle size (minified + gzip) Codecov

Read the documentation for more information

Install it

yarn add pipey

Import it to your file

import { createPipe, createPipes, fromClassPrototype, compose } from 'pipey';
// Note: compose is a regular lodash-like compose function

import _ from 'pipey/proxy'; // For proxy-based api

fromClassPrototype

const { map, filter } = fromClassPrototype(Array);

const doubleNumbers = map(x => x * 2);
const doubleOddNumbers = compose(doubleNumbers, filter(x => x % 2));

doubleOddNumbers([ 2, 3, 4, 5 ]); // Returns [ 6, 10 ]

createPipe

const forEach = createPipe('forEach');
forEach(x => console.log(x))([ 1, 2, 3, 4 ]); // Logs 1 2 3 4

createPipes

const { map, filter, split } = createPipes(['map', 'filter', 'split']);
const head = ([ first ]) => first;
const compact = filter(Boolean);

const getFirstNames = names =>
  names
    |> compact
    |> map(split(' '))
    |> map(head);

getFirstNames([ '', null, 'Akshay Nair', 'John Doe', 'Bruce Fucking Lee' ]); // Returns ['Akshay', 'John', 'Bruce']

Proxy based api

A proxy based alternative api for pipey Read the documentation

import _ from 'pipey/proxy';

const getInitials = compose(
  _.join(''),
  _.map(_.charAt(0)),
  _.split(' '),
  _.$prop('name'), // $prop is a pre-defined plugin
);

getInitials({ name: 'Akshay Nair' }) === 'AN';

Example use cases

  • Using with the amazing pipe operator
const { map, filter, reduce } = fromClassPrototype(Array);

const getInputData = () =>
  document.querySelectorAll('.js-input')
    |> map($input => [ $input.name, $input.value ])
    |> filter(([_, value]) => value)
    |> Object.fromEntries
    |> Array.from;

getInputData(); // Returns something like { email: '[email protected]', name: 'Han Solo' }
  • Working with collection methods
// Two ways to extract methods out (createPipes & fromClassPrototype)
const { map, filter } = fromClassPrototype(Array);
const { split } = createPipes(['split']);

const getFirstNames = compose(
  map(xs => xs[0]),
  map(split(' ')),
  filter(Boolean),
);

getFirstNames([ '', null, 'Akshay Nair', 'John Doe', 'Bruce Fucking Lee' ]); // Returns ['Akshay', 'John', 'Bruce']
  • Working with dom methods
const { forEach, join } = fromClassPrototype(Array);
const { setAttribute } = fromClassPrototype(HTMLInputElement);
const inputs = ['.js-input-name', '.js-input-email'];

inputs
  |> join(', ')
  |> (selector => document.querySelectorAll(selector))
  |> forEach(setAttribute('disabled', 'disabled'));

pipey's People

Contributors

phenax 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

Watchers

 avatar  avatar  avatar

Forkers

cptcrunchy

pipey's Issues

Needs api documentation

The api is pretty simple but still a formal api documentation is required for the project explaining what the function does, it's example use cases and it's type signature. The documentation should be kept inside the /docs directory as markdown files.

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.