Skip to content

Commit

Permalink
Refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurits Rijk committed Jul 5, 2017
1 parent 996260d commit fc02b54
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function* explainInvalid(values, specs, via) {
const index = _.findIndex(pairs, isInvalid);
if (index !== -1) {
const [[key, spec], val] = pairs[index];
yield* firstProblem(spec, val, {path: [key], via, _in: [index]})
yield* firstProblem(spec, val, {path: [key], via, _in: [index]});
}
}

Expand Down
42 changes: 22 additions & 20 deletions lib/mapOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,8 @@ const {checkCount} = require('./util/helper');
const describe = require('./util/describe');

function mapOf(kpred, vpred, options = {}) {
const {conformKeys = false} = options;

return {
conform: value => {
const val = getValue(value);
if (checkCount(val, options)) {
let valid = true;

const result = {};
for (let [k, v] of val) {
const conformedKey = conform(kpred, k);
const conformedValue = conform(vpred, v);
valid = valid && conformedKey !== invalidString && conformedValue !== invalidString;
result[conformKeys ? conformedKey : k] = conformedValue;
}

return valid ? result : invalidString;
} else {
return invalidString;
}
},
conform: _.partial(_conform, kpred, vpred, options),
unform: _.identity,
gen: () => tcg.object(gen(kpred), gen(vpred)),
describe: () => [mapOf.name, ...describe([kpred, vpred])],
Expand All @@ -40,6 +21,27 @@ function mapOf(kpred, vpred, options = {}) {
};
}

function _conform(kpred, vpred, options, value) {
const val = getValue(value);

if (checkCount(val, options)) {
const {conformKeys = false} = options;
let valid = true;

const result = {};
for (let [k, v] of val) {
const conformedKey = conform(kpred, k);
const conformedValue = conform(vpred, v);
valid = valid && conformedKey !== invalidString && conformedValue !== invalidString;
result[conformKeys ? conformedKey : k] = conformedValue;
}

return valid ? result : invalidString;
} else {
return invalidString;
}
}

function* explainInvalid(value, kpred, vpred, via) {
for (let val of getValue(value)) {
yield* explainPredicate(kpred, val, via, 0);
Expand Down
1 change: 1 addition & 0 deletions lib/plus.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function* explainInvalid(values, spec, via) {
via,
'in': [index]
};
// yield* firstProblem(spec, val, {via, _in: [index]});
}
}

Expand Down
26 changes: 13 additions & 13 deletions lib/star.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ const {gen} = require('./gen');
const describe = require('./util/describe');
const generate = require('./util/generate');

function _conform(spec, values) {
for (let [head, rest] of generate(values)) {
const conformHead = conform(spec, head, true);
if (conformHead !== invalidString) {
const conformRest = _conform(spec, rest);
if (conformRest !== invalidString) {
return _.concat(conformHead, conformRest);
}
}
}
return _.isEmpty(values) ? values : invalidString;
}

function star(spec) {
return {
op: 'star',
Expand All @@ -35,6 +22,19 @@ function star(spec) {
};
}

function _conform(spec, values) {
for (let [head, rest] of generate(values)) {
const conformHead = conform(spec, head, true);
if (conformHead !== invalidString) {
const conformRest = _conform(spec, rest);
if (conformRest !== invalidString) {
return _.concat(conformHead, conformRest);
}
}
}
return _.isEmpty(values) ? values : invalidString;
}

function* explainInvalid(values, spec, via) {
let index = 0;
for (let val of values) {
Expand Down
9 changes: 3 additions & 6 deletions lib/tuple.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');

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

Expand All @@ -23,10 +23,7 @@ function tuple(...predicates) {

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];
}
yield* firstProblem(f(), values, options);
}

function* explainInvalid(values, predicates, via) {
Expand All @@ -37,7 +34,7 @@ function* explainInvalid(values, predicates, via) {
const index = _.findIndex(pairs, isInvalid);
if (index !== -1) {
const [spec, val] = pairs[index];
yield* firstProblem(spec, val, {path: [index], via, _in: [index]})
yield* firstProblem(spec, val, {path: [index], via, _in: [index]});
}
}

Expand Down

0 comments on commit fc02b54

Please sign in to comment.