|
1 | 1 | # Swich |
2 | | -> More developer-friendly switch alternative. |
| 2 | +> Switch but with a typo |
3 | 3 |
|
4 | 4 | <h1> |
5 | 5 | <a href="https://github.com/Lukasz-pluszczewski/swich"> |
@@ -117,7 +117,7 @@ const instance = swich([ |
117 | 117 | ``` |
118 | 118 |
|
119 | 119 | ### Multiple defaults |
120 | | -By default (`returnMany: false`, see below) only the first match or last default is returned. In the case below, 'This is default 1' will never be returned. |
| 120 | +By default (`returnMany: false`, see below) only the first match (if not falling through) or last default is returned. In the case below, 'This is default 1' will never be returned. |
121 | 121 |
|
122 | 122 | ```js |
123 | 123 | const instance = swich([ |
@@ -247,7 +247,7 @@ const instance = swich([ |
247 | 247 |
|
248 | 248 | instance(120); // More than 50 |
249 | 249 | instance(-20); // Less than 0 |
250 | | -instance(20); // 20 |
| 250 | +instance(20); // Between 0 and 50 |
251 | 251 | ``` |
252 | 252 |
|
253 | 253 | ```js |
@@ -294,7 +294,7 @@ You can define your own *Pattern* matcher. |
294 | 294 | ```js |
295 | 295 | import { createSwich, defaultMatcher } from 'swich'; |
296 | 296 |
|
297 | | -const customSwich = createSwich({ |
| 297 | +const swich = createSwich({ |
298 | 298 | matcher: config => (valueToMatch, pattern) => typeof pattern === 'object' |
299 | 299 | ? pattern?.type === valueToMatch?.type |
300 | 300 | : defaultMatcher(config)(valueToMatch, pattern), |
@@ -331,7 +331,110 @@ instance('Uga buga'); // 'Unknown type' |
331 | 331 | ``` |
332 | 332 |
|
333 | 333 | ### Fallthrough |
334 | | -No [fallthrough](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#methods_for_multi-criteria_case) support yet, sorry. |
| 334 | +Swich supports fallthrough functionality since version 1.1.0 |
| 335 | +
|
| 336 | +You can force the case fallthrough by passing `true` as third element of pattern tuple. This is equivalent to *not* adding break keyword at the end of case in switch. |
| 337 | +```js |
| 338 | +const instance = swich([ |
| 339 | + [lt(10), () => console.log('Less than 10'), true], |
| 340 | + [gt(5), () => console.log('More than 5')], |
| 341 | + [() => console.log('I am default')], |
| 342 | +]); |
| 343 | + |
| 344 | +instance(6); // 'Less than 10' 'More than 5' |
| 345 | +``` |
| 346 | +
|
| 347 | +Please note, that by default swich will fall through to the next *Result* even if *Pattern* for that result **does not match**. This is the same behaviour as switch: |
| 348 | +```js |
| 349 | +const instance = swich([ |
| 350 | + [lt(10), () => console.log('Less than 10'), true], |
| 351 | + [gt(5), () => console.log('More than 5')], |
| 352 | + [() => console.log('I am default')], |
| 353 | +]); |
| 354 | + |
| 355 | +instance(2); // 'Less than 10' 'More than 5' |
| 356 | +``` |
| 357 | +
|
| 358 | +Swich will fall through even to default *Result*, the same as switch. In the following example all *Result* functions are called: |
| 359 | +```js |
| 360 | +const instance = swich([ |
| 361 | + [lt(10), () => console.log('Less than 10'), true], |
| 362 | + [gt(5), () => console.log('More than 5'), true], |
| 363 | + [() => console.log('I am default')], |
| 364 | +]); |
| 365 | + |
| 366 | +instance(2); // 'Less than 10' 'More than 5' 'I am default' |
| 367 | +``` |
| 368 | +
|
| 369 | +### Fallthrough with stopFallThrough flag |
| 370 | +Because the default behaviour for switch (and swich) is ridicules you can set stopFallThrough flag to true to change it. With that flag, the fallthrough will not return or trigger *Result* if *Pattern* does not match the *Value*. Non-matching *Pattern* will also stop the fall through on that element. |
| 371 | +
|
| 372 | +```js |
| 373 | +const instance = swich([ |
| 374 | + [lt(10), () => console.log('Less than 10'), true], |
| 375 | + [gt(5), () => console.log('More than 5'), true], |
| 376 | + [() => console.log('I am default')], |
| 377 | +], { stopFallThrough: true }); |
| 378 | + |
| 379 | +instance(2); // 'Less than 10' |
| 380 | +``` |
| 381 | +
|
| 382 | +### Fallthrough with returnMany flag |
| 383 | +```js |
| 384 | +const instance = swich([ |
| 385 | + [lt(10), () => 'Less than 10', true], |
| 386 | + [gt(5), () => 'More than 5', true], |
| 387 | + [() => 'I am default'], |
| 388 | +], { returnMany: true }); |
| 389 | + |
| 390 | +instance(2); // ['Less than 10', 'More than 5', 'I am default'] |
| 391 | +``` |
| 392 | +
|
| 393 | +```js |
| 394 | +const instance = swich<number, string>([ |
| 395 | + [lt(10), () => 'Less than 10', true], |
| 396 | + [gt(5), () => 'More than 5', true], |
| 397 | + [gt(1), () => 'More than 1', true], |
| 398 | + [() => 'I am default'], |
| 399 | +], { returnMany: true }); |
| 400 | + |
| 401 | +instance(2); // ['Less than 10', 'More than 5', 'More than 1', 'I am default'] |
| 402 | +``` |
| 403 | +
|
| 404 | +```js |
| 405 | +const instance = swich<number, string>([ |
| 406 | + [lt(10), () => 'Less than 10', true], |
| 407 | + [gt(5), () => 'More than 5', true], |
| 408 | + [gt(1), () => 'More than 1'], |
| 409 | + [() => 'I am default'], |
| 410 | +], { returnMany: true }); |
| 411 | + |
| 412 | +instance(2); // ['Less than 10', 'More than 5', 'More than 1'] |
| 413 | +``` |
| 414 | +
|
| 415 | +### Fallthrough with stopFallThrough and returnMany flags |
| 416 | +stopFallThrough flag prevents swich from falling into default: |
| 417 | +```js |
| 418 | +const instance = swich([ |
| 419 | + [lt(10), () => 'Less than 10', true], |
| 420 | + [gt(5), () => 'More than 5', true], |
| 421 | + [() => 'I am default'], |
| 422 | +], { returnMany: true, stopFallThrough: true }); |
| 423 | + |
| 424 | +instance(2); // ['Less than 10'] |
| 425 | +``` |
| 426 | +
|
| 427 | +Here, swich falls through to default, because *Pattern* right before it has been matched |
| 428 | +```js |
| 429 | +const instance = swich<number, string>([ |
| 430 | + [lt(10), () => 'Less than 10', true], |
| 431 | + [gt(5), () => 'More than 5', true], |
| 432 | + [gt(1), () => 'More than 1', true], |
| 433 | + [() => 'I am default'], |
| 434 | +], { returnMany: true, stopFallThrough: true }); |
| 435 | + |
| 436 | +instance(2); // ['Less than 10', 'More than 1', 'I am default'] |
| 437 | +``` |
335 | 438 |
|
336 | 439 | ### Comparator functions |
337 | 440 | Four functions for number comparisons are provided |
@@ -390,5 +493,8 @@ swich function accepts two parameters: array of *Pattern*, *Result* tuples and * |
390 | 493 |
|
391 | 494 | ## Changelog |
392 | 495 |
|
| 496 | +### 1.1.0 |
| 497 | +- Added fallThrough functionality |
| 498 | +
|
393 | 499 | ### 1.0.0 |
394 | 500 | - Initial release |
0 commit comments