Quellcode durchsuchen

9.5: creating and using generic functions.

Frederic G. MARAND vor 5 Jahren
Ursprung
Commit
5019296a14
3 geänderte Dateien mit 63 neuen und 7 gelöschten Zeilen
  1. 52 6
      app/app.ts
  2. 1 0
      app/enums.ts
  3. 10 1
      app/lib/utilityfunctions.ts

+ 52 - 6
app/app.ts

@@ -1,9 +1,12 @@
-import {Book, Logger as DamageLogger, Librarian} from './interfaces';
+import {Book, Librarian, Logger as DamageLogger} from './interfaces';
 import {Category} from './enums';
-import {Journal, ReferenceItem, UniversityLibrarian} from './classes';
-import { CalculateLateFees as CalcFee, MaxBooksAllowed }  from './lib/utilityfunctions';
+import {ReferenceItem, UniversityLibrarian} from './classes';
+import {
+  CalculateLateFees as CalcFee,
+  MaxBooksAllowed, Purge
+} from './lib/utilityfunctions';
 import refBook from './encyclopedia';
-import { LogAndReturn }from './generics';
+import {Books, LogAndReturn} from './generics';
 
 export function GetAllBooks(): Book[] {
   const books = [
@@ -209,13 +212,56 @@ function genericFunctionDemo() {
   interface Magazine { title: string }
 
   let someString: string = LogAndReturn<string>('log this');
-  console.log("Returned: ", someString);
+  console.log('Returned: ', someString);
   let newMag: Magazine = { title: 'Web dev monthly' };
   let someMag: Magazine = LogAndReturn<Magazine>(newMag);
   console.log('Returned mag', someMag);
 }
 
+function purgedemo() {
+  let inventory: Books = [
+    {
+      author: 'K & R',
+      available: true,
+      category: Category.Software,
+      id: 10,
+      title: 'The C programming language',
+    },
+    {
+      author: 'Steve McConnell',
+      available: true,
+      category: Category.Software,
+      id: 11,
+      title: 'Code Complete',
+    },
+    {
+      author: 'A. B.',
+      available: true,
+      category: Category.Software,
+      id: 12,
+      title: '8-Bit graphics with Cobol',
+    },
+    {
+      author: 'C. D.',
+      available: true,
+      category: Category.Software,
+      id: 13,
+      title: 'Cool autoexec.bat scripts',
+    }
+  ];
+
+  // Inventory is accepted because Books is an alias for Array<Book>, so the
+  // signature actually matches.
+  let purgedBooks: Books = Purge(inventory);
+  // Note that purgedBooks uses the aliases type after the assignment.
+  purgedBooks.forEach(book => console.log(book.title));
+
+  let purgedNums: Array<number> = Purge<number>([1, 2, 3, 4]);
+  console.log(purgedNums);
+}
+
 false && bookDemo();
 false && classDemo();
 false && importDemo();
-genericFunctionDemo();
+false && genericFunctionDemo();
+purgedemo();

+ 1 - 0
app/enums.ts

@@ -4,6 +4,7 @@ enum Category {
   Fiction,       // 2
   History,       // 3
   Children,      // 4
+  Software,
 }
 
 export {

+ 10 - 1
app/lib/utilityfunctions.ts

@@ -11,10 +11,19 @@ function MaxBooksAllowed(age: number): number {
   }
 }
 
+function Purge<T>(inventory: Array<T>): Array<T> {
+  // IRL, implement some fancy logic here.
+  return inventory.splice(2, inventory.length);
+}
+
 // Since it is not exported, it will be private to the namespace.
 function privateFunc(): void {
   console.log('This is private...');
 }
 
 false && privateFunc();
-export { CalculateLateFees, MaxBooksAllowed };
+export {
+  CalculateLateFees,
+  MaxBooksAllowed,
+  Purge,
+};