the input string to apply the prefix replacement to.
the prefix string of the input to replace.
the optional value to replace the the prefix with. defaults to "" (empty string).
if a matching prefix is found in the input, then it will be replaced with the given value.
otherwise, undefined will be returned if the input does not begin with the prefix.
import { assertEquals } from "jsr:@std/assert"
// aliasing our function for brevity
const
eq = assertEquals,
fn = replacePrefix
eq(fn("hello-world/abc-123", "hello-", "goodbye-"), "goodbye-world/abc-123")
eq(fn("hello-world/abc-123", "hello-"), "world/abc-123")
eq(fn("hello-world/abc-123", "abc"), undefined)
eq(fn("hello-world/abc-123", ""), "hello-world/abc-123")
eq(fn("hello-world/abc-123", "", "xyz-"), "xyz-hello-world/abc-123")
replace the
prefixof of a giveninputstring with the given replacevalue. if a matching prefix is not found, thenundefinedwill be returned.