-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,4 @@ typings/ | |
# dotenv environment variables file | ||
.env | ||
|
||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
# vybor | ||
# vybor - выбор | ||
|
||
[](https://travis-ci.org/batata-frita/vybor) | ||
[](https://www.npmjs.com/package/vybor) | ||
|
||
Function to choose comparing with a value. Like `switch`, but you don't need an IIFE. | ||
|
||
``` | ||
npm add vybor | ||
``` | ||
|
||
```js | ||
import выбор from 'vybor' | ||
|
||
console.log(выбор('jupiter')( | ||
'mars', 'red', | ||
'jupiter', 'green', | ||
'saturn', 'purple' | ||
)) // => 'green' | ||
``` | ||
|
||
If you want to set a fallback option if none match, you can just set a fallback value as a second argument of the first function: | ||
|
||
```js | ||
выбор('pluto', 'probably not a planet')( | ||
'mars', 'red', | ||
'jupiter', 'green', | ||
'saturn', 'purple' | ||
) // => 'probably not a planet' | ||
``` | ||
|
||
Mind that this means that if the selected is explicitly set to be `undefined`, it will return the fallback option: | ||
|
||
```js | ||
выбор('mars', 'probably not a planet')( | ||
'mars', undefined, | ||
'jupiter', 'green', | ||
'saturn', 'purple' | ||
) // => 'probably not a planet' | ||
``` | ||
|
||
## See also | ||
|
||
- [elegir](https://github.com/batata-frita/elegir) | ||
|
||
## License | ||
|
||
[See License](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
const выбор = (...args) => { | ||
if (args.length > 0) { | ||
if (args[0] === true) { | ||
return args[1] | ||
} | ||
import elegir from 'elegir' | ||
|
||
return выбор(...args.slice(2)) | ||
} | ||
const vybor = (y, initial) => (...args) => { | ||
const selected = elegir(...args.map((x, i) => i % 2 === 0 ? x === y : x)) | ||
return selected !== undefined ? selected : initial | ||
} | ||
|
||
export default выбор | ||
export default vybor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters