Javascript strings

// replace all occurences of a substring
let snake_case = "foo_bar_baz"
let dash_case = snake_case.replace(/_/g, "-")
console.assert(dash_case === "foo-bar-baz")

// python-like split:
// split just a number of pieces
// split by all the space characters by default
function split(s, sep, maxsplit) {
    sep = sep || /\s+/
    let parts = s.split(sep)
    if (maxsplit === undefined) {
        return parts
    }
    return [...parts.splice(0, maxsplit), parts.join(sep)]
}

console.assert(split("foo\nbar \tbaz"), ["foo", "bar", "baz"])
console.assert(split("foo-bar--baz", "-"), ["foo", "bar", "", "baz"])
console.assert(split("foo-bar-baz-spam", "-", 2), ["foo", "bar", "baz-spam"])