Wednesday, October 19, 2022

js array functions are so pleasant

Like yesterday's use of reduce(), there is something absolutely pleasant about a nice little set of JS array functions (filter, map, reduce, join) - like a programmer's mental ASMR. (And use of fat arrow functions make it even cleaner...)

Here's a shtick I did for an overly simplistic "Convert To Title Case" function:

const titleCase = (words) => words?.split(' ')
      .map(word => word.substring(0,1).toUpperCase()
                   + word.substring(1))
      .join(' ');

console.log(titleCase(null));

console.log(titleCase("this  is a test"));

console.log(titleCase("ok")); 

Just so charming.

No comments:

Post a Comment