lodash
lodash copied to clipboard
fp/gt is really weird ?
import { gt } from "lodash/fp";
const greaterThan3 = gt(3);
greaterThan3(4); // false
I would expect gt(x)
means greater than x ?
Just look at the documentation
_.gt(value, other)
Checks if value is greater than other.
_.gt(3, 4)
-> false
So your function actually checks if 3 is great than the value. So everything is right. Because 4 is not greater than 3.
according to docs, the function fp.gt
has it's original argument order. Which is (a, b)
instead of (b, a)
. So. the workaround is to use fp.lt
, placeholder or wrap it in arrow fn, like this:
import _, { gt, lt } from "lodash/fp";
const gt3lt = lt(3); // tsc friendly
const gt3p = gt(_, 3) as any; // tsc will warn u without that "as any"
const gt3 = (b: number) => gt(b, 3) // best way ig, it gives correct type
console.log({
gt3lt: {
2: gt3lt(2), // false
3: gt3lt(3), // false
4: gt3lt(4), // true
},
gt3p: {
2: gt3p(2), // false
3: gt3p(3), // false
4: gt3p(4), // true
},
gt3: {
2: gt3(2), // false
3: gt3(3), // false
4: gt3(4), // true
},
});