DEV Community

Discussion on: Mutates or not? We need both versions but there is a problem.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

With my project 'Metho' - we can easily safely add 'dynamic properties' to the Array prototype that could do what you are suggesting. We could make something like the following:

const arr = [2, 1, 3]
console.log( arr[reversed] )  // [3, 1, 2]
console.log( arr[sorted()] )  // [1, 2, 3]

const descending = (a, b)=>b-a
console.log( arr[sorted(descending)] )  // [3, 2, 1]

console.log( arr )  // [2, 1, 3] - no mutation occurred

// etc...
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonrandy profile image
Jon Randy 🎖️

I'm actually planning a metho-array library, so I'd probably include all/most of the above in that