@openbytes/ts-react-directives 0.1.7 → 0.2.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.
|
@@ -0,0 +1 @@
|
|
|
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,23 +1,24 @@
|
|
|
1
1
|
import { FC } from 'react';
|
|
2
2
|
import { PropsWithChildren } from 'react';
|
|
3
|
-
import { ReactNode } from 'react';
|
|
4
3
|
|
|
5
|
-
export declare const Else: FC<ElseProps
|
|
4
|
+
export declare const Else: FC<PropsWithChildren<ElseProps>>;
|
|
6
5
|
|
|
7
6
|
export declare const ElseIf: FC<ElseIfProps>;
|
|
8
7
|
|
|
9
|
-
declare type ElseIfProps
|
|
8
|
+
export declare type ElseIfProps = PropsWithChildren<{
|
|
9
|
+
condition: boolean;
|
|
10
|
+
}>;
|
|
10
11
|
|
|
11
|
-
declare type ElseProps
|
|
12
|
+
declare type ElseProps = {};
|
|
12
13
|
|
|
13
14
|
export declare const If: FC<IfProps>;
|
|
14
15
|
|
|
15
|
-
declare type IfProps
|
|
16
|
+
export declare type IfProps = PropsWithChildren<{
|
|
16
17
|
condition: boolean;
|
|
17
|
-
}
|
|
18
|
+
}>;
|
|
18
19
|
|
|
19
|
-
export declare const SwitchIf: FC<SwitchIfProps
|
|
20
|
+
export declare const SwitchIf: FC<PropsWithChildren<SwitchIfProps>>;
|
|
20
21
|
|
|
21
|
-
declare type SwitchIfProps
|
|
22
|
+
declare type SwitchIfProps = {};
|
|
22
23
|
|
|
23
24
|
export { }
|
|
@@ -1,101 +1,99 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import {
|
|
3
|
-
import './index.css';var
|
|
4
|
-
const
|
|
5
|
-
[
|
|
6
|
-
[
|
|
7
|
-
[
|
|
8
|
-
[
|
|
9
|
-
[
|
|
10
|
-
[
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
},
|
|
15
|
-
const
|
|
16
|
-
return e.length === 0 ? (
|
|
17
|
-
const
|
|
18
|
-
(
|
|
19
|
-
}),
|
|
20
|
-
},
|
|
21
|
-
const
|
|
22
|
-
return e.length === 0 ? (
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
}),
|
|
26
|
-
},
|
|
27
|
-
e ===
|
|
28
|
-
},
|
|
29
|
-
e ===
|
|
30
|
-
},
|
|
31
|
-
e ===
|
|
32
|
-
},
|
|
33
|
-
e !==
|
|
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 || {});
|
|
4
|
+
const v = {
|
|
5
|
+
[t.IfBlockExpected]: "Missing 'If'",
|
|
6
|
+
[t.OnlyOneIfBlockExpected]: "Can only have one 'If'",
|
|
7
|
+
[t.OnlyOneElseBlockExpected]: "Can only have one 'Else'",
|
|
8
|
+
[t.SwitchBlockExpected]: "'If', 'ElseIf', 'Else' need to be wrapped in 'Switch'",
|
|
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
|
+
}, 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) => {
|
|
15
|
+
const l = [];
|
|
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);
|
|
19
|
+
}), l);
|
|
20
|
+
}, O = (e) => {
|
|
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);
|
|
34
34
|
};
|
|
35
|
-
class
|
|
36
|
-
static get(
|
|
37
|
-
let
|
|
38
|
-
switch (
|
|
39
|
-
case
|
|
40
|
-
|
|
35
|
+
class N {
|
|
36
|
+
static get(l) {
|
|
37
|
+
let n;
|
|
38
|
+
switch (l) {
|
|
39
|
+
case m.displayName:
|
|
40
|
+
n = O;
|
|
41
41
|
break;
|
|
42
|
-
case
|
|
43
|
-
case
|
|
44
|
-
case
|
|
45
|
-
|
|
42
|
+
case i.displayName:
|
|
43
|
+
case f.displayName:
|
|
44
|
+
case o.displayName:
|
|
45
|
+
n = B;
|
|
46
46
|
break;
|
|
47
47
|
default:
|
|
48
|
-
|
|
48
|
+
n = () => [];
|
|
49
49
|
}
|
|
50
|
-
return
|
|
50
|
+
return n;
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
|
-
const
|
|
54
|
-
const { children:
|
|
55
|
-
return
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
if (f === "Else") {
|
|
75
|
-
I("__ELEMENT_FOUND_2__", f), c = t[i];
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
i++;
|
|
80
|
-
} while (i < t.length);
|
|
53
|
+
const r = (e, l) => {
|
|
54
|
+
const { children: n } = e, c = p.toArray(n);
|
|
55
|
+
return N.get(l)(c);
|
|
56
|
+
}, C = (e) => {
|
|
57
|
+
const l = r(e, "SwitchIf");
|
|
58
|
+
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
|
+
}, b = (e) => {
|
|
63
|
+
for (const l of e) {
|
|
64
|
+
if (!l || !E(l))
|
|
65
|
+
continue;
|
|
66
|
+
const { displayName: n } = l.type;
|
|
67
|
+
if (n) {
|
|
68
|
+
const { condition: c } = l.props;
|
|
69
|
+
if ((n === "If" || n === "ElseIf") && c === !0 || n === "Else")
|
|
70
|
+
return l;
|
|
71
|
+
}
|
|
81
72
|
}
|
|
82
|
-
return
|
|
83
|
-
},
|
|
84
|
-
const { children:
|
|
85
|
-
return /* @__PURE__ */
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
73
|
+
return null;
|
|
74
|
+
}, m = (e) => {
|
|
75
|
+
const { children: l } = C(e);
|
|
76
|
+
return /* @__PURE__ */ a(h, { children: l });
|
|
77
|
+
};
|
|
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 });
|
|
82
|
+
};
|
|
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 });
|
|
87
|
+
};
|
|
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 });
|
|
95
92
|
};
|
|
93
|
+
o.displayName = "Else";
|
|
96
94
|
export {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
o as Else,
|
|
96
|
+
f as ElseIf,
|
|
97
|
+
i as If,
|
|
98
|
+
m as SwitchIf
|
|
101
99
|
};
|
|
@@ -0,0 +1,2 @@
|
|
|
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"})});
|
package/package.json
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openbytes/ts-react-directives",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "SkyCodr (aka: Dulan Sudasinghe)",
|
|
7
7
|
"email": "dulan81@gmail.com"
|
|
8
8
|
},
|
|
9
|
+
"license": "MIT",
|
|
9
10
|
"files": [
|
|
10
11
|
"dist"
|
|
11
12
|
],
|
|
13
|
+
"main": "dist/ts-react-directives.cjs.js",
|
|
12
14
|
"module": "dist/ts-react-directives.es.js",
|
|
13
15
|
"types": "dist/ts-react-directives.d.ts",
|
|
14
16
|
"exports": {
|
|
15
17
|
".": {
|
|
16
|
-
"import": "./dist/ts-react-directives.es.js"
|
|
18
|
+
"import": "./dist/ts-react-directives.es.js",
|
|
19
|
+
"require": "./dist/ts-react-directives.cjs.js"
|
|
17
20
|
}
|
|
18
21
|
},
|
|
19
22
|
"dependencies": {
|
|
@@ -68,6 +71,6 @@
|
|
|
68
71
|
"test": "vitest",
|
|
69
72
|
"test:ui": "vitest --ui",
|
|
70
73
|
"test:coverage": "vitest --coverage",
|
|
71
|
-
"publish:registry": "pnpm publish
|
|
74
|
+
"publish:registry": "pnpm publish"
|
|
72
75
|
}
|
|
73
76
|
}
|