Skip to content

Commit bb42305

Browse files
Added fallthrough functionality, updated version to 1.1, updated readme, increased test coverage to 100%
1 parent ef3655a commit bb42305

File tree

8 files changed

+351
-25
lines changed

8 files changed

+351
-25
lines changed

README.md

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Swich
2-
> More developer-friendly switch alternative.
2+
> Switch but with a typo
33
44
<h1>
55
<a href="https://github.com/Lukasz-pluszczewski/swich">
@@ -117,7 +117,7 @@ const instance = swich([
117117
```
118118

119119
### 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.
121121

122122
```js
123123
const instance = swich([
@@ -247,7 +247,7 @@ const instance = swich([
247247

248248
instance(120); // More than 50
249249
instance(-20); // Less than 0
250-
instance(20); // 20
250+
instance(20); // Between 0 and 50
251251
```
252252

253253
```js
@@ -294,7 +294,7 @@ You can define your own *Pattern* matcher.
294294
```js
295295
import { createSwich, defaultMatcher } from 'swich';
296296

297-
const customSwich = createSwich({
297+
const swich = createSwich({
298298
matcher: config => (valueToMatch, pattern) => typeof pattern === 'object'
299299
? pattern?.type === valueToMatch?.type
300300
: defaultMatcher(config)(valueToMatch, pattern),
@@ -331,7 +331,110 @@ instance('Uga buga'); // 'Unknown type'
331331
```
332332
333333
### 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+
```
335438
336439
### Comparator functions
337440
Four functions for number comparisons are provided
@@ -390,5 +493,8 @@ swich function accepts two parameters: array of *Pattern*, *Result* tuples and *
390493
391494
## Changelog
392495
496+
### 1.1.0
497+
- Added fallThrough functionality
498+
393499
### 1.0.0
394500
- Initial release

build/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)