Skip to content

Commit 117c7d3

Browse files
committed
initial commit
0 parents  commit 117c7d3

File tree

8 files changed

+1605
-0
lines changed

8 files changed

+1605
-0
lines changed

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# rswitch
2+
3+
The `rswitch` library provides a compact and flexible way to implement a switch-like functionality in TypeScript. It allows you to define cases and their corresponding actions using an object literal syntax.
4+
5+
## Installation
6+
7+
Install the `rswitch` library using npm:
8+
9+
```bash
10+
npm install rswitch
11+
```
12+
13+
## Usage
14+
15+
The `rswitch` function takes a key and an object containing cases and actions. It evaluates the key against the cases and returns the corresponding action.
16+
17+
### Syntax
18+
19+
```javascript
20+
rswitch(key, casesObj, options);
21+
```
22+
23+
- `key` : The value to evaluate against the cases.
24+
- `casesObj` : An object containing cases and their actions.
25+
- `options` : (Optional) An object to customize the behavior of the rswitch function.
26+
- `returnFunction` (optional, default: false): If set to `false`, the `rswitch` function will call actions that are functions and return their values. If set to `true`, the function will return the functions as is.
27+
28+
### Example
29+
30+
```javascript
31+
import { rswitch } from "rswitch";
32+
// const {rswitch} = require("rswitch") // commonjs
33+
34+
const result = rswitch(
35+
"dev",
36+
{
37+
designer: "Designer",
38+
"dev, web": "Freelancer",
39+
"": () => {
40+
console.log("Hello");
41+
},
42+
},
43+
{
44+
returnFunction: true,
45+
}
46+
);
47+
48+
console.log(result);
49+
// Output: Freelancer
50+
```
51+
52+
In this example, the `rswitch` function evaluates the key `'dev'` against the cases defined in `casesObj`. Since it matches the case `'dev, web'`, the corresponding action `'Freelancer'` is returned and assigned to the `result` variable. Finally, the value of `result` is logged to the console.
53+
54+
### Case Definitions
55+
56+
Cases are defined as key-value pairs in the `casesObj` object.
57+
58+
- Single Case: `{ caseKey: action }`
59+
- Multiple Cases: `{ 'case1, case2, case3': action }`
60+
- Default Case: `{ '': action }`
61+
62+
> Actions can be any value or a function that returns a value. If the action is a function, and the `options` object has `returnFunction` set to `false`, it is called, and the returned value is returned.
63+
64+
If no cases match the evaluated key, the `rswitch` function checks for a default case. If a default case is defined, its corresponding action is performed. If no default case is defined or its action is not provided, `undefined` is returned.
65+
66+
<br/>
67+
If you'd like to contribute, please do submit a pull request.
68+
69+
In case you want support my work
70+
71+
[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://buymeacoffee.com/rashed.iqbal)

package.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "rswitch",
3+
"private": true,
4+
"version": "0.1.0",
5+
"type": "module",
6+
"files": [
7+
"dist"
8+
],
9+
"exports": {
10+
".": {
11+
"import": "./dist/rswitch.es.js",
12+
"require": "./dist/rswitch.umd.js"
13+
}
14+
},
15+
"main": "./dist/rswitch.umd.js",
16+
"module": "./dist/rswitch.es.js",
17+
"types": "./dist/index.d.ts",
18+
"scripts": {
19+
"dev": "vite",
20+
"build": "tsc && vite build",
21+
"preview": "vite preview"
22+
},
23+
"devDependencies": {
24+
"@types/node": "^20.4.5",
25+
"typescript": "^5.0.2",
26+
"vite": "^4.4.5",
27+
"vite-plugin-dts": "^3.4.0",
28+
"vite-tsconfig-paths": "^4.2.0"
29+
},
30+
"dependencies": {
31+
"vite-plugin-linter": "^2.0.2"
32+
}
33+
}

src/index.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
type RSwitch = {
2+
[key: string]: any;
3+
};
4+
5+
type Options = {
6+
returnFunction?: boolean;
7+
};
8+
9+
export function rswitch(
10+
key: string | undefined,
11+
rswitch: RSwitch,
12+
options?: Options
13+
): any {
14+
const fnCall = options?.returnFunction || false;
15+
const value = rswitch[key!];
16+
const keys = Object.keys(rswitch)
17+
.filter((v) => v.includes(","))
18+
.flatMap((v) => v.split(","))
19+
.map((v) => v.trim());
20+
if (!value) {
21+
if (keys.includes(key!)) {
22+
return typeof value === "function" && !fnCall ? value() : value;
23+
}
24+
const _default = rswitch[""];
25+
return typeof _default === "function" && !fnCall
26+
? _default()
27+
: _default;
28+
} else {
29+
return typeof value === "function" && !fnCall ? value() : value;
30+
}
31+
}
32+
33+
export default rswitch;

src/vite-env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

tsconfig.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"useDefineForClassFields": true,
5+
"module": "ESNext",
6+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"skipLibCheck": true,
8+
9+
/* Bundler mode */
10+
"moduleResolution": "bundler",
11+
"allowImportingTsExtensions": true,
12+
"resolveJsonModule": true,
13+
"isolatedModules": true,
14+
"noEmit": true,
15+
16+
/* Linting */
17+
"strict": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true,
20+
"noFallthroughCasesInSwitch": true,
21+
22+
/* typing */
23+
"declaration": true,
24+
"declarationMap": true,
25+
"esModuleInterop": true
26+
},
27+
"include": ["src"]
28+
}

vite.config.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { resolve } from "node:path";
2+
import { defineConfig } from "vite";
3+
4+
import dts from "vite-plugin-dts";
5+
import EsLint from "vite-plugin-linter";
6+
import tsConfigPaths from "vite-tsconfig-paths";
7+
const { EsLinter, linterPlugin } = EsLint;
8+
9+
export default defineConfig((configEnv) => ({
10+
plugins: [
11+
tsConfigPaths(),
12+
linterPlugin({
13+
include: ["./src}/**/*.{ts}"],
14+
linters: [new EsLinter({ configEnv })],
15+
}),
16+
dts({
17+
include: ["src/"],
18+
}),
19+
],
20+
build: {
21+
lib: {
22+
entry: resolve("src", "index.ts"),
23+
name: "rswitch",
24+
fileName: (format) => `rswitch.${format}.js`,
25+
},
26+
},
27+
}));

0 commit comments

Comments
 (0)