Skip to content

Commit

Permalink
Generate isPresent outside of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Oct 22, 2022
1 parent 1e94298 commit 5530c24
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions lib/types/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ module.exports = Any.extend({
for (const dep of schema.$_terms.dependencies) {
if (
dep.key !== null &&
internals.isPresent(dep.key.resolve(value, state, prefs, null, { shadow: false }), dep.options) === false
internals.isPresent(dep.options)(dep.key.resolve(value, state, prefs, null, { shadow: false })) === false
) {

continue;
}

const failed = internals.dependencies[dep.rel](schema, dep, value, state, prefs, dep.options);
const failed = internals.dependencies[dep.rel](schema, dep, value, state, prefs);
if (failed) {
const report = schema.$_createError(failed.code, value, failed.context, state, prefs);
if (prefs.abortEarly) {
Expand Down Expand Up @@ -627,13 +627,14 @@ internals.dependency = function (schema, rel, key, peers, options) {

internals.dependencies = {

and(schema, dep, value, state, prefs, options) {
and(schema, dep, value, state, prefs) {

const missing = [];
const present = [];
const count = dep.peers.length;
const isPresent = internals.isPresent(dep.options);
for (const peer of dep.peers) {
if (internals.isPresent(peer.resolve(value, state, prefs, null, { shadow: false }), options) === false) {
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false })) === false) {
missing.push(peer.key);
}
else {
Expand All @@ -656,11 +657,12 @@ internals.dependencies = {
}
},

nand(schema, dep, value, state, prefs, options) {
nand(schema, dep, value, state, prefs) {

const present = [];
const isPresent = internals.isPresent(dep.options);
for (const peer of dep.peers) {
if (internals.isPresent(peer.resolve(value, state, prefs, null, { shadow: false }), options)) {
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
present.push(peer.key);
}
}
Expand All @@ -682,10 +684,11 @@ internals.dependencies = {
};
},

or(schema, dep, value, state, prefs, options) {
or(schema, dep, value, state, prefs) {

const isPresent = internals.isPresent(dep.options);
for (const peer of dep.peers) {
if (internals.isPresent(peer.resolve(value, state, prefs, null, { shadow: false }), options)) {
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
return;
}
}
Expand All @@ -699,11 +702,12 @@ internals.dependencies = {
};
},

oxor(schema, dep, value, state, prefs, options) {
oxor(schema, dep, value, state, prefs) {

const present = [];
const isPresent = internals.isPresent(dep.options);
for (const peer of dep.peers) {
if (internals.isPresent(peer.resolve(value, state, prefs, null, { shadow: false }), options)) {
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
present.push(peer.key);
}
}
Expand All @@ -720,10 +724,11 @@ internals.dependencies = {
return { code: 'object.oxor', context };
},

with(schema, dep, value, state, prefs, options) {
with(schema, dep, value, state, prefs) {

const isPresent = internals.isPresent(dep.options);
for (const peer of dep.peers) {
if (internals.isPresent(peer.resolve(value, state, prefs, null, { shadow: false }), options) === false) {
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false })) === false) {
return {
code: 'object.with',
context: {
Expand All @@ -737,10 +742,11 @@ internals.dependencies = {
}
},

without(schema, dep, value, state, prefs, options) {
without(schema, dep, value, state, prefs) {

const isPresent = internals.isPresent(dep.options);
for (const peer of dep.peers) {
if (internals.isPresent(peer.resolve(value, state, prefs, null, { shadow: false }), options)) {
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
return {
code: 'object.without',
context: {
Expand All @@ -754,11 +760,12 @@ internals.dependencies = {
}
},

xor(schema, dep, value, state, prefs, options) {
xor(schema, dep, value, state, prefs) {

const present = [];
const isPresent = internals.isPresent(dep.options);
for (const peer of dep.peers) {
if (internals.isPresent(peer.resolve(value, state, prefs, null, { shadow: false }), options)) {
if (isPresent(peer.resolve(value, state, prefs, null, { shadow: false }))) {
present.push(peer.key);
}
}
Expand Down Expand Up @@ -789,10 +796,9 @@ internals.keysToLabels = function (schema, keys) {
};


internals.isPresent = function (resolved, options) {
internals.isPresent = function (options) {

const isPresent = typeof options.isPresent === 'function' ? options.isPresent : () => resolved !== undefined;
return isPresent(resolved);
return typeof options.isPresent === 'function' ? options.isPresent : (resolved) => resolved !== undefined;
};


Expand Down

0 comments on commit 5530c24

Please sign in to comment.