Skip to content

Commit

Permalink
patch: mapFirst move breakout conditionals inside
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jun 14, 2022
1 parent a6a86a7 commit 28db6c7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 30 deletions.
6 changes: 2 additions & 4 deletions packages/n4s/src/exports/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function compose(
composites,
(
composite: LazyRuleRunners,
breakout: (res: RuleDetailedResult) => void
breakout: (conditional: boolean, res: RuleDetailedResult) => void
) => {
/* HACK: Just a small white lie. ~~HELP WANTED~~.
The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with.
Expand All @@ -41,9 +41,7 @@ export default function compose(

const res = runLazyRule(composite, value);

if (!res.pass) {
breakout(res);
}
breakout(!res.pass, res);
}
)
);
Expand Down
4 changes: 1 addition & 3 deletions packages/n4s/src/plugins/compounds/allOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export function allOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult {
return ruleReturn.defaultToPassing(
mapFirst(rules, (rule, breakout) => {
const res = runLazyRule(rule, value);
if (!res.pass) {
breakout(res);
}
breakout(!res.pass, res);
})
);
}
4 changes: 1 addition & 3 deletions packages/n4s/src/plugins/compounds/anyOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export function anyOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult {
return ruleReturn.defaultToFailing(
mapFirst(rules, (rule, breakout) => {
const res = runLazyRule(rule, value);
if (res.pass) {
breakout(res);
}
breakout(res.pass, res);
})
);
}
4 changes: 1 addition & 3 deletions packages/n4s/src/plugins/compounds/noneOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export function noneOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult {
mapFirst(rules, (rule, breakout) => {
const res = runLazyRule(rule, value);

if (res.pass) {
breakout(ruleReturn.failing());
}
breakout(res.pass, ruleReturn.failing());
})
);
}
6 changes: 2 additions & 4 deletions packages/n4s/src/plugins/schema/isArrayOf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mapFirst from 'mapFirst';
import { ctx } from 'n4s';

import type { LazyRuleRunners } from 'genEnforceLazy';
import { ctx } from 'n4s';
import type { RuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';
Expand All @@ -17,9 +17,7 @@ export function isArrayOf(
() => runLazyRule(currentRule, currentValue)
);

if (!res.pass) {
breakout(res);
}
breakout(!res.pass, res);
})
);
}
17 changes: 8 additions & 9 deletions packages/n4s/src/runtime/genEnforceLazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ export default function genEnforceLazy(key: string) {
mapFirst(registeredRules, (rule, breakout) => {
const res = ctx.run({ value }, () => rule(value));

if (!res.pass) {
breakout(
ruleReturn(
!!res.pass,
optionalFunctionValue(lazyMessage, value, res.message) ??
res.message
)
);
}
breakout(
!res.pass,
ruleReturn(
!!res.pass,
optionalFunctionValue(lazyMessage, value, res.message) ??
res.message
)
);
})
);
},
Expand Down
10 changes: 6 additions & 4 deletions packages/shared/src/mapFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default function mapFirst<T>(
array: T[],
callback: (
item: T,
breakout: (value: unknown) => void,
breakout: (conditional: boolean, value: unknown) => void,
index: number
) => unknown
): any {
Expand All @@ -16,8 +16,10 @@ export default function mapFirst<T>(
}
}

function breakout(value: unknown) {
broke = true;
breakoutValue = value;
function breakout(conditional: boolean, value: unknown) {
if (conditional) {
broke = true;
breakoutValue = value;
}
}
}

1 comment on commit 28db6c7

@vercel
Copy link

@vercel vercel bot commented on 28db6c7 Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next.vercel.app
vest-next-ealush.vercel.app
vest-next-git-latest-ealush.vercel.app
vest-website.vercel.app

Please sign in to comment.