Javascript `substr_replace`

Like php substr_replace but in javascript

function substr_replace(str, replace, start, length) {
	if (start < 0) {
		// start position in str
		start = start + str.length;
	}
	length = length !== undefined ? length : str.length;
	if (length < 0) {
		length = length + str.length - start;
	}

	return [
		str.slice(0, start),
		replace.substr(0, length),
		replace.slice(length),
		str.slice(start + length),
	].join("");
}