From 927ee4547440056908382938dc0600e8cb3e0227 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Sat, 8 Sep 2018 11:07:30 +0200 Subject: [PATCH] feat: add dropArgs Closes #679 --- src/dropArgs.js | 23 +++++++++++++++++++++++ src/index.d.ts | 6 ++++++ src/index.js | 1 + test/dropArgs.js | 27 +++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 src/dropArgs.js create mode 100644 test/dropArgs.js diff --git a/src/dropArgs.js b/src/dropArgs.js new file mode 100644 index 0000000000..040c96a16a --- /dev/null +++ b/src/dropArgs.js @@ -0,0 +1,23 @@ +import { nAry } from 'ramda'; + +/** + * Accepts a function with any arity and returns a function with arity of zero. + * The returned function ignores any arguments supplied to it. + * + * @func dropArgs + * @memberOf RA + * @since {@link https://char0n.github.io/ramda-adjunct/2.10.0|v2.10.0} + * @category Logic + * @sig (...a -> b)-> () -> b + * @param {Function} fn The function with any arity + * @return {Function} Returns function with arity of zero + * @see {@link http://ramdajs.com/docs/#nAry|R.nAry} + * @example + * + * const fn = (a = 1, b = 2) => a + b; + * + * RA.dropArgs(fn)(3, 4); //=> 3 + */ +const dropArgs = nAry(0); + +export default dropArgs; diff --git a/src/index.d.ts b/src/index.d.ts index 0af93e0999..1793269829 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -973,6 +973,12 @@ declare namespace RamdaAdjunct { argsPass(combiningPredicate: Function, predicates: Function[]): Function; argsPass(combiningPredicate: Function): (predicates: Function[]) => Function; + /** + * Accepts a function with any arity and returns a function with arity of zero. + * The returned function ignores any arguments supplied to it. + */ + dropArgs(fn: Function): Function; + /** * Creates an array with all falsy values removed. * The values false, null, 0, "", undefined, and NaN are falsy. diff --git a/src/index.js b/src/index.js index 3ca3ad7e4c..dd3c5877fe 100644 --- a/src/index.js +++ b/src/index.js @@ -154,5 +154,6 @@ export { default as neither } from './neither'; export { default as notAllPass } from './notAllPass'; export { default as nonePass } from './nonePass'; export { default as argsPass } from './argsPass'; +export { default as dropArgs } from './dropArgs'; // Types export { default as Identity } from './fantasy-land/Identity'; diff --git a/test/dropArgs.js b/test/dropArgs.js new file mode 100644 index 0000000000..293c16c06b --- /dev/null +++ b/test/dropArgs.js @@ -0,0 +1,27 @@ +import * as R from 'ramda'; + +import * as RA from '../src'; +import eq from './shared/eq'; + +describe('dropArgs', function() { + let fn; + let zeroArityFn; + + beforeEach(function() { + fn = (a = 1, b = 2) => a + b; + zeroArityFn = RA.dropArgs(fn); + }); + + it('should drop all arguments', function() { + const actual = zeroArityFn('ignore1', 'ignore2'); + const expected = 3; + + eq(actual, expected); + }); + + it('should support placeholder to specify "gaps"', function() { + const dropArgs = RA.dropArgs(R.__); + + eq(dropArgs(fn)('ignore1', 'ignore2'), 3); + }); +});