-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
212 lines (169 loc) · 5.78 KB
/
app.ts
File metadata and controls
212 lines (169 loc) · 5.78 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
import {
Category,
ChildrensBook,
CLASS_INFO,
ElectronicBook,
Employee,
IBook,
IKeyValuePair,
ILibrarian,
IMagazine,
PublicLibrarian,
Researcher,
UniversityLibrarian,
Util,
} from "./";
import Encyclopedia from "./src/asset-management/encyclopedia";
type Frequency = "monthly" | "annually";
type PrintMaterial = IBook | IMagazine;
type Serial = IBook & IMagazine;
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach((baseCtor) => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
}
function PrintBookInfo({title: selectTitle, author: selectAuthor}: IBook): void {
console.log(`${selectTitle} was authored by ${selectAuthor}`);
}
function LogFavoriteBooks([firstBook, secondBook, ...others]: IBook[]) {
PrintBookInfo(firstBook);
PrintBookInfo(secondBook);
for (const book of others) {
PrintBookInfo(book);
}
}
function PrintTitle(material: PrintMaterial): void {
console.log(material.title);
}
function GetMagazineByFrequency(preferredFrequency: Frequency) {
// do something here.
}
function logVisitor(param: number | string) {
if (typeof param === "number") {
console.log (`${param} new visitors arrived. `);
} else {
console.log(`${param.toUpperCase()} visited.`);
}
}
console.log("Welcome to Advanced TypeScript!");
applyMixins(UniversityLibrarian, [ Employee, Researcher ]);
const [book1, book2] = Util.GetAllBooks();
LogFavoriteBooks(Util.GetAllBooks());
const {title: booktitle, author: bookauthor} = book1;
console.log(booktitle);
console.log(bookauthor);
PrintBookInfo(book1);
const schoolBooks: IBook[] = [
{ id: 10, title: "The Great Gatsby", author: " F. Scott Fitzgerald", available: true, category: Category.Fiction },
{ id: 11, title: "Crime and Punishment", author: "Fyodor Dostoevsky", available: true, category: Category.Fiction },
{ id: 12, title: "Clear Light of Day", author: "Anita Desai", available: true, category: Category.Fiction },
];
const booksRead: IBook[] = Util.GetAllBooks();
booksRead.push(...schoolBooks);
console.log(booksRead);
const poets: string[] = [ "Shelley", "Collins", "Hughes" ];
const authors: string[] = [ "Tolstoy", "Fitzgerald", ...poets ];
console.log(authors);
const catalogLocation: IKeyValuePair<string, IBook> = [ "A 123.456", book1 ];
const allBooks: IBook[] = Util.GetAllBooks();
const allMagazines: IMagazine[] = Util.GetAllMagazines();
const readingMaterial: PrintMaterial = allMagazines[0];
PrintTitle(readingMaterial);
const serialNovel: Serial = {
author: "Occasional Pen",
available: true,
category: Category.Fiction,
id: 100,
publisher: "Serial Press",
title: "The Gradual Tale",
};
const newLibrarian = new UniversityLibrarian();
newLibrarian.doResearch("Economics");
newLibrarian.hostSeminar("TypeScript and NodeJS");
// fluent API using polymorphic this
const eBook: ElectronicBook = new ElectronicBook();
const kidbook: ChildrensBook = new ChildrensBook();
kidbook.Checkin()
.Clean()
.Checkout();
eBook.Checkin()
.RemoveFromCustomerDevice()
.Checkout();
logVisitor(5);
logVisitor("Leigh Ann");
const lib: ILibrarian = new PublicLibrarian();
if (lib instanceof UniversityLibrarian) {
lib.assistFaculty();
}
if (lib instanceof PublicLibrarian) {
lib.teachCommunity();
}
const uvLib: ILibrarian = new UniversityLibrarian();
if (lib instanceof UniversityLibrarian) {
lib.assistFaculty();
}
const customTypeChecking: IBook | IMagazine = Util.randomReadingMaterialGenerator(0, 50);
if (Util.isBook(customTypeChecking)) {
console.log(`The book's author is ${customTypeChecking.author}.`);
} else {
console.log(`The magazine's publisher is ${customTypeChecking.publisher}.`);
}
const item = new Encyclopedia("Encyclopdia Britanica", 2018, 2);
item.printItem();
const mySymbol = Symbol("first_symbol");
const anotherSymbol = Symbol("first_symbol");
// console.log(mySymbol === anotherSymbol);
console.log(typeof mySymbol);
const myObject = {
[mySymbol]: "value for my symbol key",
};
console.log(myObject[mySymbol].toString());
const univLibrarian = new UniversityLibrarian();
univLibrarian.name = "Martha";
univLibrarian[CLASS_INFO]();
const libraryCustomer = {
Name: "Thorne",
assistFaculty: (facultyName: string) => {
console.log(`${name} is assisting ${facultyName}.`);
},
};
if (libraryCustomer instanceof UniversityLibrarian) {
console.log("A helpful librarian.");
} else {
console.log("Not a librarian.");
}
if (univLibrarian instanceof UniversityLibrarian) {
console.log("A helpful librarian.");
} else {
console.log("Not a librarian.");
}
try {
libraryCustomer.assistFaculty = () => console.log("assistFaculty replacement method");
(lib as PublicLibrarian).teachCommunity = () => console.log("teachCommunicty replacement method");
} catch (error) {
console.log(error.message);
}
libraryCustomer.assistFaculty("John");
(lib as PublicLibrarian).teachCommunity();
console.log("Beginning callback search...");
Util.getBooksByCategoryAsync(Category.Biography, Util.logCategorySearchAsync);
console.log("Search submitted...");
console.log("Beginning promises search...");
Util.getBooksByCategoryPromise(Category.Biography)
.then((titles) => {
Util.logCategorySearch(titles);
return titles.length;
}, (reason) => 0)
.then((numberOfBooks) => {
console.log(`Number of Books found: ${numberOfBooks}.`);
})
.catch((reason) => {
console.log(`Error Message: ${reason}`);
});
console.log("Search submitted...");
console.log("Beginning async/await search...");
Util.getBooksByCategory(Category.Fiction)
.catch((reason) => { console.log(reason); });
console.log("Search submitted...");