/**
 * @since Last modified: 2023-12-08 11:37:11
 * @function sliceStrEnd
 * @description Use keyword to cut forward or backwards from the string.
 * @param {object} options  - The parameter object of the function.
 * @property {string} options.str  - The string to be intercepted.Allow keyword many times.
 * @property {string} [options.key='#']  - Keywords participating in intercepting.
 * @property {string} [options.type='beforebegin'|'afterbegin'|'beforeend'|'afterend']  - Method of intercepting.The optional values include 'beforebegin','afterbegin','beforeend' and 'afterend'
 * @property {boolean} [options.contain=true]  - Whether the output result contains keyword.
 * @returns {string|''} - Return the interception string does not change the original string.
 * @see {@link https://codepen.io/axui/pen/NWoOKEY|demo @ codepen}
 * @example
 * ax.sliceStrEnd({str:'123#321',key:'#'});
 * <!--return interception string -->
 */
'use strict';
const sliceStrEnd = ({ str = '', key = '#', type = 'afterend', contain = true }) => {
    str = str.toString();
    key = key.toString();
    let result = '', indexKey = 0, lenKey = key.length, lenEnd = str.length, indexStart = 0;
    if (!str || !key) {
        return result;
    }
    str = str.trim();
    if (str.includes(key)) {
        if (type === 'beforebegin') {
            indexKey = str.indexOf(key);
            contain ? indexKey += lenKey : null;
            lenEnd = indexKey;
        }
        else if (type === 'afterbegin') {
            indexKey = str.indexOf(key);
            !contain ? indexKey += lenKey : null;
            indexStart = indexKey;
        }
        else if (type === 'beforeend') {
            indexKey = str.lastIndexOf(key);
            contain ? indexKey += lenKey : null;
            lenEnd = indexKey;
        }
        else if (type === 'afterend') {
            indexKey = str.lastIndexOf(key);
            !contain ? indexKey += lenKey : null;
            indexStart = indexKey;
        }
        result = str.substring(indexStart, lenEnd);
    }
    return result;
};
export default sliceStrEnd;
