-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScript Snippets Extension
More file actions
636 lines (528 loc) · 8.92 KB
/
JavaScript Snippets Extension
File metadata and controls
636 lines (528 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# JavaScript Snippets for VS Code
## Setup
Just install this package from the [Extension Marketplace](https://marketplace.visualstudio.com/VSCode), then make sure to add `"editor.snippetSuggestions": "top"` to your user settings to see these snippets on top in the suggestion popover.
## Snippets
Snippets are optimized to be short and easy to remember.
Below is a list of all available snippets and the triggers of each one. The **⇥** means the `TAB` key.
### Declarations
#### `v⇥` var statement
```javascript
var ${0}
```
#### `v=⇥` var assignment
```javascript
var ${1:name} = ${2:value};
```
#### `l⇥` let statement
```javascript
let ${0}
```
#### `l=⇥` let assignment
```javascript
let ${1:name} = ${2:value};
```
#### `dl=⇥` destructuring let assignment
```javascript
let {${1:name}} = ${2:value};
```
#### `co⇥` const statement
```javascript
const ${0}
```
#### `co=⇥` const assignment
```javascript
const ${1:name} = ${2:value};
```
#### `dco=⇥` destructuring const assignment
```javascript
const {${1:name}} = ${2:value};
```
### Flow Control
#### `if⇥` if statement
```javascript
if (${1:condition}) {
${0}
}
```
#### `el⇥` else statement
```javascript
else {
${0}
}
```
#### `ife⇥` if/else statement
```javascript
if (${1:condition}) {
${0}
} else {
}
```
#### `ei⇥` else if statement
```javascript
else if (${1:condition}) {
${0}
}
```
#### `ter⇥` ternary operator
```javascript
${1:condition} ? ${2:expression} : ${3:expression};
```
#### `fl⇥` for loop
```javascript
for (let ${1:i} = 0, ${2:len} = ${3:iterable}.length; ${1:i} < ${2:len}; ${1:i}++) {
${0}
}
```
#### `rfl⇥` reverse for loop
```javascript
for (let ${1:i} = ${2:iterable}.length - 1; ${1:i} >= 0; ${1:i}--) {
${0}
}
```
#### `fi⇥` for in loop
```javascript
for (let ${1:key} in ${2:array}) {
if (${2:array}.hasOwnProperty(${1:key})) {
${0}
}
}
```
},
#### `fo⇥` for of loop (ES6)
```javascript
for (let ${1:key} of ${2:array}) {
${0}
}
```
#### `wl⇥` while loop
```javascript
while (${1:condition}) {
${0}
}
```
#### `tc⇥` try/catch
```javascript
try {
${0}
} catch (${1:err}) {
}
```
#### `tf⇥` try/finally
```javascript
try {
${0}
} finally {
}
```
#### `tcf⇥` try/catch/finally
```javascript
try {
${0}
} catch (${1:err}) {
} finally {
}
```
#### `sw⇥` switch case
```javascript
switch (${1:expr}) {
case ${2:value}:
return $0;
default:
return;
}
```
### Functions
#### `f⇥` anonymous function
```javascript
function (${1:arguments}) {
${0}
}
```
#### `fn⇥` named function
```javascript
function ${1:name}(${2:arguments}) {
${0}
}
```
#### `iife⇥` immediately-invoked function expression (IIFE)
```javascript
((${1:arguments}) => {
${0}
})(${2});
```
#### `fa⇥` function apply
```javascript
${1:fn}.apply(${2:this}, ${3:arguments})
```
#### `fc⇥` function call
```javascript
${1:fn}.call(${2:this}, ${3:arguments})
```
#### `fb⇥` function bind
```javascript
${1:fn}.bind(${2:this}, ${3:arguments})
```
#### `af⇥` arrow function (ES6)
```javascript
(${1:arguments}) => ${2:statement}
```
#### `afb⇥` arrow function with body (ES6)
```javascript
(${1:arguments}) => {
${0}
}
```
#### `gf⇥` generator function (ES6)
```javascript
function* (${1:arguments}) {
${0}
}
```
#### `gfn⇥` named generator function (ES6)
```javascript
function* ${1:name}(${2:arguments}) {
${0}
}
```
### Iterables
#### `seq⇥` sequence of 0..n
```javascript
[...Array(${1:length}).keys()]${0}
```
#### `fe⇥` forEach loop
```javascript
${1}.forEach((${2:item}) => {
${0}
});
```
#### `map⇥` map
```javascript
${1}.map((${2:item}) => {
${0}
});
```
#### `reduce⇥` reduce
```javascript
${1}.reduce((${2:previous}, ${3:current}) => {
${0}
}${4:, initial});
```
#### `filter⇥` filter
```javascript
${1}.filter(${2:item} => {
${0}
});
```
#### `find⇥` find
```javascript
${1}.find(${2:item} => {
${0}
});
```
### Objects and Classes
#### `ol⇥` object literal
```javascript
{
kv${0}
};
```
#### `slol⇥` same-line object literal
```javascript
{ kv${0} };
```
#### `kv⇥` key/value pair
```javascript
${1:key}: ${2:value},
```
#### `c⇥` class (ES6)
```javascript
class ${1:name} {
constructor(${2:arguments}) {
${0}
}
}
```
#### `cex⇥` child class (ES6)
```javascript
class ${1:name} extends ${2:base} {
constructor(${3:arguments}) {
super(${3:arguments});
${0}
}
}
```
#### `ctor⇥` class constructor (ES6)
```javascript
constructor(${1:arguments}) {
super(${1:arguments});
${0}
}
```
#### `m⇥` method (ES6 syntax)
```javascript
${1:method}(${2:arguments}) {
${0}
}
```
#### `get⇥` getter (ES6 syntax)
```javascript
get ${1:property}() {
${0}
}
```
#### `set⇥` setter (ES6 syntax)
```javascript
set ${1:property}(${2:value}) {
${0}
}
```
#### `gs⇥` getter and setter (ES6 syntax)
```javascript
get ${1:property}() {
${0}
}
set ${1:property}(${2:value}) {
}
```
#### `pctor⇥` prototypal constructor
```javascript
var ${1:Class} = function(${2:arguments}) {
${0}
};
```
#### `proto⇥` prototype method
```javascript
${1:Class}.prototype.${2:method} = function(${3:arguments}) {
${0}
};
```
#### `oa⇥` Object.assign
```javascript
Object.assign(${1:dest}, ${2:source})
```
#### `oc⇥` Object.assign copy (shallow clone)
```javascript
Object.assign({}, ${1:original}, ${2:source})
```
### Returning values
#### `r⇥` return
```javascript
return ${0};
```
#### `rp⇥` return Promise (ES6)
```javascript
return new Promise((resolve, reject) => {
${0}
});
```
#### `rc⇥` return complex value (such as JSX components)
```javascript
return (
${0}
);
```
### Types
#### `tof⇥` typeof
```javascript
typeof ${1:source} === '${2:undefined}'
```
#### `iof⇥` instanceof
```javascript
${1:source} instanceof ${2:Object}
```
### Promises
#### `pr⇥` Promise (ES6)
```javascript
new Promise((resolve, reject) => {
${0}
})
```
#### `then⇥` Promise.then
```javascript
${1:promise}.then((${2:value}) => {
${0}
})
```
#### `catch⇥` Promise.catch
```javascript
${1:promise}.catch((${2:err}) => {
${0}
})
```
### ES6 Modules
#### `ex⇥` export (ES6)
```javascript
export ${1:member};
```
#### `exd⇥` export default (ES6)
```javascript
export default ${1:member};
```
#### `im⇥` import module (ES6)
```javascript
import ${1:*} from '${2:module}';
```
#### `ima⇥` import module as (ES6)
```javascript
import ${1:*} as ${2:name} from '${3:module}';
```
### Node.js
#### `cb⇥` Node.js style callback
```javascript
(err, ${1:value}) => {${0}}
```
#### `re⇥` require
```javascript
require('${1:module}');
```
#### `rel⇥` require local
```javascript
require('./${1:module}');
```
#### `req⇥` require assignment
```javascript
const ${1:module} = require('${1:module}');
```
#### `reql⇥` require assignment local
```javascript
const ${1:module} = require('./${1:module}');
```
#### `dreq⇥` destructuring require assignment
```javascript
const {${1:module}} = require('${1:module}');
```
#### `dreql⇥` destructuring require assignment local
```javascript
const {${1:module}} = require('./${1:module}');
```
#### `em⇥` exports.member
```javascript
exports.${1:member} = ${2:value};
```
#### `me⇥` module.exports
```javascript
module.exports = ${1:name};
```
#### `meo⇥` module exports object
```javascript
module.exports = {
${1:member}
};
```
#### `on⇥` event handler
```javascript
${1:emitter}.on('${2:event}', (${3:arguments}) => {
${0}
});
```
### BDD Testing (Mocha, Jasmine, etc.)
#### `desc⇥` describe
```javascript
describe('${1:description}', () => {
${0}
});
```
#### `cont⇥` context
```javascript
context('${1:description}', () => {
${0}
});
```
#### `it⇥` it
```javascript
it('${1:description}', () => {
${0}
});
```
#### `its⇥` it synchronous
```javascript
it('${1:description}', () => {
${0}
});
```
#### `ita⇥` it asynchronous
```javascript
it('${1:description}', (done) => {
${0}
done();
});
```
#### `bf⇥` before test suite
```javascript
before(() => {
${0}
});
```
#### `bfe⇥` before each test
```javascript
beforeEach(() => {
${0}
});
```
#### `aft⇥` after test suite
```javascript
after(() => {
${0}
});
```
#### `afe⇥` after each test
```javascript
afterEach(() => {
${0}
});
```
### Console
#### `cl⇥` console.log
```javascript
console.log(${0});
```
#### `ce⇥` console.error
```javascript
console.error(${0});
```
#### `cw⇥` console.warn
```javascript
console.warn(${0});
```
#### `cll⇥` console.log labeled
```javascript
console.log('${0}', ${0});
```
#### `cel⇥` console.error labeled
```javascript
console.error('${0}', ${0});
```
#### `cwl⇥` console.warn labeled
```javascript
console.warn('${0}', ${0});
```
### Timers
#### `st⇥` setTimeout
```javascript
setTimeout(() => {
${0}
}, ${1:delay});
```
#### `si⇥` setInterval
```javascript
setInterval(() => {
${0}
}, ${1:delay});
```
#### `sim⇥` setImmediate
```javascript
setImmediate(() => {
${0}
});
```
#### `nt⇥` process nextTick
```javascript
process.nextTick(() => {
${0}
});
```
### Miscellaneous
#### `us⇥` insert 'use strict' statement
```javascript
'use strict';
```