diff --git "a/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.txt" "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.txt" new file mode 100644 index 0000000..01c8a8a --- /dev/null +++ "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.txt" @@ -0,0 +1,17 @@ +๐Ÿฒ +I have a dog +undefined +No animal +No animal name +No animal gender +Lucy is a female dog +[] +[ 'banana', 'pineapple' ] +unknown +unknown +cabbage +Fiesta +Some Street Name +undefined +true +undefined diff --git "a/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.txt" "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.txt" new file mode 100644 index 0000000..9196a91 --- /dev/null +++ "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.txt" @@ -0,0 +1,11 @@ +USER PHONE: +Country code Phone : +34 +Phone area code: 635 +Phone base number: 538 973 + +USER DATA: +User name: Fernando +User lastname: Aparicio Galende +User DNI: 12345678S +User phone: fernando.aparicio@guidesmiths.com +User email: +34 635 538 973 diff --git "a/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.txt" "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.txt" new file mode 100644 index 0000000..a6dbf46 --- /dev/null +++ "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.txt" @@ -0,0 +1,7 @@ +Order: 1 + Client: shirts + Product: shirts + TotalAmount: NaN + + + Arrival in: 5 days. diff --git "a/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.txt" "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.txt" new file mode 100644 index 0000000..01c8a8a --- /dev/null +++ "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.txt" @@ -0,0 +1,17 @@ +๐Ÿฒ +I have a dog +undefined +No animal +No animal name +No animal gender +Lucy is a female dog +[] +[ 'banana', 'pineapple' ] +unknown +unknown +cabbage +Fiesta +Some Street Name +undefined +true +undefined diff --git "a/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.txt" "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.txt" new file mode 100644 index 0000000..9196a91 --- /dev/null +++ "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.txt" @@ -0,0 +1,11 @@ +USER PHONE: +Country code Phone : +34 +Phone area code: 635 +Phone base number: 538 973 + +USER DATA: +User name: Fernando +User lastname: Aparicio Galende +User DNI: 12345678S +User phone: fernando.aparicio@guidesmiths.com +User email: +34 635 538 973 diff --git "a/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.txt" "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.txt" new file mode 100644 index 0000000..188d009 --- /dev/null +++ "b/src/refactoring/siny/outputTxt/\354\210\230\354\240\225\355\233\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.txt" @@ -0,0 +1,7 @@ +Order: 1 + Client: siny + Product: shirts + TotalAmount: 94 + + + Arrival in: 5 days. diff --git a/src/refactoring/siny/refactor1.test.js b/src/refactoring/siny/refactor1.test.js new file mode 100644 index 0000000..6b3eff4 --- /dev/null +++ b/src/refactoring/siny/refactor1.test.js @@ -0,0 +1,65 @@ +const execSync = require('child_process').execSync; +const fs = require('fs'); +const { consoleTestUtils } = require('./testUtils.js'); + +const { Client, Product, Order, Summary } = require('./์ˆ˜์ •ํ›„-03_๋ถ€์ ์ ˆํ•œ_ํ‰๊ฐ€'); +const beforeData = { + inputDir: 'src/refactoring', + scripts: '์ˆ˜์ •์ „-01_์กฐ๊ฑด๋ถ€_๋ณต์žก์„ฑ.js', + outputDir: 'src/refactoring/siny/outputTxt', +}; +const afterData = { + inputDir: 'src/refactoring/siny', + scripts: '์ˆ˜์ •ํ›„-01_์กฐ๊ฑด๋ถ€_๋ณต์žก์„ฑ.js', + outputDir: 'src/refactoring/siny/outputTxt', +}; + +const before1 = consoleTestUtils(beforeData.scripts, beforeData.inputDir, beforeData.outputDir); + +const after1 = consoleTestUtils(afterData.scripts, afterData.inputDir, afterData.outputDir); + +describe('tests', () => { + beforeAll(() => { + execSync(before1.execSyncArgsForCreateTxtFormConsoleLog()); + execSync(after1.execSyncArgsForCreateTxtFormConsoleLog()); + }); + + test('01_์กฐ๊ฑด๋ถ€_๋ณต์žก์„ฑ', () => { + const beforeTxt = fs.readFileSync(...before1.readOutputTxtArgs); + const afterTxt = fs.readFileSync(...after1.readOutputTxtArgs); + expect(beforeTxt).toEqual(afterTxt); + }); + // ๋ฌด์—‡์„ ์›ํ•˜๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค...ใ… ใ…  + // test('02_๊ธฐ๋Šฅ_ํŠน์ •ํ•˜๊ธฐ', () => { + // const beforeTxt = fs.readFileSync(...before2.readOutputTxtArgs); + // const afterTxt = fs.readFileSync(...after2.readOutputTxtArgs); + // expect(beforeTxt).toEqual(afterTxt); + // }); + + test('03_๋ถ€์ ์ ˆํ•œ_ํ‰๊ฐ€', () => { + const client1 = new Client({ + name: 'siny', + type: 'premium', + location: 'USA', + }); + const product1 = new Product({ + value: 100, + name: 'shirts', + shipping: 5, + }); + const order1 = new Order({ + id: '1', + value: '???', + product: product1, + client: client1, + }); + + const summary = new Summary({ order: order1 }); + + expect(summary.printSummary()).toEqual(`Order: 1 + Client: siny + Product: shirts + TotalAmount: 94 + Arrival in: 5 days.`); + }); +}); diff --git a/src/refactoring/siny/testUtils.js b/src/refactoring/siny/testUtils.js new file mode 100644 index 0000000..f13cb98 --- /dev/null +++ b/src/refactoring/siny/testUtils.js @@ -0,0 +1,18 @@ +const path = require('path'); +const fs = require('fs'); + +const consoleTestUtils = (script, inputDir = './', outputDir = './') => { + return { + execSyncArgsForCreateTxtFormConsoleLog: () => { + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir); + } + return `node ${path.join(inputDir, script)} > ${path.join(outputDir, script.replace('.js', ''))}.txt`; + }, + readOutputTxtArgs: [path.join(outputDir, `${script.replace('.js', '')}.txt`), 'utf-8'], + }; +}; + +module.exports = { + consoleTestUtils, +}; diff --git a/src/refactoring/siny/utils.js b/src/refactoring/siny/utils.js index e69de29..c8e6c68 100644 --- a/src/refactoring/siny/utils.js +++ b/src/refactoring/siny/utils.js @@ -0,0 +1,38 @@ +/** + * map + * */ +const map = (fn, arr) => { + const newArr = []; + for (let i = 0; i < arr.length; i++) { + newArr.push(fn(arr[i], i, [...arr])); + } + return newArr; +}; + +/** + * filter + * */ +const filter = (condition, arr) => { + const newArr = []; + for (let i = 0; i < arr.length; i++) { + if (condition(arr[i], i, [...arr])) newArr.push(arr[i]); + } + return newArr; +}; + +/** + * reduce + * */ +const reduce = (fn, init, arr) => { + let newValue = init; + for (let i = 0; i < arr.length; i++) { + newValue = fn(newValue, arr[i], i, [...arr]); + } + return newValue; +}; + +module.exports = { + map, + filter, + reduce, +}; diff --git a/src/refactoring/siny/utils.test.js b/src/refactoring/siny/utils.test.js new file mode 100644 index 0000000..676f96f --- /dev/null +++ b/src/refactoring/siny/utils.test.js @@ -0,0 +1,44 @@ +const { map, filter, reduce } = require('./utils'); + +describe('utils', () => { + describe('map', () => { + it('๋นˆ๋ฐฐ์—ด์„ ๋ฐ›์œผ๋ฉด ๋นˆ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', () => { + expect(map(item => item, [])).toEqual([]); + }); + it('๋นˆ๋ฐฐ์—ด์„ ๋ฐ›์œผ๋ฉด ๋ฐ˜ํ™˜ํ•œ ๋ฐฐ์—ด๊ณผ input ๋ฐฐ์—ด์€ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค.', () => { + const arr = []; + expect(map(item => item, arr)).not.toBe(arr); + }); + it('fn=(item)=>({no:item}),arr=[1,2,3] ์„ ์ธ์ž๋กœ ํ•˜๋ฉด [{no:1},{no:2},{no:3}] ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', () => { + const arr = [1, 2, 3]; + const fn = item => ({ no: item }); + expect(map(fn, arr)).toEqual([{ no: 1 }, { no: 2 }, { no: 3 }]); + }); + }); + describe('filter', () => { + it('๋นˆ๋ฐฐ์—ด์„ ๋ฐ›์œผ๋ฉด ๋นˆ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', () => { + expect(filter(() => true, [])).toEqual([]); + }); + it('๋นˆ๋ฐฐ์—ด์„ ๋ฐ›์œผ๋ฉด ๋ฐ˜ํ™˜ํ•œ ๋ฐฐ์—ด๊ณผ input ๋ฐฐ์—ด์€ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค.', () => { + const arr = []; + expect(filter(() => true, arr)).not.toBe(arr); + }); + it('condition item => item % 2,arr=[1,2,3] ์„ ์ธ์ž๋กœ ํ•˜๋ฉด [1, 3] ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', () => { + const arr = [1, 2, 3]; + const odd = item => item % 2; + expect(filter(odd, arr)).toEqual([1, 3]); + }); + }); + describe('reduce', () => { + it('fn:(acc,cur)=>acc+cur,init:0,arr=[1,2,3] ์„ ์ธ์ž๋กœ ํ•˜๋ฉด 6 ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', () => { + const arr = [1, 2, 3]; + const sum = (acc, cur) => acc + cur; + expect(reduce(sum, 0, arr)).toEqual(6); + }); + it('fn:(acc,cur)=>({...acc,[crr]:crr}),init:{}, arr=[1,2,3] ์„ ์ธ์ž๋กœ ํ•˜๋ฉด {1:1,2:2,3:3} ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', () => { + const arr = [1, 2, 3]; + const ToObject = (acc, cur) => ({ ...acc, [cur]: cur }); + expect(reduce(ToObject, {}, arr)).toEqual({ 1: 1, 2: 2, 3: 3 }); + }); + }); +}); diff --git "a/src/refactoring/siny/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" "b/src/refactoring/siny/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" deleted file mode 100644 index 840e72c..0000000 --- "a/src/refactoring/siny/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" +++ /dev/null @@ -1,84 +0,0 @@ -const per2Int = (value, per) => (value * per) / 100; - -const Client = ({ name, type, location }) => { - this.offers = { - normal: 0, - premium: 20, - }; - this.name = name; - this.type = type; - this.location = location; - - getName = () => this.name; - getType = () => this.name; - getLocation = () => this.location; - getPriceByProduct = product => product.value - per2Int(product.value, this.offers[this.type]); - - return { - getName, - getType, - getLocation, - getPriceByProduct, - }; -}; - -const Product = ({ value, name, shipping }) => { - this.value = value; - this.name = name; - this.shipping = shipping; - - getValue = () => this.value; - getProductName = () => this.name; - getShipping = () => this.shipping; - - return { - getValue, - getProductName, - getShipping, - }; -}; - -const Order = ({ id, value, client, product }) => { - this.taxes = { - EU: 21, - USA: 14, - }; - this.id = id; - this.value = value; - this.client = client; - this.product = product; - - getId = () => this.id; - getValue = () => this.value; - getClient = () => this.client; - getProduct = () => this.product; - getTaxes = loc => this.getTaxes(this.taxes[loc]); - - return { - getId, - getValue, - getClient, - getProduct, - }; -}; - -const Summary = ({ order }) => { - this.order = order; - - printSummary = () => { - let client = order.getClient(); - let product = order.product(); - - return `Order: ${order.getId()} - Client: ${client.getName()} - Product: ${product.getProductName()} - TotalAmount: ${client.getPriceByProduct(product) + this.order.getTaxes(client.getLocation())} - - - Arrival in: ${this.order.product.getShipping()} days.`; - }; - - return { - printSummary, - }; -}; diff --git "a/src/refactoring/siny/\354\210\230\354\240\225\354\240\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.js" "b/src/refactoring/siny/\354\210\230\354\240\225\355\233\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.js" similarity index 55% rename from "src/refactoring/siny/\354\210\230\354\240\225\354\240\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.js" rename to "src/refactoring/siny/\354\210\230\354\240\225\355\233\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.js" index 8ba4a1d..a11bf98 100644 --- "a/src/refactoring/siny/\354\210\230\354\240\225\354\240\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.js" +++ "b/src/refactoring/siny/\354\210\230\354\240\225\355\233\204-01_\354\241\260\352\261\264\353\266\200_\353\263\265\354\236\241\354\204\261.js" @@ -1,70 +1,57 @@ -const getAnimalEmoji = animal => { - if (animal === 'dog') { - return '๐Ÿถ'; - } else if (animal === 'cat') { - return '๐Ÿฑ'; - } else if (animal === 'frog') { - return '๐Ÿธ'; - } else if (animal === 'panda') { - return '๐Ÿผ'; - } else if (animal === 'giraffe') { - return '๐Ÿฆ’'; - } else if (animal === 'monkey') { - return '๐Ÿต'; - } else if (animal === 'unicorn') { - return '๐Ÿฆ„'; - } else if (animal === 'dragon') { - return '๐Ÿฒ'; - } +const ANIMAL_MAP = { + dog: '๐Ÿถ', + cat: '๐Ÿฑ', + frog: '๐Ÿธ', + panda: '๐Ÿผ', + giraffe: '๐Ÿฆ’', + monkey: '๐Ÿต', + unicorn: '๐Ÿฆ„', + dragon: '๐Ÿฒ', }; + +const getAnimalEmoji = animal => ANIMAL_MAP[animal]; + console.log(getAnimalEmoji('dragon')); -const printMyAnimal = animal => { - if (animal === 'dog' || animal === 'cat') { +const myAnimals = ['dog', 'cat']; +const hasIn = (arr, item) => arr.includes(item); +const printMyAnimal = myAnimals => animal => { + if (hasIn(myAnimals, animal)) { console.log(`I have a ${animal}`); } }; -console.log(printMyAnimal('dog')); +console.log(printMyAnimal(myAnimals)('dog')); const getAnimalDetails = animal => { - let result; - - if (animal) { - if (animal.type) { - if (animal.name) { - if (animal.gender) { - result = `${animal.name} is a ${animal.gender} ${animal.type}`; - } else { - result = 'No animal gender'; - } - } else { - result = 'No animal name'; - } - } else { - result = 'No animal type'; - } - } else { - result = 'No animal'; + if (!animal) { + return 'No animal'; } - - return result; + if (!animal.type) { + return 'No animal type'; + } + if (!animal.name) { + return 'No animal name'; + } + if (!animal.gender) { + return 'No animal gender'; + } + return `${animal.name} is a ${animal.gender} ${animal.type}`; }; console.log(getAnimalDetails()); console.log(getAnimalDetails({ type: 'dog', gender: 'female' })); console.log(getAnimalDetails({ type: 'dog', name: 'Lucy' })); console.log(getAnimalDetails({ type: 'dog', name: 'Lucy', gender: 'female' })); +const COLOR_MAP = { + red: ['apple', 'strawberry'], + yellow: ['banana', 'pineapple'], + purple: ['grape', 'plum'], +}; + +COLOR_MAP.default = []; + const printFruits = color => { - switch (color) { - case 'red': - return ['apple', 'strawberry']; - case 'yellow': - return ['banana', 'pineapple']; - case 'purple': - return ['grape', 'plum']; - default: - return []; - } + return COLOR_MAP[color] ?? COLOR_MAP.default; }; console.log(printFruits(null)); console.log(printFruits('yellow')); diff --git "a/src/refactoring/siny/\354\210\230\354\240\225\354\240\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.js" "b/src/refactoring/siny/\354\210\230\354\240\225\355\233\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.js" similarity index 100% rename from "src/refactoring/siny/\354\210\230\354\240\225\354\240\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.js" rename to "src/refactoring/siny/\354\210\230\354\240\225\355\233\204-02_\352\270\260\353\212\245_\355\212\271\354\240\225\355\225\230\352\270\260.js" diff --git "a/src/refactoring/siny/\354\210\230\354\240\225\355\233\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" "b/src/refactoring/siny/\354\210\230\354\240\225\355\233\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" new file mode 100644 index 0000000..10675b3 --- /dev/null +++ "b/src/refactoring/siny/\354\210\230\354\240\225\355\233\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" @@ -0,0 +1,96 @@ +const per2Int = (value, per) => (value * per) / 100; + +/** + * ์ฃผ๋ฌธ์ž๋Š” ์˜ค๋กœ์ง€ ์ž๊ธฐ์ž์‹ ์— ๋Œ€ํ•œ ์ •๋ณด๋งŒ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ productPrice๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋ถˆํ•„์š”ํ•œ ์š”์†Œ์ด๋ฏ€๋กœ product๋กœ ์˜ฎ๊ฒผ๋‹ค. + * */ +function Client({ name, type, location }) { + this.offers = { + normal: 0, + premium: 20, + }; + this.name = name; + this.type = type; + this.location = location; + + getName = () => this.name; + getLocation = () => this.location; + getDiscountRate = () => this.offers[this.type]; + return { + getName, + getLocation, + getDiscountRate, + }; +} + +/** + * product ์˜ ๊ฐ€๊ฒฉ์€ product.value ์™€ client ์˜ ๋“ฑ๊ธ‰์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. + * (client์˜ ๋“ฑ๊ธ‰์ด premium์ด๋ฉด 20% ํ• ์ธ๋ฐ›๋Š” ํ˜•ํƒœ) + * ์ด๋•Œ price๊ณ„์‚ฐ์— client ๋ฅผ ์˜์กดํ•˜๊ฒŒ ํ•˜๋ฉด client์™€ product ์‚ฌ์ด์˜ ๊ฐ•ํ•œ ๊ฒฐํ•ฉ์ด ์ƒ๊ธฐ๋ฏ€๋กœ client๋ฅผ ์ง์ ‘ ๋ฐ›๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ discountRate:number๋ฅผ ๋ฐ›๊ฒŒ ํ•˜๋ฏ€๋กœ์จ + * ๋А์Šจํ•œ ๊ฒฐํ•ฉ์ธ ๊ด€๊ณ„๋กœ ์žฌ๊ตฌ์„ฑํ•˜์˜€๋‹ค. + * */ +function Product({ value, name, shipping }) { + this.value = value; + this.name = name; + this.shipping = shipping; + + getName = () => this.name; + getShipping = () => this.shipping; + calcPriceByDiscountRate = discountRate => this.value - per2Int(this.value, discountRate); + + return { + getName, + getShipping, + calcPriceByDiscountRate, + }; +} + +/** + * Summary์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๋‹ค์–‘ํ•œ ์—ญํ• ์„ order๋กœ ๋‚ด๋ ธ๋‹ค. + * */ +function Order({ id, value, client, product }) { + this.taxes = { + EU: 21, + USA: 14, + }; + this.id = id; + this.value = value; + this.client = client; + this.product = product; + + getId = () => this.id; + productShipping = () => this.product.getShipping(); + getClientName = () => this.client.getName(); + getProductName = () => this.product.getName(); + getTotalAmount = () => + this.product.calcPriceByDiscountRate(this.client.getDiscountRate()) + this.taxes[this.client.getLocation()]; + return { + getId, + productShipping, + getClientName, + getProductName, + getTotalAmount, + }; +} + +function Summary({ order }) { + this.order = order; + + printSummary = () => { + return `Order: ${this.order.getId()} + Client: ${this.order.getClientName()} + Product: ${this.order.getProductName()} + TotalAmount: ${this.order.getTotalAmount()} + Arrival in: ${this.order.productShipping()} days.`; + }; + + return { + printSummary, + }; +} + +module.exports = { + Client, + Product, + Order, + Summary, +}; diff --git "a/src/refactoring/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" "b/src/refactoring/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" index 840e72c..c0b5179 100644 --- "a/src/refactoring/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" +++ "b/src/refactoring/\354\210\230\354\240\225\354\240\204-03_\353\266\200\354\240\201\354\240\210\355\225\234_\355\217\211\352\260\200.js" @@ -10,7 +10,7 @@ const Client = ({ name, type, location }) => { this.location = location; getName = () => this.name; - getType = () => this.name; + getType = () => this.type; getLocation = () => this.location; getPriceByProduct = product => product.value - per2Int(product.value, this.offers[this.type]); @@ -52,13 +52,14 @@ const Order = ({ id, value, client, product }) => { getValue = () => this.value; getClient = () => this.client; getProduct = () => this.product; - getTaxes = loc => this.getTaxes(this.taxes[loc]); + getTaxes = loc => this.taxes[loc]; return { getId, getValue, getClient, getProduct, + getTaxes, }; }; @@ -67,7 +68,7 @@ const Summary = ({ order }) => { printSummary = () => { let client = order.getClient(); - let product = order.product(); + let product = order.getProduct(); return `Order: ${order.getId()} Client: ${client.getName()} @@ -75,10 +76,27 @@ const Summary = ({ order }) => { TotalAmount: ${client.getPriceByProduct(product) + this.order.getTaxes(client.getLocation())} - Arrival in: ${this.order.product.getShipping()} days.`; + Arrival in: ${product.getShipping()} days.`; }; return { printSummary, }; }; +const mockProduct = { + value: 100, + name: 'shirts', + shipping: 5, +}; +const client1 = Client({ name: 'siny', type: 'premium', location: 'USA' }); +const product1 = Product(mockProduct); +const order1 = Order({ + id: '1', + value: '???', + product: product1, + client: client1, +}); + +const summary = Summary({ order: order1 }); + +console.log(summary.printSummary());