<?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 Truong Trong Hai on Medium]]></title>
        <description><![CDATA[Stories by Truong Trong Hai on Medium]]></description>
        <link>https://medium.com/@truongtronghai?source=rss-2014203633b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mT1CWe0mxfGM223RWVB8jg.png</url>
            <title>Stories by Truong Trong Hai on Medium</title>
            <link>https://medium.com/@truongtronghai?source=rss-2014203633b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 14:47:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@truongtronghai/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[useMemo vs useCallback]]></title>
            <link>https://medium.com/@truongtronghai/usememo-vs-usecallback-92884a025bc8?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/92884a025bc8</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Thu, 28 Aug 2025 09:23:25 GMT</pubDate>
            <atom:updated>2025-08-28T09:23:25.912Z</atom:updated>
            <content:encoded><![CDATA[<p>Both useMemo and useCallback are React hooks that help you optimize performance by memoizing values or functions — meaning they prevent unnecessary recalculations or re-creations during re-renders.</p><p>However:</p><h3>🔁 useMemo</h3><ul><li>What it does: Runs a function and returns a memoized value.</li><li>When it runs: Only when the dependencies change.</li><li>Purpose: Avoids recalculating expensive values on every render.</li></ul><pre>const result = useMemo(() =&gt; computeSomething(a, b), [a, b]);<br>// computeSomething is executed only when a or b changes</pre><p>✅ So yes, useMemo()executes the function and stores the result.</p><h3>🔁 useCallback</h3><ul><li>What it does: Returns a memoized function.</li><li>When it runs: It does not execute the function — it just returns the same function reference unless dependencies change.</li><li>Purpose: Prevents re-creating functions unnecessarily (useful for passing to child components or dependencies in useEffect).</li></ul><pre>const handleClick = useCallback(() =&gt; {<br>  console.log(&quot;Clicked!&quot;);<br>}, []);<br>// handleClick is the same function reference unless dependencies change</pre><p>✅ So yes, useCallback()does not execute the function, it just returns a stable function reference.</p><h3>🧠 Summary</h3><p>Hook: useMemo</p><p>Executes Function: ✅ Yes</p><p>Returns: Value</p><p>Use Case: Expensive calculations</p><p>Hook: useCallback</p><p>Executes Function: ❌ No</p><p>Returns: Function</p><p>Use Case: Stable function reference</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92884a025bc8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Secure Your Frontend: Strip Logs and Comments with Terser Like a Pro ️]]></title>
            <link>https://medium.com/@truongtronghai/secure-your-frontend-strip-logs-and-comments-with-terser-like-a-pro-%EF%B8%8F-07218f2739d7?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/07218f2739d7</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Wed, 27 Aug 2025 06:26:55 GMT</pubDate>
            <atom:updated>2025-08-27T06:26:55.367Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nZPxzgTVZUUkkljP" /><figcaption>Photo by <a href="https://unsplash.com/@jackson_893?utm_source=medium&amp;utm_medium=referral">Michael Geiger</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As developers, we often use console.log and comments to debug and document our code. But did you know these can pose security risks if left in production?</p><p>Here’s why:</p><p>🔓 Sensitive Info Exposure<br>Logs and comments can unintentionally reveal API keys, tokens, internal endpoints, or user data. In client-side JavaScript, this is visible to <em>anyone</em> via browser dev tools.</p><p>🕵️‍♂️ Information Disclosure<br>Logs can expose internal logic, making it easier for attackers to understand and exploit your app.</p><p>🐢 Performance &amp; Clutter<br>Excessive logging slows down performance and clutters the console, making debugging harder.</p><h3>✅ Solution: Use Terser to Clean Your Code</h3><p>Terser is a powerful minifier that helps strip out comments and console.log statements during your build process.</p><h4>🔧 Webpack Example:</h4><pre>optimization: {<br>  minimize: true,<br>  minimizer: [new TerserPlugin({<br>    terserOptions: {<br>      compress: { drop_console: true },<br>      format: { comments: false },<br>    },<br>    extractComments: false,<br>  })],<br>}</pre><h4>🔧 Vite Example:</h4><p>Option 1: Using vite-plugin-terser</p><pre>import { terser } from &#39;vite-plugin-terser&#39;;<br><br>plugins: [<br>  terser({<br>    compress: { drop_console: true, drop_debugger: true },<br>    format: { comments: false },<br>  }),<br>]</pre><p>Option 2: Native Vite config</p><pre>build: {<br>  minify: &#39;terser&#39;,<br>  terserOptions: {<br>    compress: { drop_console: true, drop_debugger: true },<br>    format: { comments: false },<br>  },<br>}</pre><h3>🛡️ Final Tip</h3><p>Always sanitize your code before deployment. Removing logs and comments is a simple but powerful step toward better security and performance.</p><p>Have you already integrated Terser into your build process? If not, now’s a great time to start!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=07218f2739d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Customize Dropdown Styles in Ant Design Using Plain CSS (Not CSS Modules)]]></title>
            <link>https://medium.com/@truongtronghai/how-to-customize-dropdown-styles-in-ant-design-using-plain-css-not-css-modules-2eefb2699031?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2eefb2699031</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Thu, 14 Aug 2025 04:23:54 GMT</pubDate>
            <atom:updated>2025-08-14T04:23:54.093Z</atom:updated>
            <content:encoded><![CDATA[<p>Sometime, CSS module cannot solve the problem of changing some default styles of Dropdown ( or other components )in Ant Design. I have used this as workaround.</p><p>For example:</p><p>I want to change the background of selected item in Dropdown.</p><pre><br>div.myClass ul li.ant-dropdown-menu-item-selected {<br>    background-color: red !important;<br>}</pre><p>And in the Dropdown, I used prop: <strong>overlayClassName</strong></p><pre>&lt;Dropdown<br>  trigger={[&quot;click&quot;]}<br>  overlayClassName=&quot;myClass&quot;&gt;<br>// some code<br>&lt;/Dropdown&gt;</pre><h3>Conclusion</h3><p>If you struggle with customizing CSS for components in Ant Design after trying module CSS, let try my way as a solution.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2eefb2699031" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ How to Disable Future Dates in Ant Design’s RangePicker (with Custom Styling)]]></title>
            <link>https://medium.com/@truongtronghai/how-to-disable-future-dates-in-ant-designs-rangepicker-with-custom-styling-06c13438db75?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/06c13438db75</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Wed, 13 Aug 2025 09:21:53 GMT</pubDate>
            <atom:updated>2025-08-13T10:16:33.851Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Disable Future Dates in Ant Design’s RangePicker (with Custom Styling)</h3><p>When working with date ranges in web applications, it’s common to restrict users from selecting future dates — especially in dashboards, reports, or logs. Ant Design’s RangePicker makes this easy with the disabledDate prop. But what if you want to go a step further and customize how disabled dates look?</p><p>In this tutorial, we’ll walk through a practical example using Ant Design’s RangePicker with:</p><ul><li>✅ Disabled future dates</li><li>🎨 Custom styling for disabled dates</li><li>🧠 A clean, reusable approach</li></ul><h3>🔧 The Code</h3><p>Here’s the full snippet we’ll break down:</p><pre>&lt;RangePicker<br>  className=&quot;createDateRangePicker&quot;<br>  format={dateFormat}<br>  onChange={onRangeChange}<br>  disabledDate={isFutureDate}<br>  cellRender={(<br>      current: string | number | dayjs.Dayjs<br>  ) =&gt; {<br>      const style: React.CSSProperties = {};<br>      const isDisabled =<br>          dayjs.isDayjs(current) &amp;&amp;<br>          isFutureDate(current);<br><br>      if (isDisabled) {<br>          style.color = &quot;#c4c4cc&quot;;<br>          style.pointerEvents = &quot;auto&quot;;<br>          style.cursor = &quot;not-allowed&quot;;<br>      }<br><br>      return isDisabled ? (<br>          &lt;Tooltip<br>              overlayInnerStyle={{ color: &quot;red&quot; }}<br>              title=&quot;Future date is not allowed&quot;<br>          &gt;<br>              &lt;div<br>                  className=&quot;ant-picker-cell-inner&quot;<br>                  style={style}<br>              &gt;<br>                  {dayjs.isDayjs(current)<br>                      ? current.date()<br>                      : current}<br>              &lt;/div&gt;<br>          &lt;/Tooltip&gt;<br>      ) : (<br>          &lt;div<br>              className=&quot;ant-picker-cell-inner&quot;<br>              style={style}<br>          &gt;<br>              {dayjs.isDayjs(current)<br>                  ? current.date()<br>                  : current}<br>          &lt;/div&gt;<br>      );<br>  }}<br>/&gt;</pre><h3>🧩 Step-by-Step Breakdown</h3><h3>1. Disable Future Dates</h3><pre>disabledDate={isFutureDate}</pre><p>This prop takes a function that returns true for dates that should be disabled. A typical implementation might look like this:</p><pre>const isFutureDate = (current: dayjs.Dayjs) =&gt; {<br>  return current &amp;&amp; current &gt; dayjs().endOf(&#39;day&#39;);<br>};</pre><p>This ensures that only today and past dates are selectable.</p><h3>2. Custom Cell Rendering with cellRender</h3><pre>cellRender={(<br>    current: string | number | dayjs.Dayjs<br>) =&gt; {<br>    const style: React.CSSProperties = {};<br>    const isDisabled =<br>        dayjs.isDayjs(current) &amp;&amp;<br>        isFutureDate(current);<br><br>    if (isDisabled) {<br>      style.color = &quot;#c4c4cc&quot;; // Light gray text<br>      style.pointerEvents = &quot;auto&quot;; // Allow hover for tooltip<br>      style.cursor = &quot;not-allowed&quot;; // Show &quot;not allowed&quot; cursor<br>    }<br><br>    return isDisabled ? (<br>        &lt;Tooltip<br>            overlayInnerStyle={{ color: &quot;red&quot; }}<br>            title=&quot;Future date is not allowed&quot;<br>        &gt;<br>            &lt;div<br>                className=&quot;ant-picker-cell-inner&quot;<br>                style={style}<br>            &gt;<br>                {dayjs.isDayjs(current)<br>                    ? current.date()<br>                    : current}<br>            &lt;/div&gt;<br>        &lt;/Tooltip&gt;<br>    ) : (<br>        &lt;div<br>            className=&quot;ant-picker-cell-inner&quot;<br>            style={style}<br>        &gt;<br>            {dayjs.isDayjs(current)<br>                ? current.date()<br>                : current}<br>        &lt;/div&gt;<br>    );<br>}}</pre><ul><li>These styles visually indicate that the date is not selectable.</li><li>pointerEvents: &quot;auto&quot; is important —<strong> it allows the tooltip to work</strong>.</li><li><strong>Be aware that</strong> it overrides all default visual states (like selected, in-range, today). If you only want to change the text color and keep everything else, make sure to have this: <br><strong><em>className=”ant-picker-cell-inner”</em></strong></li></ul><h3>💡 Why Use Both disabledDate and cellRender?</h3><ul><li>disabledDate ensures the date is functionally disabled (e.g., can&#39;t be selected).</li><li>cellRender lets you visually customize how disabled dates appear.</li></ul><p>Together, they create a more intuitive and polished user experience and give you full control over how <strong>each date cell</strong> looks — something disabledDate alone doesn’t offer.</p><h3>✅ Conclusion</h3><p>Ant Design’s RangePicker is powerful and flexible. By combining disabledDate with cellRender, you can create a date range picker that’s both <strong>user-friendly</strong> and <strong>visually clear</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=06c13438db75" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Timezone Behavior in Ant Design’s RangePicker with Day.js]]></title>
            <link>https://medium.com/@truongtronghai/understanding-timezone-behavior-in-ant-designs-rangepicker-with-day-js-1b1714d01405?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b1714d01405</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Wed, 13 Aug 2025 07:02:41 GMT</pubDate>
            <atom:updated>2025-08-13T07:02:41.078Z</atom:updated>
            <content:encoded><![CDATA[<p>When working with date and time pickers in modern web applications, handling timezones correctly is crucial — especially when your frontend uses libraries like Ant Design and Day.js. Recently, I encountered a subtle but important behavior when using Ant Design’s RangePicker component that’s worth sharing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TfIPVyuBRSqW-zyR" /><figcaption>Photo by <a href="https://unsplash.com/@icons8?utm_source=medium&amp;utm_medium=referral">Icons8 Team</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>🔍 The Scenario</h3><p>I used Ant Design’s RangePicker to select a date range. In the onChange handler, I received two values:</p><p>At first glance, this was confusing. Why are the dates values showing one day earlier than the dateStrings?</p><h3>🧠 The Explanation</h3><p>Here’s what’s happening under the hood:</p><ul><li>dateStrings are formatted in local time (in my case, UTC+7).</li><li>dates are Day.js objects, and their internal time is in UTC.</li></ul><p>So when I selected August 5 in the picker, it represented:</p><pre>2025/08/05 00:00:00 (local time, UTC+7)<br>→ 2025-08-04T17:00:00.000Z (UTC)</pre><p>This conversion is automatic and expected. Ant Design stores the time in UTC to avoid time zone-related bugs when sending data to servers or working across regions.</p><h3>✅ How to Handle It</h3><p>If you want to work with local time, you can convert the UTC dates back using Day.js:</p><p>If you prefer to work in UTC, just be aware that the time will be shifted by your time zone offset.</p><h3>🧩 Bonus Tip: Use dayjs.utc() for Consistency</h3><p>If you’re manipulating dates and want to ensure consistent UTC behavior, use the utc plugin:</p><p>This guarantees that you’re working in UTC context, avoiding unexpected shifts due to local timezone assumptions.</p><h3>🏁 Conclusion</h3><p>Timezone handling can be tricky, but understanding how Ant Design and Day.js interpret and format dates helps you avoid subtle bugs. Always be mindful of whether you’re working in local time or UTC, especially when formatting, storing, or transmitting date values.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b1714d01405" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Module CSS of React.js and Ant Design]]></title>
            <link>https://medium.com/@truongtronghai/module-css-of-react-js-and-ant-design-afd5b106d01b?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/afd5b106d01b</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Thu, 31 Jul 2025 06:23:09 GMT</pubDate>
            <atom:updated>2025-07-31T06:23:09.635Z</atom:updated>
            <content:encoded><![CDATA[<p>In my practical projects of FE with React.js and Ant Design ( AntD ), I often get the requests to apply custom styles to elements of AntD.</p><p>This is similar to change the default styles of AntD. In this case, I chose the approach of using Module CSS. However, there’re some notes for this topic and I will show them in this article.</p><p>To use Module CSS, first you need to import like this:</p><pre>import styles from &quot;@/core/css/SomeThings.module.css&quot;;</pre><h4>Sometime, deeply specific selectors will solve the problem</h4><pre>.i2-info-table &gt; div &gt; div &gt; div &gt; div &gt; div &gt; table &gt; thead &gt; tr th {<br>    background-color: var(--neutral-9) !important;<br>}</pre><p>If I just use this:</p><pre>.i2-info-table thead &gt; tr th {<br>    background-color: var(--neutral-9) !important;<br>}<br>/* Not working */</pre><p>the browsers cannot apply that style to elements I wanted.</p><h4>The module CSS classes aren’t loaded to DOM element if those classes were not in module CSS file.</h4><p>For example: we declared a class in React.js component like this:</p><pre>&lt;div<br>    className={<br>        styles[<br>            &quot;i2-nested-cell&quot;<br>        ]<br>    }<br>&gt;<br>    {value}<br>&lt;/div&gt;</pre><p>That class won’t be loaded to DOM ( view via Inspect of Developer Tool ) until we put that class in module CSS file:</p><pre>.i2-nested-cell {<br>    padding: 4px 0 !important;<br>}<br><br>tr:first-child .i2-nested-cell {<br>    padding-bottom: 4px !important;<br>    padding-top: 16px !important;<br>}<br><br>tr:last-child .i2-nested-cell {<br>    padding-bottom: 16px !important;<br>    padding-top: 4px !important;<br>}</pre><p>If you have any other notes for this topic, let leave a comment. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afd5b106d01b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Working with 2 ends of JavaScript array]]></title>
            <link>https://medium.com/@truongtronghai/working-with-2-ends-of-javascript-array-d17ccc160811?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/d17ccc160811</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Fri, 27 Jun 2025 09:05:25 GMT</pubDate>
            <atom:updated>2025-06-27T09:05:25.219Z</atom:updated>
            <content:encoded><![CDATA[<p>A simple JavaScript example that demonstrates all four array methods: push(), pop(), unshift(), and shift().</p><pre>let fruits = [&quot;apple&quot;, &quot;banana&quot;];<br><br>// Add to the end<br>fruits.push(&quot;cherry&quot;);<br>// [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<br><br>// Remove from the end<br>let last = fruits.pop();<br>console.log(&quot;After pop:&quot;, fruits); // [&quot;apple&quot;, &quot;banana&quot;]<br>console.log(&quot;Popped:&quot;, last);      // &quot;cherry&quot;<br><br>// Add to the beginning<br>fruits.unshift(&quot;mango&quot;);<br>console.log(&quot;After unshift:&quot;, fruits); // [&quot;mango&quot;, &quot;apple&quot;, &quot;banana&quot;]<br><br>// Remove from the beginning<br>let first = fruits.shift();<br>console.log(&quot;After shift:&quot;, fruits); // [&quot;apple&quot;, &quot;banana&quot;]<br>console.log(&quot;Shifted:&quot;, first);      // &quot;mango&quot;</pre><p>They’re simple but summarizing them will make us easily remember.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d17ccc160811" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Separating camelCase word to strings in JavaScript]]></title>
            <link>https://medium.com/@truongtronghai/separating-camelcase-word-to-strings-in-javascript-57924dac1fb8?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/57924dac1fb8</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Thu, 12 Jun 2025 02:28:47 GMT</pubDate>
            <atom:updated>2025-06-12T02:28:47.100Z</atom:updated>
            <content:encoded><![CDATA[<p>In my work, I often face the demand of change the camelCase word to strings. I learnt it and share with you this exciting issue.</p><p>This function inserts a space between lowercase and uppercase letters in a camelCase string. It’s useful for converting camelCase into a more readable format.</p><pre>export function separateCamelCase(str: string): string {<br>    return str.replace(/([a-z])([A-Z])/g, &quot;$1 $2&quot;);<br>}<br><br><br>separateCamelCase(&quot;helloWorld&quot;) <br>// Output: &quot;hello World&quot;<br><br>separateCamelCase(&quot;thisIsCamelCase&quot;) <br>// Output: &quot;this Is Camel Case&quot;</pre><h3>🧠 How it works:</h3><ol><li>Input: A string in camelCase format, like &quot;helloWorld&quot; or &quot;thisIsCamelCase&quot;.</li></ol><p>2. Regex Explanation:</p><ul><li>/([a-z])([A-Z])/g is a regular expression that:</li><li>([a-z]): Captures a lowercase letter.</li><li>([A-Z]): Immediately followed by an uppercase letter.</li><li>The g flag means it applies globally (to all matches in the string).</li></ul><p>3. Replacement:</p><ul><li>&quot;$1 $2&quot; means:</li><li>$1: The first captured group (the lowercase letter).</li><li>A space &quot; &quot;.</li><li>$2: The second captured group (the uppercase letter).</li></ul><p>4. Result: It returns a new string with spaces inserted between lowercase-uppercase transitions.</p><h3>This version DOES NOT use the &quot;g&quot; flag in the regular expression.</h3><pre>export function separateCamelCase(str: string): string {<br>    return str.replace(/([a-z])([A-Z])/, &quot;$1 $2&quot;);<br>}<br><br><br>separateCamelCase(&quot;thisIsCamelCase&quot;)<br>// Output: &quot;this IsCamelCase&quot;</pre><p>Only the first transition (sI) is changed to s I. The rest (CamelCase) is left untouched.</p><h3>✅ Summary:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*QETG_EnnR-cgKvZwy7A2FA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57924dac1fb8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[replace() — Javascript — with regex pattern]]></title>
            <link>https://medium.com/@truongtronghai/replace-javascript-with-regex-pattern-1594339ea7b6?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1594339ea7b6</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Wed, 19 Mar 2025 04:38:36 GMT</pubDate>
            <atom:updated>2025-03-19T04:38:36.052Z</atom:updated>
            <content:encoded><![CDATA[<h3>replace() — Javascript — with regex pattern</h3><p>In the demand of separating words in a camel Case string, I have learnt an exciting knowledge related to the way replace() function works.</p><p>This is the code I got from AI agent:</p><pre>function separateCamelCase(str) {<br>  return str.replace(/([a-z])([A-Z])/g, &#39;$1 $2&#39;);<br>}<br><br>// Example usage:<br>const camelCaseString = &quot;thisIsCamelCase&quot;;<br>const separatedWords = separateCamelCase(camelCaseString);<br>console.log(separatedWords); // Output: &quot;this Is Camel Case&quot;</pre><p>However, I don’t really know the progress of that function works. I had two more question to make clear my confusion.</p><h3>I still have confusion about that code.<br>In my knowledge, the result will be: “this” and “IsCamelCase”. They are not separated words.</h3><h3>How It Works</h3><ol><li><strong>Pattern</strong>: /([a-z])([A-Z])/g</li></ol><ul><li><strong>([a-z])</strong>: Matches any lowercase letter and captures it as the first group.</li><li><strong>([A-Z])</strong>: Matches any uppercase letter and captures it as the second group.</li><li><strong>g</strong>: Global flag to apply the pattern to all matches in the string.</li></ul><ol><li><strong>Replacement</strong>: &#39;$1 $2&#39;</li></ol><ul><li><strong>$1</strong>: Refers to the first captured group (lowercase letter).</li><li><strong>$2</strong>: Refers to the second captured group (uppercase letter).</li><li><strong>&#39; &#39;</strong>: Inserts a space between the two captured groups.</li></ul><h3>Example Breakdown</h3><p>For the string &quot;thisIsCamelCase&quot;:</p><ul><li>The pattern matches sI in &quot;thisIsCamelCase&quot;.</li><li>s is captured by ([a-z]) and I by ([A-Z]).</li><li>The replacement string &#39;$1 $2&#39; inserts a space between s and I, resulting in &quot;this IsCamelCase&quot;.</li></ul><p>Next, it matches sC in &quot;IsCamelCase&quot;:</p><ul><li>s is captured by ([a-z]) and C by ([A-Z]).</li><li>The replacement string &#39;$1 $2&#39; inserts a space between s and C, resulting in &quot;Is CamelCase&quot;.</li></ul><p>Finally, it matches lC in &quot;CamelCase&quot;:</p><ul><li>l is captured by ([a-z]) and C by ([A-Z]).</li><li>The replacement string &#39;$1 $2&#39; inserts a space between l and C, resulting in &quot;Camel Case&quot;.</li></ul><p>So, the final result is &quot;this Is Camel Case&quot;.</p><h3>But why function replace loop</h3><p>The replace() function with the global flag (`g`) doesn&#39;t explicitly use a for loop, but it internally iterates over the string to apply the replacement pattern to all matches. Here&#39;s how it works:</p><h3>How replace() with Global Flag Works</h3><ol><li><strong>Global Flag (`g`)</strong>: When you use the global flag in a regular expression, it tells the replace() function to find all matches in the string, not just the first one.</li><li><strong>Internal Iteration</strong>: The replace() function internally loops through the string to find each match of the pattern and applies the replacement for each match.</li></ol><h3>Example</h3><p>In your case:</p><pre>const camelCaseString = &quot;thisIsCamelCase&quot;;<br>const separatedWords = camelCaseString.replace(/([a-z])([A-Z])/g, &#39;$1 $2&#39;);<br>console.log(separatedWords); // Output: &quot;this Is Camel Case&quot;</pre><ul><li>The pattern /([a-z])([A-Z])/g matches each lowercase letter followed by an uppercase letter.</li><li>The replace() function finds each match and applies the replacement &#39;$1 $2&#39;.</li><li>This process is repeated for all matches in the string.</li></ul><h3>Internal Process</h3><p>Even though you don&#39;t see a for loop, the replace() function performs an internal iteration similar to this:</p><ol><li><strong>First Match</strong>: Finds sI in &quot;thisIsCamelCase&quot;, replaces it with &quot;s I&quot;, resulting in &quot;this IsCamelCase&quot;.</li><li><strong>Second Match</strong>: Finds sC in &quot;IsCamelCase&quot;, replaces it with &quot;s C&quot;, resulting in &quot;this Is CamelCase&quot;.</li><li><strong>Third Match</strong>: Finds lC in &quot;CamelCase&quot;, replaces it with &quot;l C&quot;, resulting in &quot;this Is Camel Case&quot;.</li></ol><p>This internal iteration ensures that all matches are processed and replaced accordingly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1594339ea7b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create simple Websocket-based API with AWS API Gateway]]></title>
            <link>https://medium.com/@truongtronghai/create-simple-websocket-based-api-with-aws-api-gateway-9ae987d145a3?source=rss-2014203633b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ae987d145a3</guid>
            <dc:creator><![CDATA[Truong Trong Hai]]></dc:creator>
            <pubDate>Sun, 12 Jan 2025 00:27:36 GMT</pubDate>
            <atom:updated>2025-01-12T00:27:36.366Z</atom:updated>
            <content:encoded><![CDATA[<p>I have struggled with this idea in many hours to learn a new knowledge for my career. Searching and asking AI just brought to me old version and useless insight.</p><p>Especially with <strong>Mock integration</strong>, the way AWS provides information is tangled and ambiguous.</p><p>Finally, I found a guy in Medium who set up an API with Lambda instead of Mock BE from AWS. I learnt and adjusted some things to produce this article.</p><p>I created this to save your time, who is stuck in the situation like me just for a simple lesson from WSS with API Gateway.</p><p>I took the screenshots to show what I did rather than typing because I don’t know better way to present what I did.</p><p>Please be patient and type according to my screenshots in some places, as they’re super easy and short.</p><h3>Step-by-step</h3><h4>Prepare 3 Lambda functions for routes</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*khOAj02ckFLxeGtv-EsL-g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2ojsvEDK1L2yGAY7Ky7kmQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qR_Ios_9QDLe4JIs5Yxk7g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1bIrtG_yywYwozDjiybg8w.png" /></figure><h4>Create API</h4><p>Create Websocket API with AWS API Gateway from AWS and the result you need has things like these images with 3 basic routes for our lesson:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*41QQhwIIAAqACqc2ofp3fw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DR5O3nMmhTOHP9OzjowcVA.png" /></figure><p>With “sendmessage” route, we need to “Enable two-way communication”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Phl9ckmn-cdeNk3WCumAw.png" /></figure><p>When everything is done, go here and get Websocket URL for test:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_eVbiogEN18WWSwBYAPMwQ.png" /></figure><p>Let enjoy the outcome of your work with Postman</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pXzJrim4QTq0oprXOPtVcQ.png" /><figcaption>First, connect to WSS</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BmaR0vd2NGqx7LMJ7y6NAA.png" /><figcaption>Successfully</figcaption></figure><h3>Conclusion</h3><p>Done.</p><p>When you come to this line, I hope you have successfully called your super simple API with Websocket-base with AWS API Gateway. Of course, this is just a starting point. But, this will save your time to avoid diving to outdated and old instruction you have struggled in Internet and Copilot.</p><p>Thanks,</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ae987d145a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>