cheatsheets/101.md

215 lines
3.2 KiB
Markdown

---
title: 101
category: JavaScript libraries
updated: 2017-09-21
intro: |
[101](https://www.npmjs.com/package/101) is a JavaScript library for dealing with immutable data in a functional manner.
---
### Usage
```js
const isObject = require('101/isObject')
isObject({}) // → true
```
Every function is exposed as a module.
See: [101](https://github.com/tjmehta/101)
### Type checking
```js
isObject({})
isString('str')
isRegExp(/regexp/)
isBoolean(true)
isEmpty({})
isfunction(x => x)
isInteger(10)
isNumber(10.1)
instanceOf(obj, 'string')
```
## Objects
{: .-three-column}
### Example
{: .-prime}
```js
let obj = {}
```
#### Update
```js
obj = put(obj, 'user.name', 'John')
// → { user: { name: 'John' } }
```
#### Read
```js
pluck(name, 'user.name')
// → 'John'
```
#### Delete
```js
obj = del(obj, 'user')
// → { }
```
### Getting
```js
pluck(state, 'user.profile.name')
```
```js
pick(state, ['user', 'ui'])
pick(state, /^_/)
```
`pluck` returns values, `pick` returns subsets of objects.
See:
[pluck](https://github.com/tjmehta/101#pluck),
[pick](https://github.com/tjmehta/101#pick)
### Setting
```js
put(state, 'user.profile.name', 'john')
```
See:
[put](https://github.com/tjmehta/101#put)
### Deleting
```js
del(state, 'user.profile')
omit(state, ['user', 'data'])
```
`omit` is like `del`, but supports multiple keys to be deleted.
See:
[omit](https://github.com/tjmehta/101#omit),
[del](https://github.com/tjmehta/101#del)
### Keypath check
```js
hasKeypaths(state, ['user'])
hasKeypaths(state, { 'user.profile.name': 'john' })
```
See:
[hasKeypaths](https://github.com/tjmehta/101#haskeypaths)
### Get values
```js
values(state)
```
## Functions
### Simple functions
| `and(x, y)` | `x && y` |
| `or(x, y)` | `x || y` |
| `xor(x, y)` | `!(!x && !y) && !(x && y)` |
| `equals(x, y)` | `x === y` |
| `exists(x)` | `!!x` |
| `not(x)` | `!x` |
Useful for function composition.
See:
[and](https://github.com/tjmehta/101#and),
[equals](https://github.com/tjmehta/101#equals),
[exists](https://github.com/tjmehta/101#exists)
### Composition
```js
compose(f, g) // x => f(g(x))
curry(f) // x => y => f(x, y)
flip(f) // f(x, y) --> f(y, x)
```
See:
[compose](https://github.com/tjmehta/101#compose),
[curry](https://github.com/tjmehta/101#curry),
[flip](https://github.com/tjmehta/101#flip)
### And/or
```js
passAll(f, g) // x => f(x) && g(x)
passAny(f, g) // x => f(x) || g(x)
```
See:
[passAll](https://github.com/tjmehta/101#passall),
[passAny](https://github.com/tjmehta/101#passany)
### Converge
```js
converge(and, [pluck('a'), pluck('b')])(x)
```
```js
// → and(pluck(x, 'a'), pluck(x, 'b'))
```
See:
[converge](https://github.com/tjmehta/101#converge)
## Arrays
### Finding
```js
find(list, x => x.y === 2)
findIndex(list, x => ...)
includes(list, 'item')
last(list)
```
```js
find(list, hasProps('id'))
```
### Grouping
```js
groupBy(list, 'id')
indexBy(list, 'id')
```
## Examples
### Function composition
```js
isFloat = passAll(isNumber, compose(isInteger, not))
// n => isNumber(n) && not(isInteger(n))
```
```js
function doStuff (object, options) { ... }
doStuffForce = curry(flip(doStuff))({ force: true })
```
## Reference
- <https://github.com/tjmehta/101>