Skip to content

[siny] 과제 제출 (2번문제는 모르겠음) #2

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

Merged
merged 2 commits into from
Jan 18, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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: [email protected]
User email: +34 635 538 973
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Order: 1
Client: shirts
Product: shirts
TotalAmount: NaN


Arrival in: 5 days.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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: [email protected]
User email: +34 635 538 973
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Order: 1
Client: siny
Product: shirts
TotalAmount: 94


Arrival in: 5 days.
65 changes: 65 additions & 0 deletions src/refactoring/siny/refactor1.test.js
Original file line number Diff line number Diff line change
@@ -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.`);
});
});
18 changes: 18 additions & 0 deletions src/refactoring/siny/testUtils.js
Original file line number Diff line number Diff line change
@@ -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,
};
38 changes: 38 additions & 0 deletions src/refactoring/siny/utils.js
Original file line number Diff line number Diff line change
@@ -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,
};
44 changes: 44 additions & 0 deletions src/refactoring/siny/utils.test.js
Original file line number Diff line number Diff line change
@@ -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 });
});
});
});
84 changes: 0 additions & 84 deletions src/refactoring/siny/수정전-03_부적절한_평가.js

This file was deleted.

Loading