Is there a Prettier plugin to better handle composes? For example:
const uniqAndSortByLabel = R.compose(
R.sortBy(R.prop('label')),
R.uniqBy(R.compose(
R.toLower,
R.trim,
R.prop('label'),
)),
)
will be transformed to this:
const uniqAndSortByLabel = R.compose(
R.sortBy(R.prop('label')),
R.uniqBy(R.compose(R.toLower, R.trim, R.prop('label')))
);
I know there's the comment // prettier-ignore
, but I also don't want to put that everywhere I'm using compose
.
callN
function in ramda? e.g. if I wanted to repeat something n
times, what would be the idiomatic way of doing this in Ramda?
// What I have for turning a mongo document into a domain record
const renameIdProp = ({ _id, ...params }) => ({
...params,
id: _id,
});
map(el => 0, filter(el => el != 1, [1,2,3,4]))
_curry1
? Specifically, the path of when args is zero, return f1?export default function _curry1(fn) {
return function f1(a) {
if (arguments.length === 0 || _isPlaceholder(a)) {
return f1;
} else {
return fn.apply(this, arguments);
}
};
}
I have been going through Ramda's source code as a learning exercise and don't understand the following:
Here is a simplified curry3 function
import _curry2 from './fns/_curry2' // copy of Ramda function
import _curry3 from './fns/_curry3' // copy of Ramda function
export function curry3(fn) {
return function f3(a, b, c) {
const argsLen = arguments.length
log('curry3 argLen', argsLen)
switch (argsLen) {
case 0:
return f3
case 1:
return _curry2(function (_b, _c) {
return fn(a, _b, _c)
})
case 2:
return _curry1(function (_c) {
return fn(a, b, _c)
})
case 3:
return fn(a, b, c)
}
}
}
Wrap a function in it
const c3 = curry3(function curryMe3(a, b, c) {
return `curryMe3: ${a}, ${b}, ${c}`
})
const c3 = curry3(function curryMe3(a, b, c) {
return `curryMe3: ${a}, ${b}, ${c}`
})
And call it
log('c3-0', c3())
// -> argLen 0
// -> c3 [Function: f3]
log('c3-1', c3(1))
// -> argLen 1
// -> c3 [Function: f2]
log('c3-2', c3(1, 2))
// -> argLen 2
// -> c3 [Function: f1]
log('c3-3', c3(1, 2, 3))
// -> argLen 3
// -> c3 curryMe3: 1, 2, 3
So c3
receives 1 argument as fn
. What I dont understand is how the arguments passed to curryMe3
get into f3
as 0, 1, 2 or 3 separate arguments.
const sumEq = curry((n, list) => pipe(sum, equals(n))(list));
How can I validate that a request is a JSON object? I did something like this:
const getTags = (request) => {
if (isEmpty(request) || !is(Object, request)) {
throw new TypeError(
'The #request is required and needs to be an JSON object'
)
}
But then I realized that is(Object, [])
returns true
export const isNullOrEmpty = (request) => isNil(request) || isEmpty(request)
/**
* Check if the incoming request is either empty, null, or undefined and if it
* is not a JSON object
*
* @param {*} request - incoming request
* @returns {boolean} true when the request is either null, undefined, or empty,
* and it is not a JSON object
*/
export const isEmptyOrNotJSONObject = (request) =>
isNullOrEmpty(request) || request.constructor !== Object
Hi there!
Got a function combination problem and was wondering if Ramda could help me write it in a point-free style.
Basically, I have 2 functions both which take the same input. One function returns a Promise which resolves to a string and the second function returns a string. I want to join the results of the two functions using path.join
.
I.e, non point-free:
const fn = obj => getString1(obj).then(s1 => path.join(s1, getString2(obj))
Using ramda's join
with a forward-slash instead of path.join
is acceptable. Is there a point-free way of combining these functions?
I thought perhaps lift
could help but I figure it out with the promise-returning function.
pipeWith(f, g, x)
i can write pipeWith(x, f, g)
i don't think so. maybe you can use R.reverse
for the arguments
const pipeWhileNotNil = R.pipeWith((f, res) => R.isNil(res) ? res : f(res));
const f = pipeWhileNotNil(R.reverse[Math.pow, R.negate, R.inc])
Note this code is only for demo, prob it will not work @theqp
:point_up: Edit: i don't think so. maybe you can use R.reverse
for the arguments
const pipeWhileNotNil = R.pipeWith((f, res) => R.isNil(res) ? res : f(res));
const f = pipeWhileNotNil(R.reverse[Math.pow, R.negate, R.inc])
Note this code is only for demo, prob it will not work but you got the idea 🙂 @theqp