<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Klish3</title>
    <description>The latest articles on DEV Community by Klish3 (@klish3).</description>
    <link>https://dev.to/klish3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F58145%2Fdcb9f98b-2c90-4fc0-9216-798c5b259541.jpeg</url>
      <title>DEV Community: Klish3</title>
      <link>https://dev.to/klish3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/klish3"/>
    <language>en</language>
    <item>
      <title>Learning what I thought I knew: Pipe functions</title>
      <dc:creator>Klish3</dc:creator>
      <pubDate>Tue, 07 Jun 2022 22:24:19 +0000</pubDate>
      <link>https://dev.to/klish3/learning-what-i-thought-i-knew-pipes-functions-49j2</link>
      <guid>https://dev.to/klish3/learning-what-i-thought-i-knew-pipes-functions-49j2</guid>
      <description>&lt;p&gt;Sooooo, I got owned by a simple test in a NestJs Jest test because I honestly didn't know what I was doing. My favourite development strategy is brute force. I have no problem with this method since I realised it's a process of illumination where I try all that I know before I begin my hunt for answers on stack overflow and the rest of the big scary internet. &lt;/p&gt;

&lt;p&gt;To understand how I messed this up, I decided to break down the function I was trying to test into smaller pieces. &lt;/p&gt;

&lt;p&gt;But, enough about me.....for now.&lt;/p&gt;

&lt;h3&gt;
  
  
  The service
&lt;/h3&gt;

&lt;p&gt;The service I was testing looked like this 👇🏿&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this.service.lookup(reqUrl).pipe(map((res)=&amp;gt;res.map(.)));&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Where the lookup function returned an Array wrapped in an RxJs Observable.&lt;/p&gt;

&lt;p&gt;The service calls an endpoint to retrieve some data. Basic stuff right. Well from me trying to test this felt like I was stuck inside the upside-down without Jane (aka Eleven).&lt;/p&gt;

&lt;p&gt;The first set of errors I got was the &lt;a href=""&gt;.map&lt;/a&gt; is not a function, which is the compiler's way of saying: &lt;/p&gt;

&lt;p&gt;Array.map(): prototype of Array&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;'Yoooo, dude, this is not an Array, map won't work here.' &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Observable.map(): imported from Rxjs&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;'Yoooo, dude, this is not an Observable, map won't work here.' &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Array vs Observarble pipe
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/52753739/what-are-the-differences-between-array-map-and-rxjs-map"&gt;What are the differences between Array map and rxjs map&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The error was confusing to me since I thought that the data being returned was an Array. Essentially this wasn't the main problem but I was led down a rabbit hole for 1.5 hours, which led me to write this article. &lt;/p&gt;

&lt;p&gt;After my buddy, Toma, fixed it haha, thanks bro ✊🏿&lt;/p&gt;

&lt;h2&gt;
  
  
  Now time for the pipe dream
&lt;/h2&gt;

&lt;p&gt;Since I can know HOW the service call is invoked and that it returns data, what I didn't understand was what &lt;a href="https://rxjs.dev/guide/operators#piping"&gt;.pipe&lt;/a&gt; does.&lt;/p&gt;

&lt;p&gt;In a nutshell, &lt;a href="https://rxjs.dev/guide/operators#piping"&gt;.pipe&lt;/a&gt; takes what is being returned and passed that returned value to the next function in the chain.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
func(args In): funcRes {} &amp;lt;-- our starter function&lt;br&gt;
pipe( &amp;lt;- passes its response into the pipe function&lt;br&gt;
 func1(funcRes), &amp;lt;- func1 use it and returns a func1Res&lt;br&gt;
 func2(func1Res) &amp;lt;- func2 uses func1Res and returns func2Res&lt;br&gt;
); = func2Res &amp;lt;-  out comes our baby func2Res&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Another way to break this up is to create a custom pipe function. TypeOfNan made a simple example of how to demonstrate what .pipe does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Input string = "Stranger Things Season 4 Episode 7"
// Custom Pipe
const pipe = (...args) =&amp;gt; args.reduce((acc, el) =&amp;gt; el(acc));

// Create our 3 functions transfer
const titleFunc1 = '10 Weird Facts About Dogs';
const toLowerCaseFunc2 = (str1) =&amp;gt; str1.toLowerCase();
const addHyphensFunc3 = (str2) =&amp;gt; str2.replace(/\s/g, '-');

const slug = pipe(titleFunc1, toLowerCaseFunc2, addHyphensFunc3);

console.log(slug); 
// Return string = Stranger-things-season-4-episode-7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now knowing that the pipe function plays 'hot potato' with the return values from the functions passed through it, I can focus on the next, small parts. Thanks for reading I hope this helps.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;MDN Web docs - &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;Array.prototype.map()&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rxjs - &lt;a href="https://rxjs.dev/guide/operators#piping"&gt;Pipe&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stack overflow - &lt;a href="https://stackoverflow.com/questions/52753739/what-are-the-differences-between-array-map-and-rxjs-map"&gt;What are the differences between Array map and rxjs map?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TypeOfNan - &lt;a href="https://typeofnan.dev/how-to-use-pipe-the-pipeline-operator-in-javascript/"&gt;How to use pipe, the pipeline operator in javascript&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>rxjs</category>
      <category>array</category>
      <category>map</category>
    </item>
  </channel>
</rss>
