Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
/ evangelist Public archive

[Deprecated] 🌟 Library of helpers that are useful for functional programming

License

Notifications You must be signed in to change notification settings

eser/evangelist

Repository files navigation

🌟 evangelist

build status npm version npm download dependencies coverage status license

Update (July 2022)

DEPRECATED in favor of hex. See https://github.com/eserozvataf/hex for details.

What is the Evangelist?

Evangelist is a set of helper methods that are useful and reusable for base functional programming requirements such as function composition, function decoration, event dispatching and emitting, etc.

Plus, as a library, Evangelist is completely tree-shaking-friendly. Your favorite module bundler can easily inline the functionality you need with no extra configuration, instead of bundling the whole Evangelist package.

Quick start

Execute npm install evangelist or yarn add evangelist to install evangelist and its dependencies into your project directory.

Usage of modules

compose(...functionsForComposition)

import compose from 'evangelist/compose';

// compose - slug sample
const lower = x => x.toLowerCase();
const chars = x => x.replace(/[^\w \-]+/g, '');
const spaces = x => x.split(' ');
const dashes = x => x.join('-');

const slug = compose(lower, chars, spaces, dashes);

const message = slug('Hello World!');

// outputs 'slug: hello-world'
console.log(`slug: ${message}`);

curry(targetFunction, ...argumentsToBePrepended)

import curry from 'evangelist/curry';

// curry - sum sample
const sum = (a, b) => a + b;

const sumWith5 = curry(sum, 5);

const result = sumWith5(3);

// outputs 'result: 8'
console.log(`result: ${result}`);

curryRight(targetFunction, ...argumentsToBeAppended)

import curryRight from 'evangelist/curryRight';

// curryRight - sum sample
const dec = (a, b) => a - b;

const decWith5 = curry(dec, 5);

const result = decWith5(3);

// outputs 'result: -2'
console.log(`result: ${result}`);

decorate(functionToDecorate, decoratorFunction)

import decorate from 'evangelist/decorate';

// decorate - calculator sample
let generator = () => 5;
generator = decorate(generator, (func) => func() * 2);
generator = decorate(generator, (func) => func() + 1);

// outputs: 'generated: 11'
console.log(`generated: ${generator()}`);

dispatcher(initialState, mutators) (awaitable)

import dispatcher from 'evangelist/dispatcher';

// dispatcher - state mutation sample
const initialState = { quarter: 1, year: 2018, sum: 1 };

const actionAdd5 = (state, next) => next({ ...state, sum: state.sum + 5 });
const actionDiv2 = (state, next) => next({ ...state, sum: state.sum / 2 });

// outputs 'new state is: {"quarter":1,"year":2018,"sum":3}'
dispatcher(initialState, [ actionAdd5, actionDiv2 ])
    .then(state => console.log(`new state is: ${JSON.stringify(state)}`));

dispatcher(initialState, mutators, subscribers) (awaitable)

import dispatcher from 'evangelist/dispatcher';

// dispatcher - action logger sample
const initialState = { quarter: 1, year: 2018, sum: 1 };

const actionAdd5 = (state, next) => next({ ...state, sum: state.sum + 5 });
const actionDiv2 = (state, next) => next({ ...state, sum: state.sum / 2 });

const logger = (x) => console.log('INFO', x);

/* outputs:
   INFO { action: 'actionAdd5',
     previousState: { quarter: 1, year: 2018, sum: 1 },
     newState: { quarter: 1, year: 2018, sum: 6 } }
   INFO { action: 'actionDiv2',
     previousState: { quarter: 1, year: 2018, sum: 6 },
     newState: { quarter: 1, year: 2018, sum: 3 } }
   new state is: {"quarter":1,"year":2018,"sum":3}'
*/
dispatcher(initialState, [ actionAdd5, actionDiv2 ], [ logger ])
    .then(state => console.log(`new state is: ${JSON.stringify(state)}`));

emitter(events, eventName, eventParameters) (awaitable)

import emitter from 'evangelist/emitter';

// emitter - static pub/sub sample
const subscriberOne = (value) => console.log(`subscriberOne had value ${value}`);
const subscriberTwo = (value) => console.log(`subscriberTwo had value ${value}`);

const events = {
    printToConsole: [ subscriberOne, subscriberTwo ],
};

/* outputs:
   subscriberOne had value 5
   subscriberTwo had value 5
*/
emitter(events, 'printToConsole', [ 5 ]);

emitter(events, eventName, eventParameters, subscribers) (awaitable)

import emitter from 'evangelist/emitter';

// emitter - event logger sample
const subscriberOne = (value) => console.log(`subscriberOne had value ${value}`);
const subscriberTwo = (value) => console.log(`subscriberTwo had value ${value}`);

const logger = (x) => console.log('INFO', x);

const events = {
    printToConsole: [ subscriberOne, subscriberTwo ],
};

/* outputs:
   INFO { event: 'printToConsole',
     subscriber: 'subscriberOne',
     args: [ 5 ] }
   subscriberOne had value 5
   INFO { event: 'printToConsole',
     subscriber: 'subscriberTwo',
     args: [ 5 ] }
   subscriberTwo had value 5
*/
emitter(events, 'printToConsole', [ 5 ], [ logger ]);

iterate(iterable, func) (awaitable)

import iterate from 'evangelist/iterate';
import compose from 'evangelist/compose';

// iterate - url fetcher example
const generator = function* () {
    yield 'http://localhost/samples/1'; // { value: 1 }
    yield 'http://localhost/samples/2'; // { value: 2 }
    yield 'http://localhost/samples/3'; // { value: 3 }
};

const fetchUrl = async function (url) {
    const response = await fetch(url);
    const document = await response.json();

    return document.value;
}

const add5 = async value => await value + 5;
const printToConsole = async value => { console.log(await value); };

/* outputs:
   value is 6
   value is 7
   value is 8
*/
iterate(
    generator(),
    compose(fetchUrl, add5, printToConsole),
);

Todo List

See GitHub Projects for more.

Requirements

License

Apache 2.0, for further details, please see LICENSE file

Contributing

See contributors.md

It is publicly open for any contribution. Bugfixes, new features and extra modules are welcome.

  • To contribute to code: Fork the repo, push your changes to your fork, and submit a pull request.
  • To report a bug: If something does not work, please report it using GitHub Issues.

To Support

Visit my patreon profile at patreon.com/eserozvataf

About

[Deprecated] 🌟 Library of helpers that are useful for functional programming

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published