Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring + add "Dafalgan" feature #15

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[*]
charset = utf-8

[*.{js, ts, tsx, json, md, css}]
indent_style = space
indent_size = 2
5 changes: 4 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
env:
jest: true
node: true
parser: babel-eslint
parser: "@typescript-eslint/parser"
plugins:
- "@typescript-eslint"
extends:
- eslint:recommended
- plugin:prettier/recommended
- plugin:@typescript-eslint/recommended
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-fix
yarn test
5 changes: 5 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn start
yarn golden
119 changes: 119 additions & 0 deletions drug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { Drug } from "./drug";

describe("Drug", () => {
it("should decrease the benefit and expiresIn", () => {
const drug = new Drug("test", 2, 2);
drug.updateBenefitValue();

expect(drug.expiresIn).toEqual(1);
expect(drug.benefit).toEqual(1);
});

it("should decrease the benefit as twice once expired", () => {
const drug = new Drug("test", 0, 5);
drug.updateBenefitValue();

expect(drug.expiresIn).toEqual(-1);
expect(drug.benefit).toEqual(3);
});

it("should not have negative benefit", () => {
const drug = new Drug("test", 0, 0);
drug.updateBenefitValue();

expect(drug.benefit).toEqual(0);
});
});

describe('"Herbal Tea" Drug', () => {
it("should increase the benefit", () => {
const drug = new Drug("Herbal Tea", 2, 2);
drug.updateBenefitValue();

expect(drug.expiresIn).toEqual(1);
expect(drug.benefit).toEqual(3);
});

it("should increase the benefit twice as fast once expired", () => {
const drug = new Drug("Herbal Tea", 0, 5);
drug.updateBenefitValue();

expect(drug.expiresIn).toEqual(-1);
expect(drug.benefit).toEqual(7);
});

it("should not have benefit more than 50", () => {
const drug = new Drug("Herbal Tea", 0, 50);
drug.updateBenefitValue();

expect(drug.benefit).toEqual(50);
});
});

describe('"Magic Pill" Drug', () => {
it("should not decrease the benefit", () => {
const drug = new Drug("Magic Pill", 2, 2);
drug.updateBenefitValue();

expect(drug.benefit).toEqual(2);
});

it("should never expires", () => {
const drug = new Drug("Magic Pill", 0, 5);
drug.updateBenefitValue();

expect(drug.expiresIn).toEqual(0);
});
});

describe('"Fervex" Drug', () => {
it("should increase the benefit by 1 ten days upper expiration date", () => {
const drug = new Drug("Fervex", 11, 2);
drug.updateBenefitValue();

expect(drug.benefit).toEqual(3);
});

it("should increase the benefit by 2 nine days under expiration date and five days upper expiration date", () => {
const drug = new Drug("Fervex", 8, 2);
drug.updateBenefitValue();

expect(drug.benefit).toEqual(4);
});

it("should increase the benefit by 3 four days under expiration date", () => {
const drug = new Drug("Fervex", 3, 2);
drug.updateBenefitValue();

expect(drug.benefit).toEqual(5);
});

it("should not have benefit more than 50", () => {
const drug = new Drug("Fervex", 1, 50);
drug.updateBenefitValue();

expect(drug.benefit).toEqual(50);
});

it("should lose its benefit once expired", () => {
const drug = new Drug("Fervex", 0, 50);
drug.updateBenefitValue();

expect(drug.benefit).toEqual(0);
});
});


describe('"Dafalgan" Drug', () => {
it("should decrease benefit as twice as fast as normal drugs", () => {
const dafalgan = new Drug("Dafalgan", 2, 3);
dafalgan.updateBenefitValue();

expect(dafalgan.benefit).toEqual(1);

const expiredDafalgan = new Drug("Dafalgan", 0, 5);
expiredDafalgan.updateBenefitValue();

expect(expiredDafalgan.benefit).toEqual(1);
});
});
127 changes: 127 additions & 0 deletions drug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const HERBAL_TEA = "Herbal Tea";
const FERVEX = "Fervex";
const MAGIC_PILL = "Magic Pill";
const DAFALGAN = "Dafalgan";

const MAX_BENEFIT = 50;
const DOUBLE_BENEFIT_UNDER_DAY_EXPIRATION = 10;
const TRIPLE_BENEFIT_UNDER_DAY_EXPIRATION = 5;

export class Drug {
constructor(
public name: string,
public expiresIn: number,
public benefit: number
) {}

updateBenefitValue(): void {
if (this.name === HERBAL_TEA) {
this.updateHerbalTeaBenefitValue();
} else if (this.name === FERVEX) {
this.updateFervexBenefitValue();
} else if (this.name === MAGIC_PILL) {
this.updateMagicPillBenefitValue();
} else if (this.name === DAFALGAN) {
this.updateDafalganBenefitValue();
} else {
this.updateBenefit(-1);
}

this.updateExpiration();

if (this.expiresIn < 0) {
this.updateExpiredBenefitValue();
}
}

private updateHerbalTeaBenefitValue(): void {
this.updateBenefit(1);
}

private updateFervexBenefitValue(): void {
if (this.expiresIn <= TRIPLE_BENEFIT_UNDER_DAY_EXPIRATION) {
this.updateBenefit(3);
} else if (this.expiresIn <= DOUBLE_BENEFIT_UNDER_DAY_EXPIRATION) {
this.updateBenefit(2);
} else {
this.updateBenefit(1);
}
}

private updateMagicPillBenefitValue(): void {
// Do nothing: "Magic pill" drug never lose benefit
}

private updateDafalganBenefitValue(): void {
this.updateBenefit(-2);
}

private updateExpiration(): void {
if (this.name === HERBAL_TEA) {
this.updateHerbalTeaExpiration();
} else if (this.name === FERVEX) {
this.updateFervexExpiration();
} else if (this.name === MAGIC_PILL) {
this.updateMagicPillExpiration();
} else if (this.name === DAFALGAN) {
this.updateDafalganExpiration();
} else {
this.expiresIn -= 1;
}
}

private updateHerbalTeaExpiration(): void {
this.expiresIn -= 1;
}

private updateFervexExpiration(): void {
this.expiresIn -= 1;
}

private updateMagicPillExpiration(): void {
// Do nothing: "Magic pill" drug never expires
}

private updateDafalganExpiration(): void {
this.expiresIn -= 1;
}

private updateExpiredBenefitValue(): void {
if (this.name === HERBAL_TEA) {
this.updateExpiredHerbalTeaBenefitValue();
} else if (this.name === FERVEX) {
this.updateExpiredFervexBenefitValue();
} else if (this.name === MAGIC_PILL) {
this.updateExpiredMagicPillBenefitValue();
} else if (this.name === DAFALGAN) {
this.updateExpiredDafalganBenefitValue();
} else {
this.updateBenefit(-1);
}
}

private updateExpiredHerbalTeaBenefitValue(): void {
this.updateBenefit(1);
}

private updateExpiredFervexBenefitValue(): void {
this.benefit = this.benefit - this.benefit;
}

private updateExpiredMagicPillBenefitValue(): void {
// Do nothing: "Magic pill" drug never lose benefit
}

private updateExpiredDafalganBenefitValue(): void {
this.updateBenefit(-2);
}

private updateBenefit(value: number): void {
const newBenefitValue = this.benefit + value;

this.benefit =
value < 0
? Math.max(0, newBenefitValue)
: Math.min(MAX_BENEFIT, newBenefitValue);
}
}
11 changes: 11 additions & 0 deletions golden.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "regenerator-runtime/runtime";
import { readFile } from "fs/promises";

describe("Golden master", () => {
it("should expect same app output", async () => {
const truth = (await readFile("./output_truth.txt")).toString();
const reality = (await readFile("./output.txt")).toString();

expect(reality).toEqual(truth);
});
});
10 changes: 6 additions & 4 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { Drug, Pharmacy } from "./pharmacy";
import fs from "fs";

import { Drug } from "./drug";
import { Pharmacy } from "./pharmacy";

const drugs = [
new Drug("Doliprane", 20, 30),
new Drug("Herbal Tea", 10, 5),
new Drug("Fervex", 5, 40),
new Drug("Magic Pill", 15, 40)
new Drug("Magic Pill", 15, 40),
];
const trial = new Pharmacy(drugs);

const log = [];
const log: string[] = [];

for (let elapsedDays = 0; elapsedDays < 30; elapsedDays++) {
log.push(JSON.stringify(trial.updateBenefitValue()));
}

/* eslint-disable no-console */
fs.writeFile("output.txt", log.toString(), err => {
fs.writeFile("output.txt", log.toString(), (err) => {
if (err) {
console.log("error");
} else {
Expand Down
8 changes: 8 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { InitialOptionsTsJest } from "ts-jest";

const config: InitialOptionsTsJest = {
preset: "ts-jest",
testEnvironment: "node",
};

export default config;
1 change: 1 addition & 0 deletions output_truth.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"Doliprane","expiresIn":19,"benefit":29},{"name":"Herbal Tea","expiresIn":9,"benefit":6},{"name":"Fervex","expiresIn":4,"benefit":43},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":18,"benefit":28},{"name":"Herbal Tea","expiresIn":8,"benefit":7},{"name":"Fervex","expiresIn":3,"benefit":46},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":17,"benefit":27},{"name":"Herbal Tea","expiresIn":7,"benefit":8},{"name":"Fervex","expiresIn":2,"benefit":49},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":16,"benefit":26},{"name":"Herbal Tea","expiresIn":6,"benefit":9},{"name":"Fervex","expiresIn":1,"benefit":50},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":15,"benefit":25},{"name":"Herbal Tea","expiresIn":5,"benefit":10},{"name":"Fervex","expiresIn":0,"benefit":50},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":14,"benefit":24},{"name":"Herbal Tea","expiresIn":4,"benefit":11},{"name":"Fervex","expiresIn":-1,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":13,"benefit":23},{"name":"Herbal Tea","expiresIn":3,"benefit":12},{"name":"Fervex","expiresIn":-2,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":12,"benefit":22},{"name":"Herbal Tea","expiresIn":2,"benefit":13},{"name":"Fervex","expiresIn":-3,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":11,"benefit":21},{"name":"Herbal Tea","expiresIn":1,"benefit":14},{"name":"Fervex","expiresIn":-4,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":10,"benefit":20},{"name":"Herbal Tea","expiresIn":0,"benefit":15},{"name":"Fervex","expiresIn":-5,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":9,"benefit":19},{"name":"Herbal Tea","expiresIn":-1,"benefit":17},{"name":"Fervex","expiresIn":-6,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":8,"benefit":18},{"name":"Herbal Tea","expiresIn":-2,"benefit":19},{"name":"Fervex","expiresIn":-7,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":7,"benefit":17},{"name":"Herbal Tea","expiresIn":-3,"benefit":21},{"name":"Fervex","expiresIn":-8,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":6,"benefit":16},{"name":"Herbal Tea","expiresIn":-4,"benefit":23},{"name":"Fervex","expiresIn":-9,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":5,"benefit":15},{"name":"Herbal Tea","expiresIn":-5,"benefit":25},{"name":"Fervex","expiresIn":-10,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":4,"benefit":14},{"name":"Herbal Tea","expiresIn":-6,"benefit":27},{"name":"Fervex","expiresIn":-11,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":3,"benefit":13},{"name":"Herbal Tea","expiresIn":-7,"benefit":29},{"name":"Fervex","expiresIn":-12,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":2,"benefit":12},{"name":"Herbal Tea","expiresIn":-8,"benefit":31},{"name":"Fervex","expiresIn":-13,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":1,"benefit":11},{"name":"Herbal Tea","expiresIn":-9,"benefit":33},{"name":"Fervex","expiresIn":-14,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":0,"benefit":10},{"name":"Herbal Tea","expiresIn":-10,"benefit":35},{"name":"Fervex","expiresIn":-15,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-1,"benefit":8},{"name":"Herbal Tea","expiresIn":-11,"benefit":37},{"name":"Fervex","expiresIn":-16,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-2,"benefit":6},{"name":"Herbal Tea","expiresIn":-12,"benefit":39},{"name":"Fervex","expiresIn":-17,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-3,"benefit":4},{"name":"Herbal Tea","expiresIn":-13,"benefit":41},{"name":"Fervex","expiresIn":-18,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-4,"benefit":2},{"name":"Herbal Tea","expiresIn":-14,"benefit":43},{"name":"Fervex","expiresIn":-19,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-5,"benefit":0},{"name":"Herbal Tea","expiresIn":-15,"benefit":45},{"name":"Fervex","expiresIn":-20,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-6,"benefit":0},{"name":"Herbal Tea","expiresIn":-16,"benefit":47},{"name":"Fervex","expiresIn":-21,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-7,"benefit":0},{"name":"Herbal Tea","expiresIn":-17,"benefit":49},{"name":"Fervex","expiresIn":-22,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-8,"benefit":0},{"name":"Herbal Tea","expiresIn":-18,"benefit":50},{"name":"Fervex","expiresIn":-23,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-9,"benefit":0},{"name":"Herbal Tea","expiresIn":-19,"benefit":50},{"name":"Fervex","expiresIn":-24,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}],[{"name":"Doliprane","expiresIn":-10,"benefit":0},{"name":"Herbal Tea","expiresIn":-20,"benefit":50},{"name":"Fervex","expiresIn":-25,"benefit":0},{"name":"Magic Pill","expiresIn":15,"benefit":40}]
33 changes: 19 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@
"author": "inato",
"license": "MIT",
"private": true,
"main": "index.ts",
"engines": {
"yarn": ">=1.7.0",
"node": "=16.x"
},
"scripts": {
"lint": "eslint .",
"lint-fix": "yarn lint --fix",
"test": "jest",
"start": "babel-node index.js"
"golden": "jest -- golden.test.ts",
"start": "ts-node index.ts",
"postinstall": "husky install"
},
"devDependencies": {
"@babel/cli": "^7.1.0",
"@babel/core": "^7.1.0",
"@babel/node": "^7.0.0",
"@babel/preset-env": "^7.1.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^9.0.0",
"babel-jest": "^27.5.1",
"eslint": "^8.9.0",
"eslint-config-prettier": "^3.1.0",
"eslint-plugin-prettier": "^2.6.2",
"jest": "24.8.0",
"prettier": "^1.14.3",
"regenerator-runtime": "^0.12.1"
"@types/jest": "27.4.1",
"@types/node": "17.0.23",
"@typescript-eslint/parser": "5.18.0",
"@typescript-eslint/eslint-plugin": "5.18.0",
"eslint": "8.13.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.0.0",
"husky": "7.0.4",
"jest": "27.5.1",
"prettier": "2.6.2",
"regenerator-runtime": "0.12.1",
"typescript": "4.6.3",
"ts-jest": "27.1.4",
"ts-node": "10.7.0"
}
}
Loading