Skip to content

Commit

Permalink
Update API and README
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviervia committed Aug 3, 2017
1 parent 4bc2ce2 commit 2ccc24f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ typings/
# dotenv environment variables file
.env

dist
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
# vybor
# vybor - выбор

[![Build Status](https://travis-ci.org/batata-frita/vybor.svg)](https://travis-ci.org/batata-frita/vybor)
[![npm version](https://img.shields.io/npm/v/vybor.svg?maxAge=1000)](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)
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vybor",
"version": "0.1.2",
"description": "Function to choose. Like `switch`, but you don't need an IIFE",
"version": "0.1.0",
"description": "Function to choose comparing with a value. Like `switch`, but you don't need an IIFE",
"main": "dist/index.js",
"files": [
"dist"
Expand All @@ -27,7 +27,10 @@
"homepage": "https://github.com/batata-frita/vybor#readme",
"devDependencies": {
"babel-cli": "^6.24.1",
"jest": "^20.0.4",
"babel-preset-env": "^1.6.0"
"babel-preset-env": "^1.6.0",
"jest": "^20.0.4"
},
"dependencies": {
"elegir": "^0.1.2"
}
}
13 changes: 5 additions & 8 deletions src/index.js
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
8 changes: 8 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ describe('выбор', () => {
'three', 3
), undefined)
})

it('returns the fallback if there is fallback and nothing is matched', () => {
equal(выбор('four', 'fallback')(
'one', 1,
'two', 2,
'three', 3
), 'fallback')
})
})
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,10 @@ electron-to-chromium@^1.3.16:
version "1.3.16"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.16.tgz#d0e026735754770901ae301a21664cba45d92f7d"

elegir@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/elegir/-/elegir-0.1.2.tgz#f2dee74d6b57ff7a4df24ad77a849ef048059c58"

errno@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.4.tgz#b896e23a9e5e8ba33871fc996abd3635fc9a1c7d"
Expand Down

0 comments on commit 2ccc24f

Please sign in to comment.