/**
 * @since Last modified: 2023-12-26 10:55:35
 * @function replaceFrag
 * @description Replace string fragment.
 * @param {object} obj - Parameters.
 * @param {string} obj.origin - Origin string.
 * @param {string} obj.text - New string fragment.
 * @param {number} [obj.start] - Index start of origin string.
 * @param {number} [obj.stop] - Index end of origin string.
 * @param {number} [obj.length] - Length of string fragment replaced.
 * @returns {string}  - Returns the new string.
 * @see {@link https://codepen.io/axui/pen/yLwyzJM|demo @ codepen}
 * @example
 * ax.replaceFrag({origin:'abcdef',text:'123',start:1,stop:3});
 * <!--Return string: a123def-->
 */
'use strict';
const replaceFrag = ({ origin, text, start, stop, length }: { origin: string, text: string, start?: number, stop?: number, length?: number }) => {
    typeof origin !== 'string' ? origin = JSON.stringify(origin) : null;
    typeof text !== 'string' ? text = JSON.stringify(text) : null;
    let result = '';
    if (start === undefined && stop === undefined) {
        result = text;
    } else if (start !== undefined && stop === undefined) {
        result = length !== undefined ? origin.substring(0, start) + text + origin.substring(start + length) : origin.substring(0, start) + text;
    } else if (start === undefined && stop !== undefined) {
        result = length !== undefined ? origin.substring(stop - length) + text + origin.substring(0, start) : text + origin.substring(stop);
    } else {
        result = origin.substring(0, start as number) + text + origin.substring(stop as number);
    }
    return result;
}
export default replaceFrag;
