I am trying to refactor this code:
async function getUserDataByUsername(username, cached = true) {
const usernameRef = firestore
.collection("usernames")
.doc(username.toLowerCase());
const usernameDoc = await usernameRef.get();
if (!usernameDoc.exists) {
throw userErrors.userNotFound();
}
const { userId } = usernameDoc.data();
return memoizedGetUserData(userId, cached);
}
For that, I have thought to split it in smaller parts, as follows:
function memoizedGetUserData(userId, cached = true) {
... Fetching from LRU or DB ...
}
async function getUserId(username) {
const usernameRef = firestore
.collection("usernames")
.doc(username.toLowerCase());
const usernameDoc = await usernameRef.get();
if (!usernameDoc.exists) {
throw userErrors.userNotFound();
}
const { userId } = usernameDoc.data();
return userId;
}
async function getUserDataByUsername(username, cached = true) {
const userId = await getUserId(username);
return memoizedGetUserData(userId, cached);
}
Now, I want to apply Ramda to this module. I have never used this library before, but I have read that it is really cool, and makes the code easier to understand with some of it utilities.
What I am trying is to refactor the original method using the pipeline style, as follows:
import R from "ramda";
...
const getUserDataByUsername = R.pipeP(getUserId, memoizedGetUserData);
But… how can I pass the second optional parameter “cached”, only to the second argument of my pipe??