Skip to content

Commit 3cb2715

Browse files
author
june6723
committed
♻️ Refactored by june
1 parent 89e1c54 commit 3cb2715

4 files changed

Lines changed: 135 additions & 0 deletions

File tree

.prettierrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"singleQuote": true,
3+
"semi": true,
4+
"useTabs": false,
5+
"tabWidth": 2,
6+
"trailingComma": "all",
7+
"printWidth": 120,
8+
"bracketSpacing": true,
9+
"arrowParens": "avoid",
10+
"endOfLine": "auto"
11+
}

src/refactoring/june/example-1.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 1. setPriceByName, setShippingByName, setQuantityByName, setTaxByName에 고정 string으로 들어가는 key가 존재 (암묵적 입력)
2+
// 2. 이렇게 되면 item에 새로운 필드가 추가될때마다 함수를 새로 만들어야 하는 상황
3+
4+
function setPriceByName(cart, name, price) {
5+
var item = cart[name];
6+
var newItem = objectSet(item, 'price', price);
7+
var newCart = objectSet(cart, name, newItem);
8+
return newCart;
9+
}
10+
11+
function setShippingByName(cart, name, ship) {
12+
var item = cart[name];
13+
var newItem = objectSet(item, 'shipping', ship);
14+
var newCart = objectSet(cart, name, newItem);
15+
return newCart;
16+
}
17+
18+
function setQuantityByName(cart, name, quant) {
19+
var item = cart[name];
20+
var newItem = objectSet(item, 'quantity', quant);
21+
var newCart = objectSet(cart, name, newItem);
22+
return newCart;
23+
}
24+
25+
function setTaxByName(cart, name, tax) {
26+
var item = cart[name];
27+
var newItem = objectSet(item, 'tax', tax);
28+
var newCart = objectSet(cart, name, newItem);
29+
return newCart;
30+
}
31+
32+
function objectSet(object, key, value) {
33+
var copy = Object.assign({}, object);
34+
copy[key] = value;
35+
return copy;
36+
}
37+
38+
// result
39+
const itemKeyArr = ['price', 'shipping', 'quantity', 'tax'];
40+
const setValueByKey = (cart, name, key, value) => {
41+
if (!itemKeyArr.includes(key)) throw '아이템에 존재하지 않는 키값입니다!';
42+
const item = cart[name];
43+
const newItem = objectSet(item, key, value);
44+
const newCart = objectSet(cart, name, newItem);
45+
return newCart;
46+
};

src/refactoring/june/example-2.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 1. loop가 중복해서 쓰이고 있다.
2+
// 2. cook, eat, wash, dry, putAway등 데이터를 이용해 할 일에 집중할 수 있는 코드를 만들자
3+
4+
const foods = ['pizza', 'chungkukzang', 'ramen'];
5+
const dishes = ['main', 'size', 'personal'];
6+
7+
for (var i = 0; i < foods.length; i++) {
8+
var food = foods[i];
9+
cook(food);
10+
eat(food);
11+
}
12+
13+
for (var j = 0; j < dishes.length; j++) {
14+
var dish = dishes[j];
15+
wash(dish);
16+
dry(dish);
17+
putAway(dish);
18+
}
19+
20+
const forEach = (arr, callback) => {
21+
if (!('length' in arr)) throw '배열이 아님!';
22+
let index = 0;
23+
while (index < arr.length) {
24+
const current = arr[index];
25+
callback(current, index, arr);
26+
index += 1;
27+
}
28+
};
29+
30+
forEach(foods, food => {
31+
cook(food);
32+
eat(food);
33+
});
34+
forEach(dishes, dish => {
35+
wash(dish);
36+
dry(dish);
37+
putAway(dish);
38+
});

src/refactoring/june/example-3.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 1. fetching에 필요한 parameters, fetch function, error handling function 3가지가 같이 움직인다
2+
// 2. 만약 새로운 비동기 함수가 추가되면?
3+
var user = {
4+
id: '1',
5+
};
6+
7+
const getUserData = ({ id }) => {
8+
const response = fetch(`https://jsonplaceholder.typicode.com/users?id=${id}`);
9+
return response;
10+
};
11+
12+
const logToSnapErrors = error => console.log(`🚫 에러가 발생했어요: ${error.message}`);
13+
14+
try {
15+
getUserData(user);
16+
} catch (error) {
17+
logToSnapErrors(error);
18+
}
19+
20+
// ------------------------------------------
21+
22+
const fetchFlow = async (param, fetchCb, errorCb) => {
23+
try {
24+
await fetchCb(param);
25+
} catch (error) {
26+
errorCb(error);
27+
}
28+
};
29+
30+
fetchFlow(user, getUserData, logToSnapErrors);
31+
// 3. setUserData가 추가된다면?
32+
const setUserData = ({ id, name }) => {
33+
const response = fetch(`https://jsonplaceholder.typicode.com/user/${id}`, {
34+
method: 'POST',
35+
body: JSON.stringify({ id, name }),
36+
});
37+
return response;
38+
};
39+
40+
fetchFlow({ ...user, name: 'newName' }, setUserData, logToSnapErrors);

0 commit comments

Comments
 (0)