-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeSnippet.js
More file actions
701 lines (449 loc) · 17.4 KB
/
Copy pathcodeSnippet.js
File metadata and controls
701 lines (449 loc) · 17.4 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//# 65+ JAVASCRIPT CODE SNIPPETS WITH EXPLANATIONS
//: Avoid the "delete" keyword
const person = {
name: "therogersak",
age: "18",
};
delete person.age;
console.log(person); // { name: 'therogersak' }
// do this
const person2 = {
name: "Chris",
age: 40,
};
const { age, ...newPerson2 } = person2;
console.log(newPerson2); // { name: 'Chris' }
/* Don't use delete to remove a property from an object.
This mutates the original object and can lead to unpredictable behavior which becomes difficult to debug.
Instead, use the rest operator to create a new copy without given property*/
//: Using a Falsy Bouncer
const evenNumbersSquared = [1, 2, 3, 4, 5, 6]
.map((n) => {
if (n % 2 !== 0) {
return null;
}
return n * n;
})
.filter(Boolean);
console.log(evenNumbersSquared); // [ 4, 16, 36 ]
/* When passing the "Boolean" constructor directly to Array.filter as the first argument, it serves as a falsy bouncer.
This will filer all values that qualifies as falsy;
That is false, null, undefined, 0, NaN, and ""(empty string)
*/
//: Object Destructuring on arrays
const lords = ["Shiva", "Vishnu", "Hanuman", "Ganesh"];
const { 0: s, 1: v, 2: h, 3: g } = lords;
console.log(s, v, h, g); // Shiva Vishnu Hanuman Ganesh
/* You can destructure elements from an array using the same syntax as when destructuring for objects.
The property name corresponds to the index of the element in the array.
It's a convenient way to pull out specific elements from an array in a single clean line of code.
*/
//: Skip elements with Array Destructuring
const number = [1, 2, 3, 4, 5];
const [, , ...newNum] = number;
console.log(newNum); // [3,4,5]
/* You can use an Empty 'placeholder' comma to skip elements when destructuring arrays.
If you want to access the second or third from a list, or want to skip the first or second element (etc), this is a great and clean way to do it.
*/
//: Replacer function with JSON.stringFY
const foo = {
bar: 40,
baz: "qux",
};
const replacer = (key, value) => (key === "bar" ? value * 2 : value);
const fooStr = JSON.stringify(foo, replacer);
console.log(fooStr); // {"bar":80,"baz":"qux"}
/* The replacer function is the second argument to JSON.stringfy.
I alters the behavior of the stringfication process.
The argument can also be an array or type String or Number.
This becomes an "allowlist" that filters the properties of the object with the name included in the list
*/
//: Console.Trace
console.log("Simple log statement...");
// Simple log statement...
const someFunction = () => {
console.trace("Log with stack trace");
};
// Log with stack trace
// SomeFunction @ index.html:12
// (anonymouse) @ index.html:15
/*
If you use console.trace instead of console.log, it will show you the complete call stack when debugging.
This is very convenient when you're working with larger setups with multiple files and modules.
*/
//: Console.Time
console.time("timer-1");
// time consuming operation
const items = [];
for (let i = 0; i < 1000000; i++) {
items.push({
i,
});
}
console.timeEnd("timer-1");
/* You can set timers using console.time.
This can be useful when debugging slow loop or function calls. */
//: Console.group
console.group("User");
console.log(user.name);
console.log(access[user.name]);
console.groupEnd();
/*
console.group let's you use nested groups to help organize your output by visually associating related messages.
Addotionally, you can use the console.groupCollapsed method to create a new block that is collapsed and requires clicking a disclosure button to read it.
*/
//: Console.assert
if (!user.id) {
console.log("User Id Missing");
}
// Using consoel.assert
console.assert(user.id, "User id missing");
/*
Use console.assert to make conditional log statements.
The console.assert method writes an error message to the console if the assertion is false
If the assertion is true, nothing happens.
*/
//: Pass Arguments As An Object
const createUser = (username, date, isAdmin, isMod) => {
// create user
};
createUser("therogersak", "01-01-2004", false, true);
// Like this
const createUser2 = ({ username, data, isAdmin, isMod }) => {
// create user
};
createUser2({
username: "therogersak",
date: "01-01-2004",
isAdmin: false,
isMod: true,
});
/*
If more than 1 parameter is added to a function, it's very difficult to figure what these arguments mean, when the function is called.
Passing the argument contained is an object (a so-called DTO) makes it pretty clear from the names of the properties.
And also - the order doesn't matter anymore.
*/
//: Readable Numbers
const largeNumber = 1000000000000;
const largeNumber2 = 1000_000_000_000;
// Use like this
const largeNumber3 = 1e12;
/*
The Numeric Separators give us the ability to separete large numbers with an underscore (_) in numeric literals.
This greatly improves readability, and it will have no effect on how the Javascript engine enteprets the number.
*/
//: Pass Messages Between Tabs and Windows
const bc = new BroadcastChannel("test_channel");
bc.postMessage("Hello Word");
bc.onmessage = function (ev) {
console.log(ev);
};
/* The Broadcast Channel Api Allows basic communication between browsing contexts (windows, tabs, frames or iframes).
Using the BroadcastChannel constructor, you can recive any message that are posted to it without having to maintain a refences to frames or workers.*/
//: Remove Duplicates From Array
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const uniqueNumber = [new Set(numbers)];
[1, 2, 3, 4, 5, 6, 7, 8, 9];
/*
The simplest way to remove duplicates from an array, is to use the set constructor to create a new set object containibg unique values (of any kind).
In other words, set will automatically remove duplicates for use, and by spreading it into a new array, we can create a new array without duplicates.
*/
//: Use Modules instead of Classes
class SomeClass {
methodOne() {}
methodTwo() {}
}
// Usage
const someClass = new SomeClass();
someClass.methodOne();
//* Use this
export const functionOne = () => {};
export const functionTwo = () => {};
// usage
import * as someFunctions from "./someFunction";
someFunctions.functionOne();
/*
In Javascript, there are no classes.
It's syntactical sugar added to please developers from other languages such as Java or C#.
Most of the time, they don't serve a good purpose, and are not really useful. Instead, use modules.
*/
//: Javascript Debugger
// The browser will stop here, when executing.
if (someThingTrue) {
debugger;
}
/*
Place a debugger; Line in your code, and the browser will stop executing. This makes it easier to start investigating.
As an alternative, Vscode also ships with a greay inbuilt bebugger which works both with browsers and NodeJs.
*/
//: Do not extend the built-ins (create your own utilities instead)
//* Custom average function
Array.prototype.average = function () {
return this.reduce((acc, elem) => acc + elem / this.length);
};
const list = [1, 2, 3, 4];
const avg = list.average();
//* Use this
class ArrayUtils {
// custom average function
static average(list) {
return list.reduce((acc, elem) => acc + elem / list.length);
}
}
const avg2 = ArrayUtils.average(list);
/*
Extending the standard built-ins is considered bad practice.
Create your own utility class instead.
(And share it on NPM, if it's useful to others).
*/
//: Use Array.some (to check for occurrences in a list)
const hasActiveUsers = list.find((user) => user.isActive);
console.log(Boolean(hasActiveUsers));
// use like
const hasActiveUsers2 = list.some((user) => user.isActive);
console.log(hasActiveUser);
/* Instead of using Array.find, or manually searching a list for an occurrence, use the array method Array.some instead.
It's built for exactly that purpose*/
//: Using Optional Chaininig On Fnctions
// Using short-circuit
someFunction && someFunction();
// Using if-statement
if (someFunction) {
someFunction();
}
//* But Now
someFunction?.();
/* The Optional Chaining operator (?.) enables you to access properties that are potentially null or undefined.
It works perfectly well on function calls as well.
A great example of usage is in React, where function passed as props may be optional (and therefor potentially undefined).*/
//: Enforce required arguments using default assignment
//* Create this reusable function
const isRequired = () => {
throw Error("Argument is missing");
};
//* Example Usage
const setUsername = (username = isRequired()) => {
// Do something with "username",
// If username not provided.
// these lines of code will naver be reached
};
/* The idea here is to make a default assignment to the return-value of a function which throws an error when getting invoked.
In this way, an error will be thrown when am argument in not passed.
Be aware, that null is considered a value, hence passing null will not result in a default assignment*/
//: Adding a leading Zero
const d = new Date();
const day = `0${d.getDate()}`.slice(-2);
// 04 or 15
/* Use this one-liner to add leading zeros to numbers.
(very useful for dates).
Alternatively,you can use padStart(2,0)*/
//: Avoid DEFAULT EXPORTS
/* => Poor discoverability and autocompletion
=> Horrible when using CommonJS.
=> TypeScript struggles with auto-import
=> Re-exporting becomes tedious.
Generally, i always recommend simple exports + destructured import.*/
//: Use The Spread Operator - To Shallow Copy Objects and Arrays.
const newObject = {};
Object.keys(oldObject).forEach((key) => {
newObject[key] = oldObject[key];
});
const newArray = [];
oldArray.forEach((arr) => {
newArray.push(arr);
});
//* Use like this =>
const newObject2 = { ...oldObject };
const newArray2 = [...oldArray];
/* Use the spread operator to create shallow copies of object and arrays. It's way cleaner than iterating and manually coping over.
The spread operator was a feature added as a part of ES6 and is supported by major browsers.*/
//: Destructure Into Existing Variables
const about = {
username: "thergersak",
email: "therogersak@gmail.com",
};
let username;
let email;
({ username, email } = about);
/* There's an easy way to destructure into existing variables.
Simply wrap the destructuring syntax in parentheses - this will result in the destructured variables being assinged as variables in current scope.
Combining this with the use of the let keyword can be very useful in certain cases.*/
//: Lock An Object Using Object Freeze
const aboutUser = {
username: "therogersak",
};
Object.freeze(aboutUser);
aboutUser.username = "FakeUser";
// Throws an error in strict mode.
/* The Object.freeze method freezes an object.
A frozen object can no longer be changed and will result in an error. This means, that no properties can be reassigned or delete from the object. */
//: Create a Custom Promise Using the Async Keyword
const promise = async () => {
return "Jai Shree Ram";
};
promise().then((result) => {
console.log(result); // "Jai Shree Ram"
});
/* You probably already knew that you have to put ‘async’ in front of the function signature in order to use ‘await’ inside of it. But did you also know that ‘async’ turns the function into a promise? When adding ‘async’ in from on the function signature, the return value automatically becomes ‘awaitable’ or ‘thenable’. */
//: Creating A Reusable Pipe Using JavaScript
const pipe =
(...fns) =>
(arg) =>
fns.reduce((value, fn) => fn(value), arg);
const calculaterProfit = pipe(
// DEDUCT VAT (8%)
(value) => value * (1 - 0.08),
// DEDUCT tax (15%)
(value) => value * 1.015,
// Add External contrubution
(value) => value + 2500,
// Split with co-founders (3 c-founders)
(value) => value / 3
);
const revenue = 5e4;
const profit = calculaterProfit(revenue);
/* This is an Example of how you can create a reusable and composable pipe using JavaScript.
The data of the pipe flows left to right, calling each function with the output of the last one.
It's great and clean way to simplify a flow of processing a alue through multiple steps. */
//: STOP USING IIFEs (Immediately Invoked Function Expression)
(async function doSomeThingAsync() {
const foo = await bar();
const baz = foo.qux;
return baz;
})();
//* Use like this
async function doSomeThingAsync2() {
const foo = await bar();
const baz = foo.qux;
return baz;
}
doSomeThingAsync2();
/* IIFEs died when modules were born.
You don't need them (at least in 99% in the cases, you don't).
In a case like this, just execute the function on a new line instead
*/
//: UNDERSTANDING CLOSURES
// We can define a nested inner function which gets returned by an outer function.
const outer = () => {
let n = 42;
const inner = () => {
// inner can access "n"
console.log(n);
};
return inner;
};
//? The inner function still has access to n, even through it's called from another context.
const inner = outer();
inner();
/* Closures is one of the fundemental building-blocks in JavaScript.
A closures gives a function access to an outer function's scope, even if the inner function in invoked from a completely different context.
This pattern is often used in combination with currying.*/
//: Scroll To A Specific Element (with a smooth scrolling animation)
const element = document.getElementById("element");
element.scrollIntoView({
behavior: "smooth",
});
/* Calling Element.scrollIntoView causes the elements parent container to scroll, such that the element becomes visible to the user.
It's really easy way to invoked a smooth scrolling behavior.*/
//: Avoid unnecessary asyn-await
const fetchUser = async () => {
return await fetch("https://endpoint.com");
};
//* Right
const fetchUser2 = () => {
return fetch("https://endpoint.com");
};
// The Result is the same!
const user = await fetchUser();
const userObj = await user.json();
/* If the function returns a Promise directly, there's no need to await it. */
//: Use Proper Variables Names (Also in callbacks)
const profileWithImages = user.filter((u) => {
return u.entities.find((e) => e.type === "profile_image");
});
//* Right
const profileWithImages2 = user.filter((user) => {
return user.entities.find((entity) => entity.type === "profile_img");
});
/* Use proper variable names - also in callbacks.
Short, concise code is not necessarily an ideal.
Clarity and readablity is.
Playing with an extra line perfecly ok.*/
//: Use Object.entries (to access both ket and value)
Object.entries(someObj).forEach((key, value) => {
console.log(key, value);
});
/* You can use Object.entries() to iterate through the properties of an object and access both key and value.
No need to do an object lookup for each iteration.*/
//: Using curly braces with switch-statements
switch (foobar) {
case "foo": {
console.log("foo");
break;
}
case "qux": {
console.log("QUX");
break;
}
}
/* Did yoy know that you can curly braces with swithc-statement?
Takeaways include:
1.More readable
2.Establishes their own block scope.
*/
//: Create Your Own Custom HTML Elements
class FooElement extends HTMLElement {
connectedCallback() {
this.innerHTML = "This is custom element";
}
}
customElements.define("foo-element", FooElement);
/* Did you know that you can create your own custom HTML elements using JavaScript - and then use them in your HTML file just like any other element?.
You can create some pretty powerful experiences using this technique.*/
//: Using The Nullish Coalescing Operator
let price_1 = 0;
let price_2;
// Assign a default if "price" is not set
const defaultPrice_1 = price_1 ?? 10;
const defaultPrice_2 = price_2 ?? 50;
console.log(defaultPrice_1); // 0
console.log(defaultPrice_2); // 50
/* The Nullish coalescing operator will return its right-hand operator when the left side in null or undefined. Not Just falsy
When working with numbers, this is typically very useful.*/
//: 3 Ways to fill an array
const arr = new Array(10).fill("foo");
//* 2st
const arr2 = new Array(10);
const filledArr = [...arr2].map(() => "foo");
//* 3st
const arr3 = Array.from({ length: 10 }, () => "foo");
/* These are 3 different ways to fill an array using JavaScript.
The most commonly used is version 1 - yet, version 2 can useful when filling
the array best on business logic.
Version 3 is mostly for fun - but it can actually be done this way as well. */
//: 3 Options You can pass to AddEventListener
element.addEventListener("click", callback, {
capture: false,
once: true,
passive: false,
});
/* capture: Will use “capturing” instead of “bubbling”.
once: If true, the event will be removed after running once.
passive: If true, the function will never call preventDefault(), even if it’s included in the
callback function. */
//: How to hide all elements specified?
const hide = (...ele) => [...ele].forEach((el) => (el.style.dispaly = "none"));
hide(document.querySelectorAll("p"));
// Hides all <p> elements on the page
//: How to check if the element has the specified class?
const hasClass = (el, className) => el.classList.contain(className);
hasClass(document.querySelector("p.special"), "special");
//: How to toggle a class for an element?
const toggleClass = (el, className) => el.classList.toggle(className);
toggleClass(document.querySelector("p.special", "special"));
//: How to get the scroll position of the current page?
const getScrollPosition = (el = window) => ({
x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop,
});
getScrollPosition(); // {x:0 , y:300}