🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

afpf

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

afpf - npm Package Compare versions

Comparing version
0.0.3
to
0.0.5
+27
src/constant.js
/**
* Wraps a function and allows you to execute said function without changing the initial parameter
*
* Useful for wrapping loggers, timers and other no-op functions
*
* @function
* @since 0.0.1
* @param {Function} func The function to be wrapped
* @returns {Function} The wrapped function which returns the first parameter
* @example
*
* const { constant } = require('afpf');
*
* const log = (message) => console.log(message);
* const wrappedLog = constant(log);
* const output = wrappedLog('test');
* // => 'test'
*
*/
const constant = (func) => async (params, ...rest) => {
await func(params, ...rest);
return params;
};
module.exports = constant;
const constant = require('./constant');
const insert = require('./insert');
const lens = require('./lens');
const log = require('./log');
const pipe = require('./pipe');
module.exports = {
constant,
insert,
lens,
log,
pipe,
};
const insertArray = (index, func) => async (params, ...rest) => [
...params.slice(0, index - 1),
await func(params, ...rest),
...params.slice(index - 1),
];
const insertObject = (index, func) => async (params, ...rest) =>
Object.assign(params, { [index]: await func(params, ...rest) });
/**
* Wraps a function and allows you to execute said function without changing the initial parameter.
*
* Useful for composing objects or arrays
*
* @function
* @since 0.0.1
* @param {String|Int} index The index on the array or object that is being inserted
* @param {Function} func The function to be executed and inserted into the array or object
* @returns {Function} A function which accepts an array or object and returns the array or object with the inserted value
* @example
*
* const { insert } = require('afpf');
*
* const addFoo = () => 'bar';
* const wrappedFn = insert('foo', addFoo);
* const output = wrappedFn({});
* // => { foo: 'bar' }
*
*/
const insert = (index, func) => (params) =>
Array.isArray(params) ? insertArray(index, func)(params) : insertObject(index, func)(params);
module.exports = insert;
const lensArray = (func) => (params, ...rest) => Promise.all(params.map((value) => func(value, ...rest)));
const lensObject = (func) => async (params, ...rest) => {
const records = await Promise.all(Object.keys(params).map((key) => func(params[key], ...rest)));
return records.reduce((result, value, index) => Object.assign(result, { [Object.keys(params)[index]]: value }), {});
};
/**
* Wraps a function and allows you to execute said function on each element in an object or array.
*
* Useful for complex / async mappings of arrays or objects
*
* @function
* @since 0.0.1
* @param {Function} func The function to be executed and inserted into the array or object
* @returns {Function} A function which accepts an array or object and returns the array or object with each key processed by the initial function
* @example
*
* const { lens } = require('afpf');
*
* const addBar = (el) => `${el}Bar`;
* const wrappedFn = lens(addBar);
* const output = wrappedFn(['foo']);
* // => [ 'fooBar' ]
*
*/
const lens = (func) => (params, ...rest) =>
Array.isArray(params) ? lensArray(func)(params, ...rest) : lensObject(func)(params, ...rest);
module.exports = lens;
const constant = require('./constant');
/**
* A simple wrapper that console.logs all parameters sent to it
*
* @function
* @since 0.0.1
* @param {Any} params The parameter to be logged and returned
* @returns {Any} First parameter passed to it
* @example
*
* const { log } = require('afpf');
*
* const output = log(['foo']);
* // => console.log(["foo"]);
* // => output = ['foo'];
*
*/
const log = constant((...params) => console.log(JSON.stringify(params, 4, null)));
module.exports = log;
/* eslint no-await-in-loop: off */
/**
* A function to execute a list of functions in order, passing the response to each function as the initial parameter
*
* @function
* @since 0.0.1
* @param {Function} func Functions to be executed syncronously with their results passing to the next function
* @returns {Function} Function that will execute each function in order while passing the parameters to each function
* @example
*
* const { pipe } = require('afpf');
*
* const fn1 = (message) => `New Message: ${message}`;
* const fn2 = (message, date) => `${date} | ${message}`;
* const fn3 = (message, date, prefix) => `${prefix} === ${message}`;
*
* const output = pipe(fn1, fn2, fn3)('Hello World', '01-01-2018', 'FooBar');
* // => 'FooBar === 01-01-2018 | New Message: Hello World'
*
*/
const pipe = (...funcs) => async (param, ...rest) => {
let response = await funcs[0](param, ...rest);
for (let i = 1; i < funcs.length; i += 1) {
response = await funcs[i](response, ...rest);
}
return response;
};
module.exports = pipe;
+49
-51
{
"author": {
"email": "prefinem@gmail.com",
"name": "Prefinem",
"url": "https://prefinem.com"
},
"devDependencies": {
"codecov": "^3.2.0",
"eslint": "5.14.1",
"eslint-config-prettier": "^4.1.0",
"husky": "^1.0.0",
"jest": "^24.1.0",
"jest-junit": "^6.2.1",
"jsdoc": "^3.5.5",
"jsdoc-template": "https://github.com/braintree/jsdoc-template",
"jsdoc-to-markdown": "^4.0.1",
"lint-staged": "^8.0.0",
"pkg-ok": "^2.1.0",
"prettier": "1.15.3"
},
"homepage": "https://github.com/Prefinem/afpf",
"license": "MIT",
"lint-staged": {
"*.js": [
"eslint --fix",
"prettier --config ./.prettierrc.json --write",
"jest --bail --findRelatedTests",
"git add"
]
},
"name": "afpf",
"repository": {
"type": "git",
"url": "git+https://github.com/Prefinem/afpf"
},
"scripts": {
"build": "./build.sh",
"clean": "yarn lint && yarn prettify",
"docs": "jsdoc --configure .jsdoc.json --verbose",
"docs:md": "jsdoc2md src/**.js >> DOCS.md",
"lint": "eslint src/*.js --fix",
"prettify": "prettier --config ./.prettierrc.json --write \"src/*.js\"",
"pub": "yarn clean && yarn build && yarn publish dist/",
"staged": "lint-staged",
"test": "jest --coverage"
},
"version": "0.0.3",
"husky": {
"hooks": {
"pre-commit": "yarn docs:md && git add DOCS.md && lint-staged"
}
}
"author": {
"email": "prefinem@gmail.com",
"name": "Prefinem",
"url": "https://prefinem.com"
},
"devDependencies": {
"eslint": "6.7.2",
"eslint-config-prettier": "6.7.0",
"husky": "3.1.0",
"jest": "24.9.0",
"jest-junit": "10.0.0",
"jsdoc-to-markdown": "5.0.3",
"lint-staged": "9.5.0",
"prettier": "1.19.1"
},
"files": [
"src/"
],
"homepage": "https://github.com/Prefinem/afpf",
"husky": {
"hooks": {
"pre-commit": "yarn docs && git add DOCS.md && lint-staged"
}
},
"license": "MIT",
"lint-staged": {
"*.js": [
"eslint --fix",
"prettier --config ./.prettierrc.json --write",
"jest --bail --findRelatedTests",
"git add"
]
},
"main": "src/index.js",
"name": "afpf",
"repository": {
"type": "git",
"url": "git+https://github.com/Prefinem/afpf"
},
"scripts": {
"clean": "yarn lint && yarn prettify",
"docs": "jsdoc2md src/**.js >> DOCS.md",
"lint": "eslint src/*.js --fix",
"prettify": "prettier --config ./.prettierrc.json --write \"src/*.js\"",
"pub": "yarn clean && yarn build && yarn publish dist/",
"staged": "lint-staged",
"test": "jest --coverage"
},
"version": "0.0.5"
}
# afpf - Async Functional Programming Functions
[![npm pack age](https://nodei.co/npm/afpf.png?downloads=true&downloadRank=true&stars=true)](https://npmjs.org/package/afpf)
[![Version](https://img.shields.io/npm/v/afpf?style=for-the-badge)](https://npmjs.org/package/afpf)
[![Version](https://badge.fury.io/js/afpf.svg)](https://npmjs.org/package/afpf) [![Build Status](https://travis-ci.org/Prefinem/afpf.svg)](https://travis-ci.org/Prefinem/afpf)
[![Build Status](https://img.shields.io/github/workflow/status/Prefinem/afpf/ci?style=for-the-badge)](https://github.com/Prefinem/afpf/actions) [![Maintainability](https://img.shields.io/codeclimate/coverage-letter/Prefinem/afpf?style=for-the-badge)](https://codeclimate.com/github/Prefinem/afpf/maintainability) [![Test Coverage](https://img.shields.io/codecov/c/github/Prefinem/afpf?style=for-the-badge)](https://codecov.io/gh/Prefinem/afpf)
[![Maintainability](https://api.codeclimate.com/v1/badges/4f911850391938e811f1/maintainability)](https://codeclimate.com/github/Prefinem/afpf/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/4f911850391938e811f1/test_coverage)](https://codeclimate.com/github/Prefinem/afpf/test_coverage) [![Greenkeeper badge](https://badges.greenkeeper.io/Prefinem/afpf.svg)](https://greenkeeper.io/)
![Monthly Downloads](https://img.shields.io/npm/dm/afpf?style=for-the-badge)
![Weekly Downloads](https://img.shields.io/npm/dw/afpf.svg) ![Monthly Downloads](https://img.shields.io/npm/dm/afpf.svg) ![Yearly Downloads](https://img.shields.io/npm/dy/afpf.svg)
![Issues](https://img.shields.io/github/issues/Prefinem/afpf?style=for-the-badge) ![Pull Requests](https://img.shields.io/github/issues-pr/Prefinem/afpf?style=for-the-badge)
![Issues](https://img.shields.io/github/issues/Prefinem/afpf.svg) ![Pull Requests](https://img.shields.io/github/issues-pr/Prefinem/afpf.svg)
![Dependencies](https://img.shields.io/david/Prefinem/afpf?style=for-the-badge) ![Dev Dependencies](https://img.shields.io/david/dev/Prefinem/afpf?style=for-the-badge)
![Dependencies](https://david-dm.org/Prefinem/afpf.svg) ![Dev Dependencies](https://david-dm.org/Prefinem/afpf/dev-status.svg)
afpf is a set of functions that make it easy to write functional scripts with async / await support

@@ -16,0 +14,0 @@

/**
* Wraps a function and allows you to execute said function without changing the initial parameter
*
* Useful for wrapping loggers, timers and other no-op functions
*
* @function
* @since 0.0.1
* @param {Function} func The function to be wrapped
* @returns {Function} The wrapped function which returns the first parameter
* @example
*
* const constant = require('afpf/constant');
*
* const log = (message) => console.log(message);
* const wrappedLog = constant(log);
* const output = wrappedLog('test');
* // => 'test'
*
*/
const constant = (func) => async (params, ...rest) => {
await func(params, ...rest);
return params;
};
module.exports = constant;
const constant = require('./constant');
const insert = require('./insert');
const lens = require('./lens');
const log = require('./log');
const pipe = require('./pipe');
module.exports = {
constant,
insert,
lens,
log,
pipe,
};
const insertArray = (index, func) => async (params, ...rest) => [
...params.slice(0, index - 1),
await func(params, ...rest),
...params.slice(index - 1),
];
const insertObject = (index, func) => async (params, ...rest) =>
Object.assign(params, { [index]: await func(params, ...rest) });
/**
* Wraps a function and allows you to execute said function without changing the initial parameter.
*
* Useful for composing objects or arrays
*
* @function
* @since 0.0.1
* @param {String|Int} index The index on the array or object that is being inserted
* @param {Function} func The function to be executed and inserted into the array or object
* @returns {Function} A function which accepts an array or object and returns the array or object with the inserted value
* @example
*
* const insert = require('afpf/insert');
*
* const addFoo = () => 'bar';
* const wrappedFn = insert('foo', addFoo);
* const output = wrappedFn({});
* // => { foo: 'bar' }
*
*/
const insert = (index, func) => (params) =>
Array.isArray(params) ? insertArray(index, func)(params) : insertObject(index, func)(params);
module.exports = insert;
const lensArray = (func) => (params, ...rest) => Promise.all(params.map((value) => func(value, ...rest)));
const lensObject = (func) => async (params, ...rest) => {
const records = await Promise.all(Object.keys(params).map((key) => func(params[key], ...rest)));
return records.reduce((result, value, index) => Object.assign(result, { [Object.keys(params)[index]]: value }), {});
};
/**
* Wraps a function and allows you to execute said function on each element in an object or array.
*
* Useful for complex / async mappings of arrays or objects
*
* @function
* @since 0.0.1
* @param {Function} func The function to be executed and inserted into the array or object
* @returns {Function} A function which accepts an array or object and returns the array or object with each key processed by the initial function
* @example
*
* const lens = require('afpf/lens');
*
* const addBar = (el) => `${el}Bar`;
* const wrappedFn = lens(addBar);
* const output = wrappedFn(['foo']);
* // => [ 'fooBar' ]
*
*/
const lens = (func) => (params, ...rest) =>
Array.isArray(params) ? lensArray(func)(params, ...rest) : lensObject(func)(params, ...rest);
module.exports = lens;
const constant = require('./constant');
/**
* A simple wrapper that console.logs all parameters sent to it
*
* @function
* @since 0.0.1
* @param {Any} params The parameter to be logged and returned
* @returns {Any} First parameter passed to it
* @example
*
* const log = require('afpf/log');
*
* const output = log(['foo']);
* // => console.log(["foo"]);
* // => output = ['foo'];
*
*/
const log = constant((...params) => console.log(JSON.stringify(params, 4, null)));
module.exports = log;
/* eslint no-await-in-loop: off */
/**
* A function to execute a list of functions in order, passing the response to each function as the initial parameter
*
* @function
* @since 0.0.1
* @param {Function} func Functions to be executed syncronously with their results passing to the next function
* @returns {Function} Function that will execute each function in order while passing the parameters to each function
* @example
*
* const pipe = require('afpf/pipe');
*
* const fn1 = (message) => `New Message: ${message}`;
* const fn2 = (message, date) => `${date} | ${message}`;
* const fn3 = (message, date, prefix) => `${prefix} === ${message}`;
*
* const output = pipe(fn1, fn2, fn3)('Hello World', '01-01-2018', 'FooBar');
* // => 'FooBar === 01-01-2018 | New Message: Hello World'
*
*/
const pipe = (...funcs) => async (param, ...rest) => {
let response = await funcs[0](param, ...rest);
for (let i = 1; i < funcs.length; i += 1) {
response = await funcs[i](response, ...rest);
}
return response;
};
module.exports = pipe;