Wednesday, November 13, 2024
Unleash the power of the JavaScript pipeline operator
November 12, 2024 | | In partnership with | |
| Recommended | | | New puzzle to train your brain and test your coding understanding✨ | | | Featured content | | With the pipeline operator you'll stop writing code like this: | const names = ['USA', 'Australia', 'CodingBeauty']; const lowerCasedNames = names.map((name) => name.toLowerCase() ); const hyphenJoinedNames = lowerCasedNames.join('-'); const hyphenJoinedNamesCapitalized = capitalize( hyphenJoinedNames ); const prefixedNames = `Names: ${hyphenJoinedNamesCapitalized}`; console.log(prefixedNames); // Names: Usa-australia-codingbeauty
| and start writing code like this: | // Hack pipes: | and > ['USA', 'Australia', 'CodingBeauty'] |> %.map((name) => name.toLowerCase()) |> %.join('-') |> capitalize(%) |> `Names: ${%}` |> console.log(%); // Names: Usa-australia-codingbeauty
| So refreshingly clean -- and elegant! All those temporary variables are gone -- not to mention the time it took to come up with those names and type them (not everyone types like The Flash, unfortunately). | You may have heard this partially true quote attributed to Phil Karlton: "There are only two hard things in computer science: cache invalidation and naming things". | Using the JavaScript pipeline operator clears out the clutter to boost readability and write data-transforming code (basically all code) in a more intuitive manner. | Verbosity should be avoided as much as possible, and this works so much better to compact code than reusing short-named variables: | let buffer = await sharp('coding-beauty-v1.jpg').toBuffer(); let image = await jimp.read(buffer); image.grayscale(); buffer = await image.getBufferAsync('image/png'); buffer = await sharp(buffer).resize(250, 250).toBuffer(); image = await jimp.read(buffer); image.sepia(); buffer = await image.getBufferAsync('image/png'); await sharp(buffer).toFile('coding-beauty-v1.png');
| Hopefully, almost no one codes like this on a regular basis. It's a pretty horrible technique when done in a large scale; a perfect example showing why we embrace immutability and type systems. | Unlike the pipeline operator, there's no certainty that the variable always contains the value you set at any given point… | | | They call it mini but what it can do is far from mini. | Only 5 x 5 x 2 inches and 1.5 pounds — That's mega-light. | Yet the M4 chip makes it as dangerous as the new MacBook Pro — even though it costs much less. | | And just look at the ports: | | | And you know I saw this pic on their website and was like, What the hell is this? | Then I saw this: | | | Who really owns your audience? | Being a Creator has never been easy, but unpredictable algorithms make connecting with your audience on social media harder than ever. | Enter beehiiv, the newsletter platform used to send this very email. | beehiiv frees you from the algorithms, giving you the tools to connect and create a more direct relationship with your followers. | Plus, with a network of premium advertisers and paid subscription options, you can tap into new revenue streams from day one. | Expand your brand with the most powerful newsletter platform on the planet. Start your 30-day free trial today. | Thanks for taking the time to read today's issue. | Don't let the bugs byte, The Coding Beauty team |
| | Update your email preferences or unsubscribe here © 2024 Beneebo LLC 1603 Capitol Avenue, Suite 413A, #3255 Cheyenne, Wyoming 82001, United States of America | | Terms of Service |
|
|
|
|
|
0 Komentar untuk "New JavaScript pipeline operator: transform anything into a one-liner"