-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpackage.json
559 lines (559 loc) · 14.9 KB
/
package.json
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
{
"name": "ionicabizau-code-style",
"version": "1.0.12",
"description": "Or how I build things.",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/IonicaBizau/code-style.git"
},
"keywords": [
"code",
"style",
"ionicabizau"
],
"author": "Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)",
"license": "MIT",
"bugs": {
"url": "https://github.com/IonicaBizau/code-style/issues"
},
"homepage": "https://github.com/IonicaBizau/code-style#readme",
"blah": {
"title": ":book: Ionică Bizău's Code Style :heart:",
"show_description": false,
"description": [
{
"p": [
"This document contains guides that *I* defined and follow when building things.",
"[Open issues](/code-style/issues) with any questions, ideas, fixes etc. :innocent:"
]
},
{
"h1": "Contents"
},
{
"ul": [
[
"[Variable declarations](#variable-declarations-pencil)",
{
"ul": [
"[Variables](#variables-speech_balloon)",
"[Constants](#constants-triangular_flag_on_post)",
"[Globals](#globals-earth_africa)"
]
}
],
"[Semicolons](#semicolons-pencil2)",
"[Method and property definitions](#method-and-property-definitions-paperclip)",
"[Deleting properties](#deleting-properties-x)",
"[`eval()`](#eval)",
"[Iterating objects and arrays](#iterating-objects-and-arrays)",
"[Multiline strings](#multiline-strings-guitar)",
"[Modifying prototypes of built-in objects](#modifying-prototypes-of-built-in-objects-shit)",
"[Naming things](#naming-things-thought_balloon)",
"[Curly braces](#curly-braces-curly_loop)",
"[Array and Object Initializers](#array-and-object-initializers-file_folder)",
"[Commas](#commas)",
"[Blank lines](#blank-lines)",
"[Binary and Ternary operators](#binary-and-ternary-operators)",
"[Quotes](#quotes-speech_balloon)",
"[Comments](#comments-notes)",
"[Project naming](#project-naming)"
]
},
{
"h2": "Variable declarations :pencil:"
},
{
"h3": "Variables :speech_balloon:"
},
{
"p": "Using `var` in general or `let` when they should be accesible only in specific blocks (e.g. `if`)."
},
{
"code": {
"language": "js",
"content": [
"// One declaration",
"var foo = 1;",
"",
"// Multiple declarations",
"var foo = 1",
" , bar = \"Hello World\"",
" , anotherOne = [{ foo: \"bar\" }]",
" ;",
"",
"if (...) {",
" let baz = 42;",
" /* do something with baz */",
"}"
]
}
},
{
"h3": "Constants :triangular_flag_on_post:"
},
{
"p": "Using `const`. The constant names are written with UPPERCASE letters. I also use `const` when including libraries using `require` and when they should not be changed. In this case, the names will not be with caps."
},
{
"code": {
"language": "js",
"content": [
"// Dependencies",
"const http = require(\"http\")",
" , fs = require(\"fs\")",
" , EventEmitter = require(\"events\").EventEmitter",
"// Constants",
"const PI = Math.PI",
" , MY_CONSTANT = 42",
" ;"
]
}
},
{
"h3": "Globals :earth_africa:"
},
{
"p": "I define globals when there is no commonjs environment (this is actually handled by [`dist-it`](https://github.com/IonicaBizau/dist-it). When I manually define globals, I do that using `window.MyGlobal` (on the client) and `global.MyGlobal` (on the server)."
},
{
"h2": "Semicolons :pencil2:"
},
{
"p": "I *use* semicolons. Almost always."
},
{
"code": {
"language": "js",
"content": [
"var foo = 1;",
"function bar (x) {",
" var someMethod = function (m) {",
" console.log(m);",
" };",
" return { y: x, foo: someMethod };",
"}",
"class Foo {",
" ...",
"}"
]
}
},
{
"h2": "Method and property definitions :paperclip:"
},
{
"p": "I use the ES6 `class` for creating classes."
},
{
"code": {
"language": "js",
"content": [
"class Person {",
" constructor (name, age) {",
" this.name = name;",
" this.age = age;",
" }",
" getName () {",
" return this.name;",
" }",
"}"
]
}
},
{
"h2": "Deleting properties :x:"
},
{
"p": "I nullify the properties when that's fine:"
},
{
"code": {
"language": "js",
"content": [
"var foo = {",
" bar: 42",
"};",
"foo.bar = null;"
]
}
},
{
"p": "However, I use the `delete` keyword when I really want to delete them."
},
{
"code": {
"language": "js",
"content": [
"delete foo.bar;"
]
}
},
{
"h2": "`eval()`"
},
{
"p": "`eval` is evil. :rage: Do not use it. However I use it in some test files and in places where I have to execute the JavaScript code provided by the user."
},
{
"p": "For converting strings to JSON, use `JSON.parse(strObj)`."
},
{
"h2": "Iterating objects and arrays"
},
{
"p": "For arrays, most of times, I use the `forEach` function:"
},
{
"code": {
"language": "js",
"content": [
"arr.forEach(c => {",
" // do something",
"});"
]
}
},
{
"p": "However, using `for` loops is fine too:"
},
{
"code": {
"language": "js",
"content": [
"for (var i = 0; i < arr.length; ++i) {",
" for (var ii = 0; ii < arr[i].length; ++ii) {",
" ...",
" }",
" ...",
"}"
]
}
},
{
"p": "For objects, I use the following style:"
},
{
"code": {
"language": "js",
"content": [
"Object.keys(obj).forEach(k => {",
" var cValue = obj[k];",
" // do something",
"});"
]
}
},
{
"p": "To simplify this, I created [`iterate-object`](https://github.com/IonicaBizau/iterate-object), which abstracts this functionality:"
},
{
"code": {
"language": "js",
"content": [
"const iterateObject = require(\"iterate-object\");",
"iterateObject(obj, (value, key) => {",
" // do something",
"});"
]
}
},
{
"h2": "Multiline strings :guitar:"
},
{
"p": "I use backticks to create multiline strings:"
},
{
"code": {
"language": "js",
"content": [
"var multiLineStr = `Lorem ipsum dolor sit amet, consectetur adipisicing elit",
"sed do eiusmod tempor incididunt ut labore et dolore magna",
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation",
"ullamco laboris nisi ut aliquip ex ea commodo consequat",
"New line again...`;"
]
}
},
{
"h2": "Modifying prototypes of built-in objects :shit:"
},
{
"p": "Just don't, unless that's the scope of the library."
},
{
"h2": "Naming things :thought_balloon:"
},
{
"p": "Using camel case notation for variables, in general. For constructors I capitalize the variable name (e.g. `EventEmitter`)."
},
{
"code": {
"language": "js",
"content": [
"// Node.JS require",
"const fs = require(\"fs\")",
" , events = require(\"events\")",
" , EventEmitter = events.EventEmitter",
" ;",
"",
"// Local variables",
"var x = 1",
" , twoWords = \"Hello World\"",
" ;",
"",
"// Functions",
"function fooBar () {...}",
"",
"// Classes",
"class Person {",
" constructor (name, age) {",
" this.name = name;",
" this.age = age;",
" }",
" getName () {",
" return this.name;",
" }",
"}",
"// Object fields",
"var obj = {",
" full_name: \"Johnny B.\"",
" , age: 20",
"};",
"obj.methodA = function () {...};"
]
}
},
{
"h2": "Curly braces :curly_loop:"
},
{
"p": "Open the curly brace at the end of the line. Always put the instructions between curly braces, even there is only one instruction."
},
{
"code": {
"language": "js",
"content": [
"if (expr) {",
" instr;",
"} else {",
" instr2;",
" instr3;",
"}"
]
}
},
{
"h2": "Array and Object Initializers :file_folder:"
},
{
"p": "See examples."
},
{
"code": {
"language": "js",
"content": [
"// Arrays",
"var arr = [1, 2, 3, 4];",
"",
"var lotOfElms = [",
" 1, 2, 3, 4, 5, 6, 7",
" , 8, 9, 10, 11, 12, 13",
" , 14, 15, 16, 17, 18",
"];",
"",
"var bigElms = [",
" \"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod.\"",
" , \"Tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim.\"",
" , \"Veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea.\"",
" , \"Commodo consequat. Duis aute irure dolor in reprehenderit in voluptate\"",
"];",
"",
"// Objects",
"var obj = { a: 1 };",
"",
"var obj1 = {",
" full_name: \"Johnny B.\"",
" , age: 20",
"};"
]
}
},
{
"h2": "Commas"
},
{
"p": "Put commas at the beginning of the line, not at the end."
},
{
"code": {
"language": "js",
"content": [
"var x = 1",
" , y = 2",
" ;",
"",
"const C_1 = 42",
" , C_2 = -42",
" ;",
"",
"var obj = {",
" x: 1",
" , y: 2",
"};"
]
}
},
{
"h2": "Blank lines"
},
{
"p": "Group the instructions inserting some blank lines where it's needed."
},
{
"code": {
"language": "js",
"content": [
"foo(x);",
"bar(x);",
"",
"foo(y);",
"bar(y);"
]
}
},
{
"h2": "Binary and Ternary operators"
},
{
"p": "See examples."
},
{
"code": {
"language": "js",
"content": [
"var foo = someObj",
" .method()",
" .method2()",
" .method3()",
" ;",
"",
"var a = cond ? v1 : v2;",
"",
"var b = long_condition_here",
" ? v1 : v2",
" ;",
"",
"var c = another_long_condition_here",
" ? with_some_long_value",
" : or_another_some_long_value",
" ;"
]
}
},
{
"h2": "Quotes :speech_balloon:"
},
{
"p": "Double quotes, with some exceptions when single quotes are used."
},
{
"code": {
"language": "js",
"content": [
"var foo = \"\\\"Hello\\\", he said.\";",
"var jQuerySelector = \"div.myClass[data-foo='bar']\";"
]
}
},
{
"h2": "Comments :notes:"
},
{
"p": "Put relevant comments. The comments start with uppercase letter."
},
{
"code": {
"language": "js",
"content": [
"// Dependencies",
"const lib1 = require(\"lib1\")",
" , lib2 = require(\"lib2\")",
" ;",
"",
"// Constants",
"const FOURTY_TWO = 42;"
]
}
},
{
"p": "Use JSDoc comments for functions and methods."
},
{
"code": {
"language": "js",
"content": [
"/**",
"* sum",
"* Calculates the sum of two numbers.",
"*",
"* @name sum",
"* @function",
"* @param {Number} a The first number,",
"* @param {Number} b The second number.",
"* @return {Number} The sum of the two numbers.",
"*/",
"function sum (a, b) {",
" return a + b;",
"};"
]
}
},
{
"p": "I use the [`blah` tool](https://github.com/IonicaBizau/blah) to generate documentation."
},
{
"code": {
"language": "sh",
"content": [
"$ npm install -g blah",
"$ blah --readme",
"$ blah --docs some-file.js"
]
}
},
{
"h2": "Project naming"
},
{
"p": "I use [`name-it`](https://github.com/IonicaBizau/name-it) to generate project names."
},
{
"h2": "Project licenses"
},
{
"p": "I :sparkling_heart: open-source! I prefer the MIT license."
}
]
},
"files": [
"bin/",
"app/",
"lib/",
"dist/",
"src/",
"scripts/",
"resources/",
"menu/",
"cli.js",
"index.js",
"bloggify.js",
"bloggify.json",
"bloggify/"
]
}