OSDN Git Service

Regular updates
[twpd/master.git] / 101.md
1 ---
2 title: 101
3 category: JavaScript libraries
4 layout: 2017/sheet
5 updated: 2017-09-21
6 intro: |
7   [101](https://www.npmjs.com/package/101) is a JavaScript library for dealing with immutable data in a functional manner.
8 ---
9
10 ### Usage
11
12 ```js
13 const isObject = require('101/isObject')
14 isObject({}) // → true
15 ```
16
17 Every function is exposed as a module.
18
19 See: [101](https://github.com/tjmehta/101)
20
21 ### Type checking
22
23 ```js
24 isObject({})
25 isString('str')
26 isRegExp(/regexp/)
27 isBoolean(true)
28 isEmpty({})
29 isfunction(x => x)
30 isInteger(10)
31 isNumber(10.1)
32 instanceOf(obj, 'string')
33 ```
34
35 ## Objects
36 {: .-three-column}
37
38 ### Example
39 {: .-prime}
40
41 ```js
42 let obj = {}
43 ```
44
45 #### Update
46
47 ```js
48 obj = put(obj, 'user.name', 'John')
49 // → { user: { name: 'John' } }
50 ```
51
52 #### Read
53
54 ```js
55 pluck(name, 'user.name')
56 // → 'John'
57 ```
58
59 #### Delete
60
61 ```js
62 obj = del(obj, 'user')
63 // → { }
64 ```
65
66 ### Getting
67
68 ```js
69 pluck(state, 'user.profile.name')
70 ```
71
72 ```js
73 pick(state, ['user', 'ui'])
74 pick(state, /^_/)
75 ```
76
77 `pluck` returns values, `pick` returns subsets of objects.
78
79 See:
80 [pluck](https://github.com/tjmehta/101#pluck),
81 [pick](https://github.com/tjmehta/101#pick)
82
83 ### Setting
84
85 ```js
86 put(state, 'user.profile.name', 'john')
87 ```
88
89 See:
90 [put](https://github.com/tjmehta/101#put)
91
92 ### Deleting
93
94 ```js
95 del(state, 'user.profile')
96 omit(state, ['user', 'data'])
97 ```
98
99 `omit` is like `del`, but supports multiple keys to be deleted.
100
101 See:
102 [omit](https://github.com/tjmehta/101#omit),
103 [del](https://github.com/tjmehta/101#del)
104
105 ### Keypath check
106
107 ```js
108 hasKeypaths(state, ['user'])
109 hasKeypaths(state, { 'user.profile.name': 'john' })
110 ```
111
112 See:
113 [hasKeypaths](https://github.com/tjmehta/101#haskeypaths)
114
115 ### Get values
116
117 ```js
118 values(state)
119 ```
120
121 ## Functions
122
123 ### Simple functions
124
125 | `and(x, y)` | `x && y` |
126 | `or(x, y)` | `x || y` |
127 | `xor(x, y)` | `!(!x && !y) && !(x && y)` |
128 | `equals(x, y)` | `x === y` |
129 | `exists(x)` | `!!x` |
130 | `not(x)` | `!x` |
131
132 Useful for function composition.
133
134 See:
135 [and](https://github.com/tjmehta/101#and),
136 [equals](https://github.com/tjmehta/101#equals),
137 [exists](https://github.com/tjmehta/101#exists)
138
139 ### Composition
140
141 ```js
142 compose(f, g)       // x => f(g(x))
143 curry(f)            // x => y => f(x, y)
144 flip(f)             // f(x, y) --> f(y, x)
145 ```
146
147 See:
148 [compose](https://github.com/tjmehta/101#compose),
149 [curry](https://github.com/tjmehta/101#curry),
150 [flip](https://github.com/tjmehta/101#flip)
151
152 ### And/or
153
154 ```js
155 passAll(f, g)       // x => f(x) && g(x)
156 passAny(f, g)       // x => f(x) || g(x)
157 ```
158
159 See:
160 [passAll](https://github.com/tjmehta/101#passall),
161 [passAny](https://github.com/tjmehta/101#passany)
162
163 ### Converge
164
165 ```js
166 converge(and, [pluck('a'), pluck('b')])(x)
167 ```
168
169 ```js
170 // → and(pluck(x, 'a'), pluck(x, 'b'))
171 ```
172
173 See:
174 [converge](https://github.com/tjmehta/101#converge)
175
176 ## Arrays
177
178 ### Finding
179
180 ```js
181 find(list, x => x.y === 2)
182 findIndex(list, x => ...)
183 includes(list, 'item')
184 last(list)
185 ```
186
187 ```js
188 find(list, hasProps('id'))
189 ```
190
191 ### Grouping
192
193 ```js
194 groupBy(list, 'id')
195 indexBy(list, 'id')
196 ```
197
198 ## Examples
199
200 ### Function composition
201
202 ```js
203 isFloat = passAll(isNumber, compose(isInteger, not))
204 // n => isNumber(n) && not(isInteger(n))
205 ```
206
207 ```js
208 function doStuff (object, options) { ... }
209
210 doStuffForce = curry(flip(doStuff))({ force: true })
211 ```
212
213 ## Reference
214
215 - <https://github.com/tjmehta/101>