Skip to content

Commit dea4601

Browse files
committed
improved and optimised
1 parent 1c98c13 commit dea4601

File tree

6 files changed

+2354
-209
lines changed

6 files changed

+2354
-209
lines changed

README.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,46 @@ The `rswitch` function takes a key and an object containing cases and actions. I
2020
rswitch(key, casesObj, options);
2121
```
2222

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.
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+
- `returnFn` (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.
2727

2828
### Example
2929

3030
```javascript
31-
import { rswitch } from "rswitch";
31+
import rswitch from "rswitch";
3232
// const {rswitch} = require("rswitch") // commonjs
3333

3434
const result = rswitch(
35-
"dev",
36-
{
37-
designer: "Designer",
38-
"dev, web": "Freelancer",
39-
"": () => {
40-
console.log("Hello");
41-
},
35+
"dev",
36+
{
37+
designer: "Designer",
38+
"dev, web": "Developer",
39+
"": () => {
40+
console.log("Hello");
4241
},
43-
{
44-
returnFunction: true,
45-
}
42+
},
43+
{
44+
returnFn: true,
45+
}
4646
);
4747

4848
console.log(result);
49-
// Output: Freelancer
49+
// Output: Developer
5050
```
5151

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.
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 `'Developer'` is returned and assigned to the `result` variable. Finally, the value of `result` is logged to the console.
5353

5454
### Case Definitions
5555

5656
Cases are defined as key-value pairs in the `casesObj` object.
5757

58-
- Single Case: `{ caseKey: action }`
59-
- Multiple Cases: `{ 'case1, case2, case3': action }`
60-
- Default Case: `{ '': action }`
58+
- Single Case: `{ caseKey: action }`
59+
- Multiple Cases: `{ 'case1, case2, case3': action }`
60+
- Default Case: `{ '': action }`
6161

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.
62+
> Actions can be any value or a function that returns a value. If the action is a function, and the `options` object has `returnFn` set to `false`, it is called, and the returned value is returned.
6363
6464
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.
6565

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "rswitch",
33
"description": "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.",
4-
"version": "0.2.0",
4+
"version": "0.7.0-beta",
55
"author": "Rashed Iqbal",
66
"files": [
77
"dist"
@@ -17,7 +17,8 @@
1717
"module": "./dist/rswitch.es.js",
1818
"types": "./dist/index.d.ts",
1919
"scripts": {
20-
"build": "tsc && vite build"
20+
"build": "tsc && vite build",
21+
"test": "jest --verbose false"
2122
},
2223
"repository": {
2324
"type": "git",
@@ -35,7 +36,10 @@
3536
"homepage": "https://github.com/iqbal-rashed/rswitch",
3637
"license": "MIT",
3738
"devDependencies": {
39+
"@types/jest": "^29.5.12",
3840
"@types/node": "^20.4.5",
41+
"jest": "^29.7.0",
42+
"ts-jest": "^29.1.2",
3943
"typescript": "^5.0.2",
4044
"vite": "^4.4.5",
4145
"vite-plugin-dts": "^3.4.0",

src/index.ts

+24-23
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,33 @@ type RSwitch = {
33
};
44

55
type Options = {
6-
returnFunction?: boolean;
6+
returnFn?: boolean;
77
};
88

9-
function rswitch(
10-
key: string | undefined,
11-
rswitch: RSwitch,
9+
export function rswitch<T = any>(
10+
rcase: string | number | undefined | null,
11+
rs: RSwitch,
1212
options?: Options
13-
): any {
14-
const fnCall = options?.returnFunction || false;
15-
16-
const value = rswitch[key!];
17-
18-
const filtered = Object.keys(rswitch).filter((v) => v.includes(","));
19-
20-
const keys = filtered.flatMap((v) => v.split(",")).map((v) => v.trim());
21-
if (!value) {
22-
if (keys.includes(key!)) {
23-
const findKey = filtered.find((v) => v.includes(key!));
24-
const multiValue = rswitch[findKey!];
25-
return typeof multiValue === "function" && !fnCall
26-
? multiValue()
27-
: multiValue;
13+
): T | undefined {
14+
for (const [key, value] of Array.isArray(rs) ? rs : Object.entries(rs)) {
15+
if (key.includes(",")) {
16+
let spiltKey = key.split(",").map((v: string) => v.trim());
17+
if (spiltKey.includes(String(rcase))) {
18+
return returnFunction(value, options);
19+
}
20+
}
21+
if (key === String(rcase)) {
22+
return returnFunction(value, options);
23+
}
24+
if (key === "") {
25+
return returnFunction(value, options);
2826
}
29-
const _default = rswitch[""];
30-
return typeof _default === "function" && !fnCall ? _default() : _default;
31-
} else {
32-
return typeof value === "function" && !fnCall ? value() : value;
3327
}
3428
}
29+
30+
function returnFunction(value: any, options?: Options) {
31+
const returnFn = options?.returnFn || false;
32+
return typeof value === "function" ? (!returnFn ? value() : value) : value;
33+
}
34+
35+
export default rswitch;

tests/index.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { rswitch } from "../src/index";
2+
3+
test("first try", () => {
4+
expect(rswitch(1, { "1": () => "Hello", "2": "World" })).toBe("Hello");
5+
});
6+
7+
test("check default value", () => {
8+
expect(
9+
rswitch("default", { "1": "one", "2": "two", "3": "three", "": "default" })
10+
).toBe("default");
11+
});
12+
13+
test("check null key", () => {
14+
expect(
15+
rswitch(null, { "1": "one", "2": "two", "3": "three", "": "default" })
16+
).toBe("default");
17+
});
18+
19+
test("check undefined key", () => {
20+
expect(
21+
rswitch(undefined, { "1": "one", "2": "two", "3": "three", "": "default" })
22+
).toBe("default");
23+
});
24+
25+
test("check number key", () => {
26+
expect(
27+
rswitch(2, { "1": "one", "2": "two", "3": "three", "": "default" })
28+
).toBe("two");
29+
});
30+
31+
test("check mulitple case", () => {
32+
expect(
33+
rswitch("2", {
34+
"1": "one",
35+
"hello, 2": "two",
36+
"": "default",
37+
})
38+
).toBe("two");
39+
});

0 commit comments

Comments
 (0)