-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from mediamonks/feature/component-id-arg
Allow passing a custom component id
- Loading branch information
Showing
5 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Wraps a function to create a curried argument that can be passed optionally. | ||
* | ||
* @param optionalParamType {string} The type of the optional parameter. If the wrapped function | ||
* recieves an argument of this type, it will return a new function that can be called to pass | ||
* the other arguments. If it doesn't match, the optional argument is considered undefined and | ||
* the inner function `fn` is executed immediately with undefined as the first argument. | ||
* @param fn A function that will receive the optional curried argument in the first parameter. | ||
* @returns {Function} | ||
* | ||
* @example | ||
* const test = optionalParamCurried( | ||
* 'string', | ||
* (opt, a, b) => console.log({ opt, a, b }) | ||
* ); | ||
* | ||
* // intended usage: | ||
* test('abc')(1, 2); // logs { opt: 'abc', a: 1, b: 2 } | ||
* test(123, 456); // logs { opt: undefined, a: 123, b: 456 } | ||
* | ||
* // incorrect usage: | ||
* test(1, 2, 3); // redundant 3rd argument. logs { opt: undefined, a: 1, b: 2 } | ||
* test('abc', 1, 2); // does nothing (returns function) | ||
* | ||
*/ | ||
const optionalParamCurried = (optionalParamType, fn) => (firstArg, ...restArgs) => { | ||
// eslint-disable-next-line valid-typeof | ||
if (typeof firstArg === optionalParamType) { | ||
return (...args) => fn(firstArg, ...args); | ||
} | ||
|
||
return fn(undefined, firstArg, ...restArgs); | ||
}; | ||
|
||
export default optionalParamCurried; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import optionalParamCurried from '../../src/utils/optionalParamCurried'; | ||
|
||
describe('optionalParamCurried', () => { | ||
describe('with optionalParamType "string"', () => { | ||
it('passes undefined if first arg is of type "number"', () => { | ||
{ | ||
const spy = jest.fn(); | ||
const testFn = optionalParamCurried('string', spy); | ||
|
||
testFn(1, 2, 3) | ||
expect(spy).toHaveBeenCalledWith(undefined, 1, 2, 3); | ||
} | ||
}); | ||
it('passes the curried argument if it is of type "string"', () => { | ||
{ | ||
const spy = jest.fn(); | ||
const testFn = optionalParamCurried('string', spy); | ||
|
||
testFn('foo')(1, 2, 3); | ||
expect(spy).toHaveBeenCalledWith('foo', 1, 2, 3); | ||
} | ||
}); | ||
it('does not execute the inner function if only the optional param is passed', () => { | ||
{ | ||
const spy = jest.fn(); | ||
const testFn = optionalParamCurried('string', spy); | ||
|
||
testFn('foo', 1, 2, 3); | ||
expect(spy).not.toHaveBeenCalled(); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters