/**
 * @since Last modified: 2024/05/22 09:44:59
 * @function paramToJson
 * @description Convert text type parameter values to standard objects..
 * @param {string} str  - Json string.
 * @returns {Object|{}} - Return a json.
 * @see {@link https://codepen.io/axui/pen/abMOaEZ|demo @ codepen}
 * @example
 * ax.paramToJson('a:0,b:1,c:{x:0,y:1},d:[0,1,2],male');
 * <!--return {a:0,b:1,c:{x:0,y:1},d:[0,1,2],male:true}-->
 */







'use strict';

import sliceFrags from "./sliceFrags";

const paramToJson = (str: string) => {
    let result: any;
    if (str) {
        str = str.trim();
        
        let objStr = sliceFrags({ str }).join(''),
            arrStr = sliceFrags({ str, start: '[', end: ']' }).join(''),
            excludeStr = objStr + arrStr,
            
            std = str.split(',').filter(Boolean).map((k: string) => {
                
                
                return (!k.includes(':') && !excludeStr.includes(k)) ? `${k}:true` : k;
            }).join(',');
        try {
            result = new Function(`return {${std}}`)();
        } catch (e) {
            
            result = {};
            console.warn('Data parsing error, an empty object "{}" has been returned!');
        }
    }
    return result;
}
export default paramToJson;
