Skip to content

Commit

Permalink
Simple examples in Typescript (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
maykon-oliveira authored and neochief committed Oct 26, 2018
1 parent dbe71ca commit bfc7ac0
Show file tree
Hide file tree
Showing 68 changed files with 633 additions and 0 deletions.
7 changes: 7 additions & 0 deletions simple/typescript/consolidate-conditional-expression_after.ts
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 simple/typescript/consolidate-conditional-expression_before.ts
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
//...
}
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();
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();
}
6 changes: 6 additions & 0 deletions simple/typescript/decompose-conditional_after.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if (isSummer(date)) {
charge = summerCharge(quantity);
}
else {
charge = winterCharge(quantity);
}
6 changes: 6 additions & 0 deletions simple/typescript/decompose-conditional_before.ts
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;
}
10 changes: 10 additions & 0 deletions simple/typescript/encapsulate-field_after.ts
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;
}
}
3 changes: 3 additions & 0 deletions simple/typescript/encapsulate-field_before.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Person {
name: string;
}
15 changes: 15 additions & 0 deletions simple/typescript/extract-class_after.ts
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 {
//...
}
}
11 changes: 11 additions & 0 deletions simple/typescript/extract-class_before.ts
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 {
//...
}
}
9 changes: 9 additions & 0 deletions simple/typescript/extract-method_after.ts
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);
}
7 changes: 7 additions & 0 deletions simple/typescript/extract-method_before.ts
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());
}
11 changes: 11 additions & 0 deletions simple/typescript/extract-method_decompose-loop_after.ts
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();
}
11 changes: 11 additions & 0 deletions simple/typescript/extract-method_decompose-loop_before.ts
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);

// ...
}
}
23 changes: 23 additions & 0 deletions simple/typescript/extract-method_isolate-switch_after.ts
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;
}
}
20 changes: 20 additions & 0 deletions simple/typescript/extract-method_isolate-switch_before.ts
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;
}
}
9 changes: 9 additions & 0 deletions simple/typescript/extract-variable_after.ts
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
}
}
9 changes: 9 additions & 0 deletions simple/typescript/extract-variable_after2.ts
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
}
}
8 changes: 8 additions & 0 deletions simple/typescript/extract-variable_before.ts
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
}
}
10 changes: 10 additions & 0 deletions simple/typescript/extract-variable_before2.ts
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
}
}
6 changes: 6 additions & 0 deletions simple/typescript/inline-method_after.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PizzaDelivery {
//...
getRating(): number {
return numberOfLateDeliveries > 5 ? 2 : 1;
}
}
9 changes: 9 additions & 0 deletions simple/typescript/inline-method_before.ts
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;
}
}
3 changes: 3 additions & 0 deletions simple/typescript/inline-temp_after.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hasDiscount(order: Order): boolean {
return order.basePrice() > 1000;
}
4 changes: 4 additions & 0 deletions simple/typescript/inline-temp_before.ts
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;
}
6 changes: 6 additions & 0 deletions simple/typescript/introduce-assertion_after.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
getExpenseLimit(): number {

return (expenseLimit != NULL_EXPENSE) ?
expenseLimit:
primaryProject.getMemberExpenseLimit();
}
6 changes: 6 additions & 0 deletions simple/typescript/introduce-assertion_before.ts
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();
}
10 changes: 10 additions & 0 deletions simple/typescript/introduce-foreign-method_after.ts
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);
}
}
8 changes: 8 additions & 0 deletions simple/typescript/introduce-foreign-method_before.ts
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);
//...
}
}
16 changes: 16 additions & 0 deletions simple/typescript/introduce-null-object_after.ts
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();
6 changes: 6 additions & 0 deletions simple/typescript/introduce-null-object_before.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if (customer == null) {
plan = BillingPlan.basic();
}
else {
plan = customer.getPlan();
}
28 changes: 28 additions & 0 deletions simple/typescript/move-method_isolate-switch_after.ts
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 {
// ...
}
23 changes: 23 additions & 0 deletions simple/typescript/move-method_isolate-switch_before.ts
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;
}
}
1 change: 1 addition & 0 deletions simple/typescript/preserve-whole-object_after.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let withinPlan = plan.withinRange(daysTempRange);
3 changes: 3 additions & 0 deletions simple/typescript/preserve-whole-object_before.ts
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);
Loading

0 comments on commit bfc7ac0

Please sign in to comment.