-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch(vest): optionalFunctionValue utility
- Loading branch information
Showing
7 changed files
with
55 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
import optionalFunctionValue from 'optionalFunctionValue'; | ||
|
||
describe('optionalFunctionValue', () => { | ||
describe('When not a function', () => { | ||
it.each([0, undefined, false, true, 1, [], {}, null, NaN])( | ||
'Should return the same value', | ||
value => { | ||
expect(optionalFunctionValue(value)).toBe(value); | ||
} | ||
); | ||
}); | ||
|
||
describe('When value is a function', () => { | ||
it('Should call the function and return its return value', () => { | ||
const value = jest.fn(() => 'return value'); | ||
|
||
expect(optionalFunctionValue(value)).toBe('return value'); | ||
expect(value).toHaveBeenCalled(); | ||
}); | ||
it('Should run with arguments arry', () => { | ||
const value = jest.fn((...args) => args.join('|')); | ||
const args = [1, 2, 3, 4]; | ||
expect(optionalFunctionValue(value, args)).toBe('1|2|3|4'); | ||
expect(value).toHaveBeenCalledWith(...args); | ||
}); | ||
}); | ||
}); |
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,13 @@ | ||
import isFunction from 'isFunction'; | ||
|
||
/** | ||
* Takes a value. If it is a function, runs it and returns the result. | ||
* Otherwise, returns the value as is. | ||
* | ||
* @param {Function|*} value Value to return. Run it if a function. | ||
* @param {Any[]} [args] Arguments to pass if a function | ||
* @return {Any} | ||
*/ | ||
export default function optionalFunctionValue(value, args) { | ||
return isFunction(value) ? value.apply(null, args) : value; | ||
} |
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
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