All files / lib dom.js

44.44% Statements 8/18
0% Branches 0/8
33.33% Functions 2/6
37.5% Lines 6/16

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    5x   5x   1x             1x         1x       1x                                  
import { removeFirst } from "./strings";
 
export const isClassname = word => word.startsWith('.');
 
export const isId = word => word.startsWith('#');
 
export const DEFAULT_FULL_SIZE_STYLE = {
    position: 'absolute',
    height: '100%',
    width: '100%',
    margin: '0'
};
 
export const getOrCreateElement = (selector, type, style) => (
    document.querySelector(selector) ||
    createElementFromSelector(selector, type, style)
)
 
export const removeElement = selector => (
    document.querySelector(selector).remove()
)
 
export const createElementFromSelector = (selector, type = 'div', style = DEFAULT_FULL_SIZE_STYLE) => {
    const element = document.createElement(type);
    
    Object
        .keys(style)
        .forEach((property) => {
            element.style.setProperty(property, style[property]);
        });
 
    if (isClassname(selector)) {
        element.className = removeFirst(selector);
    } else if (isId(selector)) {
        element.id = removeFirst(selector);
    }
 
    return element;
};