toss / es-toolkit

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.
https://es-toolkit.slash.page
Other
6.28k stars 269 forks source link

Support ReadOnlyArray<T> for any collection operation #413

Open Nness opened 4 weeks ago

Nness commented 4 weeks ago

Such as orderBy, uniq etc, In lodash they support ArrayLike object, which is handy, of course the problem I am facing is less to do with ArrayLike and more related to ReadOnlyArray<T> in TypeScript. I don't think everyone is utilize ReadOnlyArray<T>, nor is the syntax provide any real value, it is a tool for me to describe the intention. Else people might modify the array, map or set that I intended treat it as immutable.

From the source code, I can see the code base mainly utilize on modern Array related function, it also use slice() to duplicate the array then perform the action such as orderBy. Then there is no reason not to support ReadOnlyArray<T>. Unless I missed something.

raon0211 commented 4 weeks ago

We aim to support read-only arrays if possible. Although not written in the documentation, most functions take readonly arrays in their arguments by default.

Is there some specific functions that you need?

Nness commented 3 weeks ago

orderBy is the one I used most, but I just went through all the array related function and found those utilities that could be support read only array. Hope that helps.

raon0211 commented 3 weeks ago

Thanks for making the lists. Let me add support for them.