Skip to content

Commit

Permalink
patch(n4s): Simplify rule result
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jun 14, 2022
1 parent b11c779 commit a6a86a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
9 changes: 5 additions & 4 deletions packages/n4s/src/exports/compose.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import invariant from 'invariant';
import assign from 'assign';
import invariant, { StringObject } from 'invariant';
import mapFirst from 'mapFirst';
import { ctx } from 'n4s';

import type { ComposeResult, LazyRuleRunners } from 'genEnforceLazy';
import { ctx } from 'n4s';
import { defaultToPassing, RuleDetailedResult } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

Expand All @@ -11,11 +12,11 @@ import runLazyRule from 'runLazyRule';
export default function compose(
...composites: LazyRuleRunners[]
): ComposeResult {
return Object.assign(
return assign(
(value: any) => {
const res = run(value);

invariant(res.pass, new String(res.message));
invariant(res.pass, StringObject(res.message));
},
{
run,
Expand Down
15 changes: 6 additions & 9 deletions packages/n4s/src/runtime/enforceEager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import invariant from 'invariant';
import invariant, { StringObject } from 'invariant';
import { isNullish } from 'isNullish';

import eachEnforceRule from 'eachEnforceRule';
import { ctx } from 'enforceContext';
import { isEmpty } from 'isEmpty';
import isProxySupported from 'isProxySupported';
import { getRule, RuleValue, Args, RuleBase, KBaseRules } from 'runtimeRules';
import { transformResult } from 'transformResult';
Expand Down Expand Up @@ -32,18 +32,15 @@ export default function enforceEager(value: RuleValue): IRules {

function genRuleCall(target: IRules, rule: RuleBase, ruleName: string) {
return function ruleCall(...args: Args) {
const transformedResult = transformResult(
ctx.run({ value }, () => rule(value, ...args)),
ruleName,
value,
...args
const transformedResult = ctx.run({ value }, () =>
transformResult(rule(value, ...args), ruleName, value, ...args)
);

invariant(
transformedResult.pass,
isEmpty(transformedResult.message)
isNullish(transformedResult.message)
? `enforce/${ruleName} failed with ${JSON.stringify(value)}`
: new String(transformedResult.message)
: StringObject(transformedResult.message)
);

return target;
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/invariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export default function invariant(
? message.valueOf()
: new Error(message ? optionalFunctionValue(message) : message);
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function StringObject(value?: Stringable): String {
return new String(optionalFunctionValue(value));
}

1 comment on commit a6a86a7

@vercel
Copy link

@vercel vercel bot commented on a6a86a7 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-website.vercel.app
vest-next.vercel.app
vest-next-ealush.vercel.app
vest-next-git-latest-ealush.vercel.app

Please sign in to comment.