@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
23 lines (22 loc) • 872 B
JavaScript
/**
* Replaces whatever substring is at the given index in the original string with the new string.
* Optionally, provide a length of the substring to get replaced.
*
* @category String
* @category Package : @augment-vir/common
* @example
*
* ```ts
* import {replaceStringAtIndex} from '@augment-vir/common';
*
* replaceStringAtIndex('eat the waffles', 4, 'his'); // outputs `'eat his waffles'`
* replaceStringAtIndex('eat the waffles', 4, 'my', 3); // outputs `'eat my waffles'`
* ```
*
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export function replaceStringAtIndex(originalString, start, newString, length = newString.length) {
const before = originalString.slice(0, Math.max(0, start));
const after = originalString.slice(Math.max(0, start + length));
return `${before}${newString}${after}`;
}