@openbytes/ts-react-directives 1.0.0 → 1.1.0
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.
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## About
|
|
4
4
|
|
|
5
|
-
**ts-react-directives** is a small React/Typescript library designed to mimic 'Angular' directives. This is not static library. It's meant to be used as declarative component. Also, it alleviates the limitations and cognitive complexity created by the ternary operations when using conditional rendering.
|
|
5
|
+
**ts-react-directives** is a small React/Typescript library designed to mimic 'Angular' directives. This is not a static library. It's meant to be used as declarative component. Also, it alleviates the limitations and cognitive complexity created by the ternary operations when using conditional rendering.
|
|
6
6
|
|
|
7
7
|
## How to install
|
|
8
8
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require('./index.css');const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require('./index.css');const o=require("react/jsx-runtime"),i=require("react");var t=(e=>(e[e.IfBlockExpected=2001]="IfBlockExpected",e[e.OnlyOneIfBlockExpected=2002]="OnlyOneIfBlockExpected",e[e.OnlyOneElseBlockExpected=2003]="OnlyOneElseBlockExpected",e[e.CheckBlockExpected=2005]="CheckBlockExpected",e[e.InvalidIfBlockOrdinal=2004]="InvalidIfBlockOrdinal",e[e.InvalidElseBlockOrdinal=2006]="InvalidElseBlockOrdinal",e[e.InvalidElseIfBlockOrdinal=2007]="InvalidElseIfBlockOrdinal",e[e.MalformedLoop=2008]="MalformedLoop",e[e.InfiniteLoopCondition=2009]="InfiniteLoopCondition",e[e.TemplateBlockExpected=2012]="TemplateBlockExpected",e[e.OnlyOneTemplateBlockExpected=2013]="OnlyOneTemplateBlockExpected",e[e.EmptyLoopSource=2014]="EmptyLoopSource",e[e.InvalidElement=3002]="InvalidElement",e[e.ChildrenExpected=3003]="ChildrenExpected",e[e.SingleChildExpected=3004]="SingleChildExpected",e))(t||{}),c=(e=>(e.If="If",e.ElseIf="ElseIf",e.Else="Else",e.Check="Check",e.Loop="Loop",e.LoopProps="LoopProps",e.Template="Template",e.Unknown="Unknown",e))(c||{});const m={[t.IfBlockExpected]:"Missing 'If'",[t.OnlyOneIfBlockExpected]:"Can only have one 'If'",[t.OnlyOneElseBlockExpected]:"Can only have one 'Else'",[t.CheckBlockExpected]:"'If', 'ElseIf', 'Else' need to be wrapped in 'Check'",[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",[t.MalformedLoop]:"Malformed loop",[t.EmptyLoopSource]:"Empty loop source",[t.InfiniteLoopCondition]:"Infinite loop condition",[t.TemplateBlockExpected]:"Missing 'Template'",[t.OnlyOneTemplateBlockExpected]:"Only one Template block expected",[t.SingleChildExpected]:"Can have only a single child"},f=({errors:e})=>o.jsx("ol",{className:"trd-error-list",children:e.map((l,n)=>o.jsx("li",{children:m[l]},`${l}-${n}`))}),x=e=>{const l=[];return e.length===0?(l.push(t.ChildrenExpected),l):(e.forEach(n=>{const{displayName:d=c.Unknown}=n.type??{};(d===c.If||d===c.ElseIf||d===c.Else)&&(l.push(t.InvalidElement),l.push(t.CheckBlockExpected))}),l)},B=e=>{const l=[],n={};return e.length===0?(l.push(t.ChildrenExpected,t.IfBlockExpected),l):(e.forEach((d,a)=>{const{displayName:s=c.Unknown}=d.type??{},p=n[s]??0;n[s]=p+1,O(s,a,n,l),y(s,a,e.length,n,l),r(s,a,l),C(s,l)}),n[c.If]||l.push(t.IfBlockExpected),l)},O=(e,l,n,d)=>{e===c.If&&(l!==0&&d.push(t.InvalidIfBlockOrdinal),n[c.If]>1&&d.push(t.OnlyOneIfBlockExpected))},y=(e,l,n,d,a)=>{e===c.Else&&(d[c.Else]>1&&a.push(t.OnlyOneElseBlockExpected),(l===0||l!==n-1)&&a.push(t.InvalidElseBlockOrdinal))},r=(e,l,n)=>{e===c.ElseIf&&l===0&&n.push(t.InvalidElseIfBlockOrdinal)},C=(e,l)=>{e!==c.If&&e!==c.ElseIf&&e!==c.Else&&l.push(t.InvalidElement)},v=(e,l)=>{const n=[];if(e.length===0)n.push(t.ChildrenExpected,t.TemplateBlockExpected);else if(e.length>1)n.push(t.SingleChildExpected,t.OnlyOneTemplateBlockExpected);else{const{displayName:d=c.Unknown}=e[0].type;d!==c.Template&&n.push(t.InvalidElement,t.TemplateBlockExpected);const{from:a=0,to:s=0,step:p=0}=l??{};(p===0||p>0&&a>s||p<0&&a<s)&&n.push(t.InfiniteLoopCondition)}return n.length&&n.push(t.MalformedLoop),n};class T{static get(l){let n=()=>[];switch(l){case c.Check:n=B;break;case c.If:case c.ElseIf:case c.Else:n=x;break;case c.Loop:n=v;break}return n}}const h=(e,l)=>{const{children:n}=e,d=i.Children.toArray(n);return i.useMemo(()=>T.get(l),[l])(d,e)},b=e=>{const l=h(e,c.Check);if(l.length)return{children:i.createElement(f,{errors:l})};const d=i.Children.toArray(e.children).filter(i.isValidElement);return{children:j(d)}},j=e=>{for(const l of e){if(!l||!i.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},I=e=>{const{children:l}=b(e);return o.jsx(o.Fragment,{children:l})};I.displayName=c.Check;const k=e=>{const l=h(e,"If"),{condition:n}=e,d=l.length===0?e.children:o.jsx(f,{errors:l});return o.jsx(o.Fragment,{children:n&&d})};k.displayName=c.If;const E=e=>{const l=h(e,"ElseIf"),{condition:n}=e,d=l.length===0?e.children:o.jsx(f,{errors:l});return o.jsx(o.Fragment,{children:n&&d})};E.displayName=c.ElseIf;const u=e=>{const l=h(e,"Else"),n=l.length===0?e.children:o.jsx(f,{errors:l});return o.jsx(o.Fragment,{children:n})};u.displayName=c.Else;c.Loop;c.Template;exports.Check=I;exports.Else=u;exports.ElseIf=E;exports.If=k;
|
|
@@ -1,97 +1,121 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { Children as
|
|
3
|
-
import './index.css';var
|
|
4
|
-
const
|
|
5
|
-
[
|
|
6
|
-
[
|
|
7
|
-
[
|
|
8
|
-
[
|
|
9
|
-
[
|
|
10
|
-
[
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
|
|
1
|
+
import { jsx as i, Fragment as p } from "react/jsx-runtime";
|
|
2
|
+
import { Children as I, useMemo as E, createElement as m, isValidElement as k } 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.CheckBlockExpected = 2005] = "CheckBlockExpected", e[e.InvalidIfBlockOrdinal = 2004] = "InvalidIfBlockOrdinal", e[e.InvalidElseBlockOrdinal = 2006] = "InvalidElseBlockOrdinal", e[e.InvalidElseIfBlockOrdinal = 2007] = "InvalidElseIfBlockOrdinal", e[e.MalformedLoop = 2008] = "MalformedLoop", e[e.InfiniteLoopCondition = 2009] = "InfiniteLoopCondition", e[e.TemplateBlockExpected = 2012] = "TemplateBlockExpected", e[e.OnlyOneTemplateBlockExpected = 2013] = "OnlyOneTemplateBlockExpected", e[e.EmptyLoopSource = 2014] = "EmptyLoopSource", e[e.InvalidElement = 3002] = "InvalidElement", e[e.ChildrenExpected = 3003] = "ChildrenExpected", e[e.SingleChildExpected = 3004] = "SingleChildExpected", e))(t || {}), o = /* @__PURE__ */ ((e) => (e.If = "If", e.ElseIf = "ElseIf", e.Else = "Else", e.Check = "Check", e.Loop = "Loop", e.LoopProps = "LoopProps", e.Template = "Template", e.Unknown = "Unknown", e))(o || {});
|
|
4
|
+
const u = {
|
|
5
|
+
[t.IfBlockExpected]: "Missing 'If'",
|
|
6
|
+
[t.OnlyOneIfBlockExpected]: "Can only have one 'If'",
|
|
7
|
+
[t.OnlyOneElseBlockExpected]: "Can only have one 'Else'",
|
|
8
|
+
[t.CheckBlockExpected]: "'If', 'ElseIf', 'Else' need to be wrapped in 'Check'",
|
|
9
|
+
[t.InvalidElseBlockOrdinal]: "Invalid ordinals, 'Else' should be the last",
|
|
10
|
+
[t.InvalidIfBlockOrdinal]: "Invalid ordinals, 'If' block should be the first",
|
|
11
|
+
[t.InvalidElseIfBlockOrdinal]: "Invalid ordinals, cannot have 'ElseIf' before 'If'",
|
|
12
|
+
[t.ChildrenExpected]: "Should at least have one child",
|
|
13
|
+
[t.InvalidElement]: "Invalid element",
|
|
14
|
+
[t.MalformedLoop]: "Malformed loop",
|
|
15
|
+
[t.EmptyLoopSource]: "Empty loop source",
|
|
16
|
+
[t.InfiniteLoopCondition]: "Infinite loop condition",
|
|
17
|
+
[t.TemplateBlockExpected]: "Missing 'Template'",
|
|
18
|
+
[t.OnlyOneTemplateBlockExpected]: "Only one Template block expected",
|
|
19
|
+
[t.SingleChildExpected]: "Can have only a single child"
|
|
20
|
+
}, f = ({ errors: e }) => /* @__PURE__ */ i("ol", { className: "trd-error-list", children: e.map((l, n) => /* @__PURE__ */ i("li", { children: u[l] }, `${l}-${n}`)) }), B = (e) => {
|
|
15
21
|
const l = [];
|
|
16
|
-
return e.length === 0 ? (l.push(
|
|
17
|
-
const { displayName:
|
|
18
|
-
(
|
|
22
|
+
return e.length === 0 ? (l.push(t.ChildrenExpected), l) : (e.forEach((n) => {
|
|
23
|
+
const { displayName: c = o.Unknown } = n.type ?? {};
|
|
24
|
+
(c === o.If || c === o.ElseIf || c === o.Else) && (l.push(t.InvalidElement), l.push(t.CheckBlockExpected));
|
|
19
25
|
}), l);
|
|
20
|
-
},
|
|
26
|
+
}, O = (e) => {
|
|
21
27
|
const l = [], n = {};
|
|
22
|
-
return e.length === 0 ? (l.push(
|
|
23
|
-
const { displayName:
|
|
24
|
-
n[
|
|
25
|
-
}), n[
|
|
26
|
-
},
|
|
27
|
-
e ===
|
|
28
|
-
},
|
|
29
|
-
e ===
|
|
28
|
+
return e.length === 0 ? (l.push(t.ChildrenExpected, t.IfBlockExpected), l) : (e.forEach((c, d) => {
|
|
29
|
+
const { displayName: a = o.Unknown } = c.type ?? {}, s = n[a] ?? 0;
|
|
30
|
+
n[a] = s + 1, x(a, d, n, l), y(a, d, e.length, n, l), C(a, d, l), v(a, l);
|
|
31
|
+
}), n[o.If] || l.push(t.IfBlockExpected), l);
|
|
32
|
+
}, x = (e, l, n, c) => {
|
|
33
|
+
e === o.If && (l !== 0 && c.push(t.InvalidIfBlockOrdinal), n[o.If] > 1 && c.push(t.OnlyOneIfBlockExpected));
|
|
34
|
+
}, y = (e, l, n, c, d) => {
|
|
35
|
+
e === o.Else && (c[o.Else] > 1 && d.push(t.OnlyOneElseBlockExpected), (l === 0 || l !== n - 1) && d.push(t.InvalidElseBlockOrdinal));
|
|
30
36
|
}, C = (e, l, n) => {
|
|
31
|
-
e ===
|
|
32
|
-
},
|
|
33
|
-
e !==
|
|
37
|
+
e === o.ElseIf && l === 0 && n.push(t.InvalidElseIfBlockOrdinal);
|
|
38
|
+
}, v = (e, l) => {
|
|
39
|
+
e !== o.If && e !== o.ElseIf && e !== o.Else && l.push(t.InvalidElement);
|
|
40
|
+
}, r = (e, l) => {
|
|
41
|
+
const n = [];
|
|
42
|
+
if (e.length === 0)
|
|
43
|
+
n.push(t.ChildrenExpected, t.TemplateBlockExpected);
|
|
44
|
+
else if (e.length > 1)
|
|
45
|
+
n.push(t.SingleChildExpected, t.OnlyOneTemplateBlockExpected);
|
|
46
|
+
else {
|
|
47
|
+
const { displayName: c = o.Unknown } = e[0].type;
|
|
48
|
+
c !== o.Template && n.push(t.InvalidElement, t.TemplateBlockExpected);
|
|
49
|
+
const { from: d = 0, to: a = 0, step: s = 0 } = l ?? {};
|
|
50
|
+
(s === 0 || s > 0 && d > a || s < 0 && d < a) && n.push(t.InfiniteLoopCondition);
|
|
51
|
+
}
|
|
52
|
+
return n.length && n.push(t.MalformedLoop), n;
|
|
34
53
|
};
|
|
35
|
-
class
|
|
54
|
+
class T {
|
|
36
55
|
static get(l) {
|
|
37
56
|
let n = () => [];
|
|
38
57
|
switch (l) {
|
|
39
|
-
case
|
|
40
|
-
n =
|
|
58
|
+
case o.Check:
|
|
59
|
+
n = O;
|
|
60
|
+
break;
|
|
61
|
+
case o.If:
|
|
62
|
+
case o.ElseIf:
|
|
63
|
+
case o.Else:
|
|
64
|
+
n = B;
|
|
41
65
|
break;
|
|
42
|
-
case
|
|
43
|
-
|
|
44
|
-
case t.Else:
|
|
45
|
-
n = u;
|
|
66
|
+
case o.Loop:
|
|
67
|
+
n = r;
|
|
46
68
|
break;
|
|
47
69
|
}
|
|
48
70
|
return n;
|
|
49
71
|
}
|
|
50
72
|
}
|
|
51
|
-
const
|
|
52
|
-
const { children: n } = e,
|
|
53
|
-
return
|
|
54
|
-
}, x = (e) => {
|
|
55
|
-
const l = I(e, t.Check);
|
|
56
|
-
if (l.length)
|
|
57
|
-
return { children: r(f, { errors: l }) };
|
|
58
|
-
const d = h.toArray(e.children).filter(E);
|
|
59
|
-
return { children: b(d) };
|
|
73
|
+
const h = (e, l) => {
|
|
74
|
+
const { children: n } = e, c = I.toArray(n);
|
|
75
|
+
return E(() => T.get(l), [l])(c, e);
|
|
60
76
|
}, b = (e) => {
|
|
77
|
+
const l = h(e, o.Check);
|
|
78
|
+
if (l.length)
|
|
79
|
+
return { children: m(f, { errors: l }) };
|
|
80
|
+
const c = I.toArray(e.children).filter(k);
|
|
81
|
+
return { children: S(c) };
|
|
82
|
+
}, S = (e) => {
|
|
61
83
|
for (const l of e) {
|
|
62
|
-
if (!l || !
|
|
84
|
+
if (!l || !k(l))
|
|
63
85
|
continue;
|
|
64
86
|
const { displayName: n } = l.type;
|
|
65
87
|
if (n) {
|
|
66
|
-
const { condition:
|
|
67
|
-
if ((n === "If" || n === "ElseIf") &&
|
|
88
|
+
const { condition: c } = l.props;
|
|
89
|
+
if ((n === "If" || n === "ElseIf") && c === !0 || n === "Else")
|
|
68
90
|
return l;
|
|
69
91
|
}
|
|
70
92
|
}
|
|
71
93
|
return null;
|
|
72
|
-
},
|
|
73
|
-
const { children: l } =
|
|
74
|
-
return /* @__PURE__ */
|
|
94
|
+
}, M = (e) => {
|
|
95
|
+
const { children: l } = b(e);
|
|
96
|
+
return /* @__PURE__ */ i(p, { children: l });
|
|
75
97
|
};
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
const l =
|
|
79
|
-
return /* @__PURE__ */
|
|
98
|
+
M.displayName = o.Check;
|
|
99
|
+
const w = (e) => {
|
|
100
|
+
const l = h(e, "If"), { condition: n } = e, c = l.length === 0 ? e.children : /* @__PURE__ */ i(f, { errors: l });
|
|
101
|
+
return /* @__PURE__ */ i(p, { children: n && c });
|
|
80
102
|
};
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
const l =
|
|
84
|
-
return /* @__PURE__ */
|
|
103
|
+
w.displayName = o.If;
|
|
104
|
+
const N = (e) => {
|
|
105
|
+
const l = h(e, "ElseIf"), { condition: n } = e, c = l.length === 0 ? e.children : /* @__PURE__ */ i(f, { errors: l });
|
|
106
|
+
return /* @__PURE__ */ i(p, { children: n && c });
|
|
85
107
|
};
|
|
86
|
-
|
|
108
|
+
N.displayName = o.ElseIf;
|
|
87
109
|
const F = (e) => {
|
|
88
|
-
const l =
|
|
89
|
-
return /* @__PURE__ */
|
|
110
|
+
const l = h(e, "Else"), n = l.length === 0 ? e.children : /* @__PURE__ */ i(f, { errors: l });
|
|
111
|
+
return /* @__PURE__ */ i(p, { children: n });
|
|
90
112
|
};
|
|
91
|
-
F.displayName =
|
|
113
|
+
F.displayName = o.Else;
|
|
114
|
+
o.Loop;
|
|
115
|
+
o.Template;
|
|
92
116
|
export {
|
|
93
|
-
|
|
117
|
+
M as Check,
|
|
94
118
|
F as Else,
|
|
95
|
-
|
|
96
|
-
|
|
119
|
+
N as ElseIf,
|
|
120
|
+
w as If
|
|
97
121
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(a,
|
|
2
|
-
/*$vite$:1*/`,document.head.appendChild(I);var
|
|
1
|
+
(function(a,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],c):(a=typeof globalThis<"u"?globalThis:a||self,c(a["ts-react-directives"]={},a["react/jsx-runtime"],a.React))})(this,function(a,c,p){"use strict";var I=document.createElement("style");I.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(I);var t=(e=>(e[e.IfBlockExpected=2001]="IfBlockExpected",e[e.OnlyOneIfBlockExpected=2002]="OnlyOneIfBlockExpected",e[e.OnlyOneElseBlockExpected=2003]="OnlyOneElseBlockExpected",e[e.CheckBlockExpected=2005]="CheckBlockExpected",e[e.InvalidIfBlockOrdinal=2004]="InvalidIfBlockOrdinal",e[e.InvalidElseBlockOrdinal=2006]="InvalidElseBlockOrdinal",e[e.InvalidElseIfBlockOrdinal=2007]="InvalidElseIfBlockOrdinal",e[e.MalformedLoop=2008]="MalformedLoop",e[e.InfiniteLoopCondition=2009]="InfiniteLoopCondition",e[e.TemplateBlockExpected=2012]="TemplateBlockExpected",e[e.OnlyOneTemplateBlockExpected=2013]="OnlyOneTemplateBlockExpected",e[e.EmptyLoopSource=2014]="EmptyLoopSource",e[e.InvalidElement=3002]="InvalidElement",e[e.ChildrenExpected=3003]="ChildrenExpected",e[e.SingleChildExpected=3004]="SingleChildExpected",e))(t||{}),o=(e=>(e.If="If",e.ElseIf="ElseIf",e.Else="Else",e.Check="Check",e.Loop="Loop",e.LoopProps="LoopProps",e.Template="Template",e.Unknown="Unknown",e))(o||{});const y={[t.IfBlockExpected]:"Missing 'If'",[t.OnlyOneIfBlockExpected]:"Can only have one 'If'",[t.OnlyOneElseBlockExpected]:"Can only have one 'Else'",[t.CheckBlockExpected]:"'If', 'ElseIf', 'Else' need to be wrapped in 'Check'",[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",[t.MalformedLoop]:"Malformed loop",[t.EmptyLoopSource]:"Empty loop source",[t.InfiniteLoopCondition]:"Infinite loop condition",[t.TemplateBlockExpected]:"Missing 'Template'",[t.OnlyOneTemplateBlockExpected]:"Only one Template block expected",[t.SingleChildExpected]:"Can have only a single child"},r=({errors:e})=>c.jsx("ol",{className:"trd-error-list",children:e.map((l,n)=>c.jsx("li",{children:y[l]},`${l}-${n}`))}),v=e=>{const l=[];return e.length===0?(l.push(t.ChildrenExpected),l):(e.forEach(n=>{const{displayName:d=o.Unknown}=n.type??{};(d===o.If||d===o.ElseIf||d===o.Else)&&(l.push(t.InvalidElement),l.push(t.CheckBlockExpected))}),l)},B=e=>{const l=[],n={};return e.length===0?(l.push(t.ChildrenExpected,t.IfBlockExpected),l):(e.forEach((d,i)=>{const{displayName:s=o.Unknown}=d.type??{},f=n[s]??0;n[s]=f+1,C(s,i,n,l),O(s,i,e.length,n,l),x(s,i,l),b(s,l)}),n[o.If]||l.push(t.IfBlockExpected),l)},C=(e,l,n,d)=>{e===o.If&&(l!==0&&d.push(t.InvalidIfBlockOrdinal),n[o.If]>1&&d.push(t.OnlyOneIfBlockExpected))},O=(e,l,n,d,i)=>{e===o.Else&&(d[o.Else]>1&&i.push(t.OnlyOneElseBlockExpected),(l===0||l!==n-1)&&i.push(t.InvalidElseBlockOrdinal))},x=(e,l,n)=>{e===o.ElseIf&&l===0&&n.push(t.InvalidElseIfBlockOrdinal)},b=(e,l)=>{e!==o.If&&e!==o.ElseIf&&e!==o.Else&&l.push(t.InvalidElement)},T=(e,l)=>{const n=[];if(e.length===0)n.push(t.ChildrenExpected,t.TemplateBlockExpected);else if(e.length>1)n.push(t.SingleChildExpected,t.OnlyOneTemplateBlockExpected);else{const{displayName:d=o.Unknown}=e[0].type;d!==o.Template&&n.push(t.InvalidElement,t.TemplateBlockExpected);const{from:i=0,to:s=0,step:f=0}=l??{};(f===0||f>0&&i>s||f<0&&i<s)&&n.push(t.InfiniteLoopCondition)}return n.length&&n.push(t.MalformedLoop),n};class g{static get(l){let n=()=>[];switch(l){case o.Check:n=B;break;case o.If:case o.ElseIf:case o.Else:n=v;break;case o.Loop:n=T;break}return n}}const h=(e,l)=>{const{children:n}=e,d=p.Children.toArray(n);return p.useMemo(()=>g.get(l),[l])(d,e)},S=e=>{const l=h(e,o.Check);if(l.length)return{children:p.createElement(r,{errors:l})};const d=p.Children.toArray(e.children).filter(p.isValidElement);return{children:F(d)}},F=e=>{for(const l of e){if(!l||!p.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},k=e=>{const{children:l}=S(e);return c.jsx(c.Fragment,{children:l})};k.displayName=o.Check;const E=e=>{const l=h(e,"If"),{condition:n}=e,d=l.length===0?e.children:c.jsx(r,{errors:l});return c.jsx(c.Fragment,{children:n&&d})};E.displayName=o.If;const m=e=>{const l=h(e,"ElseIf"),{condition:n}=e,d=l.length===0?e.children:c.jsx(r,{errors:l});return c.jsx(c.Fragment,{children:n&&d})};m.displayName=o.ElseIf;const u=e=>{const l=h(e,"Else"),n=l.length===0?e.children:c.jsx(r,{errors:l});return c.jsx(c.Fragment,{children:n})};u.displayName=o.Else,o.Loop,o.Template,a.Check=k,a.Else=u,a.ElseIf=m,a.If=E,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openbytes/ts-react-directives",
|
|
3
|
-
"
|
|
3
|
+
"description": "A collection of React directives to simplify conditional rendering and loops.",
|
|
4
|
+
"version": "1.1.0",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"author": {
|
|
6
7
|
"name": "SkyCodr (aka: Dulan Sudasinghe)",
|
|
@@ -11,6 +12,24 @@
|
|
|
11
12
|
"dist",
|
|
12
13
|
"README.md"
|
|
13
14
|
],
|
|
15
|
+
"keywords": [
|
|
16
|
+
"react",
|
|
17
|
+
"typescript",
|
|
18
|
+
"directives",
|
|
19
|
+
"conditionals",
|
|
20
|
+
"if",
|
|
21
|
+
"loops",
|
|
22
|
+
"rendering",
|
|
23
|
+
"conditional rendering"
|
|
24
|
+
],
|
|
25
|
+
"homepage": "https://skycodr.github.io/ts-react-directives/",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/skycodr/ts-react-directives.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/skycodr/ts-react-directives/issues"
|
|
32
|
+
},
|
|
14
33
|
"readme": "README.md",
|
|
15
34
|
"types": "dist/ts-react-directives.d.ts",
|
|
16
35
|
"main": "dist/ts-react-directives.cjs.js",
|
|
@@ -29,9 +48,7 @@
|
|
|
29
48
|
},
|
|
30
49
|
"dependencies": {
|
|
31
50
|
"react": "^19.0.0",
|
|
32
|
-
"react-dom": "^19.0.0"
|
|
33
|
-
"react-syntax-highlighter": "^15.6.1",
|
|
34
|
-
"react-tabs": "^6.1.0"
|
|
51
|
+
"react-dom": "^19.0.0"
|
|
35
52
|
},
|
|
36
53
|
"devDependencies": {
|
|
37
54
|
"@commitlint/cli": "^19.7.1",
|
|
@@ -57,6 +74,8 @@
|
|
|
57
74
|
"jsdom": "^26.0.0",
|
|
58
75
|
"lint-staged": "^15.4.3",
|
|
59
76
|
"prettier": "^3.5.1",
|
|
77
|
+
"react-syntax-highlighter": "^15.6.1",
|
|
78
|
+
"react-tabs": "^6.1.0",
|
|
60
79
|
"rimraf": "^6.0.1",
|
|
61
80
|
"typescript": "~5.7.3",
|
|
62
81
|
"typescript-eslint": "^8.24.0",
|
|
@@ -79,7 +98,7 @@
|
|
|
79
98
|
"preview": "vite preview --config vite.config.examples.ts",
|
|
80
99
|
"build:lib": "tsc -b && vite build",
|
|
81
100
|
"build:examples": "tsc -b && vite build --config vite.config.examples.ts",
|
|
82
|
-
"build": "
|
|
101
|
+
"build": "pnpm build:lib && pnpm build:examples",
|
|
83
102
|
"lint": "eslint src/**/*.{ts,tsx}",
|
|
84
103
|
"lint:fix": "eslint --fix src/**/*.{ts,tsx}",
|
|
85
104
|
"format": "prettier --check src/**/*.{ts,tsx}",
|