/**
 * @since Last modified: 2024/05/30 14:32:55
 * @function setAttr
 * @description Set attribute properties for the node.
 * @param {string|HTMLElement} el - node selector.
 * @param {string} key - The key of attribute.
 * @param {*} val - The value of attribute.
 * @returns {null} Return null.
 * @see {@link https://codepen.io/axui/pen/LYoxzbd|demo @ codepen}
 * @example
 * ax.setAttr('#demo','name','Lily');
 */





'use strict';
import { T_el, T_obj, T_sel } from '../../../types/ax';
import config from '../global/config';
import getDataType from './getDataType';
import getEl from './getEl';
import isEmpty from './isEmpty';
import isNull from './isNull';
import trim from './trim';

const setAttr = (el: T_sel, key: string, val: any): void => {
    let elem = getEl(el),
        valType = getDataType(val);
    if (elem && !isNull(key)) {
        if (isNull(val) || val === false) {
            
            elem.removeAttribute(key);
        } else {
            if (valType === 'Boolean' && val === true) {
                
                elem.setAttribute(key, '');
            } else if (valType === 'String') {
                
                elem.setAttribute(key, val);
            } else {
                
                
                elem.setAttribute(key, JSON.stringify(val));
            }
        }
    }
}
export default setAttr;
