Skip to content

New Simplified Value Mapping!

Compare
Choose a tag to compare
@UselessPickles UselessPickles released this 25 Jan 03:27
· 62 commits to master since this release

New Feature

Sometimes a visitor pattern is overkill when you just want to map your input value to an output value without any additional logic. The new mapString() function provides the same compile-time checks as visitString(), but allows you to skip the overhead of creating a function for each value:

import { mapString } from "ts-string-visitor";

type RGB = "r" | "g" | "b";

// Example function that uses mapString() to convert a RGB value
// to a display label
function getRgbLabel(rgb: RGB): string {
    return mapString(rgb).with({
        "r": "Red",
        "g": "Green",
        "b": "Blue"
    });
}

const result = getRgbLabel("g"); // result === "Green"

Breaking Change (reason for major version bump)

Now that ts-string-visitor exports more than one function, I have removed the default export.

If you previously imported visitString like this:

import visitString from "ts-string-visitor";

You must change it to this style:

import { visitString } from "ts-string-visitor";

NOTE: There are no changes to how visitString() works; only how it is exported.