DEV Community

Discussion on: 8 Best JavaScript One-Liners

Collapse
 
shivams1007 profile image
Shivam Singh

1. The chill Function

You're correct; my initial example did not show how to actually use the function in a meaningful way. It's a Promise-based function, so you'd typically use it with async/await or .then() to actually cause a delay in the code's execution. Here's how you could use it:

(async () => {
  console.log("Before waiting");
  await chill(2000);
  console.log("After waiting for 2 seconds");
})();
Enter fullscreen mode Exit fullscreen mode

2. The sortBy Function

You bring up a good point. The function could indeed be simplified to:

const sortBy = (arr, key) => arr.sort((a, b) => a[key] > b[key] ? 1 : -1);
Enter fullscreen mode Exit fullscreen mode

This approach assumes that a[key] and b[key] will never be equal if sorting is based on unique identifiers, which might be okay for some cases but not all.

3. The pluck Function

You're right; the input was incomplete without specifying the key. A more full example could be:

const pluck = (arr, key) => arr.map(obj => obj[key]);
console.log(pluck([{x: 1}, {x: 2}], 'x')); // Output: [1, 2]
Enter fullscreen mode Exit fullscreen mode

4. The insert Function

Your point is valid; the input example could have been more complete. Also, your shorter version using splice is more concise. However, one benefit of the original slice approach is that it does not mutate the original array.

const insert = (arr, index, newItem, a=[...arr]) => (a.splice(index, 0, newItem),a);
Enter fullscreen mode Exit fullscreen mode

5. The randomColor Function

You are correct again; the original version can indeed skip some colors and never reach #FFFFFF. Your version with padding zeros is more accurate in generating the full spectrum of colors:

const randomColor = "#" + (~~(Math.random() * 8**8)).toString(16).padStart(6,0);
Enter fullscreen mode Exit fullscreen mode

Thank you for pointing out these details; your observations are insightful and enhance the accuracy and efficiency of the examples.
I will update it according to it!

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Try the shorter sortBy with repeated identifiers. Works just fine. If the two identifiers are equal it makes no difference what order they are sorted in.

My splice example doesn't mutate the original array.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Might want to fix the glaring typo in the header image too! 😉

Thread Thread
 
shivams1007 profile image
Shivam Singh

@jonrandy which one?

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

"8 One-Linear You Will Love"

Thread Thread
 
shivams1007 profile image
Shivam Singh • Edited

ohh yes you are correct! 😊
I am open for any other suggestions

Collapse
 
shivams1007 profile image
Shivam Singh

1. The sortBy Function with Repeated Identifiers

You're right that if two elements have the same identifier, the order doesn't matter for those particular elements. In many sorting algorithms, including JavaScript's native .sort(), elements that compare as equal remain in their original order (stable sort). So, your simplified version is equally effective:

const sortBy = (arr, key) => arr.sort((a, b) => a[key] > b[key] ? 1 : -1);
Enter fullscreen mode Exit fullscreen mode

This will work just fine even with repeated identifiers.

2. The splice Example and Array Mutation

I apologize for the misunderstanding. You're correct; your example using splice actually doesn't mutate the original array because you made a shallow copy (a=[...arr]) before applying the splice.

Here's the function for clarity:

const insert = (arr, index, newItem, a=[...arr]) => (a.splice(index, 0, newItem),a);
Enter fullscreen mode Exit fullscreen mode

In this function, a=[...arr] creates a new array that is a copy of the original arr. Then, splice is called on this new array (a), so the original arr remains unchanged. Therefore, it is a non-mutating operation on the original array.

Thank you for your keen observations; they serve to improve the quality and accuracy of the discussion.