Browse Source

8.5: using namespaces in practice.

Frederic G. MARAND 5 years ago
parent
commit
086d581f38
8 changed files with 81 additions and 53 deletions
  1. 4 3
      Makefile
  2. 13 0
      Namespacedemo/app.ts
  3. 0 0
      Namespacedemo/membership.ts
  4. 21 0
      Namespacedemo/utilityfunctions.ts
  5. 41 41
      app/app.ts
  6. 0 6
      app/app_ns.ts
  7. 1 2
      app/tsconfig.json
  8. 1 1
      package.json

+ 4 - 3
Makefile

@@ -1,10 +1,11 @@
-all: js/app.js js/app_ns.js
+all: js/app.js js/app_ns.es3.js
 
 js/app.js: app/*.ts
 	yarn compile
+	node js/app.js
 
-js/app_ns.js: app/membership.ts app/app_ns.ts
-	npx tsc --outFile js/app_ns.js app/app_ns.ts
+js/app_ns.js: Namespacedemo/membership.ts Namespacedemo/app.ts Makefile
+	npx tsc --target ES2015 --outFile js/app_ns.js Namespacedemo/app.ts
 	node js/app_ns.js
 
 clean:

+ 13 - 0
Namespacedemo/app.ts

@@ -0,0 +1,13 @@
+/// <reference path="./membership.ts" />
+/// <reference path="utilityfunctions.ts" />
+
+// Notice the difference with ES2015 module import.
+import util = Utility.Fees;
+
+let memberName = 'Elaine';
+let memberNumber = 789;
+Membership.AddMember(memberName);
+Membership.Cards.IssueCard(memberNumber);
+
+let fee = util.CalculateLateFees(10);
+console.log(`Late fee: ${fee}`);

+ 0 - 0
app/membership.ts → Namespacedemo/membership.ts


+ 21 - 0
Namespacedemo/utilityfunctions.ts

@@ -0,0 +1,21 @@
+namespace Utility {
+  export namespace Fees {
+    export function CalculateLateFees(daysLate: number): number {
+      return daysLate * 0.25;
+    }
+  }
+
+  export function MaxBooksAllowed(age: number): number {
+    if (age < 12) {
+      return 3;
+    }
+    else {
+      return 10;
+    }
+  }
+
+  // Since it is not exported, it will be private to the namespace.
+  function privateFunc(): void {
+    console.log('This is private...');
+  }
+}

+ 41 - 41
app/app.ts

@@ -1,13 +1,11 @@
 import {Book, DamageLogger, Librarian} from './interfaces';
 import {Category} from "./enums";
 import {
-  Encyclopedia,
-  Journal,
   ReferenceItem,
   UniversityLibrarian
 } from "./classes";
 
-function GetAllBooks(): Book[] {
+export function GetAllBooks(): Book[] {
   const books = [
     {
       author: 'James Joyce',
@@ -42,7 +40,7 @@ function GetAllBooks(): Book[] {
   return books;
 }
 
-function LogFirstAvailable(books = GetAllBooks()): void {
+export function LogFirstAvailable(books = GetAllBooks()): void {
 
   const numberOfBooks: number = books.length;
   let firstAvailable: string = '';
@@ -57,7 +55,7 @@ function LogFirstAvailable(books = GetAllBooks()): void {
   console.log(`First available: ${firstAvailable}.`);
 }
 
-function CheckoutBooks(customer: string, ...bookIds: number[]): string[] {
+export function CheckoutBooks(customer: string, ...bookIds: number[]): string[] {
   console.log(`Checking out books for ${customer}.`);
 
   let booksCheckedOut: string[] = [];
@@ -71,7 +69,7 @@ function CheckoutBooks(customer: string, ...bookIds: number[]): string[] {
   return booksCheckedOut;
 }
 
-function CreateCustomer(name: string, age?: number, city?: string) {
+export function CreateCustomer(name: string, age?: number, city?: string) {
   console.log(`Name: ${name}.`);
   if (age) {
     console.log(`Age: ${age}.`);
@@ -81,11 +79,11 @@ function CreateCustomer(name: string, age?: number, city?: string) {
   }
 }
 
-function CreateCustomerId(chars: string, nums: number): string {
+export function CreateCustomerId(chars: string, nums: number): string {
   return chars + nums;
 }
 
-function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): Array<string> {
+export function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): Array<string> {
   console.log(`Getting books in category: ${Category[categoryFilter]}.`);
 
   const allBooks = GetAllBooks();
@@ -99,22 +97,22 @@ function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): A
   return filteredTitles;
 }
 
-function GetBookById(id: number): Book {
+export function GetBookById(id: number): Book {
   const allBooks = GetAllBooks();
   return allBooks.filter(book => book.id === id)[0];
 }
 
-function LogBookTitles(titles: string[]): void {
+export function LogBookTitles(titles: string[]): void {
   for (let title of titles) {
     console.log(title);
   }
 }
 
 // Note: no implementation.
-function GetTitles(author: string): string[];
-function GetTitles(available: boolean): string[];
+export function GetTitles(author: string): string[];
+export function GetTitles(available: boolean): string[];
 // Now an implementation:
-function GetTitles(bookProperty: any): string[] {
+export function GetTitles(bookProperty: any): string[] {
   const allBooks = GetAllBooks();
 
   const foundTitles: Array<string> = [];
@@ -141,40 +139,41 @@ function GetTitles(bookProperty: any): string[] {
   return foundTitles;
 }
 
-function PrintBook(book: Book): void {
+export function PrintBook(book: Book): void {
   console.log(`${book.title} by ${book.author}.`);
 }
 
 //******************************************************************************
 
-let myBook: Book = {
-  author: 'Jane Austen',
-  available: true,
-  category: Category.Fiction,
-  // copies: 3,
-  id: 5,
-  pages: 250,
-  title: 'Pride and prejudice',
-  // year: '1813',
-  markDamaged: reason => console.log(`Damaged: ${reason}.`),
-};
-
-// PrintBook(myBook);
-// myBook.markDamaged('Torn back cover');
-
-// let logDamage: DamageLogger = reason => console.log(`Damage reported: ${reason}.`);
-// logDamage('Coffee stains');
-
-// let favoriteLibrarian: Librarian = new UniversityLibrarian();
-// favoriteLibrarian.name = 'Sharon';
-// favoriteLibrarian.assistCustomer('Lynda');
-
-// let ref: ReferenceItem = new ReferenceItem('Facts and Figures', 2016);
-// ref.publisher = 'Random Data publisher';
-// ref.printItem();
-// console.log(ref.publisher);
+function bookDemo() {
+  let myBook: Book = {
+    author: 'Jane Austen',
+    available: true,
+    category: Category.Fiction,
+    // copies: 3,
+    id: 5,
+    pages: 250,
+    title: 'Pride and prejudice',
+    // year: '1813',
+    markDamaged: reason => console.log(`Damaged: ${reason}.`),
+  };
+
+  PrintBook(myBook);
+  myBook.markDamaged!('Torn back cover');
+  let logDamage: DamageLogger = reason => console.log(`Damage reported: ${reason}.`);
+  logDamage('Coffee stains');
+}
 
 function classDemo() {
+  let favoriteLibrarian: Librarian = new UniversityLibrarian();
+  favoriteLibrarian.name = 'Sharon';
+  favoriteLibrarian.assistCustomer('Lynda');
+
+  // let ref: ReferenceItem = new ReferenceItem('Facts and Figures', 2016);
+  // ref.publisher = 'Random Data publisher';
+  // ref.printItem();
+  // console.log(ref.publisher);
+
   let Newspaper = class extends ReferenceItem {
     printCitation(): void {
       console.log(`Newspaper: ${this.title}.`);
@@ -193,4 +192,5 @@ function classDemo() {
   console.log(favoriteNovel);
 }
 
-// classDemo();
+false && bookDemo();
+classDemo();

+ 0 - 6
app/app_ns.ts

@@ -1,6 +0,0 @@
-/// <reference path="membership.ts" />
-
-let memberName = 'Elaine';
-let memberNumber = 789;
-Membership.AddMember(memberName);
-Membership.Cards.IssueCard(memberNumber);

+ 1 - 2
app/tsconfig.json

@@ -11,7 +11,6 @@
     "removeComments": true
   },
   "files": [
-    "app.ts",
-    "app_ns.ts"
+    "app.ts"
   ]
 }

+ 1 - 1
package.json

@@ -16,7 +16,7 @@
   "name": "Pluralsight-GettingStartedWithTypeScript",
   "scripts": {
     "clean": "rm -fr js/* all/*.js",
-    "compile": "rm -fr js/* && cd app && npx tsc",
+    "compile": "rm -fr js/* && cd app && npx tsc -p tsconfig.json",
     "start": "http-server",
     "watch": "rm -fr js/* && cd app && npx tsc --watch"
   },