-
Notifications
You must be signed in to change notification settings - Fork 9
/
notes.txt
787 lines (592 loc) · 17 KB
/
notes.txt
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
Magesh Kuppan
Schedule
Commence : 10:00 Hrs
Tea Break : 11:30 Hrs (20 mins)
Lunch Break : 13:30 (1 Hr)
Tea Break : 16:00 (20 mins)
Wind Up : 18:00
Breakup:
ES6 & TypeScript (1 Day)
Advanced Typescript (1 Day)
Angular (4 Days)
State Management (2 Days)
About You
Name
Total Experience
Primary Skillset
Experience in Angular.js / Angular / React / Vue.js
Dart
CoffeeScript
TypeScript
Script#
ES6 (ECMAScript 6 / ES2015 / ESNext)
1. let (block scoped)
2. const
3. array destructuring
4. rest operator (array)
5. spread operator (array)
6. object destructuring
7. rest operator (object)
8. spread operator (object)
9. default arguments
10. arrow functions
11. object construction enhancement
12. Promise
13. Async Await
ES5
function addEven(){
var evenNumbers = Array.prototype.filter.call(arguments, function(no){
return no % 2 === 0;
})
var result = 0;
for (var i=0; i < evenNumbers.length ; i++){
result += evenNumbers[i]
}
return result;
}
in ES6
function addEven(...nos){
return nos.filter(no => no % 2 === 0)
.reduce((result, evenNo) => result + evenNo, 0)
}
const addEven = (...nos) => nos.filter(no => no % 2 === 0)
.reduce((result, evenNo) => result + evenNo, 0);
var products = [
{id : 6, name : 'Pen', cost : 50, units : 20, category : 'stationary'},
{id : 9, name : 'Ten', cost : 70, units : 70, category : 'stationary'},
{id : 3, name : 'Len', cost : 60, units : 60, category : 'grocery'},
{id : 5, name : 'Zen', cost : 30, units : 30, category : 'grocery'},
{id : 1, name : 'Ken', cost : 20, units : 80, category : 'utencil'}
];
1. Find the costliest product
products.reduce((result, product) => result.cost > product.cost ? result : product)
2. Find the product that has the least stock
products.reduce((result, product) => result.units < product.units ? result : product)
3. Group the products by category
products.reduce(
(result, product) => {
(result[product.category] = result[product.category] || []).push(product);
return result;
},
{}
)
Step: 1
1. filter
2. forEach
3. reduce
4. map
Step : 2
Functional Programming
Step : 3
underscore
lodash
Class
class Employee{
id = 0;
name = '';
salary = 0;
constructor(id, name, salary){
this.id = id;
this.name = name;
this.salary = salary;
}
display(){
console.log(this.id, this.name, this.salary);
}
}
var emp = new Employee(100, 'Magesh', 10000)
ES5
/*
function Employee(id, name, salary){
this.id = id;
this.name = name;
this.salary = salary;
this.display = function(){
console.log(this.id, this.name, this.salary);
}
}
*/
function Employee(id, name, salary){
this.id = id;
this.name = name;
this.salary = salary;
}
Employee.prototype.display = function(){
console.log(this.id, this.name, this.salary);
}
//class with private attributes & getters and setters
class Employee{
#id = 0;
#name = '';
#salary = 0;
get id(){
//console.log('getter for id triggered')
return this.#id;
}
set id(value){
//console.log('setter for id triggered')
//do validations
this.#id = value;
}
get name(){
//console.log('getter for name triggered')
return this.#name;
}
set name(value){
//console.log('setter for name triggered')
this.#name = value;
}
get salary(){
//console.log('getter for salary triggered')
return this.#salary;
}
set salary(value){
//console.log('setter for salary triggered')
this.#salary = value;
}
constructor(id, name, salary){
this.#id = id;
this.#name = name;
this.#salary = salary;
}
display(){
console.log(this.#id, this.#name, this.#salary);
}
}
//class Inheritance
class FulltimeEmployee extends Employee{
benefits = '';
constructor(id, name, salary, benefits){
super(id, name, salary);
this.benefits = benefits;
}
display(){
super.display();
console.log(this.benefits);
}
}
//iterators
var nos = [3,1,4,2,5]
for (let idx=0; idx < nos.length; idx++){
console.log(nos[idx]);
}
for (let no of nos){
console.log(no)
}
//custom iterators
function getFibonacci(count){
let fibonacci = {
[Symbol.iterator](){
let prev = 0, curr = 1;
let counter = 0;
return {
next(){
if (counter > count){
return { value : undefined, done : true }
} else {
[prev, curr, counter] = [curr, prev + curr, counter+1]
return { value : curr, done : false }
}
}
}
}
}
return fibonacci;
}
let fibonacci = getFibonacci(10)
//iterating using for..of construct
for (let fibNo of fibonacci){
console.log(fibNo)
}
//iterating manually
let iter = fibonacci[Symbol.iterator]();
iter.next();
iter.next();
Generators
===========
A function that can suspend and resume its execution at a later time.
function * genEvenNos(){
yield 2;
yield 4;
yield 6;
yield 8;
yield 10;
return;
}
var gen = genEvenNos()
gen.next()
.
.
OR
for (let evenNo of genEvenNos()){
console.log(evenNo)
}
Symbol
===========
A new data type
const Employee = (() => {
var idSymbol = Symbol('id')
class Employee{
name = '';
salary = 0;
constructor(id, name, salary){
this[idSymbol] = id;
this.name = name;
this.salary = salary;
}
display(){
console.log(this[idSymbol], this.name, this.salary);
}
}
return Employee;
})()
template strings
================
var x = 100, y = 200
var s2 = `Sum of ${x} and ${y} is ${x+y}`
var s3 = `Sum of
${x} and ${y}
is ${x+y}`
TypeScript = TypeSafety + JavaScript
superset of javascript
var x;
x = 10;
x = "hello";
x = true
x = {}
function add(x,y){
if (typeof x !== 'number' || typeof y !== 'number')
throw new Error('Invalid arguments');
return x + y;
}
add(10,20)
add("safd", "dsafs")
var x : number
function add(x : number, y : number) : number {
return x + y
}
add("dasfs", "asdf")
|
|
V
tsc
|
|
V
var x
function add(x , y ) {
return x + y
}
Enums
Interfaces
Classes
Access Modifiers (private, public, protected)
Modules
Namespaces
Generics
Challenges in UI application
- Performance
- Maintainability
- Security
Maintainability
- Fixing bugs
- Adding new features
- Improving existing features
- Removing dead features
- Changing existing code
Easy Maintainability means easy to change code
Reactive (Dumb) X Proactive (Knowledge)
Angular Building Blocks
- Module
Registry of application entities (components, directives, pipes & services)
Dependency Injection
Minimum of 1 module should be there
A module acts as the application starting point
- Component
Encapsulation of Presentation + UI Behavior (user interaction) + State (data)
Can be composed to create complext components
- Directive
Responsible for dealing with DOM
Two types of directives
- attribute directives
Manipulates the attributes of existing DOM nodes
DOES NOT change the structure of the DOM tree (adding new DOM nodes or removing existing DOM nodes)
enclosed with '[]'
- structural directives
Manipulates the structure of the DOM tree
Changes the structure of the DOM tree (adding new DOM nodes or removing existing DOM nodes)
prefixed with an '*'
- Pipe
Transforms data for presentation
- Service
Non UI logic
Angular CLI
- CLI is a command line interface for Angular
- Installation
npm install @angular/cli -g
- To create an angular application
ng new <app-name>
- To run the application
cd <app-name>
npm start
AMD
CommonJS
ES6 Modules
ES6 Modules
- Everything defined in a file is considered private
- Anything that need to be made public have to be explicitly "exported"
- If any public entity from another file is needed, they have to be explicitly "imported"
To create a component
ng generate component <component-name>
ng g c <component-name>
Implement the calculator component for the following
<h3>Calculator</h3>
<hr>
<input type="number" name="" id="">
<input type="number" name="" id="">
<br>
<input type="button" value="Add">
<input type="button" value="Subtract">
<input type="button" value="Multiply">
<input type="button" value="Divide">
<div></div>
Assignment
==========
implement salary calculator in the "first-app" application
Open Closed Principle
Your code should be OPEN for extension and CLOSED for modification
class PriceCalculator{
calculate(products) {
return products.reduce((total, product) => total + product.price, 0);
}
}
class FestivePriceCalculator{
calculate(products) {
return products.reduce((total, product) => total + product.price, 0) * 0.9;
}
}
class ShoppingCart{
products ;
constructor(pc PriceCalculator){
this.pc = pc;
}
cartValue(){
return this.pc.calculate(this.products)
}
}
To install moment.js
npm install moment
How to use the moment library (refer to the main.ts)
Pure Functions
Functions without side effects
When the Function invocation is replaced with the result of a function, the outcome should not change
Memoization
function isPrime(no){
if (no < 2) return false;
for (let i = 2; i < no; i++){
if (no % i === 0) return false;
}
return true;
}
isPrime(97)
Async Programming in JavaScript
Async Operation
An operation that is initiated but not waited for its completion
1. callbacks
2. Promises (singular)
3. Async/Await
4. Observables (stream)
Observable X Enumerable (aka Generator)
Enumerable = Iterable + lazy Evaluation
Event Driven Applications
Execution flow is determined by events (user actions etc)
Employee.dat
id, name, address, dob, salary, doj
Write a program that will calculate the average salary of all the employees
Pseudocode
let empCount, sumOfSalary
Open the file
while not eof
read the line
parse the line
empCount += 1
sumOfSalary += salary
avg = sumOfSalary / empCount
print avg
close the file
exit(0)
Rxjs
Observables
Web Browser
- User actions (stream)
- Ajax requests (singlular)
- Timers (stream)
- Server Sent Events (stream)
- Web Sockets (stream)
To create a restful server
run the following command from the folder that has the db.json file
npx json-server db.json
import { HttpClientModule } from '@angular/common/http';
regiter the HttpClientModule in app.module
To get all the bugs
this.httpClient
.get<Bug[]>('http://localhost:3000/bugs')
.subscribe(bugs => console.table(bugs))
For creating a new bug
this.httpClient
.post<Bug>('http://localhost:3000/bugs', bugData)
.subscribe(newBug => console.log(newBug)
For updating a bug
this.httpClient
.put<Bug>('http://localhost:3000/bugs/' + bug.id, bugData)
.subscribe(updatedBug => console.log(updatedBug))
For removing a bug
this.httpClient
.delete<Bug>('http://localhost:3000/bugs/' + bug.id)
.subscribe(() => console.log('Bug ' + bug.id + ' deleted'))
Routing
Change of State from one to another
Navigating from one page to another
Replacing one template with another template
https://www.amazon.in
https://www.amazon.in/electronics
https://www.amazon.in/electronics/mobile-phones
https://www.amazon.in/electronics/mobile-phones/google
https://www.amazon.in/electronics/mobile-phones/google/google-pixel-6
https://www.amazon.in/electronics/mobile-phones/google/google-pixel-6/reviews (possible)
Outlook
Outlook/Inbox
Outlook/Inbox/Projects
Outlook/Inbox/Projects/ExpenseManager
Outlook/Inbox/Projects/ExpenseManager/BugReports
Outlook/Inbox/Projects/ExpenseManager/BugReports/Bug-101 (not possible)
Bookmarking
myapp.com/products
myapp.com/calculator
myapp.com/salary-calculator
Angular 2.0 (Routing V.3.0)
Angular 4.0 (Routing V.4.0)
Assignment:
Add the "projects module" to the application
product = { id, name, description}
Modify the 'bugs' module to associate each bug with a project
when the visits the applcation
display the project list with the 'bugs' link for each project
When the bugs link is clicked display the bugs for the respective project
Route Guard
- A guard is a function that is called before the route is activated
CanActivate
TODO:
Implement a login screen with server communication
Forms
Template Forms
Driven by the template
data binding in the template
validation rules in the template
Reactive Forms
- Code driven
- Easy to automate the testing
Functionality
- data binding
- validation
- form submission
- form control state change
- user changed the data in the control
- user set the focus on the control or not
- valid or invalid
-ngPristine (Data in the control is not updated by the user)
-ngDirty (Data in the control is updated by the user)
-ngTouched (User visited the control))
-ngUntouched (User did not visit the control)
-ngValid
-ngInvalid
Testing
=======
Organizing tests
describe (xdescribe, fdescribe)
describe a feature of the application
calculator - feature
add - feature
it (xit, fit)
describe a specific behavior of the feature (test case)
calculator
add
adding two positive numbers
adding two negative numbers
adding one positive and one negative number
adding one positive number with zero
adding one negative number with zero
beforeEach()
executed before each test case
afterEach()
executed after each test case
Perform assertions
expect()
expect(actualResult).toBe(expectedResult)
expect(actualResult).toEqual(expectedResult)
expect(spy).toHaveBeenCalledWith(args)
expect(spy).toHaveBeenCalledTimes(1)
matchers
toBe()
toEqual()
toBeTruthy()
toBeFalsy()
toBeNull()
toBeDefined()
toBeUndefined()
toBeGreaterThan()
toBeLessThan()
toContain()
toBeCloseTo()
toHaveBeenCalled()
toHaveBeenCalledWith()
toHaveBeenCalledTimes()
etc
Mock the dependencies
jasmine.createSpy()
spyOn()
Alternatives
Mocha
describe, it, beforeEach, afterEach
choice of assertion library
chai.js
expect.js
assert.js
should.js
choice of mock library
sinon.js
choice of test reporters
jest.js
Assert.isEqual(actualResult, expectedResult)
actualResult.should.be.equal(expectedResult)
actualResult.should.not.be.equal(expectedResult)
TO DO:
Update the greeter component to use the greeter service
Update the component tests accordingly
State
- UI State
Data that supports the presentation needs of the application
It is highly UNLIKELY that this data is needed throughout the application
Feel free to maintain this data in the component
- Application State
Data that supports the core logic of the application
It is highly LIKELY that this data is needed throughout the application
DO NOT maintain this data in the component
State manager
Store
The whole application state is maintained in the store
The single source of truth for the application
Reducer
A function that decides the next state of the store based on the action
The reducer function has to be a pure function
The reducer function should not have side effects
The reducer function should not mutate the state
Any state changes has to happen in an immutable way
Action
An object that contains the information about the action
An action object should be unique throughout the application
An action object will carry the supporting data needed to process the action by the reducer
Action Creator
A function that encapsulates the logic for creating an action object
ngRx
npm install @ngrx/store