2016-08-25 02:27:17 +00:00
|
|
|
---
|
|
|
|
title: 101
|
|
|
|
category: JavaScript libraries
|
2017-09-21 08:25:42 +00:00
|
|
|
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.
|
2016-08-25 02:27:17 +00:00
|
|
|
---
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
### Usage
|
|
|
|
|
|
|
|
```js
|
|
|
|
const isObject = require('101/isObject')
|
|
|
|
isObject({}) // → true
|
2016-08-25 02:27:17 +00:00
|
|
|
```
|
2017-09-21 08:25:42 +00:00
|
|
|
|
|
|
|
Every function is exposed as a module.
|
|
|
|
|
|
|
|
See: [101](https://github.com/tjmehta/101)
|
|
|
|
|
|
|
|
### Type checking
|
|
|
|
|
|
|
|
```js
|
2016-08-25 02:27:17 +00:00
|
|
|
isObject({})
|
|
|
|
isString('str')
|
|
|
|
isRegExp(/regexp/)
|
|
|
|
isBoolean(true)
|
|
|
|
isEmpty({})
|
|
|
|
isfunction(x => x)
|
|
|
|
isInteger(10)
|
|
|
|
isNumber(10.1)
|
|
|
|
instanceOf(obj, 'string')
|
|
|
|
```
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
## Objects
|
|
|
|
{: .-three-column}
|
|
|
|
|
|
|
|
### Example
|
|
|
|
{: .-prime}
|
2016-08-25 02:27:17 +00:00
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
```js
|
|
|
|
let obj = {}
|
2016-08-25 02:27:17 +00:00
|
|
|
```
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
#### Update
|
2016-08-25 02:27:17 +00:00
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
```js
|
|
|
|
obj = put(obj, 'user.name', 'John')
|
|
|
|
// → { user: { name: 'John' } }
|
2016-08-25 02:27:17 +00:00
|
|
|
```
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
#### Read
|
|
|
|
|
|
|
|
```js
|
|
|
|
pluck(name, 'user.name')
|
|
|
|
// → 'John'
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Delete
|
|
|
|
|
|
|
|
```js
|
|
|
|
obj = del(obj, 'user')
|
|
|
|
// → { }
|
|
|
|
```
|
|
|
|
|
|
|
|
### Getting
|
2016-08-25 02:27:17 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
pluck(state, 'user.profile.name')
|
2017-09-21 08:25:42 +00:00
|
|
|
```
|
2016-08-25 02:27:17 +00:00
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
```js
|
2016-08-25 02:27:17 +00:00
|
|
|
pick(state, ['user', 'ui'])
|
|
|
|
pick(state, /^_/)
|
2017-09-21 08:25:42 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
`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.
|
2016-08-25 02:27:17 +00:00
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
See:
|
|
|
|
[omit](https://github.com/tjmehta/101#omit),
|
|
|
|
[del](https://github.com/tjmehta/101#del)
|
|
|
|
|
|
|
|
### Keypath check
|
|
|
|
|
|
|
|
```js
|
2016-08-25 02:27:17 +00:00
|
|
|
hasKeypaths(state, ['user'])
|
|
|
|
hasKeypaths(state, { 'user.profile.name': 'john' })
|
2017-09-21 08:25:42 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
See:
|
|
|
|
[hasKeypaths](https://github.com/tjmehta/101#haskeypaths)
|
|
|
|
|
|
|
|
### Get values
|
2016-08-25 02:27:17 +00:00
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
```js
|
2016-08-25 02:27:17 +00:00
|
|
|
values(state)
|
|
|
|
```
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
## 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)
|
|
|
|
|
2016-08-25 02:27:17 +00:00
|
|
|
## Arrays
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
### Finding
|
|
|
|
|
2016-08-25 02:27:17 +00:00
|
|
|
```js
|
|
|
|
find(list, x => x.y === 2)
|
|
|
|
findIndex(list, x => ...)
|
|
|
|
includes(list, 'item')
|
|
|
|
last(list)
|
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
find(list, hasProps('id'))
|
|
|
|
```
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
### Grouping
|
2016-08-25 02:27:17 +00:00
|
|
|
|
|
|
|
```js
|
2017-09-21 08:25:42 +00:00
|
|
|
groupBy(list, 'id')
|
|
|
|
indexBy(list, 'id')
|
2016-08-25 02:27:17 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
### Function composition
|
2016-08-25 02:27:17 +00:00
|
|
|
|
|
|
|
```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
|
|
|
|
|
2017-09-21 08:25:42 +00:00
|
|
|
- <https://github.com/tjmehta/101>
|