/**
 * @since Last modified: 2024/05/10 23:49:29
 * @function attrJoinVal
 * @param {string} attr  - one attribute of element.
 * @param {string} value  - The value corresponding to the attribute.
 * @param {array} map  - The options map.
 * @returns {Object} - Return a object.
 * @see {@link https://codepen.io/axui/pen/KKJQRwG|demo @ codepen}
 * @example
 * ax.attrJoinVal('length','30');
 * <!--return {length:30}-->
 */



'use strict';

import { T_obj } from "../../../types/ax";

const attrJoinVal = (attr: string, value: string, map?: T_obj[]) => {
    try {
        if (map) {
            let item: any = map.find((k: any) => k.attr === attr);
            if (item) {
                
                
                return (typeof item.value === 'string') ? { [item.prop]: value } :
                    (typeof item.value === 'boolean' && ['', '1', 'true'].includes(value)) ? { [item.prop]: true } :
                        (typeof item.value === 'boolean' && ['null', 'undefined', '0', 'false','NaN'].includes(value)) ? { [item.prop]: false } :
                            new Function('return ' + `{${item.prop}:${value}}`)();
            }
        } else {
            return value === '' ? { [attr]: '' } : new Function('return ' + `{${attr}:${value}}`)();
        }
    } catch {
        
        return { [attr]: value };
    }
}
export default attrJoinVal;
