-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple examples in Typescript (#102)
- Loading branch information
1 parent
dbe71ca
commit bfc7ac0
Showing
68 changed files
with
633 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
simple/typescript/consolidate-conditional-expression_after.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
disabilityAmount(): number { | ||
if (isNotEligableForDisability()) { | ||
return 0; | ||
} | ||
// compute the disability amount | ||
//... | ||
} |
13 changes: 13 additions & 0 deletions
13
simple/typescript/consolidate-conditional-expression_before.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
disabilityAmount(): number { | ||
if (seniority < 2) { | ||
return 0; | ||
} | ||
if (monthsDisabled > 12) { | ||
return 0; | ||
} | ||
if (isPartTime) { | ||
return 0; | ||
} | ||
// compute the disability amount | ||
//... | ||
} |
7 changes: 7 additions & 0 deletions
7
simple/typescript/consolidate-duplicate-conditional-fragments_after.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
if (isSpecialDeal()) { | ||
total = price * 0.95; | ||
} | ||
else { | ||
total = price * 0.98; | ||
} | ||
send(); |
8 changes: 8 additions & 0 deletions
8
simple/typescript/consolidate-duplicate-conditional-fragments_before.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
if (isSpecialDeal()) { | ||
total = price * 0.95; | ||
send(); | ||
} | ||
else { | ||
total = price * 0.98; | ||
send(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
if (isSummer(date)) { | ||
charge = summerCharge(quantity); | ||
} | ||
else { | ||
charge = winterCharge(quantity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
if (date.before(SUMMER_START) || date.after(SUMMER_END)) { | ||
charge = quantity * winterRate + winterServiceCharge; | ||
} | ||
else { | ||
charge = quantity * summerRate; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Person { | ||
private _name: string; | ||
|
||
get name() { | ||
return this._name; | ||
} | ||
setName(arg: string): void { | ||
this._name = arg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Person { | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Soldier { | ||
health: number; | ||
weapon: Weapon; | ||
attack(): void { | ||
//... | ||
} | ||
} | ||
|
||
class Weapon { | ||
damage: number; | ||
weaponStatus: number; | ||
getDamage(): number { | ||
//... | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Soldier { | ||
health: number; | ||
damage: number; | ||
weaponStatus: number; | ||
getDamage(): number { | ||
//... | ||
} | ||
attack(): void { | ||
//... | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
printOwing(): void { | ||
printBanner(); | ||
printDetails(getOutstanding()); | ||
} | ||
|
||
printDetails(outstanding: number): void { | ||
console.log('name: ' + name); | ||
console.log('amount: ' + outstanding); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
printOwing(): void { | ||
printBanner(); | ||
|
||
//print details | ||
console.log("name: " + name); | ||
console.log("amount: " + getOutstanding()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
printProperties(users: User[]): void { | ||
for (let user of users) { | ||
console.log(getProperties(user)); | ||
|
||
// ... | ||
} | ||
} | ||
|
||
getProperties(user: User): string { | ||
return user.getName() + ' ' + user.getAge(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
printProperties(users: User[]): void { | ||
for (let user of users) { | ||
let result = ''; | ||
result += user.getName(); | ||
result += ' '; | ||
result += user.getAge(); | ||
console.log(result); | ||
|
||
// ... | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Order { | ||
// ... | ||
|
||
calculateTotal():number { | ||
let total = 0; | ||
for (let product : getProducts()) { | ||
total += product.quantity * product.price; | ||
} | ||
total = applyRegionalDiscounts(total); | ||
return total; | ||
} | ||
|
||
applyRegionalDiscounts(total: number): number { | ||
let result = total; | ||
switch (user.getCountry()) { | ||
case 'US': result *= 0.85; break; | ||
case 'RU': result *= 0.75; break; | ||
case 'CN': result *= 0.9; break; | ||
// ... | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Order { | ||
// ... | ||
|
||
calculateTotal(): number { | ||
let total = 0; | ||
for (let product : getProducts()) { | ||
total += product.quantity * product.price; | ||
} | ||
|
||
// Apply regional discounts. | ||
switch (user.getCountry()) { | ||
case 'US': total *= 0.85; break; | ||
case 'RU': total *= 0.75; break; | ||
case 'CN': total *= 0.9; break; | ||
// ... | ||
} | ||
|
||
return total; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
renderBanner(): void { | ||
const isMacOs = platform.toUpperCase().indexOf("MAC") > -1; | ||
const isIE = browser.toUpperCase().indexOf("IE") > -1; | ||
const wasResized = resize > 0; | ||
|
||
if (isMacOs && isIE && wasInitialized() && wasResized) { | ||
// do something | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
renderBanner(): void { | ||
let isChanged = frame.isChanged || target.isChanged; | ||
let mustRedraw = isChanged || experiment.isRunning(); | ||
let isFullScreen = frame.getSize() == screen.getSize(); | ||
|
||
if (isFullScreen && mustRedraw) { | ||
// print banner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
renderBanner(): void { | ||
if ((platform.toUpperCase().indexOf("MAC") > -1) && | ||
(browser.toUpperCase().indexOf("IE") > -1) && | ||
wasInitialized() && resize > 0 ) | ||
{ | ||
// do something | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
renderBanner():void { | ||
// Render banner only if we're in fullscreen mode | ||
// and a change is requested either in frame or | ||
// target, or experiment is active. | ||
if (((frame.isChanged || target.isChanged) || | ||
experiment.isRunning()) && | ||
(frame.getSize() == screen.getSize())) { | ||
// print banner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class PizzaDelivery { | ||
//... | ||
getRating(): number { | ||
return numberOfLateDeliveries > 5 ? 2 : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class PizzaDelivery { | ||
//... | ||
getRating(): number { | ||
return moreThanFiveLateDeliveries() ? 2 : 1; | ||
} | ||
moreThanFiveLateDeliveries(): boolean { | ||
return numberOfLateDeliveries > 5; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hasDiscount(order: Order): boolean { | ||
return order.basePrice() > 1000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
hasDiscount(order: Order): boolean { | ||
let basePrice: number = order.basePrice(); | ||
return basePrice > 1000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
getExpenseLimit(): number { | ||
|
||
return (expenseLimit != NULL_EXPENSE) ? | ||
expenseLimit: | ||
primaryProject.getMemberExpenseLimit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
getExpenseLimit(): number { | ||
// should have either expense limit or a primary project | ||
return (expenseLimit != NULL_EXPENSE) ? | ||
expenseLimit: | ||
primaryProject.getMemberExpenseLimit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Report { | ||
//... | ||
sendReport() { | ||
let newStart: Date = nextDay(previousEnd); | ||
//... | ||
} | ||
private static nextDay(arg: Date): Date { | ||
return new Date(arg.getFullYear(), arg.getMonth(), arg.getDate() + 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Report { | ||
//... | ||
sendReport(): void { | ||
let nextDay: Date = new Date(previousEnd.getYear(), | ||
previousEnd.getMonth(), previousEnd.getDate() + 1); | ||
//... | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class NullCustomer extends Customer { | ||
isNull(): boolean { | ||
return true; | ||
} | ||
getPlan(): Plan { | ||
return new NullPlan(); | ||
} | ||
// Some other NULL functionality. | ||
} | ||
|
||
// Replace null values with Null-object. | ||
let customer = (order.customer != null) ? | ||
order.customer : new NullCustomer(); | ||
|
||
// Use Null-object as if it's normal subclass. | ||
plan = customer.getPlan(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
if (customer == null) { | ||
plan = BillingPlan.basic(); | ||
} | ||
else { | ||
plan = customer.getPlan(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Order { | ||
// ... | ||
|
||
calculateTotal(): number { | ||
// ... | ||
total = Discounts.applyRegionalDiscounts(total, user.getCountry()); | ||
total = Discounts.applyCoupons(total); | ||
// ... | ||
} | ||
|
||
|
||
class Discounts { | ||
// ... | ||
|
||
static applyRegionalDiscounts(total: number, country: string): number { | ||
let result = total; | ||
switch (country) { | ||
case 'US': result *= 0.85; break; | ||
case 'RU': result *= 0.75; break; | ||
case 'CN': result *= 0.9; break; | ||
// ... | ||
} | ||
return result; | ||
} | ||
|
||
static applyCoupons(total: number): number { | ||
// ... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Order { | ||
// ... | ||
|
||
calculateTotal(): number { | ||
let total = 0; | ||
for (let product : getProducts()) { | ||
total += product.quantity * product.price; | ||
} | ||
total = applyRegionalDiscounts(total); | ||
return total; | ||
} | ||
|
||
applyRegionalDiscounts(total: number): number { | ||
let result = total; | ||
switch (user.getCountry()) { | ||
case 'US': result *= 0.85; break; | ||
case 'RU': result *= 0.75; break; | ||
case 'CN': result *= 0.9; break; | ||
// ... | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let withinPlan = plan.withinRange(daysTempRange); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let low = daysTempRange.getLow(); | ||
let high = daysTempRange.getHigh(); | ||
let withinPlan = plan.withinRange(low, high); |
Oops, something went wrong.