<aside> 💡 아래와 같은 객체를 query string으로 만들어보자
</aside>
{ limit: 10, offset: 10, type: 'notice' }
const queryStr = pipe(
Object.entries, // 객체를 배열화 해준다 -> [['limit', 10], ['offset', 10], ['type', 'notice']]
map(([a, b]) => `${a}=${b}`),
reduce((a, b) => `${a}&${b}`)
)
log(queryStr({ limit: 10, offset: 10, type: 'notice' })) // limit=10&offset=10&type=notice
<aside> 💡 우리가 알고있는 Array.prototype.join 을 curry와 조합하고 iterable하게 만들어보자
</aside>
const join = curry((sep = ',', iter) => // 초기값은 ',' 로 적용되게
reduce((a, b) => `${a}${sep}${b}`, iter)
)
<aside> 💡 장점은 무엇일까?
</aside>
const join = curry((sep = ',', iter) =>
reduce((a, b) => `${a}${sep}${b}`, iter)
)
const a = function* () {
yield 1
yield 2
yield 3
yield 4
}
log(join(' - ', a())) // 1 - 2 - 3 - 4
<aside> 💡 이렇게 배열뿐만 아니라 제너레이터의 join도 가능해지고 이 말은 곧 join 전에 지연이 가능하다는 뜻이기도 하다 → 객체지향으로 만든 기존의 join보다 훨씬 조합성이 좋아진다고 볼 수 있다!!
</aside>
const join = curry((sep = ',', iter) =>
reduce((a, b) => `${a}${sep}${b}`, iter)
)
const queryStr = pipe(
Object.entries,
L.map(([a, b]) => `${a}=${b}`),
join('&')
)
log(queryStr({ limit: 10, offset: 10, type: 'notice' })) // limit=10&offset=10&type=notice
<aside> 💡 하지만 위의 경우 아직 아쉬운 부분이 있는데 Object.entries를 살펴보자
</aside>
const queryStr = pipe(
Object.entries,
(a) => {
console.log(a)
return a
},
L.map(([a, b]) => `${a}=${b}`),
join('&')
)
log(queryStr({ limit: 10, offset: 10, type: 'notice' }))