OSDN Git Service

Regular updates
[twpd/master.git] / lodash.md
1 ---
2 title: Lodash
3 category: JavaScript libraries
4 layout: 2017/sheet
5 weight: -3
6 updated: 2020-06-24
7 description: |
8   This is not a complete list.
9 ---
10
11 ## Collections
12
13 ### Finding
14
15 ```js
16 _.filter(list, (n) => n % 2)    // → Array
17 _.find(list, (n) => n % 2)      // → item
18 _.findLast(list, ...)           // → item
19 ```
20
21 Works for both arrays and objects.
22
23 ### Accessing
24
25 ```js
26 _.at([ abcd ], 0)               // → [ a ] - same as list[0]
27 _.at([ abcd ], [ 0, 1 ])        // → [ ab ]
28 ```
29
30 ### Set/get
31
32 ```js
33 _.set(object, 'users[0].name', value)
34 _.get(object, 'users[0].name')
35 _.get(object, ['users', 0, 'name'])
36 ```
37
38 ### Iteration
39
40 ```js
41 _.forEach(list, (item, i) => ...)
42 _.forEachRight(list, ...)
43
44 _.map(list, ...)
45 ```
46
47 ```js
48 _.every(users, (u) => u.active)  // → true|false (aka _.all)
49 _.any(users, ...)                // → true|false (aka _.some)
50 ```
51
52 ## Array
53
54 ### Arrays
55
56 ```js
57 _.chunk([ abcd ], 2)           // → [ [ab], [cd] ]
58 _.compact(list)
59
60 _.fill(Array(4), 'x')          // → [ 'x', 'x', 'x', 'x' ]
61 _.flatten
62 _.flattenDeep
63 ```
64
65 ### Filtering
66
67 ```js
68 _.drop([ abcdef ], 2)          // → [   cdef ]
69 _.dropRight([ abcdef ], 2)     // → [ abcd   ]
70 _.take([ abcdef ], 2)          // → [ ab     ]
71 _.takeRight([ abcdef ], 2)     // → [     de ]
72 _.slice([ abcdef ], 2, 4)      // → [   cd   ]
73 ```
74
75 ```js
76 _.initial([ abcdef ])          // → [ abcde  ] - dropRight(list, 1)
77 _.rest([ abcdef ])             // → [  bcdef ] - takeRight(list, 1)
78 ```
79
80 ```js
81 _.dropWhile(list, 'active')            // works like filter
82 _.dropWhile(list, 'active', true)
83 _.dropWhile(list, { active: true })
84 _.dropWhile(list, (n) => ...)
85 _.dropRightWhile(list, ...)
86 ```
87
88 ```js
89 _.without([ abcde ], b)        // → [ acde ]
90 ```
91
92 ```js
93 _.remove(list, (n) => n % 2)
94 ```
95
96 ### Accessing
97
98 ```js
99 _.first([ abcdef ])            // → a
100 _.last([ abcdef ])             // → f
101 ```
102
103 ### Sets
104
105 ```js
106 _.uniq()
107 _.difference([ abc ], [ bc ])       // → [ a    ]
108 _.intersection([ abc ], [ bcd ])    // → [  bc  ]
109 _.union([ abc ], [ bcd ])           // → [ abcd ] (unique)
110 ```
111
112 ```js
113 Array#concat()
114 ```
115
116 ### Indexes
117
118 ```js
119 _.findIndex(list, fn)
120 _.findLastIndex(list, fn)
121 ```
122
123 ```js
124 _.sortedIndex(list, val)
125 _.sortedLastIndex(list, val)
126 ```
127
128 ```js
129 _.indexOf(list, val)
130 ```
131
132 ## Functions
133
134 ### Currying
135
136 ```js
137 greet = (greeting, name) => `${greeting}, ${name}!`
138 ```
139 {: .-setup}
140
141 ```js
142 fn = _.partial(fn, 'hi')
143 fn('joe')    // → 'hi, joe!'
144
145 fn = _.partial(fn, 'joe')
146 fn('yo')     // → 'yo, joe!'
147 ```
148
149 ```js
150 _.curry(greet)('hi')         // → function(name)
151 _.curryRight(greet)('joe')   // → function(greet)
152 ```
153
154 ## Decorating functions
155
156 ### Throttling
157
158 ```js
159 _.throttle(fn)
160 _.debounce(fn)
161 ```
162
163 ### Limiting
164
165 ```js
166 _.before(5, fn)         // only works 5 times
167 _.after(5, fn)          // works only after 5 times
168 _.once(fn)              // like _.before(fn, 1)
169 ```
170
171 ### Etc
172
173 ```js
174 _.wrap(_.escape, (name) => `hi ${name}`)
175 // same as doing `name = _.escape(name)`
176
177 _.delay(fn, 2000)
178
179 _.negate(fn)
180
181 _.memoize(fn)
182 _.memoize(fn, ...)
183 ```
184
185 ## Strings
186
187 ### Capitalization
188
189 ```js
190 _.capitalize('hello world')   // → 'Hello world'
191 _.startCase('hello_world')    // → 'Hello World'
192 _.snakeCase('hello world')    // → 'hello_world'
193 _.kebabCase('hello world')    // → 'hello-world'
194 _.camelCase('hello world')    // → 'helloWorld'
195 ```
196
197 ### Padding
198
199 ```js
200 _.pad('abc', 3)           // → 'abc'
201 _.pad('abc', 8)           // → '   abc  '
202 _.pad('abc', 8, '_-')     // → '_-abc_-_'
203 _.padStart('abc', 3)      // → 'abc'
204 _.padStart('abc', 6)      // → '   abc'
205 _.padStart('abc', 6, '_-')// → '_-_abc'
206 _.padEnd('abc', 3)        // → 'abc'
207 _.padEnd('abc', 6)        // → 'abc   '
208 _.padEnd('abc', 6, '_-')  // → 'abc_-_'
209 ```
210
211 ### Trim
212
213 ```js
214 _.trim('  str  ')         // → 'str' 
215 _.trimLeft('  str  ')     // → 'str  '
216 _.trimRight('  str  ')    // → '  str'
217 ```
218
219 ### Etc
220
221 ```js
222 _.repeat('-', 2)              // → '--'
223 _.deburr('déjà vu')           // → 'deja vu'
224 _.trunc('hello world', 5)     // → 'hello...'
225 ```
226
227 ```js
228 _.startsWith('abc', 'a')   // → true
229 _.endsWith('abc', 'c')     // → true
230 ```
231
232 ## Objects
233
234 ### Keys and values
235
236 ```js
237 _.keys(obj)
238 _.values(obj)
239 ```
240
241 ## Chaining
242
243 ### Chain and value
244
245 ```js
246 _([1, 2, 3])
247   .reduce((total, n) => total + n)
248   .map((n) => n * n)
249   .tap(console.log)
250   .thru((n) => n.reverse())
251   .value()
252 ```