Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

関数とコメント

色々javascriptを書いてみた


/**
 *  @type{HTMLDivElement}
 */
const d = document.getElementById("cell");

/**
 *  @type{HTMLDivElement}
 */
const listBox = document.getElementById("listBox");


const toStr = (t) => {
    if (t == null) {
        return "null";
    } else {
        return t.toString();
    }
}

/**
 * 
 * @param {Node} node 
 * @returns {string}
 */
function nodeDesc(node) {
    const list = [node.nodeName,
    node.nodeType,
    node.nodeValue,
    node.hasChildNodes()];
    const listStrings = list.map(x => toStr(x));
    return listStrings.join(",");
}

/**
 * 
 * @param {string} txt 
 */
function log(txt) {
    console.log(txt);
}


/**
 * @param {Node} node 
 * @returns {(txt:string) => void}
 */
var logNode = (node) => (txt) => log(txt + nodeDesc(node));

/**
 * 
 * @param {Node} e 
 * @returns {HTMLDivElement}
 */
const div = (e) => {
    if (e.nodeName == "DIV") {
        return e;
    }
    return null;
};

/*
    nodes.find(x => x.nodeName == nodeName)
*/

/**
 * 
 * @param {Node} node 
 * @return {(Node) => Node}
 */
var findTag = (node) => (tagFunc) => (index) => {
    let t = node.firstChild;
    let count = 0;
    do {
        const x = tagFunc(t);
        if (x != null) {
            if (count == index) {
                return x;
            }
            count++;            
        }
        t = t.nextSibling;
    } while (t != null);

    return null;
}

var findDiv = (node) => (index) => {
    return findTag(node)(div)(index);
}



/**
 *  @type{HTMLDivElement}
 */
for (let i = 0; i < 10; i++) {
    const dc = d.cloneNode(true);
    dc.style.visibility = "visible";
    dc.id = "cell" + i;


    const found = findDiv(dc)(1);
    if (found != null) {
        found.innerText = "hoge!";
    }

    listBox.appendChild(dc);
}