All files / src/util m.js

0% Statements 0/55
0% Branches 0/12
0% Functions 0/11
0% Lines 0/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                                                                                                                                                                                           
import { components } from "../component/components";
 
const createElement = (type) => document.createElement(type);
 
const createTextNode = (content) => document.createTextNode(content);
 
const createComment = () => document.createComment("");
 
const setAttribute = (element, key, value) => {
	element.setAttribute(key, value);
};
 
const addEventListener = (element, type, handler) => {
	element.addEventListener(type, handler);
};
 
const setTextContent = (element, content) => {
	element.textContent = content;
};
 
const appendChild = (element, parent) => {
	parent.appendChild(element);
};
 
const removeChild = (element, parent) => {
	parent.removeChild(element);
};
 
const insertBefore = (element, reference, parent) => {
	parent.insertBefore(element, reference);
};
 
const directiveIf = (ifState, ifConditions, ifPortions, ifParent) => {
	for (let i = 0; i < ifConditions.length; i++) {
		if (ifConditions[i]) {
			const ifPortion = ifPortions[i];
 
			if (ifState === ifPortion) {
				ifPortion[1]();
			} else {
				if (ifState) {
					ifState[2]();
				}
 
				ifPortion[0](ifParent);
				ifPortion[1]();
 
				ifState = ifPortion;
			}
 
			return ifState;
		}
	}
};
 
const directiveFor = (forIdentifiers, forLocals, forValue, forPortion, forPortions, forParent) => {
	const previousLength = forPortions.length;
	const nextLength = forValue.length;
	const maxLength = previousLength > nextLength ? previousLength : nextLength;
 
	const keyIdentifier = forIdentifiers[1];
	const valueIdentifier = forIdentifiers[0];
 
	for (let i = 0; i < maxLength; i++) {
		if (i >= previousLength) {
			const forLocal = {};
			forLocal[keyIdentifier] = i;
			forLocal[valueIdentifier] = forValue[i];
			forLocals[i] = forLocal;
 
			const newForPortion = forPortion(forLocal);
			forPortions.push(newForPortion);
 
			newForPortion[0](forParent);
			newForPortion[1]();
		} else if (i >= nextLength) {
			forPortions.pop()[2]();
		} else {
			const forLocal = forLocals[i];
			forLocal[keyIdentifier] = i;
			forLocal[valueIdentifier] = forValue[i];
 
			forPortions[i][1]();
		}
	}
};
 
export const m = {
	c: components,
	ce: createElement,
	ctn: createTextNode,
	cc: createComment,
	sa: setAttribute,
	ael: addEventListener,
	stc: setTextContent,
	ac: appendChild,
	rc: removeChild,
	ib: insertBefore,
	di: directiveIf,
	df: directiveFor
};