Forráskód Böngészése

5.11: defining and using default and rest parameters.

Frederic G. MARAND 5 éve
szülő
commit
baaa6b3d43
1 módosított fájl, 111 hozzáadás és 40 törlés
  1. 111 40
      app/app.ts

+ 111 - 40
app/app.ts

@@ -4,42 +4,46 @@ enum Category {
   Fiction,       // 2
 }
 
+let books: ({ author: string; available: boolean; category: Category; id: number; title: string })[] = [];
+
 function GetAllBooks() {
-  let books = [
-    {
-      author: 'James Joyce',
-      available: true,
-      category: Category.Fiction,
-      id: 1,
-      title: 'Ulysses',
-    },
-    {
-      author: 'Ernest Hemingway',
-      available: false,
-      category: Category.Fiction,
-      id: 2,
-      title: 'A farewall to arms',
-    },
-    {
-      author: 'Maya Angelou',
-      available: true,
-      category: Category.Poetry,
-      id: 3,
-      title: 'I know why the caged bird sings',
-    },
-    {
-      author: 'Herman Melville',
-      available: true,
-      category: Category.Fiction,
-      id: 4,
-      title: 'Moby Dick',
-    }
-  ];
+  if (books.length < 4) {
+    books = [
+      {
+        author: 'James Joyce',
+        available: true,
+        category: Category.Fiction,
+        id: 1,
+        title: 'Ulysses',
+      },
+      {
+        author: 'Ernest Hemingway',
+        available: false,
+        category: Category.Fiction,
+        id: 2,
+        title: 'A farewall to arms',
+      },
+      {
+        author: 'Maya Angelou',
+        available: true,
+        category: Category.Poetry,
+        id: 3,
+        title: 'I know why the caged bird sings',
+      },
+      {
+        author: 'Herman Melville',
+        available: true,
+        category: Category.Fiction,
+        id: 4,
+        title: 'Moby Dick',
+      }
+    ];
+  }
 
   return books;
 }
 
-function LogFirstAvailable(books): void {
+function LogFirstAvailable(books = GetAllBooks()): void {
 
   const numberOfBooks: number = books.length;
   let firstAvailable: string = '';
@@ -54,11 +58,35 @@ function LogFirstAvailable(books): void {
   console.log(`First available: ${firstAvailable}.`);
 }
 
+function CheckoutBooks(customer: string, ...bookIds: number[]): string[] {
+  console.log(`Checking out books for ${customer}.`);
+
+  let booksCheckedOut: string[] = [];
+  for (let id of bookIds) {
+    let book = GetBookById(id);
+    if (book.available) {
+      booksCheckedOut.push(book.title);
+    }
+  }
+
+  return booksCheckedOut;
+}
+
+function CreateCustomer(name: string, age?: number, city?: string) {
+  console.log(`Name: ${name}.`);
+  if (age) {
+    console.log(`Age: ${age}.`);
+  }
+  if (city) {
+    console.log(`City: ${city}.`);
+  }
+}
+
 function CreateCustomerId(name: string, id: number): string {
   return name + id;
 }
 
-function GetBookTitlesByCategory(categoryFilter: Category): Array<string> {
+function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): Array<string> {
   console.log(`Getting books in category: ${Category[categoryFilter]}.`);
 
   const allBooks = GetAllBooks();
@@ -84,16 +112,59 @@ function LogBookTitles(titles: string[]): void {
 }
 
 //******************************************************************************
-let x: number;
-let idGenerator: (chars: string, nums: number) => string;
-idGenerator = CreateCustomerId;
+function defaultParamsDemo() {
+  CreateCustomer('Michelle');
+  CreateCustomer('Leigh', 6);
+  CreateCustomer('Marie', 12, 'Atlanta');
+}
 
-let myID: string = idGenerator('Daniel', 10);
-console.log(myID);
+function functionTypeDemos() {
+  let idGenerator: (chars: string, nums: number) => string;
+  idGenerator = CreateCustomerId;
 
-idGenerator = (name, id) => id + name;
-myID = idGenerator('Daniel', 10);
-console.log(myID);
+  let myID: string = idGenerator('Daniel', 10);
+  console.log(myID);
+
+  idGenerator = (name, id) => id + name;
+  myID = idGenerator('Daniel', 10);
+  console.log(myID);
+}
 
 // const fictionBookTitles = GetBookTitlesByCategory(Category.Fiction);
 // fictionBookTitles.forEach((val, idx, arr) => console.log(`${++idx} ${val}.`));
+// let poetryBookTitles = GetBookTitlesByCategory();
+// console.log(poetryBookTitles);
+
+function functionCallDefaultParamsDemo() {
+  console.log("Book count:", books.length);
+  books.unshift({
+    author: "Anon1",
+    available: true,
+    category: Category.Biography,
+    id: 42,
+    title: "Added1",
+  });
+  console.log("Book count:", books.length);
+  LogFirstAvailable();
+  books.unshift({
+    author: "Anon2",
+    available: true,
+    category: Category.Biography,
+    id: 42,
+    title: "Added2",
+  });
+  console.log("Book count:", books.length);
+  // Default is now 5: it is evaluated on each call.
+  LogFirstAvailable();
+}
+
+function restParamsDemo() {
+  let myBooks = CheckoutBooks('Ripper', 1, 2, 3, 4);
+  myBooks.forEach(title => console.log(title);
+}
+
+
+// defaultParamsDemo();
+// functionTypeDemos();
+// functionCallDefaultParamsDemo();
+restParamsDemo();