371p ~ 394p
π "μ‘°ννκ³ λ³κ²½νκ³ μ€μ νλ κ²μ update()
λ‘ κ΅μ²΄νκΈ°" 리ν©ν°λ§
/// Step 1: Identify get, modify, set
function halveField(item, field) {
var value = item[field]; // get
var newValue = value / 2; // modify
var newItem = objectSet(item, field, newValue); // set
return newItem;
}
/// Step 2: Replace with call to update()
function halveField(item, field) {
return update(item, field, function(value) {
return value / 2;
});
}
π μ€μ²©λ λ°μ΄ν°μ update() μ¬μ©νκΈ°
function incrementSize(item) {
var options = item.options; // 1. μ‘°ν
var size = options.size; // 2. μ‘°ν
var newSize = size + 1; // 2. λ³κ²½
var newOptions = objectSet(options, 'size', newSize); // 2. μ€μ // 1. λ³κ²½
var newItem = objectSet(item, 'options', newOptions); // 1. μ€μ
return newItem;
}
/// Refactored
function incrementSize(item) {
var options = item.options; // 1. μ‘°ν
var newOptions = update(options, 'size', increment); // here // 1. λ³κ²½
var newItem = objectSet(item, 'options', newOptions); // 1. μ€μ
return newItem;
}
/// Refactored twice
function incrementSize(item) {
return update(item, 'options', function(options) { // here
return update(options, 'size', increment);
});
}
π update2()
λμΆνκΈ°
update2()
λ₯Ό λμΆν μ μλ€.function incrementSize(item) { // 1. "Size" -> (option)
return update(item, 'options', function(options) {
return update(options, 'size', increment); // 1. "'size'" -> (option)
});
}
/// 1. With explicit option argument
function incrementOption(item, option) { // 2. "increment" -> (modify)
return update(item, 'options', function(options) {
return update(options, option, increment); // 2. "increment" -> (modify)
});
}
/// 2. With explicit modify argument
function updateOption(item, option, modify) { // 3-1. "Option" -> (key1) // 3-2. "option" -> (key2)
return update(item, 'options', function(options) { // 3-1. "'options'" -> (key1)
return update(options, option, modify); // 3-2. "option" -> (key2)
});
}
/// 3. With explicit argument
function update2(object, key1, key2, modify) {
return update(object, key1, function(value1) {
return update(value1, key2, modify);
});
}
/// cc. Original
function incrementSize(item) {
var options = item.options;
var size = options.size;
var newSize = size + 1;
var newOptions = objectSet(options, 'size', newSize);
var newItem = objectSet(item, 'options', newOptions);
return newItem;
}
π update3()
λμΆνκΈ°
function update3(object, key1, key2, key3, modify) {
return update(object, key1, function(object2) {
return update2(object2, key2, key3, modify);
});
}
π nestedUpdate()
λμΆνκΈ°
function nestedUpdate(object, keys, modify) {
if(keys.length === 0) // base case
return modify(object);
var key1 = keys[0];
var restOfKeys = drop_first(keys);
return update(object, key1, function(value1) {
return nestedUpdate(value1, restOfKeys, modify);
});
}
π κΉμ΄ μ€μ²©λ λ°μ΄ν°μ μΆμν λ²½ μ¬μ©νκΈ°
κΉμ΄ μ€μ²©λ ꡬ쑰μ nestedUpdate()
λ₯Ό μ°λ €λ©΄ κΈ΄ key κ²½λ‘ λ°°μ΄μ΄ νμνλ€.
μ΄κ²μ΄ λ무 κΈΈλ©΄ μ€κ°μ μλ κ°μ²΄λ€μ΄ μ΄λ€ ν€λ₯Ό κ°μ‘λμ§, ꡬ쑰λ₯Ό κΈ°μ΅νκΈ° μ΄λ ΅λ€.
μλ₯Ό λ€μ΄, μλμ κ°μ μ½λκ° μλ€.
httpGet("http://my-blog.com/api/category/blog", function(blogCategory) {
renderCategory(nestedUpdate(blogCategory, ['posts', '12', 'author', 'name'], capitalize));
});
apiλ‘ κ°μ Έμ¨ κ° blogCategory
λ₯Ό μ½λ°±μ ν΅ν΄ μ²λ¦¬νλ μ½λμ΄λ€.
μ΄ μ½λλ λ€μκ³Ό κ°μ μ΄ν΄λ₯Ό ν¨μΆνκ³ μλ€.
blogCategory
λ posts
key μλ "λΈλ‘κ·Έ κΈ"μ λ΄κ³ μλ ꡬ쑰μ΄λ€.ID
(12) λ₯Ό ν΅ν΄ μ κ·Όν μ μλ€.author
key μλ "κΈμ΄μ΄"λ₯Ό λ΄κ³ μλ ꡬ쑰μ΄λ€.name
key μλ "μ¬μ©μ μ΄λ¦"μ λ΄κ³ μλ ꡬ쑰μ΄λ€.κ²½λ‘λ§ λ³΄κ³ , μ΄ λͺ¨λ μ 보λ₯Ό κΈ°μ΅νκΈ°λ μ½μ§ μλ€.
κ·Έλμ, μΆμν λ²½μ λ§λ€κ³ μλ―Έμλ μ΄λ¦μ λΆμ¬, ꡬνμ κ°μΆκ³ κΈ°μ΅ν΄μΌ ν μ μ μ€μΌ μ μλ€.
λ€μ λ§ν΄, μΆμν λ²½μ μ‘΄μ¬νλ ν¨μ μμ μ΄ ν΄λΉ ꡬνμ λ³Έλ¬Έμ νκ³ μμΌλ©΄ λλ€.
ID
λ‘ "λΈλ‘κ·Έ κΈ"μ λ³κ²½νλ ν¨μλ₯Ό λ§λ λ€.function updatePostById(category, id, modifyPost) {
return nestedUpdate(category, ['posts', id], modifyPost); // modifyPost // λΈλ‘κ·Έ κΈ κ΅¬μ‘°μ λν΄μλ μ½λ°±μ λ§‘κΈ΄λ€. -> 2.
// key κ²½λ‘ λ°°μ΄μ ν¨μΆλ categoryμ ꡬ쑰λ μΆμν λ²½ λ€λ‘ μ¨κΈ΄λ€.
// μ¦, μ΄ ν¨μ μμ μ΄ μ¬κΈ°(λ³Έλ¬Έ)μ νλλ€.
}
function updateAuthor(post, modifyUser) {
return update(post, 'author', modifyUser); // modifyUser // μ¬μ©μ ꡬ쑰μ λν΄μλ μ½λ°±μ λ§‘κΈ΄λ€. -> 3.
// λΈλ‘κ·Έ κΈμ ꡬ쑰λ μ΄ ν¨μ μμ μ΄ μ¬κΈ°(λ³Έλ¬Έ)μ νλλ€.
}
function capitalizeName(user) {
return update(user, 'name', capitalize);
// μ¬μ©μμ ꡬ쑰λ μ΄ ν¨μ μμ μ΄ μ¬κΈ°(λ³Έλ¬Έ)μ νλλ€.
}
updatePostById(blogCategory, '12', function(post) {
return updateAuthor(post, capitalizeUserName);
});
κΈ°μ΅ν΄μΌ ν μ¬νμ΄ κ°λ¨ν΄ μ‘λ€.
π κ³ μ°¨ ν¨μ μ²λ¦¬
forEach()
, map()
, filter()
, reduce()
update()
, nestedUpdate()
withArrayCopy()
, withObjectCopy()
wrapLogging()
λ€μ μκ°λΆν΄ λλμ΄ λΆμ° μμ€ν μ λν΄μ μμ보μ.