/**
 * @since Last modified: 2024-07-08 16:47:05
 * @function getNestProp
 * @description Retrieve value based on property path.
 * @param {*} data  - The origin data.
 * @param {string} path  - The property path.
 * @returns {*} - Return a value from path.
 * @see {@link https://codepen.io/axui/pen/YzozZom|demo @ codepen}
 * @example
 * ax.getNestProp({a:{x:0}},'a.x');
 * <!--return 0-->
 */
'use strict';
const getNestProp = (data: any, path: string) => {
    if (!path) return data;
    return path.replace(/['"\s]/g, '').split('.').reduce((prev: any, cur: string) => {
        if (prev && prev[cur] !== undefined) return prev[cur];
        if (cur.endsWith(']')) {
            let tmp: string[] = [];
            if (cur.startsWith('[')) {
               tmp = cur.split(']').map((k: string) => k.replace('[', '')).filter(Boolean);
            } else {
                tmp = cur.split('[').map((k: string) => k.replace(']', ''));
            }
            return tmp.reduce((a: any, b: string) => a && a[b], prev);
        }
        return undefined;
    }, data);
}
export default getNestProp;
