sharp-switch
v0.0.1
Published
Simulate what C# switch syntax for assigning a value to a variable depending on an expression does.
Maintainers
Readme
sharp-switch
Simulate what C# switch syntax for assigning a value to a variable depending on an expression does.
Install
npm install sharp-switchUsage
There is this interesting syntax in C# that lets you assign a value to a variable using a switch statement. Here is what it looks like in C#:
string type = product[0] switch
{
"01" => "Sweat shirt",
"02" => "T-Shirt",
"03" => "Sweat pants",
_ => "Other",
};I wanted to make something similar in typescript, here's an example of what I came up with:
import { sharpSwitch } from "sharp-switch";
const someValues = <const>["test1", "test2", "somethingElse"];
for (const value of someValues) {
console.log(
sharpSwitch(value, <const>{
test1: "abc",
test2: "def",
default: "default value",
})
);
}Using as const values will help with the returned types of the function.
