| /** | ||
| * 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; |
+13
| 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; |
+32
| 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; |
+22
| 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; |
+33
| /* 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" | ||
| } |
+5
-7
| # afpf - Async Functional Programming Functions | ||
| [](https://npmjs.org/package/afpf) | ||
| [](https://npmjs.org/package/afpf) | ||
| [](https://npmjs.org/package/afpf) [](https://travis-ci.org/Prefinem/afpf) | ||
| [](https://github.com/Prefinem/afpf/actions) [](https://codeclimate.com/github/Prefinem/afpf/maintainability) [](https://codecov.io/gh/Prefinem/afpf) | ||
| [](https://codeclimate.com/github/Prefinem/afpf/maintainability) [](https://codeclimate.com/github/Prefinem/afpf/test_coverage) [](https://greenkeeper.io/) | ||
|  | ||
|    | ||
|   | ||
|   | ||
|   | ||
|   | ||
| afpf is a set of functions that make it easy to write functional scripts with async / await support | ||
@@ -16,0 +14,0 @@ |
-27
| /** | ||
| * 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; |
-13
| 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, | ||
| }; |
-34
| 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; |
-32
| 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; |
-22
| 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; |
-33
| /* 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; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
8
-33.33%7562
-5.88%44
-4.35%1
Infinity%