@openbytes/ts-react-directives 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require('./index.css');const c=require("react/jsx-runtime"),h=require("react");var t=(e=>(e[e.IfBlockExpected=2001]="IfBlockExpected",e[e.OnlyOneIfBlockExpected=2002]="OnlyOneIfBlockExpected",e[e.OnlyOneElseBlockExpected=2003]="OnlyOneElseBlockExpected",e[e.SwitchBlockExpected=2005]="SwitchBlockExpected",e[e.InvalidIfBlockOrdinal=2004]="InvalidIfBlockOrdinal",e[e.InvalidElseBlockOrdinal=2006]="InvalidElseBlockOrdinal",e[e.InvalidElseIfBlockOrdinal=2007]="InvalidElseIfBlockOrdinal",e[e.InvalidElement=3002]="InvalidElement",e[e.ChildrenExpected=3003]="ChildrenExpected",e))(t||{});const m={[t.IfBlockExpected]:"Missing 'If'",[t.OnlyOneIfBlockExpected]:"Can only have one 'If'",[t.OnlyOneElseBlockExpected]:"Can only have one 'Else'",[t.SwitchBlockExpected]:"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'",[t.InvalidElseBlockOrdinal]:"Invalid ordinals, 'Else' should be the last",[t.InvalidIfBlockOrdinal]:"Invalid ordinals, 'If' block should be the first",[t.InvalidElseIfBlockOrdinal]:"Invalid ordinals, cannot have 'ElseIf' before 'If'",[t.ChildrenExpected]:"Should at least have one child",[t.InvalidElement]:"Invalid element"},I=({errors:e})=>c.jsx("ol",{className:"trd-error-list",children:e.map((l,n)=>c.jsx("li",{children:m[l]},`${l}-${n}`))}),p=e=>{const l=[];return e.length===0?(l.push(t.ChildrenExpected),l):(e.forEach(n=>{const{displayName:a}=n.type??{};(a===i.displayName||a===f.displayName||a===s.displayName)&&l.push(t.SwitchBlockExpected)}),l)},k=e=>{const l=[],n={};return e.length===0?(l.push(t.ChildrenExpected,t.IfBlockExpected),l):(e.forEach((a,d)=>{const{displayName:o="unknown"}=a.type,u=n[o]??0;n[o]=u+1,v(o,d,n,l),B(o,d,e.length,n,l),y(o,d,l),O(o,l)}),n[i.name]||l.push(t.IfBlockExpected),l)},v=(e,l,n,a)=>{e===i.name&&(l!==0&&a.push(t.InvalidIfBlockOrdinal),n[i.name]>1&&a.push(t.OnlyOneIfBlockExpected))},B=(e,l,n,a,d)=>{e===s.name&&(a[s.name]>1&&d.push(t.OnlyOneElseBlockExpected),(l===0||l!==n-1)&&d.push(t.InvalidElseBlockOrdinal))},y=(e,l,n)=>{e===f.name&&l===0&&n.push(t.InvalidElseIfBlockOrdinal)},O=(e,l)=>{e!==i.name&&e!==f.name&&e!==s.name&&l.push(t.InvalidElement)};class x{static get(l){let n;switch(l){case E.displayName:n=k;break;case i.displayName:case f.displayName:case s.displayName:n=p;break;default:n=()=>[]}return n}}const r=(e,l)=>{const{children:n}=e,a=h.Children.toArray(n);return x.get(l)(a)},S=e=>{const l=r(e,"SwitchIf");if(l.length)return{children:h.createElement(I,{errors:l})};const a=h.Children.toArray(e.children).filter(h.isValidElement);return{children:w(a)}},w=e=>{for(const l of e){if(!l||!h.isValidElement(l))continue;const{displayName:n}=l.type;if(n){const{condition:a}=l.props;if((n==="If"||n==="ElseIf")&&a===!0||n==="Else")return l}}return null},E=e=>{const{children:l}=S(e);return c.jsx(c.Fragment,{children:l})};E.displayName="SwitchIf";const i=e=>{const l=r(e,"If"),{condition:n}=e,a=l.length===0?e.children:c.jsx(I,{errors:l});return c.jsx(c.Fragment,{children:n&&a})};i.displayName="If";const f=e=>{const l=r(e,"ElseIf"),{condition:n}=e,a=l.length===0?e.children:c.jsx(I,{errors:l});return c.jsx(c.Fragment,{children:n&&a})};f.displayName="ElseIf";const s=e=>{const l=r(e,"Else"),n=l.length===0?e.children:c.jsx(I,{errors:l});return c.jsx(c.Fragment,{children:n})};s.displayName="Else";exports.Else=s;exports.ElseIf=f;exports.If=i;exports.SwitchIf=E;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require('./index.css');const s=require("react/jsx-runtime"),f=require("react");var t=(e=>(e[e.IfBlockExpected=2001]="IfBlockExpected",e[e.OnlyOneIfBlockExpected=2002]="OnlyOneIfBlockExpected",e[e.OnlyOneElseBlockExpected=2003]="OnlyOneElseBlockExpected",e[e.SwitchBlockExpected=2005]="SwitchBlockExpected",e[e.InvalidIfBlockOrdinal=2004]="InvalidIfBlockOrdinal",e[e.InvalidElseBlockOrdinal=2006]="InvalidElseBlockOrdinal",e[e.InvalidElseIfBlockOrdinal=2007]="InvalidElseIfBlockOrdinal",e[e.InvalidElement=3002]="InvalidElement",e[e.ChildrenExpected=3003]="ChildrenExpected",e))(t||{}),c=(e=>(e.If="If",e.ElseIf="ElseIf",e.Else="Else",e.SwitchIf="SwitchIf",e.Unknown="Unknown",e))(c||{});const v={[t.IfBlockExpected]:"Missing 'If'",[t.OnlyOneIfBlockExpected]:"Can only have one 'If'",[t.OnlyOneElseBlockExpected]:"Can only have one 'Else'",[t.SwitchBlockExpected]:"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'",[t.InvalidElseBlockOrdinal]:"Invalid ordinals, 'Else' should be the last",[t.InvalidIfBlockOrdinal]:"Invalid ordinals, 'If' block should be the first",[t.InvalidElseIfBlockOrdinal]:"Invalid ordinals, cannot have 'ElseIf' before 'If'",[t.ChildrenExpected]:"Should at least have one child",[t.InvalidElement]:"Invalid element"},I=({errors:e})=>s.jsx("ol",{className:"trd-error-list",children:e.map((l,n)=>s.jsx("li",{children:v[l]},`${l}-${n}`))}),B=e=>{const l=[];return e.length===0?(l.push(t.ChildrenExpected),l):(e.forEach(n=>{const{displayName:i=c.Unknown}=n.type??{};(i===c.If||i===c.ElseIf||i===c.Else)&&(d("If, ElseIf, Else cannot be direct children of If, ElseIf, Else"),l.push(t.InvalidElement),l.push(t.SwitchBlockExpected))}),l)},x=e=>{const l=[],n={};return d("1. validate switch if"),e.length===0?(d("2. children expected"),l.push(t.ChildrenExpected,t.IfBlockExpected),l):(e.forEach((i,a)=>{const{displayName:o=c.Unknown}=i.type;d("3. validate switch if - display name",o);const k=n[o]??0;n[o]=k+1,d("4. element look up",n),O(o,a,n,l),w(o,a,e.length,n,l),y(o,a,l),m(o,l)}),n[r.name]||(d("5. if block expected - no if in lookup"),l.push(t.IfBlockExpected)),l)},O=(e,l,n,i)=>{d("6. validate if block",e,l,n),e===c.If&&(l!==0&&i.push(t.InvalidIfBlockOrdinal),n[c.If]>1&&i.push(t.OnlyOneIfBlockExpected))},w=(e,l,n,i,a)=>{e===c.Else&&(i[c.Else]>1&&a.push(t.OnlyOneElseBlockExpected),(l===0||l!==n-1)&&a.push(t.InvalidElseBlockOrdinal))},y=(e,l,n)=>{e===c.ElseIf&&l===0&&n.push(t.InvalidElseIfBlockOrdinal)},m=(e,l)=>{e!==c.If&&e!==c.ElseIf&&e!==c.Else&&l.push(t.InvalidElement)};class S{static get(l){let n=()=>[];switch(l){case c.SwitchIf:d("getting validation fn for switch if"),n=x;break;case c.If:case c.ElseIf:case c.Else:d("getting validation fn for switch if children"),n=B;break}return n}}const d=(e,...l)=>{const n=`__${e.trim().toLocaleUpperCase().replace(/(' ')+/g,"_")}__`;console.log(n,...l)},h=(e,l)=>{const{children:n}=e,i=f.Children.toArray(n);return S.get(l)(i)},b=e=>{const l=h(e,"SwitchIf");if(l.length)return{children:f.createElement(I,{errors:l})};const i=f.Children.toArray(e.children).filter(f.isValidElement);return{children:C(i)}},C=e=>{for(const l of e){if(!l||!f.isValidElement(l))continue;const{displayName:n}=l.type;if(n){const{condition:i}=l.props;if((n==="If"||n==="ElseIf")&&i===!0||n==="Else")return l}}return null},E=e=>{const{children:l}=b(e);return s.jsx(s.Fragment,{children:l})};E.displayName=c.SwitchIf;const r=e=>{const l=h(e,"If"),{condition:n}=e,i=l.length===0?e.children:s.jsx(I,{errors:l});return s.jsx(s.Fragment,{children:n&&i})};r.displayName=c.If;const p=e=>{const l=h(e,"ElseIf"),{condition:n}=e,i=l.length===0?e.children:s.jsx(I,{errors:l});return s.jsx(s.Fragment,{children:n&&i})};p.displayName=c.ElseIf;const u=e=>{const l=h(e,"Else"),n=l.length===0?e.children:s.jsx(I,{errors:l});return s.jsx(s.Fragment,{children:n})};u.displayName=c.Else;exports.Else=u;exports.ElseIf=p;exports.If=r;exports.SwitchIf=E;
2
2
  //# sourceMappingURL=ts-react-directives.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ts-react-directives.cjs.js","sources":["../src/fixtures/enums.ts","../src/fixtures/constants.ts","../src/components/Errors.tsx","../src/utils/validators.ts","../src/hooks/useValidate.ts","../src/directives/if/SwitchIf.tsx","../src/directives/if/If.tsx","../src/directives/if/ElseIf.tsx","../src/directives/if/Else.tsx"],"sourcesContent":["export enum LogicErrors {\r\n // Switch\r\n IfBlockExpected = 2001,\r\n OnlyOneIfBlockExpected = 2002,\r\n OnlyOneElseBlockExpected = 2003,\r\n SwitchBlockExpected = 2005,\r\n InvalidIfBlockOrdinal = 2004,\r\n InvalidElseBlockOrdinal = 2006,\r\n InvalidElseIfBlockOrdinal = 2007,\r\n\r\n // General\r\n\r\n InvalidElement = 3002,\r\n ChildrenExpected = 3003,\r\n}\r\n","import { LogicErrors } from './enums';\r\n\r\nexport const ERRORS: Record<LogicErrors, string> = {\r\n [LogicErrors.IfBlockExpected]: \"Missing 'If'\",\r\n [LogicErrors.OnlyOneIfBlockExpected]: \"Can only have one 'If'\",\r\n [LogicErrors.OnlyOneElseBlockExpected]: \"Can only have one 'Else'\",\r\n [LogicErrors.SwitchBlockExpected]: \"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'\",\r\n [LogicErrors.InvalidElseBlockOrdinal]: \"Invalid ordinals, 'Else' should be the last\",\r\n [LogicErrors.InvalidIfBlockOrdinal]: \"Invalid ordinals, 'If' block should be the first\",\r\n [LogicErrors.InvalidElseIfBlockOrdinal]: \"Invalid ordinals, cannot have 'ElseIf' before 'If'\",\r\n [LogicErrors.ChildrenExpected]: 'Should at least have one child',\r\n [LogicErrors.InvalidElement]: 'Invalid element',\r\n};\r\n","import { FC } from 'react';\r\nimport { ERRORS, LogicErrors } from '@fixtures';\r\n\r\nimport '@assets/index.css';\r\n\r\ntype ErrorProps = { errors: LogicErrors[] };\r\n\r\nconst Errors: FC<ErrorProps> = ({ errors }) => (\r\n <ol className=\"trd-error-list\">\r\n {errors.map((error, index) => (\r\n <li key={`${error}-${index}`}>{ERRORS[error]}</li>\r\n ))}\r\n </ol>\r\n);\r\n\r\nexport default Errors;\r\n","import { Else, ElseIf, If, SwitchIf } from '@directives';\r\nimport { LogicErrors } from '@fixtures';\r\n\r\nimport { ReactNode } from 'react';\r\n\r\ntype ValidatorFn = (children: Array<Exclude<ReactNode, boolean | null | undefined>>) => number[];\r\n\r\nconst validateSwitchIfChildren: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName } = child.type ?? {};\r\n\r\n // If, ElseIf, Else cannot be direct children of If, ElseIf, Else\r\n if (displayName === If.displayName || displayName === ElseIf.displayName || displayName === Else.displayName) {\r\n errors.push(LogicErrors.SwitchBlockExpected);\r\n }\r\n });\r\n\r\n return errors;\r\n};\r\n\r\nconst validateSwitchIf: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n const elementLookup: Record<string, number> = {};\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected, LogicErrors.IfBlockExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child, index) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = 'unknown' } = child.type;\r\n\r\n const count = elementLookup[displayName] ?? 0;\r\n elementLookup[displayName] = count + 1;\r\n\r\n validateIfBlock(displayName, index, elementLookup, errors);\r\n validateElseBlock(displayName, index, children.length, elementLookup, errors);\r\n validateElseIfBlock(displayName, index, errors);\r\n validateSwitchIfInvalidElement(displayName, errors);\r\n });\r\n\r\n if (!elementLookup[If.name]) {\r\n errors.push(LogicErrors.IfBlockExpected);\r\n }\r\n\r\n return errors;\r\n};\r\n\r\nconst validateIfBlock = (typeName: string, index: number, elements: Record<string, number>, errors: LogicErrors[]) => {\r\n if (typeName === If.name) {\r\n if (index !== 0) {\r\n errors.push(LogicErrors.InvalidIfBlockOrdinal);\r\n }\r\n\r\n if (elements[If.name] > 1) {\r\n errors.push(LogicErrors.OnlyOneIfBlockExpected);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseBlock = (\r\n typeName: string,\r\n index: number,\r\n length: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n if (typeName === Else.name) {\r\n if (elements[Else.name] > 1) {\r\n errors.push(LogicErrors.OnlyOneElseBlockExpected);\r\n }\r\n if (index === 0 || index !== length - 1) {\r\n errors.push(LogicErrors.InvalidElseBlockOrdinal);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseIfBlock = (typeName: string, index: number, errors: LogicErrors[]) => {\r\n if (typeName === ElseIf.name && index === 0) {\r\n errors.push(LogicErrors.InvalidElseIfBlockOrdinal);\r\n }\r\n};\r\n\r\nconst validateSwitchIfInvalidElement = (typeName: string, errors: LogicErrors[]) => {\r\n if (typeName !== If.name && typeName !== ElseIf.name && typeName !== Else.name) {\r\n errors.push(LogicErrors.InvalidElement);\r\n }\r\n};\r\n\r\nexport class ValidationFactory {\r\n static get(validator: string) {\r\n let validatorFn: ValidatorFn;\r\n switch (validator) {\r\n case SwitchIf.displayName:\r\n validatorFn = validateSwitchIf;\r\n break;\r\n case If.displayName:\r\n case ElseIf.displayName:\r\n case Else.displayName:\r\n validatorFn = validateSwitchIfChildren;\r\n break;\r\n default:\r\n validatorFn = () => [];\r\n }\r\n\r\n return validatorFn;\r\n }\r\n}\r\n","import { ValidationFactory } from '@utils';\r\nimport { Children, PropsWithChildren } from 'react';\r\n\r\nexport const useValidate = (props: PropsWithChildren, name: string) => {\r\n const { children } = props;\r\n const _children = Children.toArray(children);\r\n\r\n return ValidationFactory.get(name)(_children);\r\n};\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport {\r\n Children,\r\n createElement,\r\n FC,\r\n FunctionComponent,\r\n isValidElement,\r\n NamedExoticComponent,\r\n PropsWithChildren,\r\n ReactElement,\r\n} from 'react';\r\n\r\nexport type SwitchIfProps = {};\r\n\r\nconst useSwitchIf = (props: PropsWithChildren) => {\r\n const errors = useValidate(props, 'SwitchIf');\r\n\r\n if (errors.length) {\r\n return { children: createElement(Errors, { errors }) };\r\n }\r\n\r\n const elements = Children.toArray(props.children);\r\n const validElements = elements.filter(isValidElement);\r\n const child = findChild(validElements);\r\n\r\n return { children: child };\r\n};\r\n\r\nconst findChild = (elements: ReactElement[]) => {\r\n for (const element of elements) {\r\n if (!element || !isValidElement(element)) {\r\n continue;\r\n }\r\n\r\n const { displayName } = element.type as NamedExoticComponent | FunctionComponent;\r\n\r\n if (displayName) {\r\n const { condition } = element.props as { condition: boolean };\r\n\r\n if ((displayName === 'If' || displayName === 'ElseIf') && condition === true) {\r\n return element;\r\n }\r\n\r\n if (displayName === 'Else') {\r\n return element;\r\n }\r\n }\r\n }\r\n\r\n return null;\r\n};\r\n\r\nconst SwitchIf: FC<PropsWithChildren<SwitchIfProps>> = (props) => {\r\n const { children } = useSwitchIf(props);\r\n return <>{children}</>;\r\n};\r\n\r\nSwitchIf.displayName = 'SwitchIf';\r\n\r\nexport default SwitchIf;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type IfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst If: FC<IfProps> = (props) => {\r\n const errors = useValidate(props, 'If');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nIf.displayName = 'If';\r\n\r\nexport default If;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseIfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst ElseIf: FC<ElseIfProps> = (props) => {\r\n const errors = useValidate(props, 'ElseIf');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nElseIf.displayName = 'ElseIf';\r\n\r\nexport default ElseIf;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseProps = {};\r\n\r\nconst Else: FC<PropsWithChildren<ElseProps>> = (props) => {\r\n const errors = useValidate(props, 'Else');\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{children}</>;\r\n};\r\n\r\nElse.displayName = 'Else';\r\n\r\nexport default Else;\r\n"],"names":["LogicErrors","ERRORS","Errors","errors","error","index","jsx","validateSwitchIfChildren","children","child","displayName","If","ElseIf","Else","validateSwitchIf","elementLookup","count","validateIfBlock","validateElseBlock","validateElseIfBlock","validateSwitchIfInvalidElement","typeName","elements","length","ValidationFactory","validator","validatorFn","SwitchIf","useValidate","props","name","_children","Children","useSwitchIf","createElement","validElements","isValidElement","findChild","element","condition","Fragment"],"mappings":"wIAAY,IAAAA,GAAAA,IAEVA,EAAAA,EAAA,gBAAkB,IAAlB,EAAA,kBACAA,EAAAA,EAAA,uBAAyB,IAAzB,EAAA,yBACAA,EAAAA,EAAA,yBAA2B,IAA3B,EAAA,2BACAA,EAAAA,EAAA,oBAAsB,IAAtB,EAAA,sBACAA,EAAAA,EAAA,sBAAwB,IAAxB,EAAA,wBACAA,EAAAA,EAAA,wBAA0B,IAA1B,EAAA,0BACAA,EAAAA,EAAA,0BAA4B,IAA5B,EAAA,4BAIA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GAAU,CACnC,MAAA1B,EAASyB,EAAYC,EAAO,QAAQ,EACpC,CAAE,UAAAU,GAAcV,EAChBrB,EAAWL,EAAO,SAAW,EAAI0B,EAAM,SAAWvB,MAACJ,GAAO,OAAAC,EAAgB,EAEzE,OAAAG,EAAAA,IAAAkC,EAAAA,SAAA,CAAG,YAAahC,CAAS,CAAA,CAClC,EAEAI,EAAO,YAAc,SCVf,MAAAC,EAA0CgB,GAAU,CAClD,MAAA1B,EAASyB,EAAYC,EAAO,MAAM,EAClCrB,EAAWL,EAAO,SAAW,EAAI0B,EAAM,SAAWvB,MAACJ,GAAO,OAAAC,EAAgB,EAEhF,yBAAU,SAAAK,EAAS,CACrB,EAEAK,EAAK,YAAc"}
1
+ {"version":3,"file":"ts-react-directives.cjs.js","sources":["../src/fixtures/enums.ts","../src/fixtures/constants.ts","../src/components/Errors.tsx","../src/utils/validators.ts","../src/utils/logger.ts","../src/hooks/useValidate.ts","../src/directives/if/SwitchIf.tsx","../src/directives/if/If.tsx","../src/directives/if/ElseIf.tsx","../src/directives/if/Else.tsx"],"sourcesContent":["export enum LogicErrors {\r\n // Switch\r\n IfBlockExpected = 2001,\r\n OnlyOneIfBlockExpected = 2002,\r\n OnlyOneElseBlockExpected = 2003,\r\n SwitchBlockExpected = 2005,\r\n InvalidIfBlockOrdinal = 2004,\r\n InvalidElseBlockOrdinal = 2006,\r\n InvalidElseIfBlockOrdinal = 2007,\r\n\r\n // General\r\n\r\n InvalidElement = 3002,\r\n ChildrenExpected = 3003,\r\n}\r\n\r\nexport enum DirectiveNames {\r\n If = 'If',\r\n ElseIf = 'ElseIf',\r\n Else = 'Else',\r\n SwitchIf = 'SwitchIf',\r\n Unknown = 'Unknown',\r\n}\r\n","import { LogicErrors } from './enums';\r\n\r\nexport const ERRORS: Record<LogicErrors, string> = {\r\n [LogicErrors.IfBlockExpected]: \"Missing 'If'\",\r\n [LogicErrors.OnlyOneIfBlockExpected]: \"Can only have one 'If'\",\r\n [LogicErrors.OnlyOneElseBlockExpected]: \"Can only have one 'Else'\",\r\n [LogicErrors.SwitchBlockExpected]: \"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'\",\r\n [LogicErrors.InvalidElseBlockOrdinal]: \"Invalid ordinals, 'Else' should be the last\",\r\n [LogicErrors.InvalidIfBlockOrdinal]: \"Invalid ordinals, 'If' block should be the first\",\r\n [LogicErrors.InvalidElseIfBlockOrdinal]: \"Invalid ordinals, cannot have 'ElseIf' before 'If'\",\r\n [LogicErrors.ChildrenExpected]: 'Should at least have one child',\r\n [LogicErrors.InvalidElement]: 'Invalid element',\r\n};\r\n","import { FC } from 'react';\r\nimport { ERRORS, LogicErrors } from '@fixtures';\r\n\r\nimport '@assets/index.css';\r\n\r\ntype ErrorProps = { errors: LogicErrors[] };\r\n\r\nconst Errors: FC<ErrorProps> = ({ errors }) => (\r\n <ol className=\"trd-error-list\">\r\n {errors.map((error, index) => (\r\n <li key={`${error}-${index}`}>{ERRORS[error]}</li>\r\n ))}\r\n </ol>\r\n);\r\n\r\nexport default Errors;\r\n","import { If } from '@directives';\r\nimport { DirectiveNames, LogicErrors } from '@fixtures';\r\nimport { log } from '@utils';\r\n\r\nimport { ReactNode } from 'react';\r\n\r\ntype ValidatorFn = (children: Array<Exclude<ReactNode, boolean | null | undefined>>) => number[];\r\n\r\n/**\r\n * Validate the children of SwitchIf directive\r\n * @param children\r\n * @returns\r\n */\r\nconst validateSwitchIfChildren: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = DirectiveNames.Unknown } = child.type ?? {};\r\n\r\n // If, ElseIf, Else cannot be direct children of If, ElseIf, Else\r\n if (\r\n displayName === DirectiveNames.If ||\r\n displayName === DirectiveNames.ElseIf ||\r\n displayName === DirectiveNames.Else\r\n ) {\r\n log('If, ElseIf, Else cannot be direct children of If, ElseIf, Else');\r\n errors.push(LogicErrors.InvalidElement);\r\n errors.push(LogicErrors.SwitchBlockExpected);\r\n }\r\n });\r\n\r\n return errors;\r\n};\r\n\r\n/**\r\n * Validate the SwitchIf directive\r\n * @param children\r\n * @returns\r\n */\r\nconst validateSwitchIf: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n const elementLookup: Record<string, number> = {};\r\n\r\n log('1. validate switch if');\r\n\r\n if (children.length === 0) {\r\n log('2. children expected');\r\n errors.push(LogicErrors.ChildrenExpected, LogicErrors.IfBlockExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child, index) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = DirectiveNames.Unknown } = child.type;\r\n log('3. validate switch if - display name', displayName);\r\n\r\n const count = elementLookup[displayName] ?? 0;\r\n elementLookup[displayName] = count + 1;\r\n\r\n log('4. element look up', elementLookup);\r\n\r\n validateIfBlock(displayName, index, elementLookup, errors);\r\n validateElseBlock(displayName, index, children.length, elementLookup, errors);\r\n validateElseIfBlock(displayName, index, errors);\r\n validateSwitchIfInvalidElement(displayName, errors);\r\n });\r\n\r\n if (!elementLookup[If.name]) {\r\n log('5. if block expected - no if in lookup');\r\n errors.push(LogicErrors.IfBlockExpected);\r\n }\r\n\r\n return errors;\r\n};\r\n\r\nconst validateIfBlock = (\r\n elementName: string,\r\n index: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n log('6. validate if block', elementName, index, elements);\r\n if (elementName === DirectiveNames.If) {\r\n if (index !== 0) {\r\n errors.push(LogicErrors.InvalidIfBlockOrdinal);\r\n }\r\n\r\n if (elements[DirectiveNames.If] > 1) {\r\n errors.push(LogicErrors.OnlyOneIfBlockExpected);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseBlock = (\r\n elementName: string,\r\n index: number,\r\n length: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n if (elementName === DirectiveNames.Else) {\r\n if (elements[DirectiveNames.Else] > 1) {\r\n errors.push(LogicErrors.OnlyOneElseBlockExpected);\r\n }\r\n if (index === 0 || index !== length - 1) {\r\n errors.push(LogicErrors.InvalidElseBlockOrdinal);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseIfBlock = (elementName: string, index: number, errors: LogicErrors[]) => {\r\n if (elementName === DirectiveNames.ElseIf && index === 0) {\r\n errors.push(LogicErrors.InvalidElseIfBlockOrdinal);\r\n }\r\n};\r\n\r\nconst validateSwitchIfInvalidElement = (elementName: string, errors: LogicErrors[]) => {\r\n if (\r\n elementName !== DirectiveNames.If &&\r\n elementName !== DirectiveNames.ElseIf &&\r\n elementName !== DirectiveNames.Else\r\n ) {\r\n errors.push(LogicErrors.InvalidElement);\r\n }\r\n};\r\n\r\nexport class ValidationFactory {\r\n static get(validator: string) {\r\n let validatorFn: ValidatorFn = () => [];\r\n switch (validator) {\r\n case DirectiveNames.SwitchIf:\r\n log('getting validation fn for switch if');\r\n validatorFn = validateSwitchIf;\r\n break;\r\n case DirectiveNames.If:\r\n case DirectiveNames.ElseIf:\r\n case DirectiveNames.Else:\r\n log('getting validation fn for switch if children');\r\n validatorFn = validateSwitchIfChildren;\r\n break;\r\n }\r\n\r\n return validatorFn;\r\n }\r\n}\r\n","export const log = (message: string, ...args: any[]) => {\r\n const msg = `__${message\r\n .trim()\r\n .toLocaleUpperCase()\r\n .replace(/(' ')+/g, '_')}__`;\r\n console.log(msg, ...args);\r\n};\r\n","import { ValidationFactory } from '@utils';\r\nimport { Children, PropsWithChildren } from 'react';\r\n\r\nexport const useValidate = (props: PropsWithChildren, name: string) => {\r\n const { children } = props;\r\n const _children = Children.toArray(children);\r\n\r\n return ValidationFactory.get(name)(_children);\r\n};\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport {\r\n Children,\r\n createElement,\r\n FC,\r\n FunctionComponent,\r\n isValidElement,\r\n NamedExoticComponent,\r\n PropsWithChildren,\r\n ReactElement,\r\n} from 'react';\r\n\r\nexport type SwitchIfProps = {};\r\n\r\nconst useSwitchIf = (props: PropsWithChildren) => {\r\n const errors = useValidate(props, 'SwitchIf');\r\n\r\n if (errors.length) {\r\n return { children: createElement(Errors, { errors }) };\r\n }\r\n\r\n const elements = Children.toArray(props.children);\r\n const validElements = elements.filter(isValidElement);\r\n const child = findChild(validElements);\r\n\r\n return { children: child };\r\n};\r\n\r\nconst findChild = (elements: ReactElement[]) => {\r\n for (const element of elements) {\r\n if (!element || !isValidElement(element)) {\r\n continue;\r\n }\r\n\r\n const { displayName } = element.type as NamedExoticComponent | FunctionComponent;\r\n\r\n if (displayName) {\r\n const { condition } = element.props as { condition: boolean };\r\n\r\n if ((displayName === 'If' || displayName === 'ElseIf') && condition === true) {\r\n return element;\r\n }\r\n\r\n if (displayName === 'Else') {\r\n return element;\r\n }\r\n }\r\n }\r\n\r\n return null;\r\n};\r\n\r\nconst SwitchIf: FC<PropsWithChildren<SwitchIfProps>> = (props) => {\r\n const { children } = useSwitchIf(props);\r\n return <>{children}</>;\r\n};\r\n\r\nSwitchIf.displayName = DirectiveNames.SwitchIf;\r\n\r\nexport default SwitchIf;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type IfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst If: FC<IfProps> = (props) => {\r\n const errors = useValidate(props, 'If');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nIf.displayName = DirectiveNames.If;\r\n\r\nexport default If;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseIfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst ElseIf: FC<ElseIfProps> = (props) => {\r\n const errors = useValidate(props, 'ElseIf');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nElseIf.displayName = DirectiveNames.ElseIf;\r\n\r\nexport default ElseIf;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseProps = {};\r\n\r\nconst Else: FC<PropsWithChildren<ElseProps>> = (props) => {\r\n const errors = useValidate(props, 'Else');\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{children}</>;\r\n};\r\n\r\nElse.displayName = DirectiveNames.Else;\r\n\r\nexport default Else;\r\n"],"names":["LogicErrors","DirectiveNames","ERRORS","Errors","errors","error","index","jsx","validateSwitchIfChildren","children","child","displayName","log","validateSwitchIf","elementLookup","count","validateIfBlock","validateElseBlock","validateElseIfBlock","validateSwitchIfInvalidElement","If","elementName","elements","length","ValidationFactory","validator","validatorFn","message","args","msg","useValidate","props","name","_children","Children","useSwitchIf","createElement","validElements","isValidElement","findChild","element","condition","SwitchIf","Fragment","ElseIf","Else"],"mappings":"wIAAY,IAAAA,GAAAA,IAEVA,EAAAA,EAAA,gBAAkB,IAAlB,EAAA,kBACAA,EAAAA,EAAA,uBAAyB,IAAzB,EAAA,yBACAA,EAAAA,EAAA,yBAA2B,IAA3B,EAAA,2BACAA,EAAAA,EAAA,oBAAsB,IAAtB,EAAA,sBACAA,EAAAA,EAAA,sBAAwB,IAAxB,EAAA,wBACAA,EAAAA,EAAA,wBAA0B,IAA1B,EAAA,0BACAA,EAAAA,EAAA,0BAA4B,IAA5B,EAAA,4BAIAA,EAAAA,EAAA,eAAiB,IAAjB,EAAA,iBACAA,EAAAA,EAAA,iBAAmB,IAAnB,EAAA,mBAbUA,IAAAA,GAAA,CAAA,CAA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AQA,IAAgB,WAAa8B,IAAc,IAIpE9B,IAAgB,OACX,OAAA6B,CACT,CACF,CAGK,OAAA,IACT,EAEME,EAAkDX,GAAU,CAChE,KAAM,CAAE,SAAAtB,CAAA,EAAa0B,EAAYJ,CAAK,EACtC,yBAAU,SAAAtB,EAAS,CACrB,EAEAiC,EAAS,YAAczC,EAAe,SClDhC,MAAAmB,EAAmBW,GAAU,CAC3B,MAAA3B,EAAS0B,EAAYC,EAAO,IAAI,EAChC,CAAE,UAAAU,GAAcV,EAChBtB,EAAWL,EAAO,SAAW,EAAI2B,EAAM,SAAWxB,MAACJ,GAAO,OAAAC,EAAgB,EAEzE,OAAAG,EAAAA,IAAAoC,EAAAA,SAAA,CAAG,YAAalC,CAAS,CAAA,CAClC,EAEAW,EAAG,YAAcnB,EAAe,GCT1B,MAAA2C,EAA2Bb,GAAU,CACnC,MAAA3B,EAAS0B,EAAYC,EAAO,QAAQ,EACpC,CAAE,UAAAU,GAAcV,EAChBtB,EAAWL,EAAO,SAAW,EAAI2B,EAAM,SAAWxB,MAACJ,GAAO,OAAAC,EAAgB,EAEzE,OAAAG,EAAAA,IAAAoC,EAAAA,SAAA,CAAG,YAAalC,CAAS,CAAA,CAClC,EAEAmC,EAAO,YAAc3C,EAAe,OCV9B,MAAA4C,EAA0Cd,GAAU,CAClD,MAAA3B,EAAS0B,EAAYC,EAAO,MAAM,EAClCtB,EAAWL,EAAO,SAAW,EAAI2B,EAAM,SAAWxB,MAACJ,GAAO,OAAAC,EAAgB,EAEhF,yBAAU,SAAAK,EAAS,CACrB,EAEAoC,EAAK,YAAc5C,EAAe"}
@@ -1,6 +1,6 @@
1
- import { jsx as a, Fragment as h } from "react/jsx-runtime";
2
- import { Children as p, createElement as k, isValidElement as E } from "react";
3
- import './index.css';var t = /* @__PURE__ */ ((e) => (e[e.IfBlockExpected = 2001] = "IfBlockExpected", e[e.OnlyOneIfBlockExpected = 2002] = "OnlyOneIfBlockExpected", e[e.OnlyOneElseBlockExpected = 2003] = "OnlyOneElseBlockExpected", e[e.SwitchBlockExpected = 2005] = "SwitchBlockExpected", e[e.InvalidIfBlockOrdinal = 2004] = "InvalidIfBlockOrdinal", e[e.InvalidElseBlockOrdinal = 2006] = "InvalidElseBlockOrdinal", e[e.InvalidElseIfBlockOrdinal = 2007] = "InvalidElseIfBlockOrdinal", e[e.InvalidElement = 3002] = "InvalidElement", e[e.ChildrenExpected = 3003] = "ChildrenExpected", e))(t || {});
1
+ import { jsx as s, Fragment as f } from "react/jsx-runtime";
2
+ import { Children as E, createElement as u, isValidElement as r } from "react";
3
+ import './index.css';var t = /* @__PURE__ */ ((e) => (e[e.IfBlockExpected = 2001] = "IfBlockExpected", e[e.OnlyOneIfBlockExpected = 2002] = "OnlyOneIfBlockExpected", e[e.OnlyOneElseBlockExpected = 2003] = "OnlyOneElseBlockExpected", e[e.SwitchBlockExpected = 2005] = "SwitchBlockExpected", e[e.InvalidIfBlockOrdinal = 2004] = "InvalidIfBlockOrdinal", e[e.InvalidElseBlockOrdinal = 2006] = "InvalidElseBlockOrdinal", e[e.InvalidElseIfBlockOrdinal = 2007] = "InvalidElseIfBlockOrdinal", e[e.InvalidElement = 3002] = "InvalidElement", e[e.ChildrenExpected = 3003] = "ChildrenExpected", e))(t || {}), c = /* @__PURE__ */ ((e) => (e.If = "If", e.ElseIf = "ElseIf", e.Else = "Else", e.SwitchIf = "SwitchIf", e.Unknown = "Unknown", e))(c || {});
4
4
  const v = {
5
5
  [t.IfBlockExpected]: "Missing 'If'",
6
6
  [t.OnlyOneIfBlockExpected]: "Can only have one 'If'",
@@ -11,90 +11,93 @@ const v = {
11
11
  [t.InvalidElseIfBlockOrdinal]: "Invalid ordinals, cannot have 'ElseIf' before 'If'",
12
12
  [t.ChildrenExpected]: "Should at least have one child",
13
13
  [t.InvalidElement]: "Invalid element"
14
- }, I = ({ errors: e }) => /* @__PURE__ */ a("ol", { className: "trd-error-list", children: e.map((l, n) => /* @__PURE__ */ a("li", { children: v[l] }, `${l}-${n}`)) }), B = (e) => {
14
+ }, I = ({ errors: e }) => /* @__PURE__ */ s("ol", { className: "trd-error-list", children: e.map((l, n) => /* @__PURE__ */ s("li", { children: v[l] }, `${l}-${n}`)) }), B = (e) => {
15
15
  const l = [];
16
16
  return e.length === 0 ? (l.push(t.ChildrenExpected), l) : (e.forEach((n) => {
17
- const { displayName: c } = n.type ?? {};
18
- (c === i.displayName || c === f.displayName || c === o.displayName) && l.push(t.SwitchBlockExpected);
17
+ const { displayName: i = c.Unknown } = n.type ?? {};
18
+ (i === c.If || i === c.ElseIf || i === c.Else) && (d("If, ElseIf, Else cannot be direct children of If, ElseIf, Else"), l.push(t.InvalidElement), l.push(t.SwitchBlockExpected));
19
19
  }), l);
20
20
  }, O = (e) => {
21
21
  const l = [], n = {};
22
- return e.length === 0 ? (l.push(t.ChildrenExpected, t.IfBlockExpected), l) : (e.forEach((c, d) => {
23
- const { displayName: s = "unknown" } = c.type, u = n[s] ?? 0;
24
- n[s] = u + 1, y(s, d, n, l), x(s, d, e.length, n, l), w(s, d, l), S(s, l);
25
- }), n[i.name] || l.push(t.IfBlockExpected), l);
26
- }, y = (e, l, n, c) => {
27
- e === i.name && (l !== 0 && c.push(t.InvalidIfBlockOrdinal), n[i.name] > 1 && c.push(t.OnlyOneIfBlockExpected));
28
- }, x = (e, l, n, c, d) => {
29
- e === o.name && (c[o.name] > 1 && d.push(t.OnlyOneElseBlockExpected), (l === 0 || l !== n - 1) && d.push(t.InvalidElseBlockOrdinal));
30
- }, w = (e, l, n) => {
31
- e === f.name && l === 0 && n.push(t.InvalidElseIfBlockOrdinal);
32
- }, S = (e, l) => {
33
- e !== i.name && e !== f.name && e !== o.name && l.push(t.InvalidElement);
22
+ return d("1. validate switch if"), e.length === 0 ? (d("2. children expected"), l.push(t.ChildrenExpected, t.IfBlockExpected), l) : (e.forEach((i, a) => {
23
+ const { displayName: o = c.Unknown } = i.type;
24
+ d("3. validate switch if - display name", o);
25
+ const k = n[o] ?? 0;
26
+ n[o] = k + 1, d("4. element look up", n), w(o, a, n, l), y(o, a, e.length, n, l), m(o, a, l), x(o, l);
27
+ }), n[p.name] || (d("5. if block expected - no if in lookup"), l.push(t.IfBlockExpected)), l);
28
+ }, w = (e, l, n, i) => {
29
+ d("6. validate if block", e, l, n), e === c.If && (l !== 0 && i.push(t.InvalidIfBlockOrdinal), n[c.If] > 1 && i.push(t.OnlyOneIfBlockExpected));
30
+ }, y = (e, l, n, i, a) => {
31
+ e === c.Else && (i[c.Else] > 1 && a.push(t.OnlyOneElseBlockExpected), (l === 0 || l !== n - 1) && a.push(t.InvalidElseBlockOrdinal));
32
+ }, m = (e, l, n) => {
33
+ e === c.ElseIf && l === 0 && n.push(t.InvalidElseIfBlockOrdinal);
34
+ }, x = (e, l) => {
35
+ e !== c.If && e !== c.ElseIf && e !== c.Else && l.push(t.InvalidElement);
34
36
  };
35
- class N {
37
+ class S {
36
38
  static get(l) {
37
- let n;
39
+ let n = () => [];
38
40
  switch (l) {
39
- case m.displayName:
40
- n = O;
41
+ case c.SwitchIf:
42
+ d("getting validation fn for switch if"), n = O;
41
43
  break;
42
- case i.displayName:
43
- case f.displayName:
44
- case o.displayName:
45
- n = B;
44
+ case c.If:
45
+ case c.ElseIf:
46
+ case c.Else:
47
+ d("getting validation fn for switch if children"), n = B;
46
48
  break;
47
- default:
48
- n = () => [];
49
49
  }
50
50
  return n;
51
51
  }
52
52
  }
53
- const r = (e, l) => {
54
- const { children: n } = e, c = p.toArray(n);
55
- return N.get(l)(c);
53
+ const d = (e, ...l) => {
54
+ const n = `__${e.trim().toLocaleUpperCase().replace(/(' ')+/g, "_")}__`;
55
+ console.log(n, ...l);
56
+ }, h = (e, l) => {
57
+ const { children: n } = e, i = E.toArray(n);
58
+ return S.get(l)(i);
56
59
  }, C = (e) => {
57
- const l = r(e, "SwitchIf");
60
+ const l = h(e, "SwitchIf");
58
61
  if (l.length)
59
- return { children: k(I, { errors: l }) };
60
- const c = p.toArray(e.children).filter(E);
61
- return { children: b(c) };
62
+ return { children: u(I, { errors: l }) };
63
+ const i = E.toArray(e.children).filter(r);
64
+ return { children: b(i) };
62
65
  }, b = (e) => {
63
66
  for (const l of e) {
64
- if (!l || !E(l))
67
+ if (!l || !r(l))
65
68
  continue;
66
69
  const { displayName: n } = l.type;
67
70
  if (n) {
68
- const { condition: c } = l.props;
69
- if ((n === "If" || n === "ElseIf") && c === !0 || n === "Else")
71
+ const { condition: i } = l.props;
72
+ if ((n === "If" || n === "ElseIf") && i === !0 || n === "Else")
70
73
  return l;
71
74
  }
72
75
  }
73
76
  return null;
74
- }, m = (e) => {
77
+ }, _ = (e) => {
75
78
  const { children: l } = C(e);
76
- return /* @__PURE__ */ a(h, { children: l });
79
+ return /* @__PURE__ */ s(f, { children: l });
77
80
  };
78
- m.displayName = "SwitchIf";
79
- const i = (e) => {
80
- const l = r(e, "If"), { condition: n } = e, c = l.length === 0 ? e.children : /* @__PURE__ */ a(I, { errors: l });
81
- return /* @__PURE__ */ a(h, { children: n && c });
81
+ _.displayName = c.SwitchIf;
82
+ const p = (e) => {
83
+ const l = h(e, "If"), { condition: n } = e, i = l.length === 0 ? e.children : /* @__PURE__ */ s(I, { errors: l });
84
+ return /* @__PURE__ */ s(f, { children: n && i });
82
85
  };
83
- i.displayName = "If";
84
- const f = (e) => {
85
- const l = r(e, "ElseIf"), { condition: n } = e, c = l.length === 0 ? e.children : /* @__PURE__ */ a(I, { errors: l });
86
- return /* @__PURE__ */ a(h, { children: n && c });
86
+ p.displayName = c.If;
87
+ const U = (e) => {
88
+ const l = h(e, "ElseIf"), { condition: n } = e, i = l.length === 0 ? e.children : /* @__PURE__ */ s(I, { errors: l });
89
+ return /* @__PURE__ */ s(f, { children: n && i });
87
90
  };
88
- f.displayName = "ElseIf";
89
- const o = (e) => {
90
- const l = r(e, "Else"), n = l.length === 0 ? e.children : /* @__PURE__ */ a(I, { errors: l });
91
- return /* @__PURE__ */ a(h, { children: n });
91
+ U.displayName = c.ElseIf;
92
+ const F = (e) => {
93
+ const l = h(e, "Else"), n = l.length === 0 ? e.children : /* @__PURE__ */ s(I, { errors: l });
94
+ return /* @__PURE__ */ s(f, { children: n });
92
95
  };
93
- o.displayName = "Else";
96
+ F.displayName = c.Else;
94
97
  export {
95
- o as Else,
96
- f as ElseIf,
97
- i as If,
98
- m as SwitchIf
98
+ F as Else,
99
+ U as ElseIf,
100
+ p as If,
101
+ _ as SwitchIf
99
102
  };
100
103
  //# sourceMappingURL=ts-react-directives.es.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ts-react-directives.es.js","sources":["../src/fixtures/enums.ts","../src/fixtures/constants.ts","../src/components/Errors.tsx","../src/utils/validators.ts","../src/hooks/useValidate.ts","../src/directives/if/SwitchIf.tsx","../src/directives/if/If.tsx","../src/directives/if/ElseIf.tsx","../src/directives/if/Else.tsx"],"sourcesContent":["export enum LogicErrors {\r\n // Switch\r\n IfBlockExpected = 2001,\r\n OnlyOneIfBlockExpected = 2002,\r\n OnlyOneElseBlockExpected = 2003,\r\n SwitchBlockExpected = 2005,\r\n InvalidIfBlockOrdinal = 2004,\r\n InvalidElseBlockOrdinal = 2006,\r\n InvalidElseIfBlockOrdinal = 2007,\r\n\r\n // General\r\n\r\n InvalidElement = 3002,\r\n ChildrenExpected = 3003,\r\n}\r\n","import { LogicErrors } from './enums';\r\n\r\nexport const ERRORS: Record<LogicErrors, string> = {\r\n [LogicErrors.IfBlockExpected]: \"Missing 'If'\",\r\n [LogicErrors.OnlyOneIfBlockExpected]: \"Can only have one 'If'\",\r\n [LogicErrors.OnlyOneElseBlockExpected]: \"Can only have one 'Else'\",\r\n [LogicErrors.SwitchBlockExpected]: \"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'\",\r\n [LogicErrors.InvalidElseBlockOrdinal]: \"Invalid ordinals, 'Else' should be the last\",\r\n [LogicErrors.InvalidIfBlockOrdinal]: \"Invalid ordinals, 'If' block should be the first\",\r\n [LogicErrors.InvalidElseIfBlockOrdinal]: \"Invalid ordinals, cannot have 'ElseIf' before 'If'\",\r\n [LogicErrors.ChildrenExpected]: 'Should at least have one child',\r\n [LogicErrors.InvalidElement]: 'Invalid element',\r\n};\r\n","import { FC } from 'react';\r\nimport { ERRORS, LogicErrors } from '@fixtures';\r\n\r\nimport '@assets/index.css';\r\n\r\ntype ErrorProps = { errors: LogicErrors[] };\r\n\r\nconst Errors: FC<ErrorProps> = ({ errors }) => (\r\n <ol className=\"trd-error-list\">\r\n {errors.map((error, index) => (\r\n <li key={`${error}-${index}`}>{ERRORS[error]}</li>\r\n ))}\r\n </ol>\r\n);\r\n\r\nexport default Errors;\r\n","import { Else, ElseIf, If, SwitchIf } from '@directives';\r\nimport { LogicErrors } from '@fixtures';\r\n\r\nimport { ReactNode } from 'react';\r\n\r\ntype ValidatorFn = (children: Array<Exclude<ReactNode, boolean | null | undefined>>) => number[];\r\n\r\nconst validateSwitchIfChildren: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName } = child.type ?? {};\r\n\r\n // If, ElseIf, Else cannot be direct children of If, ElseIf, Else\r\n if (displayName === If.displayName || displayName === ElseIf.displayName || displayName === Else.displayName) {\r\n errors.push(LogicErrors.SwitchBlockExpected);\r\n }\r\n });\r\n\r\n return errors;\r\n};\r\n\r\nconst validateSwitchIf: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n const elementLookup: Record<string, number> = {};\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected, LogicErrors.IfBlockExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child, index) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = 'unknown' } = child.type;\r\n\r\n const count = elementLookup[displayName] ?? 0;\r\n elementLookup[displayName] = count + 1;\r\n\r\n validateIfBlock(displayName, index, elementLookup, errors);\r\n validateElseBlock(displayName, index, children.length, elementLookup, errors);\r\n validateElseIfBlock(displayName, index, errors);\r\n validateSwitchIfInvalidElement(displayName, errors);\r\n });\r\n\r\n if (!elementLookup[If.name]) {\r\n errors.push(LogicErrors.IfBlockExpected);\r\n }\r\n\r\n return errors;\r\n};\r\n\r\nconst validateIfBlock = (typeName: string, index: number, elements: Record<string, number>, errors: LogicErrors[]) => {\r\n if (typeName === If.name) {\r\n if (index !== 0) {\r\n errors.push(LogicErrors.InvalidIfBlockOrdinal);\r\n }\r\n\r\n if (elements[If.name] > 1) {\r\n errors.push(LogicErrors.OnlyOneIfBlockExpected);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseBlock = (\r\n typeName: string,\r\n index: number,\r\n length: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n if (typeName === Else.name) {\r\n if (elements[Else.name] > 1) {\r\n errors.push(LogicErrors.OnlyOneElseBlockExpected);\r\n }\r\n if (index === 0 || index !== length - 1) {\r\n errors.push(LogicErrors.InvalidElseBlockOrdinal);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseIfBlock = (typeName: string, index: number, errors: LogicErrors[]) => {\r\n if (typeName === ElseIf.name && index === 0) {\r\n errors.push(LogicErrors.InvalidElseIfBlockOrdinal);\r\n }\r\n};\r\n\r\nconst validateSwitchIfInvalidElement = (typeName: string, errors: LogicErrors[]) => {\r\n if (typeName !== If.name && typeName !== ElseIf.name && typeName !== Else.name) {\r\n errors.push(LogicErrors.InvalidElement);\r\n }\r\n};\r\n\r\nexport class ValidationFactory {\r\n static get(validator: string) {\r\n let validatorFn: ValidatorFn;\r\n switch (validator) {\r\n case SwitchIf.displayName:\r\n validatorFn = validateSwitchIf;\r\n break;\r\n case If.displayName:\r\n case ElseIf.displayName:\r\n case Else.displayName:\r\n validatorFn = validateSwitchIfChildren;\r\n break;\r\n default:\r\n validatorFn = () => [];\r\n }\r\n\r\n return validatorFn;\r\n }\r\n}\r\n","import { ValidationFactory } from '@utils';\r\nimport { Children, PropsWithChildren } from 'react';\r\n\r\nexport const useValidate = (props: PropsWithChildren, name: string) => {\r\n const { children } = props;\r\n const _children = Children.toArray(children);\r\n\r\n return ValidationFactory.get(name)(_children);\r\n};\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport {\r\n Children,\r\n createElement,\r\n FC,\r\n FunctionComponent,\r\n isValidElement,\r\n NamedExoticComponent,\r\n PropsWithChildren,\r\n ReactElement,\r\n} from 'react';\r\n\r\nexport type SwitchIfProps = {};\r\n\r\nconst useSwitchIf = (props: PropsWithChildren) => {\r\n const errors = useValidate(props, 'SwitchIf');\r\n\r\n if (errors.length) {\r\n return { children: createElement(Errors, { errors }) };\r\n }\r\n\r\n const elements = Children.toArray(props.children);\r\n const validElements = elements.filter(isValidElement);\r\n const child = findChild(validElements);\r\n\r\n return { children: child };\r\n};\r\n\r\nconst findChild = (elements: ReactElement[]) => {\r\n for (const element of elements) {\r\n if (!element || !isValidElement(element)) {\r\n continue;\r\n }\r\n\r\n const { displayName } = element.type as NamedExoticComponent | FunctionComponent;\r\n\r\n if (displayName) {\r\n const { condition } = element.props as { condition: boolean };\r\n\r\n if ((displayName === 'If' || displayName === 'ElseIf') && condition === true) {\r\n return element;\r\n }\r\n\r\n if (displayName === 'Else') {\r\n return element;\r\n }\r\n }\r\n }\r\n\r\n return null;\r\n};\r\n\r\nconst SwitchIf: FC<PropsWithChildren<SwitchIfProps>> = (props) => {\r\n const { children } = useSwitchIf(props);\r\n return <>{children}</>;\r\n};\r\n\r\nSwitchIf.displayName = 'SwitchIf';\r\n\r\nexport default SwitchIf;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type IfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst If: FC<IfProps> = (props) => {\r\n const errors = useValidate(props, 'If');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nIf.displayName = 'If';\r\n\r\nexport default If;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseIfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst ElseIf: FC<ElseIfProps> = (props) => {\r\n const errors = useValidate(props, 'ElseIf');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nElseIf.displayName = 'ElseIf';\r\n\r\nexport default ElseIf;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseProps = {};\r\n\r\nconst Else: FC<PropsWithChildren<ElseProps>> = (props) => {\r\n const errors = useValidate(props, 'Else');\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{children}</>;\r\n};\r\n\r\nElse.displayName = 'Else';\r\n\r\nexport default Else;\r\n"],"names":["LogicErrors","ERRORS","Errors","errors","error","index","jsx","validateSwitchIfChildren","children","child","displayName","If","ElseIf","Else","validateSwitchIf","elementLookup","count","validateIfBlock","validateElseBlock","validateElseIfBlock","validateSwitchIfInvalidElement","typeName","elements","length","ValidationFactory","validator","validatorFn","SwitchIf","useValidate","props","name","_children","Children","useSwitchIf","createElement","validElements","isValidElement","findChild","element","condition","Fragment"],"mappings":";;AAAY,IAAAA,sBAAAA,OAEVA,EAAAA,EAAA,kBAAkB,IAAlB,IAAA,mBACAA,EAAAA,EAAA,yBAAyB,IAAzB,IAAA,0BACAA,EAAAA,EAAA,2BAA2B,IAA3B,IAAA,4BACAA,EAAAA,EAAA,sBAAsB,IAAtB,IAAA,uBACAA,EAAAA,EAAA,wBAAwB,IAAxB,IAAA,yBACAA,EAAAA,EAAA,0BAA0B,IAA1B,IAAA,2BACAA,EA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AI,GAChC,EAAE,WAAAU,MAAcV,GAChBrB,IAAWL,EAAO,WAAW,IAAI0B,EAAM,WAAW,gBAAAvB,EAACJ,KAAO,QAAAC,GAAgB;AAEzE,SAAA,gBAAAG,EAAAkC,GAAA,EAAG,eAAahC,EAAS,CAAA;AAClC;AAEAG,EAAG,cAAc;ACTX,MAAAC,IAA0B,CAACiB,MAAU;AACnC,QAAA1B,IAASyB,EAAYC,GAAO,QAAQ,GACpC,EAAE,WAAAU,MAAcV,GAChBrB,IAAWL,EAAO,WAAW,IAAI0B,EAAM,WAAW,gBAAAvB,EAACJ,KAAO,QAAAC,GAAgB;AAEzE,SAAA,gBAAAG,EAAAkC,GAAA,EAAG,eAAahC,EAAS,CAAA;AAClC;AAEAI,EAAO,cAAc;ACVf,MAAAC,IAAyC,CAACgB,MAAU;AAClD,QAAA1B,IAASyB,EAAYC,GAAO,MAAM,GAClCrB,IAAWL,EAAO,WAAW,IAAI0B,EAAM,WAAW,gBAAAvB,EAACJ,KAAO,QAAAC,GAAgB;AAEhF,gCAAU,UAAAK,GAAS;AACrB;AAEAK,EAAK,cAAc;"}
1
+ {"version":3,"file":"ts-react-directives.es.js","sources":["../src/fixtures/enums.ts","../src/fixtures/constants.ts","../src/components/Errors.tsx","../src/utils/validators.ts","../src/utils/logger.ts","../src/hooks/useValidate.ts","../src/directives/if/SwitchIf.tsx","../src/directives/if/If.tsx","../src/directives/if/ElseIf.tsx","../src/directives/if/Else.tsx"],"sourcesContent":["export enum LogicErrors {\r\n // Switch\r\n IfBlockExpected = 2001,\r\n OnlyOneIfBlockExpected = 2002,\r\n OnlyOneElseBlockExpected = 2003,\r\n SwitchBlockExpected = 2005,\r\n InvalidIfBlockOrdinal = 2004,\r\n InvalidElseBlockOrdinal = 2006,\r\n InvalidElseIfBlockOrdinal = 2007,\r\n\r\n // General\r\n\r\n InvalidElement = 3002,\r\n ChildrenExpected = 3003,\r\n}\r\n\r\nexport enum DirectiveNames {\r\n If = 'If',\r\n ElseIf = 'ElseIf',\r\n Else = 'Else',\r\n SwitchIf = 'SwitchIf',\r\n Unknown = 'Unknown',\r\n}\r\n","import { LogicErrors } from './enums';\r\n\r\nexport const ERRORS: Record<LogicErrors, string> = {\r\n [LogicErrors.IfBlockExpected]: \"Missing 'If'\",\r\n [LogicErrors.OnlyOneIfBlockExpected]: \"Can only have one 'If'\",\r\n [LogicErrors.OnlyOneElseBlockExpected]: \"Can only have one 'Else'\",\r\n [LogicErrors.SwitchBlockExpected]: \"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'\",\r\n [LogicErrors.InvalidElseBlockOrdinal]: \"Invalid ordinals, 'Else' should be the last\",\r\n [LogicErrors.InvalidIfBlockOrdinal]: \"Invalid ordinals, 'If' block should be the first\",\r\n [LogicErrors.InvalidElseIfBlockOrdinal]: \"Invalid ordinals, cannot have 'ElseIf' before 'If'\",\r\n [LogicErrors.ChildrenExpected]: 'Should at least have one child',\r\n [LogicErrors.InvalidElement]: 'Invalid element',\r\n};\r\n","import { FC } from 'react';\r\nimport { ERRORS, LogicErrors } from '@fixtures';\r\n\r\nimport '@assets/index.css';\r\n\r\ntype ErrorProps = { errors: LogicErrors[] };\r\n\r\nconst Errors: FC<ErrorProps> = ({ errors }) => (\r\n <ol className=\"trd-error-list\">\r\n {errors.map((error, index) => (\r\n <li key={`${error}-${index}`}>{ERRORS[error]}</li>\r\n ))}\r\n </ol>\r\n);\r\n\r\nexport default Errors;\r\n","import { If } from '@directives';\r\nimport { DirectiveNames, LogicErrors } from '@fixtures';\r\nimport { log } from '@utils';\r\n\r\nimport { ReactNode } from 'react';\r\n\r\ntype ValidatorFn = (children: Array<Exclude<ReactNode, boolean | null | undefined>>) => number[];\r\n\r\n/**\r\n * Validate the children of SwitchIf directive\r\n * @param children\r\n * @returns\r\n */\r\nconst validateSwitchIfChildren: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = DirectiveNames.Unknown } = child.type ?? {};\r\n\r\n // If, ElseIf, Else cannot be direct children of If, ElseIf, Else\r\n if (\r\n displayName === DirectiveNames.If ||\r\n displayName === DirectiveNames.ElseIf ||\r\n displayName === DirectiveNames.Else\r\n ) {\r\n log('If, ElseIf, Else cannot be direct children of If, ElseIf, Else');\r\n errors.push(LogicErrors.InvalidElement);\r\n errors.push(LogicErrors.SwitchBlockExpected);\r\n }\r\n });\r\n\r\n return errors;\r\n};\r\n\r\n/**\r\n * Validate the SwitchIf directive\r\n * @param children\r\n * @returns\r\n */\r\nconst validateSwitchIf: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n const elementLookup: Record<string, number> = {};\r\n\r\n log('1. validate switch if');\r\n\r\n if (children.length === 0) {\r\n log('2. children expected');\r\n errors.push(LogicErrors.ChildrenExpected, LogicErrors.IfBlockExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child, index) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = DirectiveNames.Unknown } = child.type;\r\n log('3. validate switch if - display name', displayName);\r\n\r\n const count = elementLookup[displayName] ?? 0;\r\n elementLookup[displayName] = count + 1;\r\n\r\n log('4. element look up', elementLookup);\r\n\r\n validateIfBlock(displayName, index, elementLookup, errors);\r\n validateElseBlock(displayName, index, children.length, elementLookup, errors);\r\n validateElseIfBlock(displayName, index, errors);\r\n validateSwitchIfInvalidElement(displayName, errors);\r\n });\r\n\r\n if (!elementLookup[If.name]) {\r\n log('5. if block expected - no if in lookup');\r\n errors.push(LogicErrors.IfBlockExpected);\r\n }\r\n\r\n return errors;\r\n};\r\n\r\nconst validateIfBlock = (\r\n elementName: string,\r\n index: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n log('6. validate if block', elementName, index, elements);\r\n if (elementName === DirectiveNames.If) {\r\n if (index !== 0) {\r\n errors.push(LogicErrors.InvalidIfBlockOrdinal);\r\n }\r\n\r\n if (elements[DirectiveNames.If] > 1) {\r\n errors.push(LogicErrors.OnlyOneIfBlockExpected);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseBlock = (\r\n elementName: string,\r\n index: number,\r\n length: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n if (elementName === DirectiveNames.Else) {\r\n if (elements[DirectiveNames.Else] > 1) {\r\n errors.push(LogicErrors.OnlyOneElseBlockExpected);\r\n }\r\n if (index === 0 || index !== length - 1) {\r\n errors.push(LogicErrors.InvalidElseBlockOrdinal);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseIfBlock = (elementName: string, index: number, errors: LogicErrors[]) => {\r\n if (elementName === DirectiveNames.ElseIf && index === 0) {\r\n errors.push(LogicErrors.InvalidElseIfBlockOrdinal);\r\n }\r\n};\r\n\r\nconst validateSwitchIfInvalidElement = (elementName: string, errors: LogicErrors[]) => {\r\n if (\r\n elementName !== DirectiveNames.If &&\r\n elementName !== DirectiveNames.ElseIf &&\r\n elementName !== DirectiveNames.Else\r\n ) {\r\n errors.push(LogicErrors.InvalidElement);\r\n }\r\n};\r\n\r\nexport class ValidationFactory {\r\n static get(validator: string) {\r\n let validatorFn: ValidatorFn = () => [];\r\n switch (validator) {\r\n case DirectiveNames.SwitchIf:\r\n log('getting validation fn for switch if');\r\n validatorFn = validateSwitchIf;\r\n break;\r\n case DirectiveNames.If:\r\n case DirectiveNames.ElseIf:\r\n case DirectiveNames.Else:\r\n log('getting validation fn for switch if children');\r\n validatorFn = validateSwitchIfChildren;\r\n break;\r\n }\r\n\r\n return validatorFn;\r\n }\r\n}\r\n","export const log = (message: string, ...args: any[]) => {\r\n const msg = `__${message\r\n .trim()\r\n .toLocaleUpperCase()\r\n .replace(/(' ')+/g, '_')}__`;\r\n console.log(msg, ...args);\r\n};\r\n","import { ValidationFactory } from '@utils';\r\nimport { Children, PropsWithChildren } from 'react';\r\n\r\nexport const useValidate = (props: PropsWithChildren, name: string) => {\r\n const { children } = props;\r\n const _children = Children.toArray(children);\r\n\r\n return ValidationFactory.get(name)(_children);\r\n};\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport {\r\n Children,\r\n createElement,\r\n FC,\r\n FunctionComponent,\r\n isValidElement,\r\n NamedExoticComponent,\r\n PropsWithChildren,\r\n ReactElement,\r\n} from 'react';\r\n\r\nexport type SwitchIfProps = {};\r\n\r\nconst useSwitchIf = (props: PropsWithChildren) => {\r\n const errors = useValidate(props, 'SwitchIf');\r\n\r\n if (errors.length) {\r\n return { children: createElement(Errors, { errors }) };\r\n }\r\n\r\n const elements = Children.toArray(props.children);\r\n const validElements = elements.filter(isValidElement);\r\n const child = findChild(validElements);\r\n\r\n return { children: child };\r\n};\r\n\r\nconst findChild = (elements: ReactElement[]) => {\r\n for (const element of elements) {\r\n if (!element || !isValidElement(element)) {\r\n continue;\r\n }\r\n\r\n const { displayName } = element.type as NamedExoticComponent | FunctionComponent;\r\n\r\n if (displayName) {\r\n const { condition } = element.props as { condition: boolean };\r\n\r\n if ((displayName === 'If' || displayName === 'ElseIf') && condition === true) {\r\n return element;\r\n }\r\n\r\n if (displayName === 'Else') {\r\n return element;\r\n }\r\n }\r\n }\r\n\r\n return null;\r\n};\r\n\r\nconst SwitchIf: FC<PropsWithChildren<SwitchIfProps>> = (props) => {\r\n const { children } = useSwitchIf(props);\r\n return <>{children}</>;\r\n};\r\n\r\nSwitchIf.displayName = DirectiveNames.SwitchIf;\r\n\r\nexport default SwitchIf;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type IfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst If: FC<IfProps> = (props) => {\r\n const errors = useValidate(props, 'If');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nIf.displayName = DirectiveNames.If;\r\n\r\nexport default If;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseIfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst ElseIf: FC<ElseIfProps> = (props) => {\r\n const errors = useValidate(props, 'ElseIf');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nElseIf.displayName = DirectiveNames.ElseIf;\r\n\r\nexport default ElseIf;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseProps = {};\r\n\r\nconst Else: FC<PropsWithChildren<ElseProps>> = (props) => {\r\n const errors = useValidate(props, 'Else');\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{children}</>;\r\n};\r\n\r\nElse.displayName = DirectiveNames.Else;\r\n\r\nexport default Else;\r\n"],"names":["LogicErrors","DirectiveNames","ERRORS","Errors","errors","error","index","jsx","validateSwitchIfChildren","children","child","displayName","log","validateSwitchIf","elementLookup","count","validateIfBlock","validateElseBlock","validateElseIfBlock","validateSwitchIfInvalidElement","If","elementName","elements","length","ValidationFactory","validator","validatorFn","message","args","msg","useValidate","props","name","_children","Children","useSwitchIf","createElement","validElements","isValidElement","findChild","element","condition","SwitchIf","Fragment","ElseIf","Else"],"mappings":";;AAAY,IAAAA,sBAAAA,OAEVA,EAAAA,EAAA,kBAAkB,IAAlB,IAAA,mBACAA,EAAAA,EAAA,yBAAyB,IAAzB,IAAA,0BACAA,EAAAA,EAAA,2BAA2B,IAA3B,IAAA,4BACAA,EAAAA,EAAA,sBAAsB,IAAtB,IAAA,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CAACd,MAAU;AAClD,QAAA3B,IAAS0B,EAAYC,GAAO,MAAM,GAClCtB,IAAWL,EAAO,WAAW,IAAI2B,EAAM,WAAW,gBAAAxB,EAACJ,KAAO,QAAAC,GAAgB;AAEhF,gCAAU,UAAAK,GAAS;AACrB;AAEAoC,EAAK,cAAc5C,EAAe;"}
@@ -1,3 +1,3 @@
1
- (function(c,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],a):(c=typeof globalThis<"u"?globalThis:c||self,a(c["ts-react-directives"]={},c["react/jsx-runtime"],c.React))})(this,function(c,a,h){"use strict";var E=document.createElement("style");E.textContent=`:root{--trd-color: 255, 100, 200;--trd-bg-color: 0, 100, 200}ol.trd-error-list{border:.2rem double rgb(var(--trd-bg-color));background-color:rgba(var(--trd-bg-color),.1);list-style-type:none;box-sizing:border-box;padding:.5rem;margin:0}.trd-error-list>li{color:#000;background-color:rgba(var(--trd-color),.1);padding:.125rem;margin:.125rem;border:2px solid rgb(var(--trd-color));font-size:1.5rem;font-family:monospace}.trd-error-list>li:before{content:"🛈 ";font-size:1.5rem;padding-left:.125rem;margin-right:.125rem}
2
- /*$vite$:1*/`,document.head.appendChild(E);var t=(e=>(e[e.IfBlockExpected=2001]="IfBlockExpected",e[e.OnlyOneIfBlockExpected=2002]="OnlyOneIfBlockExpected",e[e.OnlyOneElseBlockExpected=2003]="OnlyOneElseBlockExpected",e[e.SwitchBlockExpected=2005]="SwitchBlockExpected",e[e.InvalidIfBlockOrdinal=2004]="InvalidIfBlockOrdinal",e[e.InvalidElseBlockOrdinal=2006]="InvalidElseBlockOrdinal",e[e.InvalidElseIfBlockOrdinal=2007]="InvalidElseIfBlockOrdinal",e[e.InvalidElement=3002]="InvalidElement",e[e.ChildrenExpected=3003]="ChildrenExpected",e))(t||{});const u={[t.IfBlockExpected]:"Missing 'If'",[t.OnlyOneIfBlockExpected]:"Can only have one 'If'",[t.OnlyOneElseBlockExpected]:"Can only have one 'Else'",[t.SwitchBlockExpected]:"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'",[t.InvalidElseBlockOrdinal]:"Invalid ordinals, 'Else' should be the last",[t.InvalidIfBlockOrdinal]:"Invalid ordinals, 'If' block should be the first",[t.InvalidElseIfBlockOrdinal]:"Invalid ordinals, cannot have 'ElseIf' before 'If'",[t.ChildrenExpected]:"Should at least have one child",[t.InvalidElement]:"Invalid element"},I=({errors:e})=>a.jsx("ol",{className:"trd-error-list",children:e.map((l,n)=>a.jsx("li",{children:u[l]},`${l}-${n}`))}),v=e=>{const l=[];return e.length===0?(l.push(t.ChildrenExpected),l):(e.forEach(n=>{const{displayName:d}=n.type??{};(d===i.displayName||d===r.displayName||d===o.displayName)&&l.push(t.SwitchBlockExpected)}),l)},k=e=>{const l=[],n={};return e.length===0?(l.push(t.ChildrenExpected,t.IfBlockExpected),l):(e.forEach((d,s)=>{const{displayName:f="unknown"}=d.type,g=n[f]??0;n[f]=g+1,y(f,s,n,l),B(f,s,e.length,n,l),O(f,s,l),b(f,l)}),n[i.name]||l.push(t.IfBlockExpected),l)},y=(e,l,n,d)=>{e===i.name&&(l!==0&&d.push(t.InvalidIfBlockOrdinal),n[i.name]>1&&d.push(t.OnlyOneIfBlockExpected))},B=(e,l,n,d,s)=>{e===o.name&&(d[o.name]>1&&s.push(t.OnlyOneElseBlockExpected),(l===0||l!==n-1)&&s.push(t.InvalidElseBlockOrdinal))},O=(e,l,n)=>{e===r.name&&l===0&&n.push(t.InvalidElseIfBlockOrdinal)},b=(e,l)=>{e!==i.name&&e!==r.name&&e!==o.name&&l.push(t.InvalidElement)};class x{static get(l){let n;switch(l){case m.displayName:n=k;break;case i.displayName:case r.displayName:case o.displayName:n=v;break;default:n=()=>[]}return n}}const p=(e,l)=>{const{children:n}=e,d=h.Children.toArray(n);return x.get(l)(d)},S=e=>{const l=p(e,"SwitchIf");if(l.length)return{children:h.createElement(I,{errors:l})};const d=h.Children.toArray(e.children).filter(h.isValidElement);return{children:w(d)}},w=e=>{for(const l of e){if(!l||!h.isValidElement(l))continue;const{displayName:n}=l.type;if(n){const{condition:d}=l.props;if((n==="If"||n==="ElseIf")&&d===!0||n==="Else")return l}}return null},m=e=>{const{children:l}=S(e);return a.jsx(a.Fragment,{children:l})};m.displayName="SwitchIf";const i=e=>{const l=p(e,"If"),{condition:n}=e,d=l.length===0?e.children:a.jsx(I,{errors:l});return a.jsx(a.Fragment,{children:n&&d})};i.displayName="If";const r=e=>{const l=p(e,"ElseIf"),{condition:n}=e,d=l.length===0?e.children:a.jsx(I,{errors:l});return a.jsx(a.Fragment,{children:n&&d})};r.displayName="ElseIf";const o=e=>{const l=p(e,"Else"),n=l.length===0?e.children:a.jsx(I,{errors:l});return a.jsx(a.Fragment,{children:n})};o.displayName="Else",c.Else=o,c.ElseIf=r,c.If=i,c.SwitchIf=m,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
1
+ (function(o,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],d):(o=typeof globalThis<"u"?globalThis:o||self,d(o["ts-react-directives"]={},o["react/jsx-runtime"],o.React))})(this,function(o,d,f){"use strict";var p=document.createElement("style");p.textContent=`:root{--trd-color: 255, 100, 200;--trd-bg-color: 0, 100, 200}ol.trd-error-list{border:.2rem double rgb(var(--trd-bg-color));background-color:rgba(var(--trd-bg-color),.1);list-style-type:none;box-sizing:border-box;padding:.5rem;margin:0}.trd-error-list>li{color:#000;background-color:rgba(var(--trd-color),.1);padding:.125rem;margin:.125rem;border:2px solid rgb(var(--trd-color));font-size:1.5rem;font-family:monospace}.trd-error-list>li:before{content:"🛈 ";font-size:1.5rem;padding-left:.125rem;margin-right:.125rem}
2
+ /*$vite$:1*/`,document.head.appendChild(p);var t=(e=>(e[e.IfBlockExpected=2001]="IfBlockExpected",e[e.OnlyOneIfBlockExpected=2002]="OnlyOneIfBlockExpected",e[e.OnlyOneElseBlockExpected=2003]="OnlyOneElseBlockExpected",e[e.SwitchBlockExpected=2005]="SwitchBlockExpected",e[e.InvalidIfBlockOrdinal=2004]="InvalidIfBlockOrdinal",e[e.InvalidElseBlockOrdinal=2006]="InvalidElseBlockOrdinal",e[e.InvalidElseIfBlockOrdinal=2007]="InvalidElseIfBlockOrdinal",e[e.InvalidElement=3002]="InvalidElement",e[e.ChildrenExpected=3003]="ChildrenExpected",e))(t||{}),c=(e=>(e.If="If",e.ElseIf="ElseIf",e.Else="Else",e.SwitchIf="SwitchIf",e.Unknown="Unknown",e))(c||{});const y={[t.IfBlockExpected]:"Missing 'If'",[t.OnlyOneIfBlockExpected]:"Can only have one 'If'",[t.OnlyOneElseBlockExpected]:"Can only have one 'Else'",[t.SwitchBlockExpected]:"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'",[t.InvalidElseBlockOrdinal]:"Invalid ordinals, 'Else' should be the last",[t.InvalidIfBlockOrdinal]:"Invalid ordinals, 'If' block should be the first",[t.InvalidElseIfBlockOrdinal]:"Invalid ordinals, cannot have 'ElseIf' before 'If'",[t.ChildrenExpected]:"Should at least have one child",[t.InvalidElement]:"Invalid element"},I=({errors:e})=>d.jsx("ol",{className:"trd-error-list",children:e.map((l,n)=>d.jsx("li",{children:y[l]},`${l}-${n}`))}),m=e=>{const l=[];return e.length===0?(l.push(t.ChildrenExpected),l):(e.forEach(n=>{const{displayName:i=c.Unknown}=n.type??{};(i===c.If||i===c.ElseIf||i===c.Else)&&(a("If, ElseIf, Else cannot be direct children of If, ElseIf, Else"),l.push(t.InvalidElement),l.push(t.SwitchBlockExpected))}),l)},B=e=>{const l=[],n={};return a("1. validate switch if"),e.length===0?(a("2. children expected"),l.push(t.ChildrenExpected,t.IfBlockExpected),l):(e.forEach((i,s)=>{const{displayName:r=c.Unknown}=i.type;a("3. validate switch if - display name",r);const _=n[r]??0;n[r]=_+1,a("4. element look up",n),O(r,s,n,l),b(r,s,e.length,n,l),w(r,s,l),x(r,l)}),n[E.name]||(a("5. if block expected - no if in lookup"),l.push(t.IfBlockExpected)),l)},O=(e,l,n,i)=>{a("6. validate if block",e,l,n),e===c.If&&(l!==0&&i.push(t.InvalidIfBlockOrdinal),n[c.If]>1&&i.push(t.OnlyOneIfBlockExpected))},b=(e,l,n,i,s)=>{e===c.Else&&(i[c.Else]>1&&s.push(t.OnlyOneElseBlockExpected),(l===0||l!==n-1)&&s.push(t.InvalidElseBlockOrdinal))},w=(e,l,n)=>{e===c.ElseIf&&l===0&&n.push(t.InvalidElseIfBlockOrdinal)},x=(e,l)=>{e!==c.If&&e!==c.ElseIf&&e!==c.Else&&l.push(t.InvalidElement)};class g{static get(l){let n=()=>[];switch(l){case c.SwitchIf:a("getting validation fn for switch if"),n=B;break;case c.If:case c.ElseIf:case c.Else:a("getting validation fn for switch if children"),n=m;break}return n}}const a=(e,...l)=>{const n=`__${e.trim().toLocaleUpperCase().replace(/(' ')+/g,"_")}__`;console.log(n,...l)},h=(e,l)=>{const{children:n}=e,i=f.Children.toArray(n);return g.get(l)(i)},S=e=>{const l=h(e,"SwitchIf");if(l.length)return{children:f.createElement(I,{errors:l})};const i=f.Children.toArray(e.children).filter(f.isValidElement);return{children:C(i)}},C=e=>{for(const l of e){if(!l||!f.isValidElement(l))continue;const{displayName:n}=l.type;if(n){const{condition:i}=l.props;if((n==="If"||n==="ElseIf")&&i===!0||n==="Else")return l}}return null},u=e=>{const{children:l}=S(e);return d.jsx(d.Fragment,{children:l})};u.displayName=c.SwitchIf;const E=e=>{const l=h(e,"If"),{condition:n}=e,i=l.length===0?e.children:d.jsx(I,{errors:l});return d.jsx(d.Fragment,{children:n&&i})};E.displayName=c.If;const k=e=>{const l=h(e,"ElseIf"),{condition:n}=e,i=l.length===0?e.children:d.jsx(I,{errors:l});return d.jsx(d.Fragment,{children:n&&i})};k.displayName=c.ElseIf;const v=e=>{const l=h(e,"Else"),n=l.length===0?e.children:d.jsx(I,{errors:l});return d.jsx(d.Fragment,{children:n})};v.displayName=c.Else,o.Else=v,o.ElseIf=k,o.If=E,o.SwitchIf=u,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
3
3
  //# sourceMappingURL=ts-react-directives.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ts-react-directives.umd.js","sources":["../src/fixtures/enums.ts","../src/fixtures/constants.ts","../src/components/Errors.tsx","../src/utils/validators.ts","../src/hooks/useValidate.ts","../src/directives/if/SwitchIf.tsx","../src/directives/if/If.tsx","../src/directives/if/ElseIf.tsx","../src/directives/if/Else.tsx"],"sourcesContent":["export enum LogicErrors {\r\n // Switch\r\n IfBlockExpected = 2001,\r\n OnlyOneIfBlockExpected = 2002,\r\n OnlyOneElseBlockExpected = 2003,\r\n SwitchBlockExpected = 2005,\r\n InvalidIfBlockOrdinal = 2004,\r\n InvalidElseBlockOrdinal = 2006,\r\n InvalidElseIfBlockOrdinal = 2007,\r\n\r\n // General\r\n\r\n InvalidElement = 3002,\r\n ChildrenExpected = 3003,\r\n}\r\n","import { LogicErrors } from './enums';\r\n\r\nexport const ERRORS: Record<LogicErrors, string> = {\r\n [LogicErrors.IfBlockExpected]: \"Missing 'If'\",\r\n [LogicErrors.OnlyOneIfBlockExpected]: \"Can only have one 'If'\",\r\n [LogicErrors.OnlyOneElseBlockExpected]: \"Can only have one 'Else'\",\r\n [LogicErrors.SwitchBlockExpected]: \"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'\",\r\n [LogicErrors.InvalidElseBlockOrdinal]: \"Invalid ordinals, 'Else' should be the last\",\r\n [LogicErrors.InvalidIfBlockOrdinal]: \"Invalid ordinals, 'If' block should be the first\",\r\n [LogicErrors.InvalidElseIfBlockOrdinal]: \"Invalid ordinals, cannot have 'ElseIf' before 'If'\",\r\n [LogicErrors.ChildrenExpected]: 'Should at least have one child',\r\n [LogicErrors.InvalidElement]: 'Invalid element',\r\n};\r\n","import { FC } from 'react';\r\nimport { ERRORS, LogicErrors } from '@fixtures';\r\n\r\nimport '@assets/index.css';\r\n\r\ntype ErrorProps = { errors: LogicErrors[] };\r\n\r\nconst Errors: FC<ErrorProps> = ({ errors }) => (\r\n <ol className=\"trd-error-list\">\r\n {errors.map((error, index) => (\r\n <li key={`${error}-${index}`}>{ERRORS[error]}</li>\r\n ))}\r\n </ol>\r\n);\r\n\r\nexport default Errors;\r\n","import { Else, ElseIf, If, SwitchIf } from '@directives';\r\nimport { LogicErrors } from '@fixtures';\r\n\r\nimport { ReactNode } from 'react';\r\n\r\ntype ValidatorFn = (children: Array<Exclude<ReactNode, boolean | null | undefined>>) => number[];\r\n\r\nconst validateSwitchIfChildren: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName } = child.type ?? {};\r\n\r\n // If, ElseIf, Else cannot be direct children of If, ElseIf, Else\r\n if (displayName === If.displayName || displayName === ElseIf.displayName || displayName === Else.displayName) {\r\n errors.push(LogicErrors.SwitchBlockExpected);\r\n }\r\n });\r\n\r\n return errors;\r\n};\r\n\r\nconst validateSwitchIf: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n const elementLookup: Record<string, number> = {};\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected, LogicErrors.IfBlockExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child, index) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = 'unknown' } = child.type;\r\n\r\n const count = elementLookup[displayName] ?? 0;\r\n elementLookup[displayName] = count + 1;\r\n\r\n validateIfBlock(displayName, index, elementLookup, errors);\r\n validateElseBlock(displayName, index, children.length, elementLookup, errors);\r\n validateElseIfBlock(displayName, index, errors);\r\n validateSwitchIfInvalidElement(displayName, errors);\r\n });\r\n\r\n if (!elementLookup[If.name]) {\r\n errors.push(LogicErrors.IfBlockExpected);\r\n }\r\n\r\n return errors;\r\n};\r\n\r\nconst validateIfBlock = (typeName: string, index: number, elements: Record<string, number>, errors: LogicErrors[]) => {\r\n if (typeName === If.name) {\r\n if (index !== 0) {\r\n errors.push(LogicErrors.InvalidIfBlockOrdinal);\r\n }\r\n\r\n if (elements[If.name] > 1) {\r\n errors.push(LogicErrors.OnlyOneIfBlockExpected);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseBlock = (\r\n typeName: string,\r\n index: number,\r\n length: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n if (typeName === Else.name) {\r\n if (elements[Else.name] > 1) {\r\n errors.push(LogicErrors.OnlyOneElseBlockExpected);\r\n }\r\n if (index === 0 || index !== length - 1) {\r\n errors.push(LogicErrors.InvalidElseBlockOrdinal);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseIfBlock = (typeName: string, index: number, errors: LogicErrors[]) => {\r\n if (typeName === ElseIf.name && index === 0) {\r\n errors.push(LogicErrors.InvalidElseIfBlockOrdinal);\r\n }\r\n};\r\n\r\nconst validateSwitchIfInvalidElement = (typeName: string, errors: LogicErrors[]) => {\r\n if (typeName !== If.name && typeName !== ElseIf.name && typeName !== Else.name) {\r\n errors.push(LogicErrors.InvalidElement);\r\n }\r\n};\r\n\r\nexport class ValidationFactory {\r\n static get(validator: string) {\r\n let validatorFn: ValidatorFn;\r\n switch (validator) {\r\n case SwitchIf.displayName:\r\n validatorFn = validateSwitchIf;\r\n break;\r\n case If.displayName:\r\n case ElseIf.displayName:\r\n case Else.displayName:\r\n validatorFn = validateSwitchIfChildren;\r\n break;\r\n default:\r\n validatorFn = () => [];\r\n }\r\n\r\n return validatorFn;\r\n }\r\n}\r\n","import { ValidationFactory } from '@utils';\r\nimport { Children, PropsWithChildren } from 'react';\r\n\r\nexport const useValidate = (props: PropsWithChildren, name: string) => {\r\n const { children } = props;\r\n const _children = Children.toArray(children);\r\n\r\n return ValidationFactory.get(name)(_children);\r\n};\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport {\r\n Children,\r\n createElement,\r\n FC,\r\n FunctionComponent,\r\n isValidElement,\r\n NamedExoticComponent,\r\n PropsWithChildren,\r\n ReactElement,\r\n} from 'react';\r\n\r\nexport type SwitchIfProps = {};\r\n\r\nconst useSwitchIf = (props: PropsWithChildren) => {\r\n const errors = useValidate(props, 'SwitchIf');\r\n\r\n if (errors.length) {\r\n return { children: createElement(Errors, { errors }) };\r\n }\r\n\r\n const elements = Children.toArray(props.children);\r\n const validElements = elements.filter(isValidElement);\r\n const child = findChild(validElements);\r\n\r\n return { children: child };\r\n};\r\n\r\nconst findChild = (elements: ReactElement[]) => {\r\n for (const element of elements) {\r\n if (!element || !isValidElement(element)) {\r\n continue;\r\n }\r\n\r\n const { displayName } = element.type as NamedExoticComponent | FunctionComponent;\r\n\r\n if (displayName) {\r\n const { condition } = element.props as { condition: boolean };\r\n\r\n if ((displayName === 'If' || displayName === 'ElseIf') && condition === true) {\r\n return element;\r\n }\r\n\r\n if (displayName === 'Else') {\r\n return element;\r\n }\r\n }\r\n }\r\n\r\n return null;\r\n};\r\n\r\nconst SwitchIf: FC<PropsWithChildren<SwitchIfProps>> = (props) => {\r\n const { children } = useSwitchIf(props);\r\n return <>{children}</>;\r\n};\r\n\r\nSwitchIf.displayName = 'SwitchIf';\r\n\r\nexport default SwitchIf;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type IfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst If: FC<IfProps> = (props) => {\r\n const errors = useValidate(props, 'If');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nIf.displayName = 'If';\r\n\r\nexport default If;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseIfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst ElseIf: FC<ElseIfProps> = (props) => {\r\n const errors = useValidate(props, 'ElseIf');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nElseIf.displayName = 'ElseIf';\r\n\r\nexport default ElseIf;\r\n","import { Errors } from '@components';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseProps = {};\r\n\r\nconst Else: FC<PropsWithChildren<ElseProps>> = (props) => {\r\n const errors = useValidate(props, 'Else');\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{children}</>;\r\n};\r\n\r\nElse.displayName = 'Else';\r\n\r\nexport default Else;\r\n"],"names":["LogicErrors","ERRORS","Errors","errors","error","index","jsx","validateSwitchIfChildren","children","child","displayName","If","ElseIf","Else","validateSwitchIf","elementLookup","count","validateIfBlock","validateElseBlock","validateElseIfBlock","validateSwitchIfInvalidElement","typeName","elements","length","ValidationFactory","validator","validatorFn","SwitchIf","useValidate","props","name","_children","Children","useSwitchIf","createElement","validElements","isValidElement","findChild","element","condition","Fragment"],"mappings":";2CAAY,IAAAA,GAAAA,IAEVA,EAAAA,EAAA,gBAAkB,IAAlB,EAAA,kBACAA,EAAAA,EAAA,uBAAyB,IAAzB,EAAA,yBACAA,EAAAA,EAAA,yBAA2B,IAA3B,EAAA,2BACAA,EAAAA,EAAA,oBAAsB,IAAtB,EAAA,sBACAA,EAAAA,EAAA,sBAAwB,IAAxB,EAAA,wBACAA,EAAAA,EAAA,wBAA0B,IAA1B,EAAA,0BACAA,EAAAA,EAAA,0BAA4B,IAA5B,EAAA,4BAIAA,EAAAA,EAAA,eAAiB,IAAjB,EAAA,iBACAA,EAAAA,EAAA,iBAAmB,IAAnB,EAAA,mBAbUA,IAAAA,GAAA,CAAA,CAAA,ECEL,MAAMC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,MAACJ,GAAO,OAAAC,EAAgB,EAEzE,OAAAG,EAAAA,IAAAkC,EAAAA,SAAA,CAAG,YAAahC,CAAS,CAAA,CAClC,EAEAI,EAAO,YAAc,SCVf,MAAAC,EAA0CgB,GAAU,CAClD,MAAA1B,EAASyB,EAAYC,EAAO,MAAM,EAClCrB,EAAWL,EAAO,SAAW,EAAI0B,EAAM,SAAWvB,MAACJ,GAAO,OAAAC,EAAgB,EAEhF,yBAAU,SAAAK,EAAS,CACrB,EAEAK,EAAK,YAAc"}
1
+ {"version":3,"file":"ts-react-directives.umd.js","sources":["../src/fixtures/enums.ts","../src/fixtures/constants.ts","../src/components/Errors.tsx","../src/utils/validators.ts","../src/utils/logger.ts","../src/hooks/useValidate.ts","../src/directives/if/SwitchIf.tsx","../src/directives/if/If.tsx","../src/directives/if/ElseIf.tsx","../src/directives/if/Else.tsx"],"sourcesContent":["export enum LogicErrors {\r\n // Switch\r\n IfBlockExpected = 2001,\r\n OnlyOneIfBlockExpected = 2002,\r\n OnlyOneElseBlockExpected = 2003,\r\n SwitchBlockExpected = 2005,\r\n InvalidIfBlockOrdinal = 2004,\r\n InvalidElseBlockOrdinal = 2006,\r\n InvalidElseIfBlockOrdinal = 2007,\r\n\r\n // General\r\n\r\n InvalidElement = 3002,\r\n ChildrenExpected = 3003,\r\n}\r\n\r\nexport enum DirectiveNames {\r\n If = 'If',\r\n ElseIf = 'ElseIf',\r\n Else = 'Else',\r\n SwitchIf = 'SwitchIf',\r\n Unknown = 'Unknown',\r\n}\r\n","import { LogicErrors } from './enums';\r\n\r\nexport const ERRORS: Record<LogicErrors, string> = {\r\n [LogicErrors.IfBlockExpected]: \"Missing 'If'\",\r\n [LogicErrors.OnlyOneIfBlockExpected]: \"Can only have one 'If'\",\r\n [LogicErrors.OnlyOneElseBlockExpected]: \"Can only have one 'Else'\",\r\n [LogicErrors.SwitchBlockExpected]: \"'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'\",\r\n [LogicErrors.InvalidElseBlockOrdinal]: \"Invalid ordinals, 'Else' should be the last\",\r\n [LogicErrors.InvalidIfBlockOrdinal]: \"Invalid ordinals, 'If' block should be the first\",\r\n [LogicErrors.InvalidElseIfBlockOrdinal]: \"Invalid ordinals, cannot have 'ElseIf' before 'If'\",\r\n [LogicErrors.ChildrenExpected]: 'Should at least have one child',\r\n [LogicErrors.InvalidElement]: 'Invalid element',\r\n};\r\n","import { FC } from 'react';\r\nimport { ERRORS, LogicErrors } from '@fixtures';\r\n\r\nimport '@assets/index.css';\r\n\r\ntype ErrorProps = { errors: LogicErrors[] };\r\n\r\nconst Errors: FC<ErrorProps> = ({ errors }) => (\r\n <ol className=\"trd-error-list\">\r\n {errors.map((error, index) => (\r\n <li key={`${error}-${index}`}>{ERRORS[error]}</li>\r\n ))}\r\n </ol>\r\n);\r\n\r\nexport default Errors;\r\n","import { If } from '@directives';\r\nimport { DirectiveNames, LogicErrors } from '@fixtures';\r\nimport { log } from '@utils';\r\n\r\nimport { ReactNode } from 'react';\r\n\r\ntype ValidatorFn = (children: Array<Exclude<ReactNode, boolean | null | undefined>>) => number[];\r\n\r\n/**\r\n * Validate the children of SwitchIf directive\r\n * @param children\r\n * @returns\r\n */\r\nconst validateSwitchIfChildren: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n\r\n if (children.length === 0) {\r\n errors.push(LogicErrors.ChildrenExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = DirectiveNames.Unknown } = child.type ?? {};\r\n\r\n // If, ElseIf, Else cannot be direct children of If, ElseIf, Else\r\n if (\r\n displayName === DirectiveNames.If ||\r\n displayName === DirectiveNames.ElseIf ||\r\n displayName === DirectiveNames.Else\r\n ) {\r\n log('If, ElseIf, Else cannot be direct children of If, ElseIf, Else');\r\n errors.push(LogicErrors.InvalidElement);\r\n errors.push(LogicErrors.SwitchBlockExpected);\r\n }\r\n });\r\n\r\n return errors;\r\n};\r\n\r\n/**\r\n * Validate the SwitchIf directive\r\n * @param children\r\n * @returns\r\n */\r\nconst validateSwitchIf: ValidatorFn = (children) => {\r\n const errors: LogicErrors[] = [];\r\n const elementLookup: Record<string, number> = {};\r\n\r\n log('1. validate switch if');\r\n\r\n if (children.length === 0) {\r\n log('2. children expected');\r\n errors.push(LogicErrors.ChildrenExpected, LogicErrors.IfBlockExpected);\r\n return errors;\r\n }\r\n\r\n children.forEach((child, index) => {\r\n // @ts-expect-error child.type exist\r\n const { displayName = DirectiveNames.Unknown } = child.type;\r\n log('3. validate switch if - display name', displayName);\r\n\r\n const count = elementLookup[displayName] ?? 0;\r\n elementLookup[displayName] = count + 1;\r\n\r\n log('4. element look up', elementLookup);\r\n\r\n validateIfBlock(displayName, index, elementLookup, errors);\r\n validateElseBlock(displayName, index, children.length, elementLookup, errors);\r\n validateElseIfBlock(displayName, index, errors);\r\n validateSwitchIfInvalidElement(displayName, errors);\r\n });\r\n\r\n if (!elementLookup[If.name]) {\r\n log('5. if block expected - no if in lookup');\r\n errors.push(LogicErrors.IfBlockExpected);\r\n }\r\n\r\n return errors;\r\n};\r\n\r\nconst validateIfBlock = (\r\n elementName: string,\r\n index: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n log('6. validate if block', elementName, index, elements);\r\n if (elementName === DirectiveNames.If) {\r\n if (index !== 0) {\r\n errors.push(LogicErrors.InvalidIfBlockOrdinal);\r\n }\r\n\r\n if (elements[DirectiveNames.If] > 1) {\r\n errors.push(LogicErrors.OnlyOneIfBlockExpected);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseBlock = (\r\n elementName: string,\r\n index: number,\r\n length: number,\r\n elements: Record<string, number>,\r\n errors: LogicErrors[],\r\n) => {\r\n if (elementName === DirectiveNames.Else) {\r\n if (elements[DirectiveNames.Else] > 1) {\r\n errors.push(LogicErrors.OnlyOneElseBlockExpected);\r\n }\r\n if (index === 0 || index !== length - 1) {\r\n errors.push(LogicErrors.InvalidElseBlockOrdinal);\r\n }\r\n }\r\n};\r\n\r\nconst validateElseIfBlock = (elementName: string, index: number, errors: LogicErrors[]) => {\r\n if (elementName === DirectiveNames.ElseIf && index === 0) {\r\n errors.push(LogicErrors.InvalidElseIfBlockOrdinal);\r\n }\r\n};\r\n\r\nconst validateSwitchIfInvalidElement = (elementName: string, errors: LogicErrors[]) => {\r\n if (\r\n elementName !== DirectiveNames.If &&\r\n elementName !== DirectiveNames.ElseIf &&\r\n elementName !== DirectiveNames.Else\r\n ) {\r\n errors.push(LogicErrors.InvalidElement);\r\n }\r\n};\r\n\r\nexport class ValidationFactory {\r\n static get(validator: string) {\r\n let validatorFn: ValidatorFn = () => [];\r\n switch (validator) {\r\n case DirectiveNames.SwitchIf:\r\n log('getting validation fn for switch if');\r\n validatorFn = validateSwitchIf;\r\n break;\r\n case DirectiveNames.If:\r\n case DirectiveNames.ElseIf:\r\n case DirectiveNames.Else:\r\n log('getting validation fn for switch if children');\r\n validatorFn = validateSwitchIfChildren;\r\n break;\r\n }\r\n\r\n return validatorFn;\r\n }\r\n}\r\n","export const log = (message: string, ...args: any[]) => {\r\n const msg = `__${message\r\n .trim()\r\n .toLocaleUpperCase()\r\n .replace(/(' ')+/g, '_')}__`;\r\n console.log(msg, ...args);\r\n};\r\n","import { ValidationFactory } from '@utils';\r\nimport { Children, PropsWithChildren } from 'react';\r\n\r\nexport const useValidate = (props: PropsWithChildren, name: string) => {\r\n const { children } = props;\r\n const _children = Children.toArray(children);\r\n\r\n return ValidationFactory.get(name)(_children);\r\n};\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport {\r\n Children,\r\n createElement,\r\n FC,\r\n FunctionComponent,\r\n isValidElement,\r\n NamedExoticComponent,\r\n PropsWithChildren,\r\n ReactElement,\r\n} from 'react';\r\n\r\nexport type SwitchIfProps = {};\r\n\r\nconst useSwitchIf = (props: PropsWithChildren) => {\r\n const errors = useValidate(props, 'SwitchIf');\r\n\r\n if (errors.length) {\r\n return { children: createElement(Errors, { errors }) };\r\n }\r\n\r\n const elements = Children.toArray(props.children);\r\n const validElements = elements.filter(isValidElement);\r\n const child = findChild(validElements);\r\n\r\n return { children: child };\r\n};\r\n\r\nconst findChild = (elements: ReactElement[]) => {\r\n for (const element of elements) {\r\n if (!element || !isValidElement(element)) {\r\n continue;\r\n }\r\n\r\n const { displayName } = element.type as NamedExoticComponent | FunctionComponent;\r\n\r\n if (displayName) {\r\n const { condition } = element.props as { condition: boolean };\r\n\r\n if ((displayName === 'If' || displayName === 'ElseIf') && condition === true) {\r\n return element;\r\n }\r\n\r\n if (displayName === 'Else') {\r\n return element;\r\n }\r\n }\r\n }\r\n\r\n return null;\r\n};\r\n\r\nconst SwitchIf: FC<PropsWithChildren<SwitchIfProps>> = (props) => {\r\n const { children } = useSwitchIf(props);\r\n return <>{children}</>;\r\n};\r\n\r\nSwitchIf.displayName = DirectiveNames.SwitchIf;\r\n\r\nexport default SwitchIf;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\n\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type IfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst If: FC<IfProps> = (props) => {\r\n const errors = useValidate(props, 'If');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nIf.displayName = DirectiveNames.If;\r\n\r\nexport default If;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseIfProps = PropsWithChildren<{\r\n condition: boolean;\r\n}>;\r\n\r\nconst ElseIf: FC<ElseIfProps> = (props) => {\r\n const errors = useValidate(props, 'ElseIf');\r\n const { condition } = props;\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{condition && children}</>;\r\n};\r\n\r\nElseIf.displayName = DirectiveNames.ElseIf;\r\n\r\nexport default ElseIf;\r\n","import { Errors } from '@components';\r\nimport { DirectiveNames } from '@fixtures';\r\nimport { useValidate } from '@hooks';\r\nimport { FC, PropsWithChildren } from 'react';\r\n\r\nexport type ElseProps = {};\r\n\r\nconst Else: FC<PropsWithChildren<ElseProps>> = (props) => {\r\n const errors = useValidate(props, 'Else');\r\n const children = errors.length === 0 ? props.children : <Errors errors={errors} />;\r\n\r\n return <>{children}</>;\r\n};\r\n\r\nElse.displayName = DirectiveNames.Else;\r\n\r\nexport default Else;\r\n"],"names":["LogicErrors","DirectiveNames","ERRORS","Errors","errors","error","index","jsx","validateSwitchIfChildren","children","child","displayName","log","validateSwitchIf","elementLookup","count","validateIfBlock","validateElseBlock","validateElseIfBlock","validateSwitchIfInvalidElement","If","elementName","elements","length","ValidationFactory","validator","validatorFn","message","args","msg","useValidate","props","name","_children","Children","useSwitchIf","createElement","validElements","isValidElement","findChild","element","condition","SwitchIf","Fragment","ElseIf","Else"],"mappings":";2CAAY,IAAAA,GAAAA,IAEVA,EAAAA,EAAA,gBAAkB,IAAlB,EAAA,kBACAA,EAAAA,EAAA,uBAAyB,IAAzB,EAAA,yBACAA,EAAAA,EAAA,yBAA2B,IAA3B,EAAA,2BACAA,EAAAA,EAAA,oBAAsB,IAAtB,EAAA,sBACAA,EAAAA,EAAA,sBAAwB,IAAxB,EAAA,wBACAA,EAAAA,EAAA,wBAA0B,IAA1B,EAAA,0BACAA,EAAAA,EAAA,0BAA4B,IAA5B,EAAA,4BAIAA,EAAAA,EAAA,eAAiB,IAAjB,EAAA,iBACAA,EAAAA,EAAA,iBAAmB,IAAnB,EAAA,mBAbUA,IAAAA,GAAA,CAAA,CAAA,EAgBAC,GAAAA,IACVA,EAAA,GAAK,KACLA,EAAA,OAAS,SACTA,EAAA,KAAO,OACPA,EAAA,SAAW,WACXA,EAAA,QAAU,UALAA,IAAAA,GAA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AtB,CAAA,EAAa0B,EAAYJ,CAAK,EACtC,yBAAU,SAAAtB,EAAS,CACrB,EAEAiC,EAAS,YAAczC,EAAe,SClDhC,MAAAmB,EAAmBW,GAAU,CAC3B,MAAA3B,EAAS0B,EAAYC,EAAO,IAAI,EAChC,CAAE,UAAAU,GAAcV,EAChBtB,EAAWL,EAAO,SAAW,EAAI2B,EAAM,SAAWxB,MAACJ,GAAO,OAAAC,EAAgB,EAEzE,OAAAG,EAAAA,IAAAoC,EAAAA,SAAA,CAAG,YAAalC,CAAS,CAAA,CAClC,EAEAW,EAAG,YAAcnB,EAAe,GCT1B,MAAA2C,EAA2Bb,GAAU,CACnC,MAAA3B,EAAS0B,EAAYC,EAAO,QAAQ,EACpC,CAAE,UAAAU,GAAcV,EAChBtB,EAAWL,EAAO,SAAW,EAAI2B,EAAM,SAAWxB,MAACJ,GAAO,OAAAC,EAAgB,EAEzE,OAAAG,EAAAA,IAAAoC,EAAAA,SAAA,CAAG,YAAalC,CAAS,CAAA,CAClC,EAEAmC,EAAO,YAAc3C,EAAe,OCV9B,MAAA4C,EAA0Cd,GAAU,CAClD,MAAA3B,EAAS0B,EAAYC,EAAO,MAAM,EAClCtB,EAAWL,EAAO,SAAW,EAAI2B,EAAM,SAAWxB,MAACJ,GAAO,OAAAC,EAAgB,EAEhF,yBAAU,SAAAK,EAAS,CACrB,EAEAoC,EAAK,YAAc5C,EAAe"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openbytes/ts-react-directives",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "SkyCodr (aka: Dulan Sudasinghe)",