101: big update

This commit is contained in:
Rico Sta. Cruz 2017-09-21 16:25:42 +08:00
parent 394ab3d1b7
commit 2c4ae49491
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
4 changed files with 194 additions and 42 deletions

186
101.md
View File

@ -1,9 +1,26 @@
--- ---
title: 101 title: 101
category: JavaScript libraries category: JavaScript libraries
layout: 2017/sheet
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({}) isObject({})
isString('str') isString('str')
isRegExp(/regexp/) isRegExp(/regexp/)
@ -15,69 +32,172 @@ isNumber(10.1)
instanceOf(obj, 'string') instanceOf(obj, 'string')
``` ```
## Function composition
```
compose(f, g) // x => f(g(x))
curry(f) // x => y => f(x, y)
flip(f) // f(x, y) --> f(y, x)
passAll(f, g) // x => f(x) && g(x)
passAny(f, g) // x => f(x) || g(x)
converge(and, [pluck('a'), pluck('b')])(x)
// and(pluck(x, 'a'), pluck(x, 'b'))
```
## Objects ## 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 ```js
del(state, 'user.profile') del(state, 'user.profile')
put(state, 'user.profile.name', 'john') omit(state, ['user', 'data'])
pluck(state, 'user.profile.name') ```
omit(state, 'user.profile') `omit` is like `del`, but supports multiple keys to be deleted.
pick(state, ['user', 'ui'])
pick(state, /^_/)
See:
[omit](https://github.com/tjmehta/101#omit),
[del](https://github.com/tjmehta/101#del)
### Keypath check
```js
hasKeypaths(state, ['user']) hasKeypaths(state, ['user'])
hasKeypaths(state, { 'user.profile.name': 'john' }) hasKeypaths(state, { 'user.profile.name': 'john' })
```
See:
[hasKeypaths](https://github.com/tjmehta/101#haskeypaths)
### Get values
```js
values(state) 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 ## Arrays
### Finding
```js ```js
find(list, x => x.y === 2) find(list, x => x.y === 2)
findIndex(list, x => ...) findIndex(list, x => ...)
includes(list, 'item') includes(list, 'item')
last(list) last(list)
groupBy(list, 'id')
indexBy(list, 'id')
``` ```
```js ```js
find(list, hasProps('id')) find(list, hasProps('id'))
``` ```
## Simple ### Grouping
Useful for function composition.
```js ```js
and(x, y) // x && y groupBy(list, 'id')
or(x, y) // x || y indexBy(list, 'id')
xor(x, y) // !(!x && !y) && !(x && y)
equal(x, y) // x === y
exists(x) // !!x
not(x) // !x
``` ```
## Examples ## Examples
Function composition: ### Function composition
```js ```js
isFloat = passAll(isNumber, compose(isInteger, not)) isFloat = passAll(isNumber, compose(isInteger, not))
@ -92,4 +212,4 @@ doStuffForce = curry(flip(doStuff))({ force: true })
## Reference ## Reference
<https://github.com/tjmehta/101> - <https://github.com/tjmehta/101>

View File

@ -43,6 +43,14 @@ make
{: .-four-column} {: .-four-column}
{: .-six-column} {: .-six-column}
`h3` sections can have:
pre
p
ul
table
h4
## Frontmatter ## Frontmatter
Each sheet supports these metadata: Each sheet supports these metadata:

View File

@ -111,6 +111,20 @@
margin: 0; margin: 0;
} }
/* Headings in between bodies */
& > h4 {
@include font-size(-1);
margin: 0;
padding: 4px 16px;
font-weight: normal;
background: $gray-bg;
color: $gray-text;
& + * {
border-top: solid 1px $line-color;
}
}
/* Description paragraphs */ /* Description paragraphs */
& > pre ~ p, & > pre ~ p,
& > ul ~ p, & > ul ~ p,

View File

@ -394,30 +394,40 @@ import PropTypes from 'prop-types'
See: [Typechecking with PropTypes](https://facebook.github.io/react/docs/typechecking-with-proptypes.html) See: [Typechecking with PropTypes](https://facebook.github.io/react/docs/typechecking-with-proptypes.html)
| PropType | Description |
| --- | --- |
| `any` | Anything | | `any` | Anything |
| --- | --- |
#### Basic
| `string` | | | `string` | |
| `number` | | | `number` | |
| `func` | Function | | `func` | Function |
| `bool` | True or false | | `bool` | True or false |
| --- | --- |
#### Enum
| `oneOf`_(any)_ | Enum types | | `oneOf`_(any)_ | Enum types |
| `oneOfType`_(type array)_ | Union | | `oneOfType`_(type array)_ | Union |
| --- | --- |
#### Array
| `array` | | | `array` | |
| `arrayOf`_(...)_ | | | `arrayOf`_(...)_ | |
| --- | --- |
#### Object
| `object` | | | `object` | |
| `objectOf`_(...)_ | Object with values of a certain type | | `objectOf`_(...)_ | Object with values of a certain type |
| `instanceOf`_(...)_ | Instance of a class | | `instanceOf`_(...)_ | Instance of a class |
| `shape`_(...)_ | | | `shape`_(...)_ | |
| --- | --- |
#### Elements
| `element` | React element | | `element` | React element |
| `node` | DOM node | | `node` | DOM node |
| --- | --- |
| `.isRequired` | Required | #### Required
| `(···).isRequired` | Required |
### Basic types ### Basic types