UNPKG

@redocly/theme

Version:

Shared UI components lib

30 lines (22 loc) 901 B
import type { CodeWalkthroughConditionsObject } from '@redocly/config'; import { isObject } from './type-guards'; export function matchCodeWalkthroughConditions( conditions: CodeWalkthroughConditionsObject = {}, state: Record<string, { value: string | boolean }>, ) { const { when, unless } = conditions; function evaluateCondition(key: string, value?: string | string[] | boolean) { const stateValue = state[key]?.value; if (Array.isArray(value) && typeof stateValue === 'string') { return value.includes(stateValue); } return stateValue === value; } const matchesWhen = isObject(when) ? Object.entries(when).every(([key, value]) => evaluateCondition(key, value)) : true; const matchesUnless = isObject(unless) ? Object.entries(unless).some(([key, value]) => evaluateCondition(key, value)) : false; return matchesWhen && !matchesUnless; }