@bingo347/fn

A library for declarative programming with higher order functions

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
00Oct 29, 2019May 31, 2018Minified + gzip package size for @bingo347/fn in KB

Readme

fn

A library for declarative programming with higher order functions

Installation

npm i -S @bingo347/fn

or

yarn add @bingo347/fn

Usage example

wrap DOMReady wait to Promise

import makeSubscriber from '@bingo347/fn/make/subscriber';
import extractField from '@bingo347/fn/mappers/extractField';
import chain from '@bingo347/fn/mappers/chain';
import equal from '@bingo347/fn/logic/equal';
import or from '@bingo347/fn/logic/or';

const subscribe = makeSubscriber('addEventListener', 'removeEventListener');
const isDocumentReady = chain(
    extractField('readyState'),
    or(equal('complete'), equal('interactive'))
);

function domReady(doc = document) {
    return (isDocumentReady(doc)
        ? Promise.resolve()
        : new Promise(resolve => {
            const doResolve = () => resolvers.forEach(resolver => resolver());
            const resolvers = [
                subscribe(doc, 'DOMContentLoaded', doResolve),
                subscribe(doc.defaultView, 'load', doResolve),
                resolve
            ];
        })
    );
}

domReady().then(() => {
    //current document is ready
});
domReady(iframe.contentDocument).then(() => {
    //iframe document is ready
});

Two module systems support

All library functions are provided in two variants:

  • ES6 modules with .mjs extension, for use with module bundler (webpack) or use in node.js with --experimental-modules flag or with esm
  • commonJS modules with .js extension, for use in node.js without any helpers

ES5 support

All library functions are provided in ES6 (ES2015) compatible code.
It don't transpiled, but you can use webpack with babel for transpile it for old browsers

// webpack.config.js
module.exports = {
    module: {
        rules: [
            {
                test: /\.m?js$/,
                include: modulePath => {
                    if(modulePath.includes('node_modules/@bingo347/fn')) {
                        // transpile this library
                        return true;
                    }
                    if(modulePath.includes('node_modules')) {
                        // don't transpile other libraries
                        return false;
                    }
                    // transpile your code
                    return true;
                },
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: [['env', {modules: false}]]
                    }
                }]
            },
            // your other webpack module.rules
        ]
    },
    // your other webpack options
}

Documentation

See on the github

License

MIT

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.