├── Chapter05 ├── LLNode.txt ├── arraySum.txt ├── count.txt ├── getFeed.txt ├── binarySearch.txt ├── skipOdd.txt ├── createOrReplace.txt ├── traverse.txt ├── createOrReplace2.txt ├── MathOperations.txt ├── filterList.txt ├── reverse.txt ├── tests.txt └── LinkedList.txt ├── Chapter03 ├── declarationmerging │ ├── Questions │ │ ├── namespacewithdeclr.txt │ │ ├── multipleinterface.txt │ │ ├── namespacewithdeclr2.txt │ │ └── multiplenamepace.txt │ └── Answers │ │ ├── ans3.txt │ │ ├── ans4.txt │ │ ├── ans1.txt │ │ ├── ans2.txt │ │ └── ans5.txt ├── Mixin │ └── Tree.txt └── Arrowfunc │ └── Treenode.txt ├── Chapter08 ├── Gulp │ └── gulptask.txt └── MSBuild │ └── project.txt ├── Chapter04 ├── Event loop │ ├── feedFetchCompleted.txt │ ├── feedQuery.txt │ ├── loadHomeScreen_Sybchronous.txt │ ├── loadHomeScreen_Asynchronous.txt │ ├── getFeed_NeverReturns.txt │ ├── FetchMultipleFeeds.txt │ └── FeedCategory.txt ├── Synchronous fetch │ └── synchronous.txt ├── Asynchronous fetch │ └── asynchrnous.txt └── Asyncawait │ ├── PromiseFunction.txt │ └── asyncawait.txt ├── .gitattributes ├── Chapter02 ├── Namespaces │ ├── file2.ts │ ├── file3.ts │ └── file1.ts └── VariableDeclrtn │ └── vardeclr.txt ├── Chapter01 ├── Operators │ ├── logical.txt │ └── compoundassignment.txt ├── Classes and Interfaces │ ├── FeedRenderer.txt │ └── SNFG.txt ├── Strings │ ├── Concatenation.txt │ ├── Replacement.txt │ └── Literals.txt ├── Loop │ ├── forloop.txt │ └── forinloop.txt └── Array │ └── Sorting.txt ├── Chapter06 └── critical rendering path │ ├── Application1.txt │ └── index.html ├── .gitignore ├── Chapter07 ├── memoryprof.txt └── sortalgrthm.txt ├── LICENSE └── README.md /Chapter05/LLNode.txt: -------------------------------------------------------------------------------- 1 | // Linked List Node 2 | class LLNode { 3 | public data: number; 4 | public next: LLNode; 5 | 6 | constructor(data: number) { 7 | this.data = data; 8 | this.next = null; 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter05/arraySum.txt: -------------------------------------------------------------------------------- 1 | public arraySum(arr: number[]): number { 2 | if (!arr || arr.length == 0) return 0; 3 | let result: number = 0; 4 | for (let i: number = 0; i < arr.length; i++) 5 | result += arr[i]; 6 | return result; 7 | } -------------------------------------------------------------------------------- /Chapter05/count.txt: -------------------------------------------------------------------------------- 1 | // returns the number of elements in the linked list 2 | public count(): number { 3 | let temp: LLNode = this.head; 4 | let count: number = 0; 5 | while(temp != null) { 6 | temp = temp.next; 7 | count++; 8 | } 9 | return count; 10 | } -------------------------------------------------------------------------------- /Chapter03/declarationmerging/Questions/namespacewithdeclr.txt: -------------------------------------------------------------------------------- 1 | namespace TreeNode { 2 | export let node: TreeNode = TreeNode.AVLTreeNode; 3 | let id: number = 54; 4 | } 5 | 6 | enum TreeNode { 7 | RedBlackTreeNode, 8 | AVLTreeNode, 9 | TrieNode, 10 | SuffixTreeNode 11 | } -------------------------------------------------------------------------------- /Chapter05/getFeed.txt: -------------------------------------------------------------------------------- 1 | public getFeed(feedCategory: FeedCategory): IUserFeed[] { 2 | const resultFeed: IUserFeed[] = []; 3 | this.fakeUserFeed.forEach((userFeed: IUserFeed) => { 4 | if (userFeed.feedCategory === feedCategory) 5 | resultFeed.push(userFeed); 6 | }); 7 | return resultFeed; 8 | } -------------------------------------------------------------------------------- /Chapter08/Gulp/gulptask.txt: -------------------------------------------------------------------------------- 1 | var gulp = require("gulp"); 2 | gulp.task("default", function () { 3 | var ts = require("gulp-typescript"); 4 | var tsResult = gulp.src("src/*.ts") 5 | .pipe(ts({ 6 | noImplicitAny: true, 7 | out: "output.js" 8 | })); 9 | return tsResult.js.pipe(gulp.dest("built/local")); 10 | }); -------------------------------------------------------------------------------- /Chapter03/declarationmerging/Answers/ans3.txt: -------------------------------------------------------------------------------- 1 | namespace TreeNode { 2 | export let node: TreeNode = TreeNode.AVLTreeNode; // refers 3 | to the enum 'TreeNode' 4 | let id: number = 54; 5 | } 6 | 7 | enum TreeNode { 8 | RedBlackTreeNode, 9 | AVLTreeNode, 10 | TrieNode, 11 | SuffixTreeNode 12 | } -------------------------------------------------------------------------------- /Chapter05/binarySearch.txt: -------------------------------------------------------------------------------- 1 | public binarySearch(arr: number[], key: number): number { 2 | if (key > arr[4]) { 3 | if (key > arr[7]) { 4 | if (key > arr[8]) { 5 | if (key == arr[9]) { 6 | return 9; 7 | } 8 | } 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter05/skipOdd.txt: -------------------------------------------------------------------------------- 1 | public skipOdd(): void { 2 | if (!this.head) { 3 | return; 4 | } 5 | 6 | let temp: LLNode = this.head.next; 7 | this.head = temp; 8 | 9 | while (temp != null && temp.next != null) { 10 | temp.next = temp.next.next; 11 | temp = temp.next; 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter05/createOrReplace.txt: -------------------------------------------------------------------------------- 1 | // create or replace the linked list starting at 'head' 2 | public createOrReplace(input: number[]): void { 3 | this.head = new LLNode(input[0]); 4 | let temp: LLNode = this.head; 5 | 6 | for(let i: number = 1; i < input.length; i++) { 7 | temp.next = new LLNode(input[i]); 8 | temp = temp.next; 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter05/traverse.txt: -------------------------------------------------------------------------------- 1 | // returns a string representation of all the elements in the 2 | linked list starting from head 3 | public traverse(): string { 4 | let temp: LLNode = this.head; 5 | let result: string = ''; 6 | while(temp != null) { 7 | result+=temp.data + ','; 8 | temp = temp.next; 9 | } 10 | return result; 11 | } -------------------------------------------------------------------------------- /Chapter04/Event loop/feedFetchCompleted.txt: -------------------------------------------------------------------------------- 1 | const feedFetchCompleted: ICallback = (result: IUserFeed[], 2 | err: Error): void => { 3 | console.log('Callback called!', Date.now()-baseStartTime); 4 | if (err) { 5 | console.log('Error fetching feed: ', err); 6 | } else { 7 | console.log('Succesfully fetched feed of length: ', 8 | result.length); 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter04/Event loop/feedQuery.txt: -------------------------------------------------------------------------------- 1 | const feedQuery: FeedQuery = new FeedQuery(); 2 | const baseStartTime: number = Date.now(); 3 | console.log('Fetching Text Feed - ', Date.now() - 4 | baseStartTime); 5 | feedQuery.getFeed(FeedCategory.Text, feedFetchCompleted); 6 | console.log('Fetching Audio Feed', Date.now() - 7 | baseStartTime); 8 | feedQuery.getFeed(FeedCategory.Audio, feedFetchCompleted); -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /Chapter02/Namespaces/file2.ts: -------------------------------------------------------------------------------- 1 | namespace TSCafe { 2 | export class BreakRoomCafe extends Cafe implements ICafe { 3 | makeSelection(selection: CoffeeSelections): void { 4 | if (selection === CoffeeSelections.Macchiato) { 5 | console.log('Sorry, this selection is not available at 6 | Break Room!'); 7 | } else { 8 | super.makeSelection(selection); 9 | } 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter05/createOrReplace2.txt: -------------------------------------------------------------------------------- 1 | public createOrReplace(input: number[]): void { 2 | if (!input || input.length == 0) { 3 | this.head = null; 4 | return; 5 | } 6 | 7 | this.head = new LLNode(input[0]); 8 | let temp: LLNode = this.head; 9 | 10 | for(let i: number = 1; i < input.length; i++) { 11 | temp.next = new LLNode(input[i]); 12 | temp = temp.next; 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter05/MathOperations.txt: -------------------------------------------------------------------------------- 1 | class MathOperations { 2 | public square(x: number): number { 3 | return x * x; 4 | } 5 | 6 | public add(x: number, y: number): number { 7 | return x + y; 8 | } 9 | 10 | public square(x: string): number { 11 | let num: number = Number(x); 12 | if (!x) { 13 | return -1; 14 | } 15 | return num * num; 16 | } 17 | } -------------------------------------------------------------------------------- /Chapter01/Operators/logical.txt: -------------------------------------------------------------------------------- 1 | const condition: boolean = x > 0 && x < 10; // simple 2 | condition AND 3 | 4 | this.instanceVariable && this.instanceVariable.DoOperation(); 5 | /* AND used to check for non-null instance variable, and then 6 | perform the operation on that instance. */ 7 | 8 | // This could also be written as 9 | 10 | if (this.instanceVariable) { 11 | this.instanceVariable.DoOperation(); 12 | } -------------------------------------------------------------------------------- /Chapter05/filterList.txt: -------------------------------------------------------------------------------- 1 | public filterList(maxValue: number): LinkedList { 2 | let temp: LLNode = this.head; 3 | const arr: number[] = []; 4 | 5 | while(temp != null) { 6 | if (temp.data > maxValue) break; // return 7 | arr.push(temp.data); 8 | temp = temp.next; 9 | } 10 | const resultList: LinkedList = new LinkedList(); 11 | resultList.createOrReplace(arr); 12 | return resultList; 13 | } -------------------------------------------------------------------------------- /Chapter02/Namespaces/file3.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | const receptionCafe: TSCafe.ReceptionCafe = new 5 | TSCafe.ReceptionCafe(); 6 | receptionCafe.pay(TSCafe.PaymentOptions.Credit); 7 | 8 | const breakRoomCafe: TSCafe.BreakRoomCafe = new 9 | TSCafe.BreakRoomCafe(); 10 | breakRoomCafe.pay(TSCafe.PaymentOptions.Debit); 11 | breakRoomCafe.makeSelection(TSCafe.CoffeeSelections.Macchiato); 12 | breakRoomCafe.makeSelection(TSCafe.CoffeeSelections.Pune); 13 | breakRoomCafe.dispense(); -------------------------------------------------------------------------------- /Chapter03/declarationmerging/Answers/ans4.txt: -------------------------------------------------------------------------------- 1 | class TreeNode { 2 | private root: TreeNode = TreeNode.node; 3 | // private id: number = TreeNode.id; // error: 'id' not 4 | exported and thus cannot be accessed here 5 | private data: number; 6 | 7 | constructor(data: number) { 8 | this.data = data; 9 | } 10 | } 11 | 12 | // re-ordered namespace to follow after the class declaration 13 | namespace TreeNode { 14 | export let node: TreeNode = new TreeNode(10); 15 | let id: number = 54; 16 | } -------------------------------------------------------------------------------- /Chapter03/declarationmerging/Answers/ans1.txt: -------------------------------------------------------------------------------- 1 | interface TreeNode { 2 | data: number; 3 | left: TreeNode; 4 | right: TreeNode; 5 | createNode: (data: number) => TreeNode; 6 | } 7 | 8 | interface ITreeOperations {// root: TreeNode; 9 | // error: same name member declarations 10 | cannot merge 11 | mirrorTree: (node: TreeNode) => TreeNode; 12 | putRoot(nodeValue: number): void; 13 | getRoot(): TreeNode; 14 | traverseTree: (node: TreeNode) => void; 15 | putRoot(node: TreeNode): void; 16 | getRoot(): TreeNode; 17 | } -------------------------------------------------------------------------------- /Chapter03/declarationmerging/Questions/multipleinterface.txt: -------------------------------------------------------------------------------- 1 | interface TreeNode { 2 | data: number; 3 | left: TreeNode; 4 | right: TreeNode; 5 | createNode: (data: number) => TreeNode; 6 | } 7 | 8 | interface ITreeOperations { 9 | root: TreeNode; 10 | traverseTree: (node: TreeNode) => void; 11 | putRoot(node: TreeNode): void; 12 | getRoot(): TreeNode; 13 | } 14 | 15 | interface ITreeOperations { 16 | root: TreeNode; 17 | mirrorTree: (node: TreeNode) => TreeNode; 18 | putRoot(nodeValue: number): void; 19 | getRoot(): TreeNode; 20 | } -------------------------------------------------------------------------------- /Chapter05/reverse.txt: -------------------------------------------------------------------------------- 1 | // reverse the linked list and returns the head of the 2 | reversed list 3 | public reverse(): LLNode { 4 | let prev: LLNode = null; 5 | let curr: LLNode = this.head; 6 | let next: LLNode; 7 | 8 | // start traversing from the head 9 | while (curr != null) 10 | { 11 | next = curr.next; 12 | curr.next = prev; // flip the next pointer of the current 13 | node 14 | prev = curr; // current node will become the next node's 15 | previous 16 | curr = next; // move to the next node 17 | } 18 | return prev; 19 | } -------------------------------------------------------------------------------- /Chapter04/Synchronous fetch/synchronous.txt: -------------------------------------------------------------------------------- 1 | const loadHomeScreen_Synchronous = (startTime: number) => { 2 | // fetch public feed synchronously, takes around ~10s 3 | for (let i = 0; i < 5000000000; i++) {} 4 | console.log('Fetched Profile Info - ', Date.now() - startTime); 5 | console.log('Fetched Friends Feed - ', Date.now() - startTime); 6 | console.log('Fetched and Loaded Notifications - ', Date.now() - 7 | startTime); 8 | console.log('Fetched and Loaded Friend Suggestions - ', 9 | Date.now() - startTime); 10 | console.log('Fetched Public Feed - ', Date.now() - startTime); 11 | } 12 | 13 | // Synchronous Data Fetch 14 | loadHomeScreen_Synchronous(Date.now()); -------------------------------------------------------------------------------- /Chapter04/Event loop/loadHomeScreen_Sybchronous.txt: -------------------------------------------------------------------------------- 1 | const loadHomeScreen_Synchronous = (startTime: number) => { 2 | // fetch public feed synchronously, takes around ~10s 3 | for (let i = 0; i < 5000000000; i++) {} 4 | console.log('Fetched Public Feed - ', Date.now() - 5 | startTime); 6 | console.log('Fetched Profile Info - ', Date.now() - 7 | startTime); 8 | console.log('Fetched Friends Feed - ', Date.now() - 9 | startTime); 10 | console.log('Fetched and Loaded Notifications - ', 11 | Date.now() - startTime); 12 | console.log('Fetched and Loaded Friend Suggestions - ', 13 | Date.now() - startTime); 14 | } -------------------------------------------------------------------------------- /Chapter04/Asynchronous fetch/asynchrnous.txt: -------------------------------------------------------------------------------- 1 | const loadHomeScreen_Asynchronous = (startTime: number) => { 2 | // fetch public feed asynchronously, takes around ~10s 3 | setTimeout(() => { 4 | console.log('Fetched Public Feed - ', Date.now() - startTime); 5 | }, 10000); 6 | console.log('Fetched Profile Info - ', Date.now() - startTime); 7 | console.log('Fetched Friends Feed - ', Date.now() - startTime); 8 | console.log('Fetched and Loaded Notifications - ', Date.now() - 9 | startTime); 10 | console.log('Fetched and Loaded Friend Suggestions - ', 11 | Date.now() - startTime); 12 | } 13 | 14 | // Asynchronous Data Fetch 15 | loadHomeScreen_Asynchronous(Date.now()); -------------------------------------------------------------------------------- /Chapter04/Event loop/loadHomeScreen_Asynchronous.txt: -------------------------------------------------------------------------------- 1 | const loadHomeScreen_Asynchronous = (startTime: number) => { 2 | // fetch public feed asynchronously, takes around ~10s 3 | setTimeout(() => { 4 | console.log('Fetched Public Feed - ', Date.now() - 5 | startTime); 6 | }, 10000); 7 | console.log('Fetched Profile Info - ', Date.now() - 8 | startTime); 9 | console.log('Fetched Friends Feed - ', Date.now() - 10 | startTime); 11 | console.log('Fetched and Loaded Notifications - ', 12 | Date.now() - startTime); 13 | console.log('Fetched and Loaded Friend Suggestions - ', 14 | Date.now() - startTime); 15 | } -------------------------------------------------------------------------------- /Chapter04/Asyncawait/PromiseFunction.txt: -------------------------------------------------------------------------------- 1 | function PromiseFunction(): Promise { 2 | return new Promise((resolve, reject) => { 3 | setTimeout(() => { 4 | resolve("Hello World"); 5 | }, 5000); 6 | }); 7 | } 8 | 9 | // Function prefixed with the "async" keyword 10 | async function AsyncFunction() { 11 | const result: string = await PromiseFunction(); 12 | console.log('Asynchronous work completes - ', Date.now() - 13 | baseStartTime); 14 | console.log('Result: ', result); 15 | } 16 | 17 | console.log('Asynchronous work starts - ', Date.now() - 18 | baseStartTime); 19 | AsyncFunction(); 20 | console.log('Other work continues - ', Date.now() - 21 | baseStartTime); -------------------------------------------------------------------------------- /Chapter04/Event loop/getFeed_NeverReturns.txt: -------------------------------------------------------------------------------- 1 | public getFeed_NeverReturns(feedCategory: FeedCategory, callback: 2 | ICallback): void { 3 | console.log(FeedCategory[feedCategory] + ' fetch begins', 4 | Date.now() - baseStartTime); 5 | 6 | // simulating an asynchronous network request 7 | setTimeout(() => { 8 | let resultFeed: IUserFeed[] = []; 9 | this.fakeUserFeed.forEach((userFeed: IUserFeed) => { 10 | if (userFeed.feedCategory === feedCategory) { 11 | resultFeed.push(userFeed); 12 | } 13 | }); 14 | console.log('resultFeed is available to be returned but callback 15 | never called!'); 16 | }, 5000); 17 | } 18 | 19 | feedQuery.getFeed_NeverReturns(FeedCategory.Text, 20 | feedFetchCompleted); -------------------------------------------------------------------------------- /Chapter06/critical rendering path/Application1.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 |