Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for grouping conditions with and/or operators #76

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 144 additions & 4 deletions src/includeConditionalArg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ describe('testValue', () => {

describe('includeConditionalArg', () => {
describe('errors', () => {
it('should throw if neither arg nor global is specified', () => {
expect(() =>
includeConditionalArg({ if: {} as Conditional }, {}, {})
).toThrowErrorMatchingInlineSnapshot(`"Invalid conditional value {}"`);
it.each([
['empty if', { if: {} as Conditional }],
['empty and', { if: { and: [{} as Conditional] } }],
['empty or', { if: { or: [{} as Conditional] } }],
])('should throw if neither arg, global, and nor or is specified; %s', (_name, argType) => {
expect(() => includeConditionalArg(argType, {}, {})).toThrowErrorMatchingInlineSnapshot(
`"Invalid conditional value {}"`
);
});
it('should throw if arg and global are both specified', () => {
expect(() =>
Expand Down Expand Up @@ -151,4 +155,140 @@ describe('includeConditionalArg', () => {
});
});
});
describe('and/or collections', () => {
describe('and', () => {
it.each([
['and false', { if: { and: [{ global: 'a' }, { global: 'b' }] } }, {}, { a: 1 }, false],
['and true', { if: { and: [{ global: 'a' }, { global: 'b' }] } }, {}, { a: 1, b: 1 }, true],
[
'mix args and globals',
{ if: { and: [{ arg: 'a' }, { global: 'b' }] } },
{ a: 1 },
{ b: 1 },
true,
],
])('%s', (_name, argType, args, globals, expected) => {
expect(includeConditionalArg(argType, args, globals)).toBe(expected);
});
});
describe('or', () => {
it.each([
['or true', { if: { or: [{ global: 'a' }, { global: 'b' }] } }, {}, { a: 1 }, true],
['or true', { if: { or: [{ global: 'a' }, { global: 'b' }] } }, {}, { c: 1 }, false],
[
'mix args and globals, check arg',
{ if: { or: [{ arg: 'a' }, { global: 'b' }] } },
{ a: 1 },
{},
true,
],
[
'mix args and globals, check global',
{ if: { or: [{ arg: 'a' }, { global: 'b' }] } },
{},
{ b: 1 },
true,
],
])('%s', (_name, argType, args, globals, expected) => {
expect(includeConditionalArg(argType, args, globals)).toBe(expected);
});
});
describe('nesting', () => {
it.each([
['both true', { a: 1, b: 2 }, {}, true],
['first false', { a: 0, b: 2 }, {}, false],
['second false', { a: 1, b: 0 }, {}, false],
['both false', { a: 0, b: 0 }, {}, false],
])('and/or, %s', (_name, args, globals, expected) => {
expect(
includeConditionalArg(
{
if: {
and: [
{
or: [
{ arg: 'a', eq: 1 },
{ arg: 'a', eq: 2 },
],
},
{
or: [
{ arg: 'b', eq: 1 },
{ arg: 'b', eq: 2 },
],
},
],
},
},
args,
globals
)
).toBe(expected);
});

it.each([
['both true', { a: 1, b: 2 }, {}, true],
['first true', { a: 0, b: 2 }, {}, true],
['second true', { a: 1, b: 0 }, {}, true],
['both false', { a: 0, b: 0 }, {}, false],
])('or/or, %s', (_name, args, globals, expected) => {
expect(
includeConditionalArg(
{
if: {
or: [
{
or: [
{ arg: 'a', eq: 1 },
{ arg: 'a', eq: 2 },
],
},
{
or: [
{ arg: 'b', eq: 1 },
{ arg: 'b', eq: 2 },
],
},
],
},
},
args,
globals
)
).toBe(expected);
});

it.each([
['both true', { a: 0, b: 0 }, {}, true],
['first false', { a: 1, b: 0 }, {}, false],
['second false', { a: 0, b: 2 }, {}, false],
['both false', { a: 1, b: 2 }, {}, false],
])('and/and, %s', (_name, args, globals, expected) => {
expect(
includeConditionalArg(
{
if: {
and: [
{
and: [
{ arg: 'a', neq: 1 },
{ arg: 'a', neq: 2 },
],
},
{
and: [
{ arg: 'b', neq: 1 },
{ arg: 'b', neq: 2 },
],
},
],
},
},
args,
globals
)
).toBe(expected);
});
});
});
});
18 changes: 15 additions & 3 deletions src/includeConditionalArg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,21 @@ export const testValue = (cond: Omit<Conditional, 'arg' | 'global'>, value: any)
export const includeConditionalArg = (argType: InputType, args: Args, globals: Globals) => {
if (!argType.if) return true;

const { arg, global } = argType.if as any;
if (count([arg, global]) !== 1) {
throw new Error(`Invalid conditional value ${JSON.stringify({ arg, global })}`);
const { arg, global, and, or } = argType.if as any;
if (count([arg, global, and, or]) !== 1) {
throw new Error(`Invalid conditional value ${JSON.stringify({ arg, global, and, or })}`);
}

if (and) {
return and.every((condition: Conditional) =>
includeConditionalArg({ if: condition }, args, globals)
);
}

if (or) {
return or.some((condition: Conditional) =>
includeConditionalArg({ if: condition }, args, globals)
);
}

const value = arg ? args[arg] : globals[global];
Expand Down
4 changes: 3 additions & 1 deletion src/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export interface StrictParameters {

type ConditionalTest = { truthy?: boolean } | { exists: boolean } | { eq: any } | { neq: any };
type ConditionalValue = { arg: string } | { global: string };
export type Conditional = ConditionalValue & ConditionalTest;
type ConditionalGroup = { and: Conditional[] } | { or: Conditional[] };
type ConditionalItem = ConditionalValue & ConditionalTest;
export type Conditional = ConditionalGroup | ConditionalItem;
export interface InputType {
name?: string;
description?: string;
Expand Down