mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.08k stars 30 forks source link

`A.take` does not return empty array when `n` greater than length of array (as documented) #102

Open josh- opened 6 months ago

josh- commented 6 months ago

The docs for A.take state:

https://github.com/mobily/ts-belt/blob/c825d9709de1e5d15b1b362429e0cee56c96c516/src/Array/index.ts#L147

however if n is greater than the length of the array, the whole array is returned instead of an empty array.

for example:

console.log(A.take([1, 2, 3], 10));

logs [ 1, 2, 3 ] whereas it should log [].


when using a negative n this does correctly print out an empty array.

for example:

console.log(A.take([1, 2, 3], -10));

logs [].

JUSTIVE commented 6 months ago

The current behavior is more natural/usable. As the take function is just an alias for A.slice(0, x), it should take all elements when the index surpasses the array's length. So, I think the documentation should be matched to its behavior. for the strict action for the take action, we already have one: takeExactly

JUSTIVE commented 6 months ago

But that's my opinion, If the purpose of the A.take was what the docs said, the code should be changed. But I'm also scared because some of the codes I wrote work based on the current behavior.