helper functions

๐Ÿ”ฐ CSS โŸฉ Layout โŸฉ System โŸฉ

// 0.1.7 + arr.last()
// 0.1.8 + toString()
// 0.1.9 + isEqualSets()
// 0.2.0 - toString(): + Map, Set

/* ------- โญ๏ธ debug ------- */

// โญ๏ธ log()
export function log(x){ console.log(x) }

/* ------- โญ๏ธ Object ------- */

// โญ๏ธ toString(obj)
export function toString(obj){

    // Set
    if (String(obj) === '[object Set]') 
        return `{${[...obj].map(x=>toString(x)).join(', ')}}`;
    
    // Map
    if (String(obj) === '[object Map]') 
        return `{${[...obj].map(p => `${p[0]}โ†’${toString(p[1])}`).join(', ')}}`;
    
    // function: return its code
    if (typeof obj === 'function') return obj.toString();
    
    // NaN
    if (obj !== null && obj !== undefined && obj.toString() === 'NaN') return 'NaN';
    
    // convert to string and check if it's an object
    let str = JSON.stringify(obj);
    let isObject = (/^\{.*\}$/g).test(str);   // { ... }
    
    // not an object, return str anyway
    if (!isObject) return str;
    
    // IS an object, try to get its methods as well.
    let result = [];
    for (let key in obj) {
        result.push(`${key}: ${toString(obj[key])}`);
    }
    return `{${result.join(', ')}}`;
}

// ---------- Set Equality ----------

// isEqualSets(a,b)
export function isEqualSets(a, b){
    if (a.size !== b.size) return false;
    return Array.from(a).every(elem => b.has(elem));
}

// setA.equal(setB)
// Set.prototype.equal = function(setB){
//     return isEqualSets(this, setB);
// }

/* ------- โญ๏ธ Array ------- */

// โญ๏ธ escapeHTML()
export function lastOfArray(arr){
    return arr[this.length - 1];
}

// โญ๏ธ arr.last()
// Array.prototype.last = function(){
//     return this[this.length - 1];
// }


/* ------- โญ๏ธ String ------- */

// โญ๏ธ escapeHTML()
export function escapeHTML(str){
    return new Option(str).innerHTML;
}

// โญ๏ธ mapString(str, mapf)
export function mapString(str, mapf){    
    return Array.prototype.map.call(str, mapf);
};

// โญ๏ธ StringfromCharCodes(65, 70) -> 'ABCDEF'
export function StringfromCharCodes(from, to) {
    let array = [];
    for (let i = from; i <= to; i++) array.push(i);
    return array.map((code) => String.fromCharCode(code)).join('');
}

/* ------- โญ๏ธ DOM: selectors ------- */

// โญ๏ธ $(): select first
export function $(selector, parent = document){
  return parent.querySelector(selector);
}

// โญ๏ธ $(): select all
export function $all(selector, parent = document){
  return parent.querySelectorAll(selector);
}

/* ------- โญ๏ธ random ------- */

// โญ๏ธ randomInt(min, max): min ... max (included)
export function randomInt(min, max) {
  return Math.floor(Math.random()*(max-min+1)) + min;
}

// โญ๏ธ randomElement(arr)
export function randomElement(arr){
    return arr[randomInt(0, arr.length - 1)];
}

// โญ๏ธ randomChar(str)
// โญ๏ธ Note: this may not work well with Unicodeโ—๏ธ
export function randomChar(str) {
    return str[randomInt(0, str.length - 1)];
}

// โญ๏ธ randomColor()
//   randomColor(0.5): random color with alpha = 0.5
export function randomColor(alpha) {
    const [r,g,b] = [
        randomInt(0,255), 
        randomInt(0,255), 
        randomInt(0,255)
    ];
    return `rgb(${r} ${g} ${b}${alpha >=0 ? ` / ${alpha}` : ''})`;
}

/**
 * โญ๏ธ shuffle(arr)
 * - arr is shuffled in place
 * @param arr array to shuffle
 */
export function shuffle(arr) {

    var n = arr.length;

    for (let i = n - 1; i > 0; i--) {
        let j = randomInt(0, i);
        [arr[i], arr[j]] = [arr[j], arr[i]];
    }

    return arr;
}

/**
 * โญ๏ธ shuffleString()
 *    shuffle string
 * @param str [string] string to shuffle
 */
export function shuffleString(str){
    return shuffle(str.split('')).join('');
}

/* ------- โญ๏ธ Element ------- */

/**
 * โญ๏ธ attr(elem, attrName[, value])
 * - get: `attr(elem, 'padding')`
 * - set: `attr(elem, 'padding', '8px')`
 */
export function attr(elem, attrName, value=undefined){
    if (value) { elem.setAttribute(attrName, value) };
    return elem.getAttribute(attrName) || undefined;
};

// โญ๏ธ tag(name[, {textContent, attributes}])
//    tag('div')
//    tag('div', {
//        textContent = 'hello',
//        attributes = {'--color': 'red'}
//    }
export function tag(name, {
    textContent,
    attributes = {}
}={}){

    // create element
    let elem = document.createElement(name);

    // set text content if necessary
    if(textContent) elem.textContent = textContent;
    
    // set attributes
    for (const [key, value] of Object.entries(attributes)) {
        elem.setAttribute(key, value);
    }
    
    // return element
    return elem;
}

// โญ๏ธ appendTagTo(node, tag)
export function appendTagTo(node, tagName, {
    textContent,
    attributes = {}
}={}){
    let elem = tag(tagName, {textContent, attributes});
    node.appendChild(elem);
    return elem;
};

Last updated