Узел логического оператора в AST. Представляет логический оператор && с условным отображением.
<div> ${context.isAdmin && html`<button>Админ-панель</button>`}</div> Copy
<div> ${context.isAdmin && html`<button>Админ-панель</button>`}</div>
Результат:
{ "tag": "div", "type": "el", "child": [ { "type": "log", "data": "/context/isAdmin", "child": [ { "tag": "button", "type": "el", "child": [ { "type": "text", "value": "Админ-панель" } ] } ] } ]} Copy
{ "tag": "div", "type": "el", "child": [ { "type": "log", "data": "/context/isAdmin", "child": [ { "tag": "button", "type": "el", "child": [ { "type": "text", "value": "Админ-панель" } ] } ] } ]}
<div> ${core.notifications.length > 0 && html` <div class="notifications"> ${core.notifications.map(n => html`<div>${n.message}</div>`)} </div> `}</div> Copy
<div> ${core.notifications.length > 0 && html` <div class="notifications"> ${core.notifications.map(n => html`<div>${n.message}</div>`)} </div> `}</div>
{ "tag": "div", "type": "el", "child": [ { "type": "log", "data": "/core/notifications.length", "expr": "${[0]} > 0", "child": [ { "tag": "div", "type": "el", "string": { "class": "notifications" }, "child": [ { "type": "map", "data": "/core/notifications", "child": [ { "tag": "div", "type": "el", "child": [ { "type": "text", "data": "[item]/message" } ] } ] } ] } ] } ]} Copy
{ "tag": "div", "type": "el", "child": [ { "type": "log", "data": "/core/notifications.length", "expr": "${[0]} > 0", "child": [ { "tag": "div", "type": "el", "string": { "class": "notifications" }, "child": [ { "type": "map", "data": "/core/notifications", "child": [ { "tag": "div", "type": "el", "child": [ { "type": "text", "data": "[item]/message" } ] } ] } ] } ] } ]}
elements = parse<Context, Core>(({ html, context, core }) => html` <div> ${core.user && context.isAdmin && html` <div class="admin">Admin Panel</div> `} </div>`) Copy
elements = parse<Context, Core>(({ html, context, core }) => html` <div> ${core.user && context.isAdmin && html` <div class="admin">Admin Panel</div> `} </div>`)
[ { tag: "div", type: "el", child: [ { type: "log", data: ["/core/user", "/context/isAdmin"], expr: "_[0] && _[1]", child: [ { tag: "div", type: "el", string: { class: "admin", }, child: [ { type: "text", value: "Admin Panel", }, ], }, ], }, ], },] Copy
[ { tag: "div", type: "el", child: [ { type: "log", data: ["/core/user", "/context/isAdmin"], expr: "_[0] && _[1]", child: [ { tag: "div", type: "el", string: { class: "admin", }, child: [ { type: "text", value: "Admin Panel", }, ], }, ], }, ], },]
Структура узла:
type
data
expr
child
Тип узла - всегда "log" для логических операторов
Путь(и) к данным для условия
data: "/context/isAdmin" Copy
data: "/context/isAdmin"
data: ["/context/notifications", "/context/count"] Copy
data: ["/context/notifications", "/context/count"]
Optional
Выражение с индексами (если условие сложное)
expr: "${[0]} === 'admin' && ${[1]}.includes('delete')" Copy
expr: "${[0]} === 'admin' && ${[1]}.includes('delete')"
Дочерние узлы, которые отображаются только если условие истинно
Узел логического оператора в AST. Представляет логический оператор && с условным отображением.
Example: Простое логическое условие
Результат:
Example: Логическое условие с проверкой массива
Результат:
Сложное логическое условие
Структура узла:
type- всегда "log" для логических операторовdata- путь(и) к данным для условияexpr- выражение с индексами (если условие сложное)child- дочерние узлы, которые отображаются только если условие истинно