<?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: Eka Prasetia</title>
    <description>The latest articles on DEV Community by Eka Prasetia (@ekaone).</description>
    <link>https://dev.to/ekaone</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%2F585158%2F643f7276-80ea-4c81-a8df-874c08ce31ef.jpg</url>
      <title>DEV Community: Eka Prasetia</title>
      <link>https://dev.to/ekaone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ekaone"/>
    <language>en</language>
    <item>
      <title>Tired of Copy-Pasting Commands?, I Built a Better Way</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Tue, 14 Apr 2026 12:28:06 +0000</pubDate>
      <link>https://dev.to/ekaone/why-your-terminal-needs-a-json-logic-layer-32gl</link>
      <guid>https://dev.to/ekaone/why-your-terminal-needs-a-json-logic-layer-32gl</guid>
      <description>&lt;p&gt;At a complex sequence of &lt;code&gt;git&lt;/code&gt;, &lt;code&gt;pnpm&lt;/code&gt;, and &lt;code&gt;docker&lt;/code&gt; commands, trying to remember the exact flags for a clean build and deploy. Usually, this involves a round-trip to an LLM, a "hope this doesn't break everything" copy-paste, and a prayer to the terminal gods. &lt;/p&gt;

&lt;p&gt;The problem isn't the AI's ability to help, it's the lack of a validation layer between a model's potential &lt;strong&gt;hallucination&lt;/strong&gt; and your actual file system. We need a way to turn "plain English" into executable, schema-validated plans that we can actually review before hitting enter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ekaone/json-cli" rel="noopener noreferrer"&gt;json-cli&lt;/a&gt; solves this by acting as a strict task runner. It takes your &lt;code&gt;intent&lt;/code&gt;, generates a JSON plan validated by a Zod schema, and checks it against a &lt;code&gt;whitelist&lt;/code&gt; of allowed commands. It's essentially a "&lt;strong&gt;Safety First&lt;/strong&gt;" AI wrapper for your terminal that supports Claude, OpenAI, and even local models via Ollama. By moving from raw strings to validated JSON, you get the speed of AI with the &lt;code&gt;guardrails&lt;/code&gt; of a &lt;strong&gt;predictable&lt;/strong&gt; state machine.&lt;/p&gt;

&lt;p&gt;Here is how you can put your terminal on autopilot without losing control:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Natural Language Task Execution
&lt;/h2&gt;

&lt;p&gt;Instead of remembering every flag, just describe the goal. The CLI handles the translation into a valid command string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Single intent&lt;/span&gt;
json-cli &lt;span class="s2"&gt;"please run tests"&lt;/span&gt;

&lt;span class="c"&gt;# Multiple intents in one go&lt;/span&gt;
json-cli &lt;span class="s2"&gt;"heyy ... run typecheck, test and then check git status"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Flexible Provider Support
&lt;/h2&gt;

&lt;p&gt;You aren't locked into one API. You can use Claude for complex logic or Ollama if you want to keep your task descriptions entirely local and private.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Using OpenAI&lt;/span&gt;
pnpm dev &lt;span class="s2"&gt;"run build and publish"&lt;/span&gt; &lt;span class="nt"&gt;--provider&lt;/span&gt; openai

&lt;span class="c"&gt;# Using Ollama (Local)&lt;/span&gt;
pnpm dev &lt;span class="s2"&gt;"start dev server"&lt;/span&gt; &lt;span class="nt"&gt;--provider&lt;/span&gt; ollama
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. The Safety Catalog
&lt;/h2&gt;

&lt;p&gt;The runner doesn't just execute anything. It checks a whitelist of allowed commands (pnpm, npm, git, etc.) to prevent the AI from accidentally running something destructive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# The workflow looks like this:
# User Prompt -&amp;gt; AI Provider -&amp;gt; JSON Plan (Zod Validated) -&amp;gt; Catalog Check -&amp;gt; User Confirm -&amp;gt; Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrap Up
&lt;/h2&gt;

&lt;p&gt;The future of CLI work isn't memorizing every flag in a man page, it's about describing the &lt;code&gt;goal&lt;/code&gt; and having a validated system handle the boilerplate. By moving to a &lt;code&gt;JSON-first&lt;/code&gt; execution model, &lt;a href="https://github.com/ekaone/json-cli" rel="noopener noreferrer"&gt;json-cli&lt;/a&gt; gives you a "Confirm before you Commit" workflow that makes AI-assisted terminal work actually safe for production.&lt;/p&gt;

&lt;p&gt;Check out the task &lt;a href="https://github.com/ekaone/json-cli/tree/main/src/catalogs" rel="noopener noreferrer"&gt;catalog&lt;/a&gt;, review the &lt;a href="https://github.com/ekaone/json-cli/blob/main/src/planner.ts" rel="noopener noreferrer"&gt;validation schema&lt;/a&gt;, or contribute to the project at this repository: &lt;a href="https://github.com/ekaone/json-cli" rel="noopener noreferrer"&gt;github.com/ekaone/json-cli&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy intent 😉&lt;/p&gt;

</description>
      <category>ai</category>
      <category>cli</category>
    </item>
    <item>
      <title>Stop Guessing, Start Measuring: Why Your App Needs a Shannon Entropy Check</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Mon, 23 Mar 2026 08:35:06 +0000</pubDate>
      <link>https://dev.to/ekaone/stop-guessing-start-measuring-why-your-app-needs-a-shannon-entropy-check-10co</link>
      <guid>https://dev.to/ekaone/stop-guessing-start-measuring-why-your-app-needs-a-shannon-entropy-check-10co</guid>
      <description>&lt;p&gt;We've all been there: a user sets their password to &lt;code&gt;password123&lt;/code&gt;, or worse, a junior dev accidentally hardcodes a "test" API key into a frontend component. Standard regex checks for length and character sets are fine, but they don't actually tell you how predictable a string is. This is the "hidden" security debt in most apps. If you can't quantify the &lt;code&gt;randomness&lt;/code&gt; of a secret, you're just crossing your fingers and hoping for the best. By measuring &lt;strong&gt;Shannon Entropy&lt;/strong&gt;, we move from "this looks like a string" to "this string is mathematically complex enough to be secure."&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Here are a few ways to bake these checks into your workflow using a lightweight approach:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Validating API Keys in Middleware
&lt;/h3&gt;

&lt;p&gt;Stop invalid or "dummy" keys before they hit your database. If the entropy is too low, it’s probably a placeholder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;calculate&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@ekaone/entropy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;apiKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;key_12345&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Way too predictable&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;entropy&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;3.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Security Risk: Token entropy is too low.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Smart Password Strength Meters
&lt;/h3&gt;

&lt;p&gt;Instead of yelling at users for missing a capital letter, give them a real-time "Complexity Score" based on actual data density.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userPass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CorrectHorseBatteryStaple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userPass&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;entropy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Password Complexity: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; bits`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// A much more "human" way to handle security UX.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Detecting Secret Leaks in CI/CD
&lt;/h3&gt;

&lt;p&gt;You can run a quick scan over your config files. If a string suddenly spikes in entropy, there’s a high chance someone accidentally pasted a private key where it doesn't belong.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;configValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;configValue&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;entropy&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;4.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Caution: High entropy detected. Ensure this secret is masked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Finally
&lt;/h3&gt;

&lt;p&gt;It’s a small addition to your stack that pays off the moment it catches a "123456" hiding in your production environment.&lt;/p&gt;

&lt;p&gt;You can grab the source code, check the math, or contribute to the footprint-reduction movement over at the repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install it, no judgment here:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @ekaone/entropy
&lt;span class="c"&gt;# or&lt;/span&gt;
yarn add @ekaone/entropy
&lt;span class="c"&gt;# or&lt;/span&gt;
pnpm add @ekaone/entropy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code lives here, fully open, zero drama:&lt;br&gt;
👉 GitHub: &lt;a href="https://github.com/ekaone/entropy" rel="noopener noreferrer"&gt;https://github.com/ekaone/entropy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy coding!!&lt;/p&gt;

</description>
      <category>security</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Stop Leaking Names Like It’s Harmless</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Fri, 20 Mar 2026 13:25:30 +0000</pubDate>
      <link>https://dev.to/ekaone/stop-leaking-names-like-its-harmless-505d</link>
      <guid>https://dev.to/ekaone/stop-leaking-names-like-its-harmless-505d</guid>
      <description>&lt;p&gt;Masking emails? Obvious. Tokens? Critical. But names? Somehow they always sneak into logs, dashboards, exports—fully visible like it’s no big deal. Until it &lt;strong&gt;is&lt;/strong&gt;. Names are still personal data, and once they’re everywhere, good luck cleaning that up. That’s exactly where &lt;strong&gt;mask-name&lt;/strong&gt; comes in: a tiny, zero-dependency utility to &lt;strong&gt;mask names properly across Latin and CJK (Chinese/Japanese) scripts&lt;/strong&gt;, without breaking readability or context. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Basic usage (Latin names)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;maskName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@ekaone/mask-name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;maskName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eka Prasetia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// → { masked: "E*a P*****ia", script: "latin", original: "Eka Prasetia" }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You still recognize the person. You just don’t expose the full name to the world. That’s the sweet spot.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. CJK support (this is where it gets interesting)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;张伟&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// → { masked: "张*", script: "cjk", original: "张伟" }&lt;/span&gt;

&lt;span class="nf"&gt;maskName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;田中さくら&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;locale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ja&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// → { masked: "田**くら", script: "cjk", original: "田中さくら" }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No hacks, no weird splitting logic. It understands character-based names out of the box. &lt;/p&gt;

&lt;h3&gt;
  
  
  3. Real-world messy input (mixed scripts)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John 田中&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// → { masked: "J**n 田*", script: "cjk", original: "John 田中" }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because real data is never clean. And yes, this still works without you babysitting it.&lt;/p&gt;

&lt;h2&gt;
  
  
  That’s it — keep it simple
&lt;/h2&gt;

&lt;p&gt;If your app logs names, shows user data, or exports anything remotely sensitive, this is one of those utilities.&lt;/p&gt;

&lt;p&gt;Wanna try, install it 👇🏼&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @ekaone/mask-name
&lt;span class="c"&gt;# or&lt;/span&gt;
yarn add @ekaone/mask-name
&lt;span class="c"&gt;# or&lt;/span&gt;
pnpm add @ekaone/mask-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Codebase: &lt;a href="https://github.com/ekaone/mask-name" rel="noopener noreferrer"&gt;https://github.com/ekaone/mask-name&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Small, predictable, and does exactly what it should—no more, no less.&lt;/p&gt;

&lt;p&gt;Happy coding!😄&lt;/p&gt;

</description>
      <category>security</category>
      <category>tooling</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>A Developer-Friendly Way to Mask API Tokens Without Losing Context</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Mon, 09 Feb 2026 04:16:19 +0000</pubDate>
      <link>https://dev.to/ekaone/a-developer-friendly-way-to-mask-api-tokens-without-losing-context-4dfn</link>
      <guid>https://dev.to/ekaone/a-developer-friendly-way-to-mask-api-tokens-without-losing-context-4dfn</guid>
      <description>&lt;p&gt;Yooo—real talk for a second.&lt;br&gt;
If you’ve ever logged an API token “just for debugging” and immediately promised yourself you’d remove it later… this post is for you. Tokens are short, powerful, and absolutely not something you want hanging around in logs, UIs, or error traces.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mask-token&lt;/code&gt; is a tiny TypeScript utility built for one simple job: hide sensitive tokens without breaking developer ergonomics. It doesn’t try to guess what your token is or where it came from. It just masks it, predictably, safely, and with defaults that make sense.&lt;/p&gt;
&lt;h2&gt;
  
  
  Basic usage
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sk_live_1234567890abcdef&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// sk_live_***************cdef&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You still recognize the token. Attackers don’t. Everyone wins.&lt;/p&gt;

&lt;p&gt;The API stays intentionally flat and boring (again: a compliment). You control how much is visible at the start or end, what character is used for masking, and nothing more than that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ghp_abcdef1234567890&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;showFirst&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;showLast&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;// ghp_************7890&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No config objects the size of a novel. No security theater. Just enough flexibility to cover logs, dashboards, and admin panels.&lt;/p&gt;

&lt;p&gt;This example shows where &lt;code&gt;mask-token&lt;/code&gt; goes a step further than simple string masking. By enabling &lt;code&gt;includeMetadata&lt;/code&gt;, the function doesn’t just hide the sensitive parts of the token—it explains what it’s looking at. The result includes a safely masked value for logs or UI, plus structured metadata that identifies the token type (here, an NPM token), its prefix, and a confidence score indicating how likely this string is a real secret. On top of that, you get lightweight validation and risk signals without exposing the original value. It’s a very dev-friendly balance: your app stays privacy-safe, while you still get enough context to debug, audit, or alert intelligently—without ever printing the actual secret.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;maskToken&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@ekaone/mask-token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;maskToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;npm_secret123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;includeMetadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// { &lt;/span&gt;
&lt;span class="c1"&gt;// masked: 'npm_••••••••t123', &lt;/span&gt;
&lt;span class="c1"&gt;// metadata: { &lt;/span&gt;
&lt;span class="c1"&gt;//  type: 'NPM Token', &lt;/span&gt;
&lt;span class="c1"&gt;//  prefix: 'npm_', &lt;/span&gt;
&lt;span class="c1"&gt;//  confidence: 1.0, &lt;/span&gt;
&lt;span class="c1"&gt;//  isLikelyToken: true &lt;/span&gt;
&lt;span class="c1"&gt;// }, // validation: { &lt;/span&gt;
&lt;span class="c1"&gt;//  valid: true, &lt;/span&gt;
&lt;span class="c1"&gt;//  warnings: [], &lt;/span&gt;
&lt;span class="c1"&gt;//  riskScore: 0 &lt;/span&gt;
&lt;span class="c1"&gt;// }, &lt;/span&gt;
&lt;span class="c1"&gt;// original: { &lt;/span&gt;
&lt;span class="c1"&gt;//  length: 17, &lt;/span&gt;
&lt;span class="c1"&gt;//  hasPrefix: true &lt;/span&gt;
&lt;span class="c1"&gt;//  } &lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JWT style
&lt;/h2&gt;

&lt;p&gt;For JWT, this example gets even more interesting with structured secrets like JWTs. Instead of treating the token as one long opaque string, mask-token can switch modes and mask by segment. With mode: 'jwt', each part of the token (header, payload, signature) is partially revealed and safely obscured in a way that’s still recognizable to developers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jwt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// → 'eyJ•••.eyJ•••.doz•••'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Recognize 40+ token formats
&lt;/h2&gt;

&lt;p&gt;One of the quietly powerful parts of &lt;code&gt;mask-token&lt;/code&gt; is its built-in awareness of real-world secrets. It can automatically recognize 40+ token formats across version control, CI/CD, payments, cloud providers, and AI platforms—everything from &lt;code&gt;npm_&lt;/code&gt; and &lt;code&gt;ghp_&lt;/code&gt; to Stripe, AWS, Slack, and OpenAI keys. This means the library doesn’t just mask strings blindly; it understands what kind of secret it’s dealing with and reacts accordingly. For developers, that translates to smarter masking, better metadata, and fewer false positives—without maintaining a growing pile of regexes that everyone is afraid to touch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nc"&gt;NPM &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;npm_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;GitHub &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ghp_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;gho_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ghu_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ghs_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ghr_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;GitLab &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;glpat&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;gldt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;Docker&lt;/span&gt; &lt;span class="nc"&gt;Hub &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dckr_pat_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;Payment&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;Commerce&lt;/span&gt;

&lt;span class="nc"&gt;Stripe &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk_test_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sk_live_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pk_test_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pk_live_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Shopify &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shpat_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shpca_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shpss_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;Communication&lt;/span&gt;

&lt;span class="nc"&gt;Slack &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xoxb&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;xoxp&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;xoxa&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;xoxr&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Twilio &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;SK&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;AC&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;SendGrid &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;SG&lt;/span&gt;&lt;span class="p"&gt;.)&lt;/span&gt;
&lt;span class="nx"&gt;Cloud&lt;/span&gt; &lt;span class="nx"&gt;Providers&lt;/span&gt;

&lt;span class="nc"&gt;AWS &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AKIA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ASIA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;Google&lt;/span&gt; &lt;span class="nc"&gt;Cloud &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AIza&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;DigitalOcean &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dop_v1_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;AI&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;ML&lt;/span&gt; &lt;span class="nx"&gt;Services&lt;/span&gt;

&lt;span class="nc"&gt;OpenAI &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;proj&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Anthropic &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;ant&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Under the hood, &lt;code&gt;mask-token&lt;/code&gt; is dependency-free and defensive by default. Short tokens stay readable, absurd option values won’t leak data, and edge cases won’t surprise you in production. Privacy-friendly by design, it helps you stay aligned with GDPR and general data-protection best practices—without pretending to be a full security framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install it, no judgment here:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @ekaone/mask-token
# or
yarn add @ekaone/mask-token
# or
pnpm add @ekaone/mask-token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code lives here, fully open, zero drama:&lt;br&gt;
👉 GitHub: &lt;a href="https://github.com/ekaone/mask-token" rel="noopener noreferrer"&gt;https://github.com/ekaone/mask-token&lt;/a&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>apikey</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Tiny TypeScript Utility to Mask Emails - GDPR-Friendly by Design</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Thu, 05 Feb 2026 07:45:11 +0000</pubDate>
      <link>https://dev.to/ekaone/a-tiny-typescript-utility-to-mask-emails-gdpr-friendly-by-design-1f3b</link>
      <guid>https://dev.to/ekaone/a-tiny-typescript-utility-to-mask-emails-gdpr-friendly-by-design-1f3b</guid>
      <description>&lt;p&gt;Hi, do you ever dumped logs to debug something simple ...? and suddenly realized you just logged &lt;em&gt;real email addresses&lt;/em&gt;? Yeah. That little chill down your spine is called "future GDPR paperwork". This tiny TypeScript utility exists to make sure that moment never happens again.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mask-email&lt;/code&gt; does one job and does it cleanly: mask email addresses without being clever in the wrong places. By default, it keeps a couple of characters visible and hides the rest—enough to identify a user, not enough to get you fired.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ekaone3033@gmail.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// ek********@gmail.com&lt;/span&gt;

&lt;span class="nf"&gt;maskEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john.doe@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// jo******@example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No regex soup, no assumptions about email length, and no surprises when usernames are short or weird.&lt;/p&gt;

&lt;p&gt;When you need more control, the options stay flat and readable. You can tweak visible characters, change the mask symbol, or even mask the domain when you’re dealing with internal systems or logs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;admin@mail.company.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;visibleChars&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;maskDomain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;// a****@m***.c******.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There’s also a &lt;code&gt;viewable&lt;/code&gt; switch for those moments when masking needs to be conditionally disabled—because real-world code paths are rarely pure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;secret@company.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;viewable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// Output: 'secret@company.com'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Under the hood, it’s small, dependency-free, and aggressively boring (again: a compliment). Inputs are validated, edge cases are handled, and nothing tries to be smarter than it should be. &lt;strong&gt;Privacy-friendly by design&lt;/strong&gt;, it helps you stay aligned with GDPR and common data-protection practices—without turning a simple utility into a legal document. Drop it into UI, logs, or analytics, and move on with your day. &lt;/p&gt;

&lt;h3&gt;
  
  
  Wanna try it? 😏
&lt;/h3&gt;

&lt;p&gt;The code lives here, fully open, zero drama:&lt;br&gt;
👉 &lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/ekaone/mask-email" rel="noopener noreferrer"&gt;https://github.com/ekaone/mask-email&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install it, no judgment here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @ekaone/mask-email
&lt;span class="c"&gt;# or&lt;/span&gt;
yarn add @ekaone/mask-email
&lt;span class="c"&gt;# or&lt;/span&gt;
pnpm add @ekaone/mask-email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. No post-install rituals, no peer-dependency surprises 😲&lt;/p&gt;

&lt;p&gt;Happy coding — and may your emails stay masked.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>typescript</category>
      <category>npm</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Raw phone numbers leaking will ruin your whole day</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Wed, 04 Feb 2026 04:30:48 +0000</pubDate>
      <link>https://dev.to/ekaone/raw-phone-numbers-leaking-will-ruin-your-whole-day-4ib2</link>
      <guid>https://dev.to/ekaone/raw-phone-numbers-leaking-will-ruin-your-whole-day-4ib2</guid>
      <description>&lt;p&gt;"Yooo—quick question. Are you thirsty?&lt;br&gt;
because I’m about to spill some digits… just not all of them."&lt;/p&gt;

&lt;p&gt;Shipping phone masking sounds trivial ... until you meet international formats, weird inputs, and that one QA test with &lt;code&gt;+62 (812)-3456..7890&lt;/code&gt;. This TypeScript phone masking utility is built for that reality, follows &lt;a href="https://en.wikipedia.org/wiki/General_Data_Protection_Regulation" rel="noopener noreferrer"&gt;GDPR&lt;/a&gt; and data protection standards, zero locale assumptions, accepts &lt;strong&gt;strings&lt;/strong&gt; or &lt;strong&gt;numbers&lt;/strong&gt;, and safely normalizes whatever chaos you throw at it. No country rules, no phone metadata databases, predictable masking that doesn’t fight you.&lt;/p&gt;

&lt;p&gt;The default behavior is intentionally boring (in a good way), mask everything except the last 4 digits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskPhone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1234567890&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// ******7890&lt;/span&gt;

&lt;span class="nf"&gt;maskPhone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;+62 812 3456 7890&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// +*********7890&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don’t need to configure anything to get sane output, which is exactly how a utility like this should behave.&lt;/p&gt;

&lt;p&gt;When you &lt;em&gt;do&lt;/em&gt; need control, the API stays flat and readable. Want to expose a few digits at the front? Preserve formatting for UI display?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskPhone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;628123456789&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;showFirst&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;showLast&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;// 628*******89&lt;/span&gt;

&lt;span class="nf"&gt;maskPhone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;+1 (555) 123-4567&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;preserveFormat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;// +* (***) ***-4567&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No magic. No surprises. The function does exactly what the options say—nothing more, nothing less.&lt;/p&gt;

&lt;p&gt;And for the “I know what I’m doing” crowd, there’s &lt;code&gt;customMask&lt;/code&gt;. This option overrides everything and gives you full control per character.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;maskPhone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1234567890&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;customMask&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;// *2*4*6*8*0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is this something you &lt;em&gt;should&lt;/em&gt; do? Maybe not. But when you need it, you’ll be very glad it’s there.&lt;/p&gt;

&lt;p&gt;Under the hood, the code stays tiny, dependency-free, and aggressively defensive 👉: nulls won’t crash it, absurd option values won’t leak data, and long inputs won’t melt your logs. &lt;/p&gt;

&lt;p&gt;It’s the kind of utility you drop into UI, analytics, or logging and then forget about—which is the highest compliment you can give masking code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wanna try it? (you know you do 😏)
&lt;/h3&gt;

&lt;p&gt;The code lives here, fully open, zero drama:&lt;br&gt;
👉 &lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/ekaone/mask-phone" rel="noopener noreferrer"&gt;https://github.com/ekaone/mask-phone&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install it, no judgment here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @ekaone/mask-phone
&lt;span class="c"&gt;# or&lt;/span&gt;
yarn add @ekaone/mask-phone
&lt;span class="c"&gt;# or&lt;/span&gt;
pnpm add @ekaone/mask-phone
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. No post-install rituals, no peer-dependency surprises 😲, &lt;strong&gt;&lt;em&gt;no&lt;/em&gt;&lt;/strong&gt; "why is this 3MB?".&lt;/p&gt;

</description>
      <category>programming</category>
      <category>npm</category>
    </item>
    <item>
      <title>Uncontrolled vs Controlled React Components</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Wed, 04 Feb 2026 03:50:08 +0000</pubDate>
      <link>https://dev.to/ekaone/uncontrolled-vs-controlled-react-components-1eli</link>
      <guid>https://dev.to/ekaone/uncontrolled-vs-controlled-react-components-1eli</guid>
      <description>&lt;p&gt;React offers developers various methods to manage and handle components within applications. Two prevalent approaches to managing components are &lt;code&gt;uncontrolled&lt;/code&gt; and &lt;code&gt;controlled&lt;/code&gt; components. While both serve the same purpose of managing user inputs and interactions, they differ significantly in their implementation and use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Uncontrolled Components:
&lt;/h2&gt;

&lt;p&gt;Uncontrolled components in React are those where the component state is handled by the &lt;strong&gt;DOM itself rather than by React&lt;/strong&gt;. This means that React doesn't have direct control over the values within these components. Instead, the state is managed by the DOM, and React simply interfaces with it. Uncontrolled components are typically initialized with default values and then allow the DOM to manage changes thereafter.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Advantages of Uncontrolled Components:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;DOM-Driven&lt;/strong&gt;: In an uncontrolled component, the form data is managed by the DOM itself. The DOM maintains the state of form elements (e.g., input values) independently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integration with Non-React Code&lt;/strong&gt;: Uncontrolled components are useful when integrating React with non-React code. Since the source of truth remains in the DOM, it’s easier to work with external libraries or legacy code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplified Implementation&lt;/strong&gt;: Uncontrolled components are often simpler to implement, especially for forms with a large number of inputs. Since there's no need to manage state within React, developers can focus solely on the presentation layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better Performance&lt;/strong&gt;: Uncontrolled components can offer better performance in certain scenarios, especially when dealing with large forms. Since React isn't directly involved in managing the state, there's less overhead involved in re-rendering components.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Disadvantages of Uncontrolled Components:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Limited Control&lt;/strong&gt;: With uncontrolled components, React relinquishes control over the component state to the DOM. This can lead to issues when trying to enforce certain validation rules or when integrating with other React features like &lt;code&gt;PropTypes&lt;/code&gt; or &lt;code&gt;Context API&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Difficulty in Testing&lt;/strong&gt;: Testing uncontrolled components can be more challenging since the component state is not managed by React. This makes it harder to simulate user interactions and assert expected outcomes during testing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;UncontrolledForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Submitted value:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="nx"&gt;onSubmit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;defaultValue&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;UncontrolledForm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the input field is uncontrolled because its value is managed by the DOM itself. The &lt;code&gt;defaultValue&lt;/code&gt; attribute sets the initial value of the input field, but React doesn't control subsequent changes to its value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Controlled Components:
&lt;/h2&gt;

&lt;p&gt;Controlled components, on the other hand, are components where React controls the state of the component. This means that React manages the component's state and updates it in response to user interactions. In controlled components, the component's state is kept in sync with React's state.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Advantages of Controlled Components:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Full Control&lt;/strong&gt;: With controlled components, React maintains full control over the component's state. This allows for easier enforcement of validation rules, integration with other React features, and better overall predictability in application behavior.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Testing&lt;/strong&gt;: Testing controlled components is often easier since React manages the component's state. Developers can simulate user interactions by directly modifying the component's state and assert expected outcomes more effectively.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State Management&lt;/strong&gt;: In a controlled component, the form data (such as input values) is stored in the component’s state. Each form element (like input, select, textarea) is associated with a state variable. When the user interacts with the form (typing, selecting, etc.), the component’s state is updated accordingly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event Handling&lt;/strong&gt;: We use event handlers (like &lt;code&gt;onChange&lt;/code&gt;) to capture user input. For instance, when the user types in an input field, the &lt;code&gt;onChange&lt;/code&gt; event updates the corresponding state variable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Disadvantages of Controlled Components:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Increased Complexity&lt;/strong&gt;: Controlled components can be more complex to implement, especially for forms with a large number of inputs. Developers need to manage the component's state explicitly, which can lead to boilerplate code and increased cognitive load.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Potential Performance Overhead&lt;/strong&gt;: Since React manages the component's state, there can be performance implications, especially for large forms or components with frequent updates. Re-rendering controlled components can be more resource-intensive compared to uncontrolled components.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ControlledForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Submitted value:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="nx"&gt;onSubmit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ControlledForm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the input field is controlled because its value is managed by React state. The &lt;code&gt;value&lt;/code&gt; attribute is set to the &lt;code&gt;name&lt;/code&gt; state variable, and the &lt;code&gt;onChange&lt;/code&gt; event handler updates the state whenever the input value changes.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article. Happy coding! 🚀&lt;/p&gt;

</description>
      <category>react</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Tokenizers The Building Blocks of Generative AI</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Wed, 04 Feb 2026 03:50:01 +0000</pubDate>
      <link>https://dev.to/ekaone/tokenizers-the-building-blocks-of-generative-ai-4c9k</link>
      <guid>https://dev.to/ekaone/tokenizers-the-building-blocks-of-generative-ai-4c9k</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Originally written in 2023. Republished here.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Tokenizers are essential components of generative AI models, such as &lt;a href="https://openai.com/gpt-4" rel="noopener noreferrer"&gt;GPT-4&lt;/a&gt;, that can create various types of content, such as text, code, music, and images. Tokenizers are responsible for converting the input and output data into a format that the model can understand and manipulate. In this article, we will explore what tokenizers are, how they work, and why they are important for generative AI.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are tokenizers?
&lt;/h2&gt;

&lt;p&gt;Tokenizers are algorithms that split a given input into smaller units, called tokens, that can be processed by a generative AI model. Tokens can be words, characters, subwords, or even pixels, depending on the type and granularity of the input data. For example, a text tokenizer can split a sentence into words or subwords, while an image tokenizer can split an image into patches or pixels.&lt;/p&gt;

&lt;p&gt;The output of a tokenizer is a sequence of tokens, each represented by a unique numerical identifier, called a &lt;strong&gt;token ID&lt;/strong&gt;. The token IDs are then fed into the generative AI model as input or used to decode the output of the model. For example, a text tokenizer can map the word "hello" to the token ID 1234, and the word "world" to the token ID 5678. The input sequence [1234, 5678] can then be used to generate a new text output, such as [7890, 4321], which can be decoded back to words using the same tokenizer.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do tokenizers work?
&lt;/h2&gt;

&lt;p&gt;Tokenizers can be implemented in different ways, depending on the type and complexity of the input data. Some common types of tokenizers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Character-level tokenizers: These tokenizers split the input into individual characters, such as letters, digits, punctuation, and symbols. Character-level tokenizers are simple and flexible, but they can result in long sequences of tokens and limited vocabulary size.&lt;/li&gt;
&lt;li&gt;Word-level tokenizers: These tokenizers split the input into words, based on whitespace and punctuation. Word-level tokenizers are intuitive and easy to understand, but they can result in out-of-vocabulary (OOV) tokens and misspellings.&lt;/li&gt;
&lt;li&gt;Subword-level tokenizers: These tokenizers split the input into subwords, which are smaller units of words that capture common prefixes, suffixes, and stems. Subword-level tokenizers are more efficient and robust, as they can handle OOV tokens and rare words, but they can also result in unnatural splits and ambiguity.&lt;/li&gt;
&lt;li&gt;Pixel-level tokenizers: These tokenizers split the input into pixels, which are the smallest units of an image. Pixel-level tokenizers are straightforward and universal, but they can result in high-dimensional and noisy input data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why are tokenizers important for generative AI?
&lt;/h2&gt;

&lt;p&gt;Tokenizers are important for generative AI because they enable the model to learn from and generate diverse and complex types of data. Tokenizers can affect the performance and quality of the generative AI model in several ways, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data representation: Tokenizers determine how the input and output data are represented and encoded, which can influence the information and structure that the model can capture and manipulate.&lt;/li&gt;
&lt;li&gt;Data processing: Tokenizers determine how the input and output data are processed and decoded, which can influence the speed and efficiency of the model's computation and generation.&lt;/li&gt;
&lt;li&gt;Data quality: Tokenizers determine how the input and output data are split and mapped, which can influence the accuracy and diversity of the model's output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for reading this article, have fun with generative AI! 🤖&lt;/p&gt;

</description>
      <category>ai</category>
    </item>
    <item>
      <title>Spread Operator</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Wed, 04 Feb 2026 03:49:55 +0000</pubDate>
      <link>https://dev.to/ekaone/spread-operator-41lf</link>
      <guid>https://dev.to/ekaone/spread-operator-41lf</guid>
      <description>&lt;p&gt;In JavaScript, the spread operator (&lt;code&gt;...&lt;/code&gt;) allows you to "spread" an iterable (like an array or object) into another. This can be used to achieve different effects, including merging arrays and objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Merging Arrays with Spread Operator
&lt;/h2&gt;

&lt;p&gt;Here's how you can merge two arrays using the spread operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mergedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;array2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mergedArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 2, 3, 4, 5, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're spreading the elements of &lt;code&gt;array1&lt;/code&gt; and &lt;code&gt;array2&lt;/code&gt; into a new array &lt;code&gt;mergedArray&lt;/code&gt;. This creates a new array containing all the elements from both original arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  Merging Objects with Spread Operator
&lt;/h2&gt;

&lt;p&gt;While merging objects with the spread operator is possible, it's important to understand its behavior:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Merging properties:&lt;/strong&gt; When merging objects, the spread operator adds properties from the rightmost object to the leftmost one. If a property exists in both objects, the value from the rightmost object takes precedence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shallow copy:&lt;/strong&gt; The spread operator only creates a shallow copy of the object. Nested objects within the merged object remain linked to the original object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;New York&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mergedObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mergedObj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: { name: "Alice", age: 35, city: "New York" }&lt;/span&gt;

&lt;span class="c1"&gt;// Modifying nested object in mergedObj affects obj1&lt;/span&gt;
&lt;span class="nx"&gt;mergedObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 40&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the &lt;code&gt;age&lt;/code&gt; property from &lt;code&gt;obj2&lt;/code&gt; takes precedence in the merged object. Additionally, modifying the nested object inside &lt;code&gt;mergedObj&lt;/code&gt; also affects the original &lt;code&gt;obj1&lt;/code&gt; due to the shallow copy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cautions and Alternatives
&lt;/h2&gt;

&lt;p&gt;While the spread operator provides a concise way to merge arrays and objects, be cautious of its shallow copy behavior when dealing with nested data. For deep merging objects, consider using alternative methods like &lt;code&gt;Object.assign&lt;/code&gt; or libraries like Lodash_.merge that handle nested objects correctly.&lt;/p&gt;

&lt;p&gt;I hope this explanation clarifies the concept of merging with the spread operator in JavaScript!&lt;/p&gt;

&lt;p&gt;Merge with Spread Operator more easy. Take a &lt;code&gt;note&lt;/code&gt;, if the objects contains same property, the right-post will overwrite.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;merged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// { name: 'Dow', age: 35, admin: true }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for reading this article, I hope you find it useful. Happy coding! 🔥&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>An Introduction to Retrieval Augmented Generation (RAG)</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Wed, 04 Feb 2026 03:49:46 +0000</pubDate>
      <link>https://dev.to/ekaone/an-introduction-to-retrieval-augmented-generation-rag-1lhj</link>
      <guid>https://dev.to/ekaone/an-introduction-to-retrieval-augmented-generation-rag-1lhj</guid>
      <description>&lt;h2&gt;
  
  
  An Introduction to Retrieval Augmented Generation (RAG)
&lt;/h2&gt;

&lt;p&gt;Retrieval augmented generation (RAG) is an approach in natural language processing that combines a neural retriever with a neural generator to produce coherent and factual text. RAG models have shown promising results in tasks like open-ended question answering, summarization, and dialogue.&lt;/p&gt;

&lt;h2&gt;
  
  
  How RAG Models Work
&lt;/h2&gt;

&lt;p&gt;RAG models consist of two main components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Retriever&lt;/strong&gt;: This is responsible for retrieving relevant context documents or passages of text from a large corpus or database given a query or prompt. The retriever ranks and returns the most relevant texts. Popular retrievers include dense retrievers based on bi-encoders and sparse retrievers based on inverted indexes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Generator&lt;/strong&gt;: This takes the query and retrieved documents as input and generates an output text. The generator is usually a pre-trained language model like GPT-3. By conditioning the generation on relevant texts, the generator can produce more factual, specific, and coherent outputs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During inference, the retriever first retrieves relevant contexts for the given query or prompt. The generator then conditions on these contexts as well as the original query to generate the final output. The retriever and generator are trained jointly, often with a Generative QA (GenQA) objective of maximizing the likelihood of generating the ground truth text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of RAG Models
&lt;/h2&gt;

&lt;p&gt;Some key benefits of RAG models include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Knowledgeable&lt;/strong&gt;: By retrieving relevant information, RAG models can incorporate facts and knowledge into generated text. This makes them better at factual correctness.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Specific&lt;/strong&gt;: Retrieved texts provide useful cues to generate more focused and specific outputs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Coherent&lt;/strong&gt;: The generator can draw connections between the retrieved contexts and query to produce holistic coherent text.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalable training&lt;/strong&gt;: RAG models provide a more sample efficient way to inject knowledge into generators compared to having to train end-to-end on massive datasets.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  RAG Applications
&lt;/h2&gt;

&lt;p&gt;RAG models have shown promising results on several NLP tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Question answering&lt;/strong&gt;: RAG can answer open-ended questions by retrieving related passages and generating from them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Summarization&lt;/strong&gt;: RAG can summarize long documents by retrieving salient passages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dialogue&lt;/strong&gt;: RAG can make chatbots and assistants more knowledgeable by retrieving useful context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multi-hop reasoning&lt;/strong&gt;: RAG can do complex inference by chaining and reasoning over multiple retrieved passages.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, RAG offers an effective way to make NLG models more factual, specific, and knowledgeable. As retrieval indexes and pre-trained models continue to improve, we can expect further advances in RAG's capabilities.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>programming</category>
    </item>
    <item>
      <title>React Function and Class Components</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Wed, 04 Feb 2026 03:49:41 +0000</pubDate>
      <link>https://dev.to/ekaone/react-function-and-class-components-2m51</link>
      <guid>https://dev.to/ekaone/react-function-and-class-components-2m51</guid>
      <description>&lt;p&gt;In React, you have two main ways to define components: &lt;strong&gt;function components&lt;/strong&gt; and &lt;strong&gt;class components&lt;/strong&gt;. Both serve the same purpose—to create reusable blocks of UI—but they have different approaches and use cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Components:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defined as simple JavaScript functions that accept props (properties from parent components) as arguments and return JSX (a syntax extension for defining UI elements).&lt;/li&gt;
&lt;li&gt;Easier to write and understand, especially for simpler components.&lt;/li&gt;
&lt;li&gt;Don't have access to lifecycle methods (like &lt;code&gt;componentDidMount&lt;/code&gt;) or state directly.&lt;/li&gt;
&lt;li&gt;Since React 16.8, can use hooks (like &lt;code&gt;useState&lt;/code&gt;, &lt;code&gt;useEffect&lt;/code&gt;) to handle state and side effects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Class Components:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defined as JavaScript classes that extend &lt;code&gt;React.Component&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;More complex syntax, but offer lifecycle methods and direct state management.&lt;/li&gt;
&lt;li&gt;Suitable for components with complex behavior or interactions with external data sources.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;You&lt;/span&gt; &lt;span class="nx"&gt;clicked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another ways sample React JS &lt;code&gt;Function&lt;/code&gt; and &lt;code&gt;Class&lt;/code&gt; components&lt;/p&gt;

&lt;h2&gt;
  
  
  Function component
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Class component
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WelcomeClass&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Eka"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elementClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;WelcomeClass&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Eka"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elementClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root-class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Choosing the Right Component:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simple UI:&lt;/strong&gt; Use function components for basic rendering without state or lifecycle needs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complex behavior or state:&lt;/strong&gt; Use class components for components with state management, external data fetching, or lifecycle methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modern React:&lt;/strong&gt; Since React 16.8, function components with hooks are often preferred due to their simplicity and compatibility with future features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Additional Considerations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Function components are generally considered slightly faster than class components, especially for simpler ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readability:&lt;/strong&gt; Both can be readable, but function components with hooks sometimes provide cleaner syntax.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Community preference:&lt;/strong&gt; Function components with hooks are the current trend and often recommended in the React community.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ultimately, the best choice depends on your specific component's needs and complexity. Consider all factors and experiment to find what works best for you and your project.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article, I hope you find it useful. Happy coding! 🔥&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Method chains allow a series of functions</title>
      <dc:creator>Eka Prasetia</dc:creator>
      <pubDate>Wed, 04 Feb 2026 03:49:35 +0000</pubDate>
      <link>https://dev.to/ekaone/method-chains-allow-a-series-of-functions-28o6</link>
      <guid>https://dev.to/ekaone/method-chains-allow-a-series-of-functions-28o6</guid>
      <description>&lt;p&gt;Method chaining in JavaScript involves chaining multiple method calls together on the same object or result. This pattern is commonly used with array methods like &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, and others, where each method returns a new array or value, allowing you to call another method on the result.&lt;/p&gt;

&lt;p&gt;Let's take an example using array methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Filter even numbers&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Double each remaining number&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Sum up the doubled numbers&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs 18 (2*2 + 4*2 + 5*2)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, numbers is an array, and we chain three array methods together: &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, and &lt;code&gt;reduce&lt;/code&gt;. Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Filter: The filter method is used to create a new array containing only the even numbers from the original array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Map: The map method is then applied to double each number in the filtered array, creating a new array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduce: Finally, the reduce method is used to sum up all the doubled numbers in the mapped array, resulting in a single value (18 in this case).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Method chaining improves code readability and reduces the need for intermediate variables. It allows you to express a series of operations on data in a more concise and declarative manner.&lt;/p&gt;

&lt;p&gt;Another common use case for method chaining is with &lt;code&gt;array of objects&lt;/code&gt;, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Drink&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.12&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Steak&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;45.15&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Drink&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;11.01&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;drinkTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cart&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Drink&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Total Drink Cost $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;drinkTotal&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Total Drink $14.13&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for reading this article, I hope you find it useful. Happy coding! 🔥&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>method</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
