Skip to content

Commit

Permalink
patch(vest): optionalFunctionValue utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Dec 23, 2020
1 parent 7fce10c commit f3bb418
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 18 deletions.
7 changes: 5 additions & 2 deletions jsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions packages/__shared/__tests__/optionalFunctionValue.test.js
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);
});
});
});
13 changes: 13 additions & 0 deletions packages/__shared/src/optionalFunctionValue.js
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;
}
4 changes: 2 additions & 2 deletions packages/n4s/src/enforce/bindLazyRule.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { RUN_RULE } from 'enforceKeywords';
import genRuleProxy from 'genRuleProxy';
import isFunction from 'isFunction';
import optionalFunctionValue from 'optionalFunctionValue';
import runtimeRules from 'runtimeRules';
import setFnName from 'setFnName';
import withArgs from 'withArgs';
Expand All @@ -27,7 +27,7 @@ export default function bindLazyRule(ruleName) {
// This inversion of control when getting the value is
// required in order to pass the function over to `shape`
// so it can make the decision which args to pass to `optional`
return !!fn(isFunction(getValue) ? getValue(fn.name) : getValue);
return !!fn(optionalFunctionValue(getValue, [fn.name]));
} catch (e) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/n4s/src/enforce/enforce.template.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import runner from 'enforceRunner';
import genRuleProxy from 'genRuleProxy';
import isFunction from 'isFunction';
import optionalFunctionValue from 'optionalFunctionValue';
import runLazyRules from 'runLazyRules';
import runtimeRules from 'runtimeRules';
import withArgs from 'withArgs';
Expand All @@ -19,7 +19,7 @@ export default function bindTemplate(enforce) {
};

template.test = getValue =>
runLazyRules(rule, isFunction(getValue) ? getValue() : getValue);
runLazyRules(rule, optionalFunctionValue(getValue));

return template;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/n4s/src/lib/transformResult.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isBooleanValue from 'isBooleanValue';
import isFunction from 'isFunction';
import optionalFunctionValue from 'optionalFunctionValue';
import throwError from 'throwError';

export function validateResult(result, rule) {
Expand Down Expand Up @@ -49,7 +49,7 @@ export function transformResult(result, { rule, value }) {
if (result.message) {
defaultResult.message = formatResultMessage(
rule,
isFunction(result.message) ? result.message() : result.message
optionalFunctionValue(result.message)
);
}
return defaultResult;
Expand Down
14 changes: 4 additions & 10 deletions packages/vest/src/core/state/state.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

import asArray from 'asArray';
import callEach from 'callEach';
import context from 'ctx';
import isFunction from 'isFunction';
import optionalFunctionValue from 'optionalFunctionValue';

export default (function createState() {
function registerHandler(initialValue) {
Expand All @@ -13,12 +15,7 @@ export default (function createState() {
if (reg) {
key = reg.key;
if (!stateRef.current().hasOwnProperty(key)) {
stateRef.set(
key,
isFunction(initialValue)
? initialValue.apply(null, reg.args)
: initialValue
);
stateRef.set(key, optionalFunctionValue(initialValue, reg.args));
}
return;
}
Expand All @@ -31,10 +28,7 @@ export default (function createState() {
const { stateRef } = context.use();
const currentState = stateRef.current();

stateRef.set(
key,
isFunction(patcher) ? patcher(currentState[key]) : patcher
);
stateRef.set(key, optionalFunctionValue(patcher, [currentState[key]]));
}

return [stateRef.current()[key], update];
Expand Down

0 comments on commit f3bb418

Please sign in to comment.