Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.

Commit aa88460

Browse files
committed
refactor(core): move tableElementRE as a seperate var
1 parent 66e3b9f commit aa88460

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/core/directives/for.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import computeExpression from '@utils/computeExpression';
88
import { expressionPropRE, parenthesisWrapReplaceRE } from '@utils/patterns';
99

1010
// This directive is size-based, not content-based, since everything is compiled and rerendered
11+
// It's also quite expensive on performance, and should be refactored in the future
1112
export const forDirective = ({ el, data, state, node }: DirectiveProps): void => {
1213
const originalAST = el[COMPONENT_FLAG];
13-
// Initial compilation
1414
if (!originalAST) el[COMPONENT_FLAG] = compile(el, state);
1515

1616
const forLoopRE = /\s+(?:in|of)\s+/gim;
@@ -24,15 +24,17 @@ export const forDirective = ({ el, data, state, node }: DirectiveProps): void =>
2424
const template = el[FOR_TEMPLATE_FLAG];
2525
if (el.innerHTML.trim() === template) el.innerHTML = '';
2626

27+
// This just checks if there is deviation from both (removal/addition/nochange)
2728
const arrayDiff = currArray?.length - el.children.length;
29+
const tableElementRE = /^[^\S]*?<(t(?:head|body|foot|r|d|h))/i;
2830

2931
if (currArray?.length === 0) el.innerHTML = '';
3032
else if (arrayDiff !== 0) {
3133
for (let i = Math.abs(arrayDiff); i > 0; --i) {
3234
if (arrayDiff < 0) el.removeChild(el.lastChild as Node);
3335
else {
3436
let content = String(template);
35-
const isTable = /^[^\S]*?<(t(?:head|body|foot|r|d|h))/i.test(content);
37+
const isTable = tableElementRE.test(content);
3638

3739
/* istanbul ignore next */
3840
if (item) {

src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ export { component };
1010
export const init = (element: HTMLElement | Document = document): void => {
1111
const stateDirective = `${DIRECTIVE_PREFIX}state`;
1212
const componentElements = element.querySelectorAll<HTMLElement>(`[${stateDirective}]`);
13-
const uninitializedComponents = [...componentElements].filter(
14-
(el) => el[COMPONENT_FLAG] === undefined,
15-
);
13+
const uninitializedComponents = [...componentElements].filter((el) => !el[COMPONENT_FLAG]);
1614

1715
uninitializedComponents.forEach((uninitializedComponent) => {
1816
const stateExpression = uninitializedComponent.getAttribute(stateDirective);

0 commit comments

Comments
 (0)