
Research
SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains
An emerging npm supply chain attack that infects repos, steals CI secrets, and targets developer AI toolchains for further compromise.
The rswitch library provides a compact and flexible way to implement switch-like functionality in JavaScript. It allows you to define cases and their corresponding actions using an object literal syntax.
The rswitch library provides a flexible and compact way to implement switch-like functionality in TypeScript. It allows you to define cases and their corresponding actions using object literals or arrays. This version includes both synchronous and asynchronous handling of cases and actions.
To install the rswitch library, use the following npm command:
npm install rswitch
The rswitch function evaluates a given key against predefined cases and returns the corresponding action. There are two versions of this function available:
rswitch – works for synchronous operations.rswitchAsync – works for cases where the action is asynchronous (i.e., it returns a Promise).rswitch(key: string | number | undefined | null, casesObj: RSwitch, options?: Options): T | undefined;
rswitchAsync(key: string | number | undefined | null, casesObj: RSwitch, options?: Options): Promise<T | undefined>;
key: The value to evaluate against the cases (can be a string, number, undefined, or null).casesObj: An object or an array of key-value pairs defining the cases and their corresponding actions.options (optional): Configuration options to customize the behavior of the rswitch function.
callFn (optional, default: true): If set to true, the function will call actions that are functions and return their result. If set to false, the function will return the function itself without calling it.rswitch Exampleimport rswitch from "rswitch";
// const {rswitch} = require("rswitch") // commonjs
// Example usage of rswitch (synchronous)
const result = rswitch("dev", {
designer: "Designer",
"dev, web": "Developer",
"": "No match found", // Default case
});
console.log(result); // Output: "Developer"
rswitchAsync Exampleimport { rswitchAsync } from "rswitch";
// const {rswitchAsync} = require("rswitch") // commonjs
// Example usage of rswitchAsync (asynchronous)
async function testAsyncSwitch() {
const result = await rswitchAsync("web", {
"dev, web": async () => {
return "Developer"; // Async case
},
designer: "Designer",
"": "No match found", // Default case
});
console.log(result); // Output: "Developer" (async case)
}
testAsyncSwitch();
Cases are defined as key-value pairs within the casesObj. Here’s how you can define various cases:
Single Case: Use a direct key-value pair for a single match.
{ 'dev': 'Developer' }
Multiple Cases: You can group several cases together in one key, separated by commas (,), and the key will match any of those cases.
{ 'dev, web': 'Developer' }
Default Case: The empty string key ("") can be used for the default case, which is executed when no other key matches.
{ '': 'No match found' }
Actions can be:
Promise, it will be awaited.If the action is a function:
Synchronous: The function will be called and its result will be returned.
Asynchronous: The function will return a Promise, which will be resolved before returning the result.
If options.callFn is set to false, the function will not be invoked, and the function itself will be returned.
If rcase is undefined or null, and no default case ("") is defined in casesObj, the function will return undefined. If a default case is defined, it will return the value of the default case.
import rswitch, { rswitchAsync } from "rswitch";
// const { rswitch, rswitchAsync } = require("rswitch"); // commonjs
const result1 = rswitch("web", {
"dev, web": "Developer",
designer: "Designer",
"": "No match found", // Default case
});
console.log("sync", result1); // Output: "Developer"
async function testAsyncSwitch() {
const result2 = await rswitchAsync("web", {
"dev, web": async () => {
return "Developer"; // Async case
},
designer: "Designer",
"": "No match found", // Default case
});
console.log("async", result2); // Output: "Developer" (async case)
}
testAsyncSwitch();
'dev, web'."" key is treated as the default case, executed when no other match is found.rswitch, ensuring that the provided key, casesObj, and options match expected types.We welcome contributions to the rswitch library! If you find any issues or want to add new features, please fork the repository and submit a pull request.
FAQs
The rswitch library provides a compact and flexible way to implement switch-like functionality in JavaScript. It allows you to define cases and their corresponding actions using an object literal syntax.
We found that rswitch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
An emerging npm supply chain attack that infects repos, steals CI secrets, and targets developer AI toolchains for further compromise.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.