@openbytes/ts-react-directives 0.1.4 → 0.1.5
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,7 +1,7 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { isValidElement as
|
|
1
|
+
import { jsx as d, Fragment as I } from "react/jsx-runtime";
|
|
2
|
+
import { isValidElement as p, Children as m, createElement as O } from "react";
|
|
3
3
|
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 || {});
|
|
4
|
-
const
|
|
4
|
+
const x = {
|
|
5
5
|
[t.IfBlockExpected]: "Missing 'If'",
|
|
6
6
|
[t.OnlyOneIfBlockExpected]: "Can only have one 'If'",
|
|
7
7
|
[t.OnlyOneElseBlockExpected]: "Can only have one 'Else'",
|
|
@@ -11,38 +11,38 @@ const u = {
|
|
|
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
|
-
},
|
|
14
|
+
}, r = ({ errors: e }) => /* @__PURE__ */ d("ol", { className: "error-list", children: e.map((n, l) => /* @__PURE__ */ d("li", { children: x[n] }, `${n}-${l}`)) }), y = (e) => {
|
|
15
15
|
const n = [];
|
|
16
16
|
return e.length === 0 ? (n.push(t.ChildrenExpected), n) : (e.forEach((l) => {
|
|
17
|
-
const
|
|
18
|
-
(
|
|
17
|
+
const c = p(l) ? l.type.name : "unknown";
|
|
18
|
+
(c === o.name || c === h.name || c === f.name) && n.push(t.SwitchBlockExpected);
|
|
19
19
|
}), n);
|
|
20
|
-
},
|
|
20
|
+
}, w = (e) => {
|
|
21
21
|
const n = [], l = {};
|
|
22
|
-
return e.length === 0 ? (n.push(t.ChildrenExpected, t.IfBlockExpected), n) : (e.forEach((
|
|
23
|
-
const
|
|
24
|
-
l[
|
|
22
|
+
return e.length === 0 ? (n.push(t.ChildrenExpected, t.IfBlockExpected), n) : (e.forEach((c, s) => {
|
|
23
|
+
const a = p(c) ? c.type.name : "unknown", i = l[a] ?? 0;
|
|
24
|
+
l[a] = i + 1, S(a, s, l, n), C(a, s, e.length, l, n), b(a, s, n), F(a, n);
|
|
25
25
|
}), l[o.name] || n.push(t.IfBlockExpected), n);
|
|
26
|
-
},
|
|
27
|
-
e === o.name && (n !== 0 &&
|
|
28
|
-
},
|
|
29
|
-
e ===
|
|
30
|
-
},
|
|
31
|
-
e ===
|
|
32
|
-
},
|
|
33
|
-
e !== o.name && e !==
|
|
26
|
+
}, S = (e, n, l, c) => {
|
|
27
|
+
e === o.name && (n !== 0 && c.push(t.InvalidIfBlockOrdinal), l[o.name] > 1 && c.push(t.OnlyOneIfBlockExpected));
|
|
28
|
+
}, C = (e, n, l, c, s) => {
|
|
29
|
+
e === f.name && (c[f.name] > 1 && s.push(t.OnlyOneElseBlockExpected), (n === 0 || n !== l - 1) && s.push(t.InvalidElseBlockOrdinal));
|
|
30
|
+
}, b = (e, n, l) => {
|
|
31
|
+
e === h.name && n === 0 && l.push(t.InvalidElseIfBlockOrdinal);
|
|
32
|
+
}, F = (e, n) => {
|
|
33
|
+
e !== o.name && e !== h.name && e !== f.name && n.push(t.InvalidElement);
|
|
34
34
|
};
|
|
35
|
-
class
|
|
35
|
+
class R {
|
|
36
36
|
static get(n) {
|
|
37
37
|
let l;
|
|
38
38
|
switch (n) {
|
|
39
|
-
case
|
|
40
|
-
l =
|
|
39
|
+
case v.name:
|
|
40
|
+
l = w;
|
|
41
41
|
break;
|
|
42
42
|
case o.name:
|
|
43
|
-
case f.name:
|
|
44
43
|
case h.name:
|
|
45
|
-
|
|
44
|
+
case f.name:
|
|
45
|
+
l = y;
|
|
46
46
|
break;
|
|
47
47
|
default:
|
|
48
48
|
l = () => [];
|
|
@@ -50,34 +50,41 @@ class _ {
|
|
|
50
50
|
return l;
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
|
-
const
|
|
54
|
-
const { children: l } = e,
|
|
55
|
-
return
|
|
56
|
-
},
|
|
57
|
-
const n =
|
|
53
|
+
const u = (e, n) => {
|
|
54
|
+
const { children: l } = e, c = m.toArray(l);
|
|
55
|
+
return R.get(n)(c);
|
|
56
|
+
}, V = (e) => {
|
|
57
|
+
const n = u(e, v.name);
|
|
58
58
|
if (n.length)
|
|
59
|
-
return { children: O(
|
|
59
|
+
return { children: O(r, { errors: n }) };
|
|
60
60
|
const { children: l } = e;
|
|
61
|
-
return { children:
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
return { children: m.toArray(l).reduce((a, i) => {
|
|
62
|
+
if (a)
|
|
63
|
+
return a;
|
|
64
|
+
const { type: k } = i ?? {};
|
|
65
|
+
if (k) {
|
|
66
|
+
const { name: E } = k, { condition: B } = (i == null ? void 0 : i.props) ?? {};
|
|
67
|
+
if ((E === "If" || E === "ElseIf") && B === !0 || E === "Else")
|
|
68
|
+
return i;
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
64
71
|
}, null) };
|
|
65
|
-
},
|
|
66
|
-
const { children: n } =
|
|
67
|
-
return /* @__PURE__ */
|
|
72
|
+
}, v = (e) => {
|
|
73
|
+
const { children: n } = V(e);
|
|
74
|
+
return /* @__PURE__ */ d(I, { children: n });
|
|
68
75
|
}, o = (e) => {
|
|
69
|
-
const n =
|
|
70
|
-
return /* @__PURE__ */
|
|
71
|
-
}, f = (e) => {
|
|
72
|
-
const n = E(e, f.name), l = n.length === 0 ? e.children : /* @__PURE__ */ i(m, { errors: n });
|
|
73
|
-
return /* @__PURE__ */ i(I, { children: l });
|
|
76
|
+
const n = u(e, o.name), l = n.length === 0 ? e.children : /* @__PURE__ */ d(r, { errors: n });
|
|
77
|
+
return /* @__PURE__ */ d(I, { children: l });
|
|
74
78
|
}, h = (e) => {
|
|
75
|
-
const n =
|
|
76
|
-
return /* @__PURE__ */
|
|
79
|
+
const n = u(e, h.name), l = n.length === 0 ? e.children : /* @__PURE__ */ d(r, { errors: n });
|
|
80
|
+
return /* @__PURE__ */ d(I, { children: l });
|
|
81
|
+
}, f = (e) => {
|
|
82
|
+
const n = u(e, f.name), l = n.length === 0 ? e.children : /* @__PURE__ */ d(r, { errors: n });
|
|
83
|
+
return /* @__PURE__ */ d(I, { children: l });
|
|
77
84
|
};
|
|
78
85
|
export {
|
|
79
|
-
|
|
80
|
-
|
|
86
|
+
f as Else,
|
|
87
|
+
h as ElseIf,
|
|
81
88
|
o as If,
|
|
82
|
-
|
|
89
|
+
v as SwitchIf
|
|
83
90
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(c
|
|
1
|
+
(function(d,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):(d=typeof globalThis<"u"?globalThis:d||self,c(d["ts-react-directives"]={},d["react/jsx-runtime"],d.React))})(this,function(d,c,I){"use strict";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 B={[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"},E=({errors:e})=>c.jsx("ol",{className:"error-list",children:e.map((n,l)=>c.jsx("li",{children:B[n]},`${n}-${l}`))}),O=e=>{const n=[];return e.length===0?(n.push(t.ChildrenExpected),n):(e.forEach(l=>{const a=I.isValidElement(l)?l.type.name:"unknown";(a===s.name||a===r.name||a===f.name)&&n.push(t.SwitchBlockExpected)}),n)},m=e=>{const n=[],l={};return e.length===0?(n.push(t.ChildrenExpected,t.IfBlockExpected),n):(e.forEach((a,h)=>{const i=I.isValidElement(a)?a.type.name:"unknown",o=l[i]??0;l[i]=o+1,y(i,h,l,n),x(i,h,e.length,l,n),w(i,h,n),S(i,n)}),l[s.name]||n.push(t.IfBlockExpected),n)},y=(e,n,l,a)=>{e===s.name&&(n!==0&&a.push(t.InvalidIfBlockOrdinal),l[s.name]>1&&a.push(t.OnlyOneIfBlockExpected))},x=(e,n,l,a,h)=>{e===f.name&&(a[f.name]>1&&h.push(t.OnlyOneElseBlockExpected),(n===0||n!==l-1)&&h.push(t.InvalidElseBlockOrdinal))},w=(e,n,l)=>{e===r.name&&n===0&&l.push(t.InvalidElseIfBlockOrdinal)},S=(e,n)=>{e!==s.name&&e!==r.name&&e!==f.name&&n.push(t.InvalidElement)};class C{static get(n){let l;switch(n){case k.name:l=m;break;case s.name:case r.name:case f.name:l=O;break;default:l=()=>[]}return l}}const u=(e,n)=>{const{children:l}=e,a=I.Children.toArray(l);return C.get(n)(a)},b=e=>{const n=u(e,k.name);if(n.length)return{children:I.createElement(E,{errors:n})};const{children:l}=e;return{children:I.Children.toArray(l).reduce((i,o)=>{if(i)return i;const{type:v}=o??{};if(v){const{name:p}=v,{condition:F}=(o==null?void 0:o.props)??{};if((p==="If"||p==="ElseIf")&&F===!0||p==="Else")return o}return null},null)}},k=e=>{const{children:n}=b(e);return c.jsx(c.Fragment,{children:n})},s=e=>{const n=u(e,s.name),l=n.length===0?e.children:c.jsx(E,{errors:n});return c.jsx(c.Fragment,{children:l})},r=e=>{const n=u(e,r.name),l=n.length===0?e.children:c.jsx(E,{errors:n});return c.jsx(c.Fragment,{children:l})},f=e=>{const n=u(e,f.name),l=n.length===0?e.children:c.jsx(E,{errors:n});return c.jsx(c.Fragment,{children:l})};d.Else=f,d.ElseIf=r,d.If=s,d.SwitchIf=k,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openbytes/ts-react-directives",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "SkyCodr (aka: Dulan Sudasinghe)",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
],
|
|
12
12
|
"main": "dist/ts-react-directives.umd.js",
|
|
13
13
|
"module": "dist/ts-react-directives.es.js",
|
|
14
|
-
"types": "
|
|
14
|
+
"types": "dist/ts-react-directives.d.ts",
|
|
15
15
|
"exports": {
|
|
16
16
|
".": {
|
|
17
17
|
"import": "./dist/ts-react-directives.es.js",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=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.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 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"},m=({errors:e})=>d.jsx("ol",{className:"error-list",children:e.map((n,l)=>d.jsx("li",{children:v[n]},`${n}-${l}`))}),u=e=>{const n=[];return e.length===0?(n.push(t.ChildrenExpected),n):(e.forEach(l=>{const a=I.isValidElement(l)?l.type.name:"unknown";(a===s.name||a===f.name||a===h.name)&&n.push(t.SwitchBlockExpected)}),n)},B=e=>{const n=[],l={};return e.length===0?(n.push(t.ChildrenExpected,t.IfBlockExpected),n):(e.forEach((a,o)=>{const i=I.isValidElement(a)?a.type.name:"unknown",c=l[i]??0;l[i]=c+1,O(i,o,l,n),x(i,o,e.length,l,n),y(i,o,n),w(i,n)}),l[s.name]||n.push(t.IfBlockExpected),n)},O=(e,n,l,a)=>{e===s.name&&(n!==0&&a.push(t.InvalidIfBlockOrdinal),l[s.name]>1&&a.push(t.OnlyOneIfBlockExpected))},x=(e,n,l,a,o)=>{e===h.name&&(a[h.name]>1&&o.push(t.OnlyOneElseBlockExpected),(n===0||n!==l-1)&&o.push(t.InvalidElseBlockOrdinal))},y=(e,n,l)=>{e===f.name&&n===0&&l.push(t.InvalidElseIfBlockOrdinal)},w=(e,n)=>{e!==s.name&&e!==f.name&&e!==h.name&&n.push(t.InvalidElement)};class S{static get(n){let l;switch(n){case r.name:l=B;break;case s.name:case f.name:case h.name:l=u;break;default:l=()=>[]}return l}}const E=(e,n)=>{const{children:l}=e,a=I.Children.toArray(l);return S.get(n)(a)},j=e=>{const n=E(e,r.name);if(n.length)return{children:I.createElement(m,{errors:n})};const{children:l}=e;return{children:I.Children.toArray(l).reduce((i,c)=>{var k,p;return(k=i==null?void 0:i.props)!=null&&k.condition?i:(p=c==null?void 0:c.props)!=null&&p.condition||(c==null?void 0:c.type.name)==="Else"?c:i},null)}},r=e=>{const{children:n}=j(e);return d.jsx(d.Fragment,{children:n})},s=e=>{const n=E(e,s.name),l=n.length===0?e.children:d.jsx(m,{errors:n});return d.jsx(d.Fragment,{children:l})},f=e=>{const n=E(e,f.name),l=n.length===0?e.children:d.jsx(m,{errors:n});return d.jsx(d.Fragment,{children:l})},h=e=>{const n=E(e,h.name),l=n.length===0?e.children:d.jsx(m,{errors:n});return d.jsx(d.Fragment,{children:l})};exports.Else=h;exports.ElseIf=f;exports.If=s;exports.SwitchIf=r;
|