Skip to content

Commit

Permalink
Refactor some code for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurits Rijk committed Jun 29, 2017
1 parent 134bdd8 commit 105c852
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
7 changes: 2 additions & 5 deletions lib/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const _ = require('lodash');
const {gen: tcg} = require('testcheck');

const {invalidString} = require('./conform');
const {explainData} = require('./explainData');
const {firstProblem} = require('./explainData');
const {gen} = require('./gen');

const isValid = require('./isValid');
Expand Down Expand Up @@ -34,10 +34,7 @@ function keys({req = [], opt = []}) {
function* explainInvalid(val, keys, via, required) {
for (let key of keys) {
if (val[key]) {
const expl = explainData(key, val[key], {via, _in: [key]});
if (expl) {
yield expl.problems[0];
}
yield* firstProblem(key, val[key], {via, _in: [key]});
} else if (required) {
yield {
path: [],
Expand Down
21 changes: 12 additions & 9 deletions lib/tuple.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ function tuple(...predicates) {
gen: () => _.map(predicates, gen),
describe: () => [tuple.name, ...describe(predicates)],
explain: function*(values, {via}) {
const f = new Function('', `return values => values.length === ${predicates.length}`);
yield* explainPredicate(f(), values, {via});
yield* explainPredicate(values, predicates, {via});
yield* explainInvalid(values, predicates, via);
}
};
}

function* explainPredicate(pred, value, options) {
if (!pred(value)) {
yield explainData(pred, value, options).problems[0];
function* explainPredicate(values, predicates, options) {
const f = new Function('', `return values => values.length === ${predicates.length}`);
const pred = f();
if (!pred(values)) {
yield explainData(pred, values, options).problems[0];
}
}

function* explainInvalid(values, predicates, via) {
if (values.length !== predicates.length) return;

const index = _.findIndex(_.zip(predicates, values), ([spec, value]) => !isValid(spec, value));

const pairs = _.zip(predicates, values);
const isInvalid = _.negate(_.spread(isValid));
const index = _.findIndex(pairs, isInvalid);
if (index !== -1) {
const val = values[index];
const spec = predicates[index];
const [spec, val] = pairs[index];

const {pred} = explainData(spec, val).problems[0];
yield {
path: [index],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"gitbook-cli": "^2.3.0",
"istanbul": "^0.4.5",
"mocha": "^3.4.2",
"sinon": "^2.3.5",
"sinon": "^2.3.6",
"stryker": "^0.6.6",
"stryker-api": "^0.5.5",
"stryker-html-reporter": "^0.4.6",
Expand Down

0 comments on commit 105c852

Please sign in to comment.