<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gogh Ha on Medium]]></title>
        <description><![CDATA[Stories by Gogh Ha on Medium]]></description>
        <link>https://medium.com/@goghha?source=rss-a8b49130c33c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_54q_cSXIYMdErb-.</url>
            <title>Stories by Gogh Ha on Medium</title>
            <link>https://medium.com/@goghha?source=rss-a8b49130c33c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 07:02:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@goghha/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[มาเล่น Deno กันเถอะ (Deno Get started)]]></title>
            <link>https://medium.com/@goghha/%E0%B8%A1%E0%B8%B2%E0%B9%80%E0%B8%A5%E0%B9%88%E0%B8%99-deno-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-deno-get-started-d41f0a79710e?source=rss-a8b49130c33c------2</link>
            <guid isPermaLink="false">https://medium.com/p/d41f0a79710e</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[deno]]></category>
            <dc:creator><![CDATA[Gogh Ha]]></dc:creator>
            <pubDate>Tue, 09 Jun 2020 17:54:11 GMT</pubDate>
            <atom:updated>2020-06-09T17:54:11.765Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*vU06iDT1CZkp7YCOXdOpJQ.jpeg" /></figure><p>Deno คืออิหยัง? Deno คือ javascript &amp; typescript runtime ที่สร้างโดย Ryan Dahl เป็นคนเดียวกับพี่พัฒนา Node js ขึ้นมา Deno นั้นเป็น runtime ที่ build in typescript เข้ามาทำให้ไม่ต้องลง babel เพื่อทำอะไรให้ยุ่งยาก แถวด้วย Security ที่ดีขึ้น ที่สำคัญไม่ต้องมี <strong>node_module </strong>ตัวใหญ่ๆที่ชอบทำให้เรา มีปัญหาแถมรกด้วย</p><p>Deno นั้น Dependenciese พื้นฐานจะถูกเรียกผ่าน uri ซึ้งทำให้เบาไปได้เยอะมาก</p><p>ขายมาเยอะแล้วมาเริ่มต้นกัน</p><p><strong>Require</strong></p><ul><li>Node js version 8+ <a href="https://nodejs.org/en/download/">dowload here</a></li></ul><p><strong>Installation</strong></p><p>ในส่วนนี้จะขอลงรายละเอียดเฉพาะ window นะครับ ส่วน OS อื่นสามารถดูได้จากตรงนี้ครับ <a href="https://deno.land/#installation">installation</a></p><ol><li>เปิด Power Shell ขึ้นมาจะหน้าตาประมาณนี้นะครับ</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8YvhGQcMLVZBrlvDsidLPA.png" /></figure><p>2. copy command นี้ไปวางครับ iwr <a href="https://deno.land/x/install/install.ps1">https://deno.land/x/install/install.ps1</a> -useb | iex</p><p>3. หลังจากติดตั้งเสร็จแล้วลองพิมพ์ <strong>deno -help</strong> จะได้หน้าตาประมาณนี้เป็นอันเสร็จครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CW3CtMlXj8POvi_G40vAoQ.png" /></figure><p><strong>Getting Started</strong></p><ul><li>สร้าง Folder เปล่าขึ้นมา แล้วสร้างไฟล์ app.ts ประมาณนี้ครับ</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z4V63bOrsKAbu7nwpgasug.png" /></figure><ul><li>สร้าง server ขึ้นมาประมาณนี้ครับ</li></ul><pre>import { serve } from &quot;https://deno.land/std@0.56.0/http/server.ts&quot;;</pre><pre>const s = serve({ port: 8000 });</pre><pre>console.log(&quot;http://localhost:8000/&quot;);</pre><pre>for await (const req of s) {</pre><pre>req.respond({ body: &quot;Hello World\n&quot; });</pre><pre>}</pre><ul><li>ทดสอบรันไฟล์โดยใช้คำสั่ง</li></ul><pre>deno run app.ts</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*1i09ZG9HLfwPQ2mq3F-9HQ.png" /></figure><p>จะเจอ error ประมาณนี้ครับ วิธีแก้คือเราต้องใส่ flag — allow-net เข้าไปครับ ลองดูอีกที</p><pre>deno run app.ts --allow-net</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*TEeC54WB2PfyPSveIJ4BDw.png" /></figure><p>ผ่ามมมมมมมมม error อีก whattttttttttt ถ้างั้นลองใส่ข้างหน้าชื่อไฟล์แล้วกัน</p><pre>deno run --allow-net app.ts</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*XuubPaqp8tMay1F76IjHZw.png" /></figure><p>ได้แล้วเย้ๆๆๆๆ คราวนี้เราก็ไปเปิด <a href="http://localhost:8000/">http://localhost:8000</a> เพื่อเช็คว่า app เราใช้งานได้แล้วยัง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*VVTTiX0JtteKB4mBXWWjAA.png" /></figure><p>ถ้าใครขึ้น Hello World แสดงว่าคุณมากถูกทางแล้ว ถ้าใครยังเปิดไปได้ลองทบทวนดูอีกทีครับว่าพลาดจุดไหน</p><p><strong>Deno API Getting Start</strong></p><p>สำหรับใครที่ขี้เกียจพิมพ์สามารถไป clone ได้จากที่นี่ครับ</p><pre><a href="https://github.com/gogh-zaa/deno101">https://github.com/gogh-zaa/deno101</a></pre><ul><li><strong>GET API</strong></li></ul><p>สร้าง file routers.ts, controller.ts</p><p>controller.ts</p><pre>// สร้าง Interface<br>interface IGame {</pre><pre>   id: number;</pre><pre>   name: string;</pre><pre>   hits: number;</pre><pre>}</pre><pre>// mock data</pre><pre>let games: Array&lt;IGame&gt; = [</pre><pre>{</pre><pre>   id: &#39;0&#39;,</pre><pre>   name: &#39;Maplestory&#39;,</pre><pre>   hits: 1523</pre><pre>},</pre><pre>{</pre><pre>   id: &#39;1&#39;,</pre><pre>   name: &#39;Yulgang&#39;,</pre><pre>   hits: 15231</pre><pre>},</pre><pre>{</pre><pre>   id: 2,</pre><pre>   name: &#39;RO&#39;,</pre><pre>   hits: 152325456</pre><pre>},</pre><pre>];</pre><pre>// function getGames เวลามีคนเรียกจะ return games ทั้งหมดที่มี</pre><pre>const getGames = ({ response }: { response: any }) =&gt; {</pre><pre>    response.body = games</pre><pre>}</pre><pre>// export funtion ให้หน้าอื่นเรียกใช้ได้ *** ถ้าไม่ใส่หน้าอื่นจะเรียกไม่เจอ</pre><pre>export { getGames }</pre><p>routers.ts</p><pre>// import router module<br>import { Router }from &#39;https://deno.land/x/oak/mod.ts&#39;;</pre><pre>// import controller function<br>import { getGames } from &#39;./controller.ts&#39;</pre><pre>const router = new Router()</pre><pre>// สร้าง path แล้ว function ที่จะทำงานเวลามีคนเรียก path นี้<br>router.get(&#39;/games&#39;, getGames)<br></pre><pre>// export router<br>export default router</pre><p>ใน file app.ts</p><pre>// import module application<br>import { Application } from &#39;https://deno.land/x/oak/mod.ts&#39;;</pre><pre>// import router<br>import router from &#39;./routers.ts&#39;;</pre><pre>// port<br>const port = 8000</pre><pre>const app = new Application();</pre><pre>// apply router<br>app.use(router.routes())</pre><pre>// allow method<br>app.use(router.allowedMethods())</pre><pre>console.log(`Server running on port ${port}`)<br></pre><pre>await app.listen({ port });</pre><p>ลองเทส</p><pre>deno run --allow-net app.ts</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fuaQSwYgT3sfCO8kGpynGw.png" /></figure><p>เอ้าทำไมถึงหาไม่เจอ เพราะว่า router ที่เราทำไว้มี path เฉพาะ games ลองใส่ path เข้าไป</p><pre><a href="http://localhost:8000/games">http://localhost:8000/games</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8k8k_7kw4mbAi_rW9lg2Ag.png" /></figure><p>จะได้ของออกมาประมาณนี้</p><ul><li><strong>GET API with Parameter</strong></li></ul><p>controller.ts เพิ่ม code ต่อจากของเดิมประมาณนี้ครับ</p><pre><strong>// function ค้นหา game<br>const searchGameByID = (id: string): ( IGame | undefined ) =&gt; games.find((game) =&gt; game.id === Number(id));</strong></pre><pre><strong>// function รับ parameter จาก response<br>const getGame = ({ params, response }: { params: { id: string }; response: any }) =&gt; {</strong></pre><pre><strong>const game: IGame | undefined = searchGameByID(params.id)</strong></pre><pre><strong> if (game) {</strong></pre><pre><strong>  response.status = 200</strong></pre><pre><strong>  response.body = game</strong></pre><pre><strong> } else {</strong></pre><pre><strong>  response.status = 404</strong></pre><pre><strong>  response.body = { message: `Game not found.` }</strong></pre><pre><strong> }</strong></pre><pre><strong>}</strong></pre><pre>export { getGames, <strong>getGame </strong>}</pre><p>routers.ts เพิ่ม code ต่อจากของเดิมประมาณนี้ครับ</p><pre>import { Router }from &#39;https://deno.land/x/oak/mod.ts&#39;;</pre><pre>import { getGames, <strong>getGame </strong>} from &#39;./controller.ts&#39;</pre><pre>const router = new Router()</pre><pre>router</pre><pre>.get(&#39;/games&#39;, getGames)</pre><pre><strong>.get(&#39;/games/:id&#39;, getGame)</strong></pre><pre>export default router</pre><p>Test api <a href="http://localhost:8000/game/0">http://localhost:8000/games/0</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*U3CKOKKjz8CeqDQP-i_ebw.png" /></figure><ul><li><strong>POST API</strong></li></ul><p>controller.ts</p><pre><strong>const addGame = async ({ request, response }: { request: any; response: any }) =&gt; {</strong></pre><pre><strong> // แกะของที่ส่งมาจาก body<br> const body = await request.body()</strong></pre><pre><strong>// หลังจากอ่านค่าจาก body ต้องมาแกะเอา value <br> const game: IGame = body.value</strong></pre><pre><strong> // เพิ่มข้อมูลลงใน Array<br> games.push(game)</strong></pre><pre><strong> response.body = { message: &#39;OK&#39; }</strong></pre><pre><strong> response.status = 200</strong></pre><pre><strong>}</strong></pre><pre>export { getGames, getGame, <strong>addGame </strong>}</pre><p>routers.ts</p><pre>import { Router }from &#39;https://deno.land/x/oak/mod.ts&#39;;</pre><pre>import { getGames, getGame, <strong>addGame </strong>} from &#39;./controller.ts&#39;</pre><pre>const router = new Router()</pre><pre>router</pre><pre>.get(&#39;/games&#39;, getGames)</pre><pre>.get(&#39;/games/:id&#39;, getGame)</pre><pre><strong>.post(&#39;/games&#39;, addGame)</strong></pre><pre>export default router</pre><p>Test on Post</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/1*1qOTrGkciubWMe2CxGLG6A.png" /></figure><p>ลอง get ตัวที่เรา add เข้าไปจาก <a href="http://localhost:8000/game/0">http://localhost:8000/games/</a>3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*BvP2PijG0N_VObOJveLR-g.png" /></figure><ul><li><strong>PUT API</strong></li></ul><p>controller.ts</p><pre><strong>const updateGame = async ({ params, request, response }: { params: { id: string }; request: any; response: any }) =&gt; {</strong></pre><pre><strong> // ค้นหาว่ามีข้อมูลที่ต้องการ update อยู่รึเปล่า<br> let game: IGame | undefined = searchGameByID(params.id)</strong></pre><pre><strong> if (game) {</strong></pre><pre><strong> const body = await request.body()</strong></pre><pre><strong> // แกะเอาข้อมูลที่ต้องการ update<br> const updateInfos: { name?: string; hits?: number } = body.value</strong></pre><pre><strong> game = { ...game, ...updateInfos}</strong></pre><pre><strong> games = [...games.filter(obj =&gt; obj.id !== Number(params.id)),    game]</strong></pre><pre><strong>  response.status = 200</strong></pre><pre><strong>  response.body = { message: &#39;OK&#39; }</strong></pre><pre><strong> } else {</strong></pre><pre><strong>  response.status = 404</strong></pre><pre><strong>  response.body = { message: `Game not found` }</strong></pre><pre><strong> }</strong></pre><pre><strong>}</strong></pre><pre>export { getGames, getGame, addGame, <strong>updateGame </strong>}</pre><p>routers.ts</p><pre>import { Router }from &#39;https://deno.land/x/oak/mod.ts&#39;;</pre><pre>import { getGames, getGame, addGame, <strong>updateGame </strong>} from &#39;./controller.ts&#39;</pre><pre>const router = new Router()</pre><pre>router</pre><pre>.get(&#39;/games&#39;, getGames)</pre><pre>.get(&#39;/games/:id&#39;, getGame)</pre><pre>.post(&#39;/games&#39;, addGame)</pre><pre><strong>.put(&#39;/games/:id&#39;, updateGame)</strong></pre><pre>export default router</pre><p>Test Put</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*9oqxKGpqaewWOc8FrUTpKw.png" /></figure><p>เช็คผลลัพธ์ว่า update สำเร็จไหม <a href="http://localhost:8000/games/0">http://localhost:8000/games/0</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*LflxgQC5vBlH3qwoJUpcbg.png" /></figure><ul><li><strong>DELETE API</strong></li></ul><p>controller.ts</p><pre><strong>const deleteGame = ({ params, response }: { params: { id: string }; response: any }) =&gt; {</strong></pre><pre><strong> // ค้นหาตัวที่จะ delete<br> let game: IGame | undefined = searchGameByID(params.id)</strong></pre><pre><strong> if (game) {</strong></pre><pre><strong> // filter ตัวที่ต้องการออกไป <br> games = games.filter(game =&gt; game.id !== Number(params.id))</strong></pre><pre><strong>  response.body = { message: &#39;OK&#39; }</strong></pre><pre><strong>  response.status = 200</strong></pre><pre><strong> } else {</strong></pre><pre><strong>  response.status = 404</strong></pre><pre><strong>  response.body = { message: `Game not found` }</strong></pre><pre><strong> }</strong></pre><pre><strong>}</strong></pre><pre>export { getGames, getGame, addGame, updateGame, <strong>deleteGame</strong> }</pre><p>routers.ts</p><pre>import { Router }from &#39;https://deno.land/x/oak/mod.ts&#39;;</pre><pre>import { getGames, getGame, addGame, updateGame, <strong>deleteGame </strong>} from &#39;./controller.ts&#39;</pre><pre>const router = new Router()</pre><pre>router</pre><pre>.get(&#39;/games&#39;, getGames)</pre><pre>.get(&#39;/games/:id&#39;, getGame)</pre><pre>.post(&#39;/games&#39;, addGame)</pre><pre>.put(&#39;/games/:id&#39;, updateGame)</pre><pre><strong>.delete(&#39;/games/:id&#39;, deleteGame)</strong></pre><pre>export default router</pre><p>Test API</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*fKxb7HjKiNPgGYB0SLn1zQ.png" /></figure><p>เช็คผลลัพธ์ว่า delete สำเร็จไหม <a href="http://localhost:8000/games/0">http://localhost:8000/games/0</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*50Z8y2e0z5LmfNcXgywrEQ.png" /></figure><p><strong>สรุป</strong></p><p>ส่วนตัวเท่าที่เล่นมาสรุปได้ประมาณนี้ครับ</p><p>ข้อดี</p><ul><li>เขียนง่าย</li><li>เบาเพราะไม่มี node_module</li></ul><p>ข้อเสีย</p><ul><li>ยังใหม่อยู่ทำให้ยังไม่ค่อยมีคนมา contribute พวก dependecies ต่างๆแต่คิดว่าแค่ที่มีอยู่ก็น่าจะเยอะแล้ว</li></ul><p><strong>อ้างอิงจาก</strong></p><ul><li><a href="https://dev.to/am77/deno-v1-0-303j">https://dev.to/am77/deno-v1-0-303j</a></li><li><a href="https://www.borntodev.com/2020/05/21/deno-%E0%B8%99%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B9%83%E0%B8%AB%E0%B8%A1%E0%B9%88%E0%B8%A1%E0%B8%B2%E0%B9%81%E0%B8%A3%E0%B8%87/">https://www.borntodev.com/2020/05/21/deno-%E0%B8%99%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B9%83%E0%B8%AB%E0%B8%A1%E0%B9%88%E0%B8%A1%E0%B8%B2%E0%B9%81%E0%B8%A3%E0%B8%87/</a></li><li><a href="https://deno.land/">https://deno.land/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d41f0a79710e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use multiple Node JS version]]></title>
            <link>https://medium.com/@goghha/how-to-use-multiple-node-js-version-dfcf395dacbe?source=rss-a8b49130c33c------2</link>
            <guid isPermaLink="false">https://medium.com/p/dfcf395dacbe</guid>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[nvm]]></category>
            <dc:creator><![CDATA[Gogh Ha]]></dc:creator>
            <pubDate>Sun, 21 Jul 2019 06:01:07 GMT</pubDate>
            <atom:updated>2020-03-26T02:09:56.778Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XivFYwCQRfAqhziMrbOptg.jpeg" /></figure><p>How to use multiple Node JS version</p><p>วันนี้ก็จะมาเล่าเรื่องของ พระเอกของยุคนี้ อย่าง Node JS ที่ทำให้เกิด Framework ใหม่ๆ ทั้ง Font-End และ Back-End กันนะครับ</p><p>จุดเริ่มต้นที่อยากแชร์เรื่องนี้ เพราะว่าสมัยที่ผมเพิ่งหัด Dev ใหม่ๆ ผมมักจะมีปัญหากับการใช้ Node version มาก เพราะ บางครั้ง version ของเราใหม่มากๆ เวลาจะกลับไปแก้ งานเก่าๆ ก็ต้องลง Node ใหม่ซึ่งทำให้เสียเวลามาก จนวันนึงผม ได้รู้จัก NVM</p><h3>NVM คือ?</h3><p><strong>NVM (Node Version Management)</strong> เป็น package ที่ช่วยให้เราจัดการ Node บนเครื่องของเราได้ง่ายมากขึ้น ทำให้เราสามารถใช้ Node ได้หลายๆ เวอร์ชั่นโดยไม่ต้องลบเเล้วลงใหม่ทุกครั้ง หรือจะ install เวอร์ชั่นเก่าๆ เราก็ไม่ต้องไปไล่หาตัว install สามารถ install ผ่าน NVM ได้เลย</p><h3>NVM ต้องลงยังไง?</h3><p>-windows</p><p>เข้าไปที่ <a href="https://github.com/coreybutler/nvm-windows/releases">https://github.com/coreybutler/nvm-windows/releases</a></p><p>จะเจอหน้าแบบนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TLBeJcJLHghVWPXngzpq5Q.png" /></figure><p>เลือก Download <a href="https://github.com/coreybutler/nvm-windows/releases/download/1.1.7/nvm-setup.zip">nvm-setup.zip</a></p><p>จะได้ไฟล์แบบนี้ คลิกที่ nvm-setup.exe ได้เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/1*jee3VD_52AWpEIbt_fm73Q.png" /></figure><p>พอเข้าไปก็จะเจอหน้าแบบนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*44I8_EFB_1sVdGOwLpItaw.png" /></figure><p>ลงตามขั้นตอนไปเรื่อยๆเลยจ้า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*Co6ml_qr6NWEMZQf_OUtOQ.png" /></figure><p>พอลงเสร็จแล้วก็ทดสอบดูว่าใช้ได้แล้วยัง</p><p>เปิด command ขึ้นมาแล้วรัน</p><pre>nvm</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*-Xz4EhtMuY7TuD7eEMCULw.png" /></figure><p>ถ้าได้หน้าตาประมาณนี้แสดงว่าพร้อมใช้งานแล้ว</p><h3>NVM ใช้ยังไง?</h3><p>คำสั่งหลักๆที่ผมใช้บ่อยๆก็จะมี</p><p>คำสั่งติดตั้ง node version</p><pre>nvm install x.x.x</pre><pre>if you found this</pre><pre>Could not retrieve  <a href="https://nodejs.org/dist/latest/SHASUMS256.txt.">https://nodejs.org/dist/latest/SHASUMS256.txt.</a></pre><pre>please try to add proxy</pre><pre>nvm proxy [url]</pre><pre>or</pre><pre>nvm install x.x.x --insecure</pre><p>ตัวอย่างการลง node version 8.0.0</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*who1MmDtJbbHwLqL58as2g.png" /></figure><p>คำสั่งที่ดูว่าในเครื่องของเรามี node verion อะไรบ้าง</p><pre>nvm list</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*_CasIkrzALECrNGEIo9ZJA.png" /></figure><p>คำสั่งสุดท้ายที่จะแนะนำ</p><pre>nvm use x.x.x</pre><p>คือคำสั่งใช้ node version ที่เราต้องการเปลี่ยน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*mjAkzZkb7kOC9IUrrdub5Q.png" /></figure><p>credit:</p><ul><li><a href="https://danielarancibia.wordpress.com/2017/03/28/install-or-upgrade-nodejs-with-nvm-for-windows/">Install or Upgrade NodeJS with NVM for Windows</a></li><li><a href="https://github.com/coreybutler">coreybutler - Overview</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dfcf395dacbe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JS มือใหม่มาหัดใช้ undersrocejs กันเถอะ]]></title>
            <link>https://medium.com/@goghha/js-%E0%B8%A1%E0%B8%B7%E0%B8%AD%E0%B9%83%E0%B8%AB%E0%B8%A1%E0%B9%88%E0%B8%A1%E0%B8%B2%E0%B8%AB%E0%B8%B1%E0%B8%94%E0%B9%83%E0%B8%8A%E0%B9%89-undersrocejs-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-a6cd35e9d37f?source=rss-a8b49130c33c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6cd35e9d37f</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Gogh Ha]]></dc:creator>
            <pubDate>Sun, 02 Jun 2019 19:39:35 GMT</pubDate>
            <atom:updated>2019-06-02T19:39:35.329Z</atom:updated>
            <content:encoded><![CDATA[<p>เคยไหมเวลาเขียน filter ข้อมูลแต่ไม่รู้จะทำยังไง เคยไหมเวลาที่เราต้องทำอะไรกับ Array รู้สึกว่ามันยุ่งยาก วันนี้ผมจะมา ทำให้ชีวิตคุณง่ายขึ้นด้วย UnderscoreJS</p><p><strong>underscorejs คืออะไร?</strong></p><p>underscorejs คือ javascript libraly ที่รวบรวมเอาคำสั่งที่ใช้จัดการ array และ object แพ็ครวมกันให้เราสามารถเขียนโค้ดได้ง่ายขึ้น และยังทำให้โค้ดของเราดู clean ขึ้นด้วย</p><p>How to set up underscorejs in Project?</p><p>ในที่นี้ผมจะ set up underscroejs ในโปรเจคของ Angular นะครับเพื่อนๆสามารถเข้าไปดูวิธี set up เบื้องต้นได้<a href="https://underscorejs.org/">ที่นี่</a></p><p>อันดับแรกเราก็มาสร้างโปรเจคกันก่อนโดยใช้คำสั่ง</p><pre>ng new testProjectUnderscorejs</pre><p>หลังจากสร้างเสร็จก็ไปที่โปรเจคของเราโดย</p><pre>cd testProjectUnderscorejs</pre><p>ลองทดสอบดูว่าโปรเจคที่เราสร้างใช้ได้ไหมโดยใช้คำสั่ง</p><pre>ng serve --open</pre><p>ก็จะได้หน้าตาประมาณนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j2YsmUrVMYe_3n4_9MdWtg.png" /></figure><p>ที่นี้ก็มาถึงพระเอกของเรากันแล้วเปิด terminal ขึ้นมาแล้วพิมพ์</p><pre>npm install underscore --save</pre><p>เริ่มใช้กันเถอะในหน้า app.component.ts เราจะ import underscorejs เข้ามาแบบนี้</p><pre>import { _ } from &#39;underscore&#39;;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xfu6wzBd4A1919pLTeNwvQ.png" /></figure><p>ในส่วนของตัวอย่างผมจะขออธิบายเฉพาะตัวที่ผมคิดว่าทุกคนน่าจะได้ใช้บ่อยนะครับ</p><p>เรามาเริ่มฟังก์ชันแรกกันเลย _.<strong>each()</strong></p><p>คือฟังก์ชั่นที่ใช้ทำการวนลูปหลายๆคนก็คงสงสัยในเมื่อ es6 มี forEach ทำไมถึงใช้ตัวนี้ เพราะตัว each ตัวนี้มันไม่ได้ support แค่ Array น่ะสิ มันวน object ได้ด้วย Ohhh Wowwwwwwwwwwwwww</p><p>Example of _.<strong>each()</strong></p><pre>constructor() {</pre><pre>    _.each([1, 2, 3], this.log);</pre><pre>    _.each({one: 1, two: 2, three: 3}, this.log);</pre><pre>}</pre><pre>log(item) {</pre><pre>    console.log(item);</pre><pre>}</pre><p><strong>_.map()</strong></p><p>ตัวนี้ก็คือการที่เราวนลูปแล้วต้องการเปลี่ยนแปลงค่าของข้อมูลข้างในนะครับก็เหมือนกับข้างบนก็คือตัวนี้จะ support ทั้ง Array และ Object นะครับ</p><p>Example of <strong>_.map()</strong></p><pre>อันแรกจะเป็นการเอา Array ทุกตำแหน่งมา คูณด้วย 3 </pre><pre>_.map([1, 2, 3], function(num){ return num * 3; });<br>=&gt; [3, 6, 9]</pre><pre>อันนี้ก็คือการเอา object ทุกตำแหน่งมา คูณด้วย 3</pre><pre>_.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });<br>=&gt; [3, 6, 9]</pre><pre>อันนี้อาจจะทำให้งงนิดนึงนะครับ ให้มองเป็น ตัว map มี 2 element พอเข้าแต่ละรอบ จะหยิบเฉพาะ element ตำแหน่งที่ 0 ออกมาก็จะได้ผลลัพธ์ตามด้านล่างเลยครับ</pre><pre>_.map([[1, 2], [3, 4]], _.first);<br>=&gt; [1, 3]</pre><p><strong>_.find()</strong></p><p>ตัวนี้ก็ตามชื่อเลยครับคือจะเข้าไปหาตัวที่ตรงเงื่อนไขตัวแรกและ return ตัวนั้นออกมาจะมีข้อดีคือบ้างครั้งเรา find หาในArray of Object เราจะได้เป็นตำแหน่งมาซึ่งผมรู้สึกว่ามันไม่ค่อยสวยเท่าไหร่แต่ตัวนี้จะได้ เเป็น object มาให้เลยสามารถเอาไปใช้ต่อได้สบายมากๆ</p><p>Example of <strong>_.find()</strong></p><pre>อันนี้คือการที่หาตัวที่ 2 หารลงตัวเป็นตัวแรกครับ</pre><pre>_.find([1, 2, 3, 4, 5, 6], (num) =&gt; { return num % 2 === 0; });</pre><pre>=&gt; 2</pre><pre>_.find([{name: &#39;John&#39;, id: 1},{name: &#39;John&#39;, id: 2},] (item) =&gt; {</pre><pre>   return item.name === &#39;John&#39;;<br>} );</pre><pre>=&gt; {name: &#39;John&#39;, id: 1}</pre><p><strong>_.sortBy()</strong></p><p>ก็ตามชื่อเลยครับ มันคือการ sort ซึ่งเราสามารถเลือกได้ว่าเราจะ sort จาก field อะไรโดยที่ default จะ sort by ascending ถ้าเรา จะ sort by descending<strong> </strong>ให้เราเติม reverse()</p><pre><br>var stooges = [{name: &#39;moe&#39;, age: 40}, {name: &#39;larry&#39;, age: 50}, {name: &#39;curly&#39;, age: 60}];</pre><pre><strong>Ascending</strong></pre><pre>_.sortBy(stooges, &#39;name&#39;);<br>=&gt; [{name: &#39;curly&#39;, age: 60}, {name: &#39;larry&#39;, age: 50}, {name: &#39;moe&#39;, age: 40}];</pre><pre><strong>Descending</strong></pre><pre>_.sortBy(stooges, &#39;name&#39;).reverse();<br>=&gt; [{name: &#39;moe&#39;, age: 40}, {name: &#39;larry&#39;, age: 50}, {name: &#39;curly&#39;, age: 60}];</pre><p><strong>_.filter()</strong></p><p>ตัวนี้ก็คือการ เอาเฉพาะค่าที่เราต้องการมาใช้ครับอาจจะพลิกไปใช้สำหรับการค้นหาข้อมูลสำหรับ filter table ก็ได้</p><p>Example of filter Basic</p><pre>const evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });<br>=&gt; [2, 4, 6]</pre><p>Example of filter show data</p><pre>let showData = [{name: &#39;A&#39;, status: &#39;admin&#39;}, {name: &#39;B&#39;, status: &#39;user&#39;}];<br>let allData = [{name: &#39;A&#39;, status: &#39;admin&#39;}, {name: &#39;B&#39;, status: &#39;user&#39;}, {name: &#39;C&#39; status: &#39;user&#39;}];</pre><pre>สมมุติว่าเราต้องการข้อมูลเฉพาะ user</pre><pre>showData = _.filter(allData, (item) =&gt; { return item.status === &#39;user&#39;;});</pre><pre>=&gt; [ {name: &#39;B&#39;, status: &#39;user&#39;}, {name: &#39;C&#39;, status: &#39;user&#39;}]</pre><p><strong>_.isEmpty()</strong></p><p>เคยไหมที่เวลาคุณเขียนดัก if ไว้แต่ function ดันเข้าทั้งๆที่ไม่มี data ส่วนใหญ่จะเกิดจากเคสที่ ข้อมูล default มาเป็น {} เรามีตัวนี้ไว้ช่วยเช็คค่าต่างๆว่ามีค่าไหมโดยจะ return true ถ้าเกิดไม่มีข้อมูล</p><pre>_.isEmpty([1, 2, 3]);<br>=&gt; false<br>_.isEmpty({});<br>=&gt; true</pre><p>Use case</p><pre>const data = {};</pre><pre>if (!_.isEmpty(data)) {</pre><pre>  // do some thing<br>}</pre><p><strong>_.isEqual()</strong></p><p>คือฟังก์ชันที่เอาไว้ compare ค่าว่าเท่ากันรึเปล่าซึ่งโดยปกติเราจะไม่สามารถนำ object 2 ตัวมา compare แล้วหาว่ามันเท่ากันได้ไหมแต่ตัวนี้ทำได้นะเอ่อ</p><pre>const stooge = {name: &#39;moe&#39;, luckyNumbers: [13, 27, 34]};<br>const clone  = {name: &#39;moe&#39;, luckyNumbers: [13, 27, 34]};</pre><pre>ปกติ compare ไม่ได้<br>stooge === clone;<br>=&gt; false</pre><pre>แต่ _.isEqual ทำได้นะ<br>_.isEqual(stooge, clone);<br>=&gt; true</pre><p><strong>_.union()</strong></p><p>คงจะคุ้นๆชื่อกันเหมือนตอนเรียน set ใช่ไหมละ แน่นอนมันทำงานเหมือน set ในวิชาคณิตศาสตร์เลยแหละก็คือเอาทุกตัวที่มี มา group เป็นก้อนเดียว</p><pre>_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);<br>=&gt; [1, 2, 3, 101, 10]</pre><p><strong>_.intersection()</strong></p><p>เหมือนกันอีกแล้วในคณิตศาสตร์ตัวนี้จะเอาทุกตัวที่ซ้ำกันมา group ด้วยกัน</p><pre>_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);<br>=&gt; [1, 2]</pre><p><strong>_.difference()</strong></p><p>อันนี้ก็เช่นกันโดยตัวนี้จะเอาตัวหน้าตั้ง แล้วตัวหลังมีอะไร กะจะหยิบจากตัวหน้าไป พูด งงๆไปดูตัวอย่างเลยจ้า</p><pre>_.difference([1, 2, 3, 4, 5], [5, 2, 10]);<br>=&gt; [1, 3, 4]</pre><p><strong>_.uniq()</strong></p><p>อันนี้ส่วนตัว love มันมากเพราะช่วยให้จัดการอะไรหลายๆอย่างง่ายขึ้นเยอะ โดยจะทำการหยิบทุกตัวมา ละทำให้ตัวที่ซ้ำกันอยู่หายปายยยย</p><pre>_.uniq([1, 2, 1, 4, 1, 3]);<br>=&gt; [1, 2, 4, 3]</pre><p><strong>_.zip() and _.unzip()</strong></p><p>อันนี้ต้องมาเป็นแพคคู่เพราะทำให้เรา grouping ข้อมูลได้ wow สุดๆ</p><pre>เอาแต่ละ Array มา group กันง่ายๆ</pre><pre>_.zip([&#39;moe&#39;, &#39;larry&#39;, &#39;curly&#39;], [30, 40, 50], [true, false, false]);<br>=&gt; [[&quot;moe&quot;, 30, true], [&quot;larry&quot;, 40, false], [&quot;curly&quot;, 50, false]]</pre><pre>เอาเข้าเราก็เอาออกได้โดย</pre><pre>_.unzip([[&quot;moe&quot;, 30, true], [&quot;larry&quot;, 40, false], [&quot;curly&quot;, 50, false]]);<br>=&gt; [[&#39;moe&#39;, &#39;larry&#39;, &#39;curly&#39;], [30, 40, 50], [true, false, false]]<br></pre><p><strong>_.object()</strong></p><p>อันนี้ก็ตัวสุด love อีกตัวที่ทำให้ Array ของเราสามารถแปลงเป็น object แบบชิลๆ</p><pre>_.object([&#39;moe&#39;, &#39;larry&#39;, &#39;curly&#39;], [30, 40, 50]);<br>=&gt; {moe: 30, larry: 40, curly: 50}<br><br>_.object([[&#39;moe&#39;, 30], [&#39;larry&#39;, 40], [&#39;curly&#39;, 50]]);<br>=&gt; {moe: 30, larry: 40, curly: 50}</pre><p>คงจะลายตากันไปหมดแล้วงั้นวันนี้ผมขอพอไว้เท่านี้ก่อนดีกว่า ผมก็หวังว่า post นี้จะมีประโยชน์ไม่มากก็น้อยสำหรับน้องๆ ที่เริ่มเขียน js กันนะครับ ถ้ามีส่วนไหนพิดพลาดรบกวน comment บอกด้วยนะครับ ไม่แน่ใจว่าจะอ่านกันรู้เรื่องรึเปล่า 55555</p><p>ขอขอบคุณ <a href="https://underscorejs.org/">underscorejs</a> ที่ทำให้เราเขียน โค้ดได้ง่ายขึ้น <a href="https://underscorejs.org/">https://underscorejs.org</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6cd35e9d37f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>