<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Prashant Bansal — Writing</title><description>Personal website of Prashant Bansal. Writing on software, data and AI.</description><link>https://prashantb.me/</link><language>en-us</language><item><title>Engineering a Self Improving Multi Agent RAG System</title><link>https://prashantb.me/engineering-a-self-improving-multi-agent-rag-system/</link><guid isPermaLink="true">https://prashantb.me/engineering-a-self-improving-multi-agent-rag-system/</guid><description>How domain routing, hybrid retrieval with code and SQL lineage, a guarded executor, and a human-feedback loop turned an internal AI prototype into a system teams relied on.</description><pubDate>Sun, 17 May 2026 03:38:01 GMT</pubDate><content:encoded>&lt;p&gt;As companies grow, knowledge gets scattered everywhere. Business logic lives in backend services, product decisions are buried in docs, and metrics are hidden inside SQL dashboards. Simple questions like:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“How exactly is placement rate calculated?”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;suddenly require multiple people to answer. Over time, this becomes expensive. Engineers spend time answering repetitive questions. Support teams become bottlenecks. Business teams wait hours, sometimes days, for simple operational clarity. I wanted to build a system that could act as a unified intelligence layer for the organization.&lt;/p&gt;
&lt;p&gt;Something that could:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Answer process questions&lt;/li&gt;
&lt;li&gt;Explain code logic&lt;/li&gt;
&lt;li&gt;Query live data&lt;/li&gt;
&lt;li&gt;Understand business context&lt;/li&gt;
&lt;li&gt;And eventually automate safe operational tasks&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That’s how this multi-agent RAG system started. Instead of using one giant LLM prompt with every tool attached to it, I moved to a multi-agent architecture where each agent specializes in one type of task.&lt;/p&gt;
&lt;h2 id=&quot;the-architecture&quot;&gt;The Architecture&lt;/h2&gt;
&lt;p&gt;The system has one parent orchestrator, one shared context-enrichment layer, and multiple domain-specific specialist agents. The orchestrator’s job is to understand the user query and prepare it for the right downstream agent. But the important detail is this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The query is enriched before it reaches the specialist agent.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That means the parent orchestrator does not immediately send the raw user question to a Data Agent, Knowledge Agent, or Executor Agent. Instead, it first passes the request through the Hybrid RAG Engine. The Hybrid RAG Engine gathers relevant context from docs, code, SQL models, dashboards, schemas, and past corrections. Once the request is enriched, the orchestrator can route it to the right specialist. There are also multiple versions of each specialist agent based on domain.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Domain A Data Agent&lt;/li&gt;
&lt;li&gt;Domain B Data Agent&lt;/li&gt;
&lt;li&gt;Domain A Knowledge Agent&lt;/li&gt;
&lt;li&gt;Domain B Knowledge Agent&lt;/li&gt;
&lt;li&gt;Domain-specific Executor Agents&lt;/li&gt;
&lt;li&gt;…..&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This matters because different teams often use different definitions, systems, metrics, and workflows. A generic Data Agent might misunderstand a question, but a domain-specific Data Agent can answer using the right business context.&lt;/p&gt;
&lt;p&gt;The figure at the top of this post shows the full flow.&lt;/p&gt;
&lt;p&gt;The Knowledge Agents handle:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Docs&lt;/li&gt;
&lt;li&gt;Codebases&lt;/li&gt;
&lt;li&gt;Product logic&lt;/li&gt;
&lt;li&gt;System architecture&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The Data Agents handle:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;KPI questions&lt;/li&gt;
&lt;li&gt;SQL generation&lt;/li&gt;
&lt;li&gt;Dashboard explanations&lt;/li&gt;
&lt;li&gt;Metric definitions&lt;/li&gt;
&lt;li&gt;Structured analysis&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The Executor Agent handles safe operational actions.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;“Can you reset this workflow?”&lt;/li&gt;
&lt;li&gt;“Can you re-run this sync?”&lt;/li&gt;
&lt;li&gt;“Can you update this configuration?”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If the request falls within a verified list of safe actions, the Executor Agent can perform it automatically. Anything outside that safe boundary is never executed blindly. Instead, the system automatically creates a support ticket and routes it to the appropriate human team. That safety layer ended up being extremely important because it allows automation without losing operational control.&lt;/p&gt;
&lt;h2 id=&quot;why-standard-rag-wasnt-enough&quot;&gt;Why Standard RAG Wasn’t Enough&lt;/h2&gt;
&lt;p&gt;Traditional RAG works fine for FAQs. But enterprise systems are relational. A metric definition might exist in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A wiki page&lt;/li&gt;
&lt;li&gt;A dbt model&lt;/li&gt;
&lt;li&gt;Backend transformation code&lt;/li&gt;
&lt;li&gt;Dashboard SQL&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Simple vector similarity misses those relationships. So I built a hybrid retrieval system using:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;BM25 keyword search&lt;/li&gt;
&lt;li&gt;Vector similarity search&lt;/li&gt;
&lt;li&gt;Reranking&lt;/li&gt;
&lt;li&gt;Code lineage expansion&lt;/li&gt;
&lt;li&gt;SQL lineage expansion&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If the system finds a function call, it automatically retrieves the function definition. If it finds a dashboard query, it traces upstream tables and schemas. That extra context massively improved answer quality. Instead of returning shallow summaries, the system could explain:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Where a metric comes from&lt;/li&gt;
&lt;li&gt;Which SQL transformations are involved&lt;/li&gt;
&lt;li&gt;Which upstream systems contribute to it&lt;/li&gt;
&lt;li&gt;Why two dashboards might disagree&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That depth is what made the answers genuinely useful.&lt;/p&gt;
&lt;h2 id=&quot;the-biggest-improvement-human-feedback&quot;&gt;The Biggest Improvement: Human Feedback&lt;/h2&gt;
&lt;p&gt;The first version was only ~70% accurate. Good enough to feel useful. Dangerous enough to slowly lose trust. Prompt tuning helped a little. But the real improvement came from feedback loops.&lt;/p&gt;
&lt;p&gt;Every response includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;👍 Thumbs Up&lt;/li&gt;
&lt;li&gt;👎 Thumbs Down&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When users downvote an answer, the system logs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The prompt&lt;/li&gt;
&lt;li&gt;Retrieved context&lt;/li&gt;
&lt;li&gt;Routing decision&lt;/li&gt;
&lt;li&gt;Model output&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A domain expert then reviews failures and creates verified corrections. Those corrections get stored as “Golden Conversations.” Later, when similar questions appear again, the system injects those verified examples directly into the prompt. So the system gradually learns from real production mistakes without needing fine-tuning. That feedback loop pushed the system from roughly 70% to around 80% reliable accuracy.&lt;/p&gt;
&lt;p&gt;More importantly, users could actually feel the system improving over time.&lt;/p&gt;
&lt;h2 id=&quot;why-this-matters-as-companies-scale&quot;&gt;Why This Matters As Companies Scale&lt;/h2&gt;
&lt;p&gt;The most valuable part of this system isn’t just better answers. It’s operational leverage. As organizations grow, teams spend huge amounts of time:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Answering repetitive internal questions&lt;/li&gt;
&lt;li&gt;Debugging metric confusion&lt;/li&gt;
&lt;li&gt;Looking up historical decisions&lt;/li&gt;
&lt;li&gt;Handling simple operational requests&lt;/li&gt;
&lt;li&gt;Routing tickets manually&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A system like this reduces that overhead significantly. Instead of scaling support linearly with company size, the AI layer absorbs a growing percentage of repetitive knowledge and operational work.&lt;/p&gt;
&lt;p&gt;That saves:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Engineering time&lt;/li&gt;
&lt;li&gt;Analytics bandwidth&lt;/li&gt;
&lt;li&gt;Support costs&lt;/li&gt;
&lt;li&gt;Operational delays&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And because the system continuously learns through feedback loops, it becomes more valuable over time instead of degrading. The Executor Agent also creates an interesting middle ground between:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pure chatbots&lt;/li&gt;
&lt;li&gt;Full autonomous AI systems&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The system can safely automate repetitive verified actions while still escalating risky or ambiguous requests to humans. That balance is probably where a lot of practical enterprise AI systems will evolve.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;/h2&gt;
&lt;p&gt;The biggest lesson from building this project was simple:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The model matters less than the system around the model.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Routing, retrieval quality, lineage expansion, safety layers, and feedback loops had a much bigger impact than swapping between foundation models. The most useful AI systems won’t just answer questions. They’ll understand organizational context, safely take action, continuously learn from failures, and reduce operational overhead as companies scale. That’s what ultimately transformed this project from:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“a cool AI demo”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;into something teams could actually rely on.&lt;/p&gt;
</content:encoded><category>AI &amp; Agents</category></item><item><title>Why Your Documents Deserve Better: Introducing Pinnacle</title><link>https://prashantb.me/why-your-documents-deserve-better-introducing-pinnacle/</link><guid isPermaLink="true">https://prashantb.me/why-your-documents-deserve-better-introducing-pinnacle/</guid><description>Why I built Pinnacle: a local-first document intelligence app in Rust, Tauri and Svelte 5, where your files never leave your machine.</description><pubDate>Mon, 11 May 2026 02:38:47 GMT</pubDate><content:encoded>&lt;p&gt;In a world where “the cloud” is the default answer for everything, we’ve inadvertently traded our most sensitive data for convenience. Think about the last time you needed to sign a contract, merge two bank statements, or redact a social security number from a PDF.&lt;/p&gt;
&lt;p&gt;Most of us head straight to a search engine, type “online PDF editor,” and upload our most private life details to a server we don’t own, managed by a company we don’t know, sitting in a country we’ve never visited.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;That felt wrong to me. So I built Pinnacle.&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;the-invisible-problem-with-modern-document-tools&quot;&gt;The “Invisible” Problem with Modern Document Tools&lt;/h2&gt;
&lt;p&gt;We’ve become comfortable with a “Cloud-First” reality. But for documents, the cloud is often a liability, not an asset.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Privacy Tax:&lt;/strong&gt; When you upload a PDF to a “free” online converter, you aren’t just converting a file; you’re handing over data. For businesses, legal teams, or anyone with a bank account, this is a massive security risk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Subscription Fatigue:&lt;/strong&gt; Why does it cost $20/month just to delete a page from a PDF? Professional tools are bloated and expensive, while free tools are suspicious and riddled with ads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Connection Anxiety:&lt;/strong&gt; If your internet goes down, your productivity shouldn’t.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h2 id=&quot;what-is-pinnacle&quot;&gt;What is Pinnacle?&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Pinnacle&lt;/strong&gt; is a local-first, privacy-focused document intelligence application. It’s designed to be your “Digital Swiss Army Knife” for PDFs, but with a major difference: &lt;strong&gt;your files never leave your computer.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Built using the lightning-fast performance of &lt;strong&gt;Rust&lt;/strong&gt; and the sleek, reactive interface of &lt;strong&gt;Svelte 5&lt;/strong&gt;, Pinnacle combines the power of professional desktop software with the ease of a modern web app.&lt;/p&gt;
&lt;h3 id=&quot;its-more-than-just-an-editor-its-document-intelligence&quot;&gt;It’s more than just an editor. It’s “Document Intelligence.”&lt;/h3&gt;
&lt;p&gt;While Pinnacle handles the basics (merging, splitting, signing), it’s built for the future of document management:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Knowledge Graphing:&lt;/strong&gt; Ever wish you could see how your documents relate to one another? Pinnacle helps you visualize the connections between your files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Forensic Redaction:&lt;/strong&gt; We don’t just put a black box over text. We use Rust-level precision to ensure the data is &lt;em&gt;actually&lt;/em&gt; gone, not just hidden.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Local AI Insights:&lt;/strong&gt; Need a summary of a 50-page legal brief? Pinnacle can analyze it locally, using your own hardware, so your secrets stay yours.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;how-it-works-the-human-version&quot;&gt;How it Works (The Human Version)&lt;/h2&gt;
&lt;p&gt;I wanted Pinnacle to feel “alive” and tactile. We used a design style called &lt;strong&gt;Neubrutalism meets Glassmorphism&lt;/strong&gt;. It’s high-contrast, sharp, and modern.&lt;/p&gt;
&lt;p&gt;Using it is simple:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Drag and Drop:&lt;/strong&gt; Toss your files into the Library.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Choose Your Tool:&lt;/strong&gt; Need to compress a massive file for email? Click “Compression.” Need to pull data from a form? Hit “Extract.”&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Process at Warp Speed:&lt;/strong&gt; Because it’s powered by Rust (the same language used for high-performance systems), tasks that take minutes in a browser happen in milliseconds here.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Save and Forget:&lt;/strong&gt; Export your changes. No accounts, no “cloud syncing” icons, no “trial expired” popups.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h2 id=&quot;why-use-pinnacle-when-adobe-exists&quot;&gt;Why Use Pinnacle When Adobe Exists?&lt;/h2&gt;
&lt;p&gt;It’s a fair question. Adobe Acrobat is the industry giant. But Pinnacle isn’t trying to be Adobe; it’s trying to be &lt;em&gt;better&lt;/em&gt; for the modern user.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Privacy by Design:&lt;/strong&gt; In Pinnacle, “Private” isn’t a setting you toggle; it’s the foundation. There is literally no code in the app that sends your document content to a server.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Speed without Bloat:&lt;/strong&gt; Pinnacle starts up instantly. It doesn’t run fifteen background processes that slow down your computer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A One-Time Solution:&lt;/strong&gt; No recurring fees. No “Creative Cloud” logins. Just a tool that works when you need it.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;the-tech-under-the-hood&quot;&gt;The Tech Under the Hood&lt;/h2&gt;
&lt;p&gt;For the tech-curious: Pinnacle is a &lt;strong&gt;Tauri&lt;/strong&gt; app.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Brains (Rust):&lt;/strong&gt; We use Rust for all the heavy lifting. When you merge 100 PDFs, Rust ensures it’s memory-safe and incredibly fast.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Beauty (Svelte 5):&lt;/strong&gt; We used the latest Svelte “Runes” to make the UI feel fluid. Every click and transition is snappy and reactive.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Safety:&lt;/strong&gt; Because it’s a desktop app, it has restricted access to your system. It only sees what you tell it to see.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;reclaiming-your-digital-paperwork&quot;&gt;Reclaiming Your Digital Paperwork&lt;/h2&gt;
&lt;p&gt;Pinnacle was born out of a simple belief: &lt;strong&gt;Your documents are your business.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Whether you’re a student organizing research, a lawyer protecting client privilege, or just someone tired of “Cloud-everything,” Pinnacle is built for you. It’s time we brought our documents back home.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ready to reach the peak of document management?&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Pinnacle is currently in active development. Stay tuned for the first stable release.&lt;/em&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;want-to-see-the-code-or-contribute&quot;&gt;Want to see the code or contribute?&lt;/h3&gt;
&lt;p&gt;Check out our &lt;a href=&quot;https://github.com/pbse/Pinnacle&quot;&gt;GitHub repository&lt;/a&gt; and join the movement toward local-first software.&lt;/p&gt;
</content:encoded><category>Building Products</category><category>AI &amp; Agents</category></item><item><title>From Pipeline Purgatory to Data Nirvana</title><link>https://prashantb.me/from-pipeline-purgatory-to-data-nirvana/</link><guid isPermaLink="true">https://prashantb.me/from-pipeline-purgatory-to-data-nirvana/</guid><description>Replacing hand-maintained ETL with a managed ELT stack (Fivetran, BigQuery, dbt): what changed in cost, freshness and on-call load.</description><pubDate>Tue, 09 Sep 2025 04:36:58 GMT</pubDate><content:encoded>&lt;h2 id=&quot;the-ghost-in-the-machine-remembering-the-bad-old-days&quot;&gt;The Ghost in the Machine: Remembering the Bad Old Days&lt;/h2&gt;
&lt;p&gt;Picture this: It’s 2:47 AM on a Tuesday. My phone buzzes with that distinctive Slack notification sound that still triggers my fight-or-flight response. The nightly ETL job has failed. Again. Something about a malformed JSON response from an API that worked perfectly fine yesterday.&lt;/p&gt;
&lt;p&gt;I drag myself to my laptop, squinting at terminal windows filled with stack traces that might as well be hieroglyphics. The culprit? Facebook changed a field from &lt;code&gt;campaign_id&lt;/code&gt; to &lt;code&gt;campaignId&lt;/code&gt;. One capital letter. Four hours of sleep, gone.&lt;/p&gt;
&lt;p&gt;This was a day in my life circa 2018. Every pipeline was a house of cards built on quicksand during an earthquake. I had more monitoring dashboards than actual dashboards. My GitHub commits read like the diary of someone slowly losing their grip on reality: “Fixed the thing,” “Really fixed the thing this time,” “Why won’t you work,” “Please work,” “IT WORKS DON’T TOUCH ANYTHING.”&lt;/p&gt;
&lt;p&gt;The worst part? I thought this was normal. We all did. It was our shared trauma, swapped like war stories at conferences over overpriced craft beer.&lt;/p&gt;
&lt;p&gt;Then everything changed.&lt;/p&gt;
&lt;h2 id=&quot;the-day-i-stopped-writing-etl-scripts&quot;&gt;The Day I Stopped Writing ETL Scripts&lt;/h2&gt;
&lt;p&gt;Here’s what happened: Our startup was growing faster than our infrastructure could handle. We had data scattered across seventeen different systems (yes, I counted), each with its own special snowflake API. Our analysts were begging for unified reporting. The CEO wanted real-time dashboards. And there I was, maintaining a Frankenstein’s monster of Apache Airflow DAGs held together by environmental variables and prayer.&lt;/p&gt;
&lt;p&gt;A colleague mentioned Fivetran casually, the way you’d recommend a good taco place. “It just works,” he said. Those three words should have been a red flag. Nothing in data engineering “just works.”&lt;/p&gt;
&lt;p&gt;But desperation makes you try things.&lt;/p&gt;
&lt;p&gt;I logged into Fivetran’s interface expecting the usual: incomprehensible documentation, configuration files that require a PhD in YAML, and at least three authentication methods that would somehow still fail. Instead, I got something that looked suspiciously like it was designed by someone who actually had to use it.&lt;/p&gt;
&lt;p&gt;The PostgreSQL connector asked for five things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Host&lt;/li&gt;
&lt;li&gt;Port&lt;/li&gt;
&lt;li&gt;Database name&lt;/li&gt;
&lt;li&gt;Username&lt;/li&gt;
&lt;li&gt;Password&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That’s it. No SSH tunnel configuration. No SSL certificate wrangling. No sacrificial offerings to the networking gods.&lt;/p&gt;
&lt;p&gt;I clicked “Test Connection,” fully prepared for the familiar red error message. Instead: a green checkmark. My production database was talking to Fivetran. Just like that.&lt;/p&gt;
&lt;p&gt;The cynic in me was screaming. This had to be a trick.&lt;/p&gt;
&lt;h2 id=&quot;the-moment-everything-clicked-literally&quot;&gt;The Moment Everything Clicked (Literally)&lt;/h2&gt;
&lt;p&gt;Within an hour—and I’m not exaggerating—I had connected:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Our production PostgreSQL database (with CDC enabled for real-time replication)&lt;/li&gt;
&lt;li&gt;Segment’s entire event stream (goodbye, manual webhook handlers)&lt;/li&gt;
&lt;li&gt;Facebook Ads Manager (no more rate limit nightmares)&lt;/li&gt;
&lt;li&gt;Stripe billing data (with automatic schema evolution)&lt;/li&gt;
&lt;li&gt;Even our quirky internal REST API (using Fivetran’s Function connector)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each connection followed the same pattern: authenticate, select tables, choose sync frequency, done. It was almost insulting how straightforward it was. Years of accumulated scar tissue from building these integrations by hand, and now they were just… checkboxes.&lt;/p&gt;
&lt;p&gt;But here’s where it got interesting: BigQuery.&lt;/p&gt;
&lt;p&gt;Setting up BigQuery as the destination was equally anticlimactic. Fivetran asked for a service account key, I provided one, and suddenly data started flowing. Not trickling—flowing. Schemas materialized automatically in BigQuery, perfectly typed and organized. Tables appeared with names that actually made sense. Foreign keys were preserved. Data types were correctly mapped.&lt;/p&gt;
&lt;p&gt;I watched the sync logs, waiting for the other shoe to drop. An out-of-memory error, maybe. A timeout. Something. Anything to justify my years of suffering.&lt;/p&gt;
&lt;p&gt;Nothing happened. Well, that’s not true—everything happened. The data moved. Reliably. Consistently. Boringly.&lt;/p&gt;
&lt;h2 id=&quot;the-numbers-that-made-my-cfo-smile&quot;&gt;The Numbers That Made My CFO Smile&lt;/h2&gt;
&lt;p&gt;Let me paint you a picture with actual metrics:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Before Fivetran + BigQuery:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pipeline development time: 2-3 weeks per source&lt;/li&gt;
&lt;li&gt;Maintenance overhead: 40% of my time&lt;/li&gt;
&lt;li&gt;Data freshness: 1-24 hours (when things worked)&lt;/li&gt;
&lt;li&gt;Monthly infrastructure costs: ~$8,000 (servers, monitoring, alerting)&lt;/li&gt;
&lt;li&gt;Sleep quality: What sleep?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;After Fivetran + BigQuery:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pipeline development time: 1-2 hours per source&lt;/li&gt;
&lt;li&gt;Maintenance overhead: Maybe 5% of my time&lt;/li&gt;
&lt;li&gt;Data freshness: 5-15 minutes for most sources&lt;/li&gt;
&lt;li&gt;Monthly costs: ~$3,000 for Fivetran, ~$1,500 for BigQuery&lt;/li&gt;
&lt;li&gt;Sleep quality: Like a baby (one that actually sleeps)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We were processing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;50 million Segment events monthly&lt;/li&gt;
&lt;li&gt;200GB of PostgreSQL changes weekly&lt;/li&gt;
&lt;li&gt;15 marketing platform integrations&lt;/li&gt;
&lt;li&gt;All updating every 15 minutes to 6 hours depending on the source&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The kicker? We scaled from 10GB to 2TB of warehouse data without changing a single configuration. BigQuery just… handled it. No capacity planning meetings. No frantic vertical scaling at 11 PM on a Friday.&lt;/p&gt;
&lt;h2 id=&quot;the-plot-twist-learning-to-let-go&quot;&gt;The Plot Twist: Learning to Let Go&lt;/h2&gt;
&lt;p&gt;Here’s something nobody tells you about modern data infrastructure: the hardest part isn’t technical—it’s psychological.&lt;/p&gt;
&lt;p&gt;I spent the first month after setting up Fivetran checking the logs obsessively. Surely something would break. I’d refresh the sync status page like it was my Twitter feed during an election. Every successful sync felt like I was getting away with something.&lt;/p&gt;
&lt;p&gt;Then came the transformations. Fivetran deliberately doesn’t handle the “T” in ELT, and initially, that bothered me. Where was my complete solution? But this constraint turned out to be liberating. We adopted dbt (data build tool) for transformations, and suddenly our analysts were writing their own SQL models. They weren’t blocked on me anymore. They could iterate, experiment, fail fast—all the startup mantras we preached but rarely practiced.&lt;/p&gt;
&lt;p&gt;Our dbt models looked something like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;-- models/marts/marketing/campaign_performance.sql&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; facebook_spend &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    date&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    campaign_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    SUM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(spend) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; daily_spend,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    SUM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(impressions) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; impressions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {{ ref(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;stg_facebook_ads__campaigns&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  GROUP BY&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;revenue_attribution &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    DATE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;timestamp&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; date&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    utm_campaign &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; campaign_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    SUM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(revenue) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; attributed_revenue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {{ ref(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;fct_conversions&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  WHERE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; utm_source &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos;facebook&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  GROUP BY&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;  f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;  f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;campaign_name&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;  f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;daily_spend&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;  f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;impressions&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;  COALESCE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;attributed_revenue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; revenue,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  SAFE_DIVIDE(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;attributed_revenue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;daily_spend&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; roas&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; facebook_spend f&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;LEFT JOIN&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; revenue_attribution r &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, campaign_name)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Clean. Testable. Version controlled. The analysts owned it, understood it, and could modify it without fear of breaking production data flows.&lt;/p&gt;
&lt;h2 id=&quot;the-uncomfortable-truths&quot;&gt;The Uncomfortable Truths&lt;/h2&gt;
&lt;p&gt;Now, let’s talk about the catches, because there are always catches.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Price of Convenience:&lt;/strong&gt;&lt;br&gt;
Fivetran’s MAR (Monthly Active Rows) pricing model is clever until you’re syncing that chatty microservice that logs every mouse movement. We had one table that was 80% of our MAR but provided 2% of our value. The conversation went like this:&lt;/p&gt;
&lt;p&gt;Me: “Do we really need to track every page scroll event?”&lt;br&gt;
Product Manager: “Absolutely essential.”&lt;br&gt;
Me: “It’s costing us $800/month just for scroll data.”&lt;br&gt;
Product Manager: “Oh. Maybe daily aggregates are fine.”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Connector Gap:&lt;/strong&gt;&lt;br&gt;
Fivetran has 400+ connectors, which sounds like a lot until you need number 401. We use this obscure inventory management system that was apparently coded in someone’s garage in 2003. No connector. No API documentation. Just tears.&lt;/p&gt;
&lt;p&gt;The solution? Fivetran’s Function connector let me write a lightweight Lambda function to bridge the gap. It wasn’t perfect, but it was manageable:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; handler&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(request, context):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    # Fetch data from obscure system&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    weird_api_data &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; fetch_inventory_data()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    # Transform to Fivetran format&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;state&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;last_updated&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: datetime.now().isoformat()},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;insert&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;            &quot;inventory_items&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: item[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;ID&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;quantity&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: item[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;QTY&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;], &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;                 &quot;updated_at&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: item[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;LAST_MOD&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;                for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; item &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; weird_api_data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;The Black Box Problem:&lt;/strong&gt;&lt;br&gt;
When something does go wrong (and it will, because computers), debugging can be frustrating. Fivetran’s error messages sometimes read like fortune cookies: “Sync failed due to unexpected response.” Thanks, very helpful.&lt;/p&gt;
&lt;p&gt;The support team is responsive, but there’s something unsettling about not being able to SSH into a server and fix things yourself. It’s like being a mechanic who’s only allowed to look at the dashboard lights.&lt;/p&gt;
&lt;h2 id=&quot;the-philosophical-question-nobody-asks&quot;&gt;The Philosophical Question Nobody Asks&lt;/h2&gt;
&lt;p&gt;Here’s what keeps me up at night now (besides nothing, because my pipelines don’t break): Have we traded too much control for convenience?&lt;/p&gt;
&lt;p&gt;There’s an entire generation of data engineers who will never know the joy of writing a custom PostgreSQL replication slot handler. They’ll never experience the triumph of finally getting that SOAP API to work after three days of XML wrestling. Is something lost in this transaction?&lt;/p&gt;
&lt;p&gt;Maybe. But you know what else is lost? Burnout. Frustration. The opportunity cost of building commodity infrastructure instead of solving actual business problems.&lt;/p&gt;
&lt;p&gt;I used to pride myself on being able to build anything from scratch. Now I pride myself on knowing when not to. That’s growth, I think.&lt;/p&gt;
&lt;h2 id=&quot;the-verdict-a-love-letter-to-boring-technology&quot;&gt;The Verdict: A Love Letter to Boring Technology&lt;/h2&gt;
&lt;p&gt;Fivetran and BigQuery aren’t sexy. They don’t use the latest JavaScript framework (thank god). They won’t impress anyone at a hackathon. They’re boring in the best possible way—the way that electricity is boring, or indoor plumbing.&lt;/p&gt;
&lt;p&gt;They work. Consistently. Reliably. Invisibly.&lt;/p&gt;
&lt;p&gt;Our data team has tripled in size, but our data engineering team hasn’t grown at all. We’re handling 100x the data volume with the same headcount. Our analysts are self-sufficient. Our executives have real-time dashboards that actually show real-time data. Our customer success team can see user behavior patterns before users complain.&lt;/p&gt;
&lt;p&gt;Most importantly, I get to focus on interesting problems now:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Designing data models that will scale for the next five years&lt;/li&gt;
&lt;li&gt;Building predictive models instead of plumbing&lt;/li&gt;
&lt;li&gt;Actually talking to stakeholders about what insights they need&lt;/li&gt;
&lt;li&gt;Teaching analysts SQL optimization techniques&lt;/li&gt;
&lt;li&gt;Having lunch away from my desk&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Is Fivetran + BigQuery the right choice for everyone? Probably not. If you’re Netflix or Uber, you need custom everything. If you’re a 5-person startup, you probably don’t need it yet. But if you’re in that sweet spot—growing fast, data becoming critical, engineering resources precious—this combination is magic.&lt;/p&gt;
&lt;h2 id=&quot;the-final-plot-twist&quot;&gt;The Final Plot Twist&lt;/h2&gt;
&lt;p&gt;Remember that obscure inventory system I mentioned? Last month, Fivetran released a connector for it. Turns out we weren’t the only ones suffering.&lt;/p&gt;
&lt;p&gt;I decommissioned my Lambda function with a single click. It felt like saying goodbye to an old friend—an annoying, high-maintenance friend who called at 3 AM, but still.&lt;/p&gt;
&lt;p&gt;That’s the thing about the modern data stack: it keeps getting better while you’re sleeping. Literally sleeping. Eight hours a night. It’s revolutionary.&lt;/p&gt;
&lt;p&gt;So here’s my advice to past me, and maybe to you: Stop building pipelines. Start building value. Let Fivetran and BigQuery handle the plumbing. Trust me, your future self will thank you.&lt;/p&gt;
&lt;p&gt;And your phone? It can finally stay on silent.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;P.S. - To the three people who will email me about how “real engineers build their own infrastructure”: I built my own for seven years. I have the gray hairs and git history to prove it. Sometimes the most sophisticated engineering decision is choosing not to engineer something. Now if you’ll excuse me, I have a 5 PM meeting to attend. At 5:01, I’ll be logged off, because my pipelines don’t need me anymore. And that’s exactly how it should be.&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>Data Infrastructure</category></item><item><title>Data Lake Challenges and Apache Iceberg</title><link>https://prashantb.me/data-lake-challenges-and-apache-iceberg/</link><guid isPermaLink="true">https://prashantb.me/data-lake-challenges-and-apache-iceberg/</guid><description>Why traditional data lakes struggle with consistency, schema evolution and concurrent access, and how Apache Iceberg’s table format addresses them.</description><pubDate>Wed, 01 Jan 2025 01:25:26 GMT</pubDate><content:encoded>&lt;blockquote&gt;
&lt;p&gt;Data storage and processing have evolved rapidly over the past decade, moving from on-site servers to scalable cloud-based systems. These modern solutions, often referred to as data lakes, can handle massive streams of data—such as billions of credit card transactions, website interactions, or customer activities—all in near real-time.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;the-great-data-lake-delusion&quot;&gt;The Great Data Lake Delusion&lt;/h3&gt;
&lt;p&gt;Sarah stared at her Slack notifications as they multiplied like digital rabbits. The quarterly board presentation was in three hours, and somehow her company’s “state-of-the-art” data lake was reporting that they had simultaneously gained and lost the same 50,000 customers. The marketing team swore they were heroes, the finance team was preparing for bankruptcy, and the data science team had locked themselves in a conference room, muttering about “eventual consistency” like it was some kind of religious mantra.&lt;/p&gt;
&lt;p&gt;This wasn’t supposed to happen. Two years ago, Sarah’s company had invested millions in a “revolutionary” data lake architecture. The consultants promised it would be their competitive advantage. The vendor demos showed beautiful dashboards updating in real-time. The PowerPoint slides were pristine.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reality, as usual, had other plans.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Meanwhile, somewhere in Los Gatos, California, Netflix engineers were having their own existential crisis. Their data lake wasn’t just inconsistent—it was actively hostile. Billions of viewing events were scattered across their storage like confetti after a particularly chaotic New Year’s party, and traditional Hive tables were about as reliable as a chocolate teapot under pressure.&lt;/p&gt;
&lt;p&gt;But here’s where the story gets interesting: instead of just complaining about it on Twitter like the rest of us, Netflix actually did something about it. They built Apache Iceberg, and in doing so, accidentally created the solution to every data engineer’s recurring nightmares.&lt;/p&gt;
&lt;h2 id=&quot;the-four-horsemen-of-data-lake-hell&quot;&gt;The Four Horsemen of Data Lake Hell&lt;/h2&gt;
&lt;h3 id=&quot;the-everything-is-fine-consistency-crisis&quot;&gt;The “Everything is Fine” Consistency Crisis&lt;/h3&gt;
&lt;p&gt;Let’s be brutally honest about data lakes: they were designed by people who clearly never had to explain to a CEO why the revenue numbers changed three times during a single meeting. Traditional data lakes treat data consistency the way teenagers treat curfews—more of a suggestion than an actual rule.&lt;/p&gt;
&lt;p&gt;Here’s what typically happens in the wild:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Team A&lt;/strong&gt; updates customer records&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Team B&lt;/strong&gt; reads “the latest” data (which is actually from 20 minutes ago)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Team C&lt;/strong&gt; overwrites Team A’s changes without knowing they existed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Team D&lt;/strong&gt; generates a report that makes everyone question reality&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The result? Data that’s about as consistent as a toddler’s nap schedule. You think you know what’s happening, but five minutes later, everything has changed and no one can explain why.&lt;/p&gt;
&lt;h3 id=&quot;schema-evolution-aka-lets-break-everything&quot;&gt;Schema Evolution: AKA “Let’s Break Everything”&lt;/h3&gt;
&lt;p&gt;“Hey, can we just add a simple field to track customer preferences?”&lt;/p&gt;
&lt;p&gt;&lt;em&gt;[Cue the dramatic music and slow-motion disaster footage]&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the pre-Iceberg world, this innocent request would trigger what data engineers lovingly call “the schema apocalypse”:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Emergency architecture review meetings&lt;/li&gt;
&lt;li&gt;Three-week migration planning sessions&lt;/li&gt;
&lt;li&gt;Mandatory downtime windows at 3 AM on Sunday&lt;/li&gt;
&lt;li&gt;Prayer circles and ritual sacrifices to the database gods&lt;/li&gt;
&lt;li&gt;At least one engineer stress-eating pizza at midnight while rebuilding indexes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Schema changes in traditional systems are handled with all the grace and elegance of performing heart surgery with gardening tools. It’s technically possible, but everyone involved is going to have a bad time.&lt;/p&gt;
&lt;h3 id=&quot;the-multi-user-thunderdome&quot;&gt;The Multi-User Thunderdome&lt;/h3&gt;
&lt;p&gt;Modern organizations are basically data zoos where everyone wants to feed the animals at the same time. You’ve got:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Marketing teams extracting customer behavior patterns like digital archaeologists&lt;/li&gt;
&lt;li&gt;Finance teams generating compliance reports with the urgency of defusing bombs&lt;/li&gt;
&lt;li&gt;Data scientists training ML models that consume resources like teenagers consume pizza&lt;/li&gt;
&lt;li&gt;Operations teams monitoring dashboards like air traffic controllers&lt;/li&gt;
&lt;li&gt;Executives demanding “real-time insights” about data that’s still being processed&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Without proper coordination, this creates a digital version of bumper cars—lots of noise, occasional crashes, and someone always ends up dizzy and confused.&lt;/p&gt;
&lt;h3 id=&quot;the-historical-data-hoarding-problem&quot;&gt;The Historical Data Hoarding Problem&lt;/h3&gt;
&lt;p&gt;Organizations collect data like digital pack rats. Every click, every transaction, every customer sneeze gets stored “for analytics.” But here’s the kicker: storing petabytes of historical data while keeping it performant and cost-effective is like trying to organize a library where books keep multiplying overnight and occasionally change their own content.&lt;/p&gt;
&lt;p&gt;You need the data for compliance (lawyers are scary), analytics (executives demand insights), and machine learning (the algorithms are hungry), but traditional storage solutions handle this about as well as a paper umbrella handles a hurricane.&lt;/p&gt;
&lt;h2 id=&quot;apache-iceberg-the-accidental-hero-story&quot;&gt;Apache Iceberg: The Accidental Hero Story&lt;/h2&gt;
&lt;p&gt;Back in 2017, Netflix had a problem. Actually, they had several problems, but the big one was that their data infrastructure was buckling under the weight of their own success. Millions of users streaming billions of hours of content generates data at a scale that makes most databases weep quietly in server rooms.&lt;/p&gt;
&lt;p&gt;Their existing Hive tables were failing spectacularly—like watching a house of cards collapse in slow motion, except the cards were made of data and the collapse was affecting recommendations for 200+ million subscribers.&lt;/p&gt;
&lt;p&gt;So Netflix did what any sensible engineering organization would do: they built something completely new. Not because they wanted to become open-source heroes (though that’s a nice side effect), but because they literally had no choice. Their business was growing faster than their data infrastructure could handle.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Apache Iceberg wasn’t born from strategic planning—it was born from desperation.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;And thank goodness for that, because the rest of us were drowning too. We just didn’t have Netflix’s resources to build our own life rafts.&lt;/p&gt;
&lt;h2 id=&quot;how-iceberg-became-the-data-worlds-superhero&quot;&gt;How Iceberg Became the Data World’s Superhero&lt;/h2&gt;
&lt;h3 id=&quot;acid-transactions-because-chaos-isnt-a-feature&quot;&gt;ACID Transactions: Because Chaos Isn’t a Feature&lt;/h3&gt;
&lt;p&gt;Apache Iceberg brings &lt;strong&gt;ACID transactions&lt;/strong&gt; to data lakes, which is like giving your data operations a really good therapist. Suddenly, everything that was chaotic and unpredictable becomes calm and orderly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Atomicity&lt;/strong&gt;: Changes either happen completely or not at all&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Consistency&lt;/strong&gt;: Data always makes sense&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Isolation&lt;/strong&gt;: Multiple teams can work without accidentally sabotaging each other&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Durability&lt;/strong&gt;: Committed changes survive system failures&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s the difference between a peaceful meditation garden and a toddler birthday party in terms of chaos levels.&lt;/p&gt;
&lt;h3 id=&quot;schema-evolution-without-the-drama&quot;&gt;Schema Evolution Without the Drama&lt;/h3&gt;
&lt;p&gt;With Iceberg, adding that customer preference field becomes almost disappointingly simple:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; customers &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; COLUMN preferences MAP&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;STRING, STRING&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s it. No table rebuilds, no downtime, no midnight emergency deployments. The system just… handles it.&lt;/p&gt;
&lt;p&gt;Features include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Column additions&lt;/strong&gt; that don’t break existing applications&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Type promotions&lt;/strong&gt; that happen automatically&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Column renames&lt;/strong&gt; with full backward compatibility&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Concurrent schema updates&lt;/strong&gt; without teams stepping on each other&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;time-travel-because-sometimes-you-need-to-go-back&quot;&gt;Time Travel: Because Sometimes You Need to Go Back&lt;/h3&gt;
&lt;p&gt;Iceberg’s &lt;strong&gt;time travel capabilities&lt;/strong&gt; are like having a time machine for your data:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; sales_data &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;FOR&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; TIMESTAMP&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; OF &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;2024-01-01 00:00:00&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This makes debugging systematic instead of chaotic. &lt;strong&gt;Snapshot isolation&lt;/strong&gt; ensures each team gets their own consistent view of the data.&lt;/p&gt;
&lt;h3 id=&quot;storage-management-that-actually-works&quot;&gt;Storage Management That Actually Works&lt;/h3&gt;
&lt;p&gt;Iceberg separates metadata from data files, enabling optimizations that feel almost magical:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Automatic file compaction&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Partition evolution&lt;/strong&gt; that adapts to changing patterns&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Metadata-level query pruning&lt;/strong&gt; that makes queries fast&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-tier storage&lt;/strong&gt; that optimizes costs&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;real-world-success-stories&quot;&gt;Real-World Success Stories&lt;/h2&gt;
&lt;h3 id=&quot;the-retail-giants-redemption-arc&quot;&gt;The Retail Giant’s Redemption Arc&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Real-time personalization actually works&lt;/li&gt;
&lt;li&gt;Data consistency issues dropped to near zero&lt;/li&gt;
&lt;li&gt;The 3 AM emergency calls stopped&lt;/li&gt;
&lt;li&gt;Customer satisfaction improved&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;the-financial-services-breakthrough&quot;&gt;The Financial Services Breakthrough&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;40% reduction in storage costs&lt;/li&gt;
&lt;li&gt;Compliance reports started matching reality&lt;/li&gt;
&lt;li&gt;Happier auditors and executives&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;the-developer-experience-revolution&quot;&gt;The Developer Experience Revolution&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Engineers spend time building features, not fighting infra&lt;/li&gt;
&lt;li&gt;Job satisfaction improved&lt;/li&gt;
&lt;li&gt;Attrition dropped&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-competition-battle-of-the-table-formats&quot;&gt;The Competition: Battle of the Table Formats&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Apache Iceberg&lt;/th&gt;
&lt;th&gt;Delta Lake&lt;/th&gt;
&lt;th&gt;Apache Hudi&lt;/th&gt;
&lt;th&gt;Traditional Hive&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ACID Transactions&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ Actually works&lt;/td&gt;
&lt;td&gt;✅ Works well&lt;/td&gt;
&lt;td&gt;✅ Decent&lt;/td&gt;
&lt;td&gt;❌ Good luck&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Schema Evolution&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🏆 Seamless&lt;/td&gt;
&lt;td&gt;✅ Solid&lt;/td&gt;
&lt;td&gt;✅ Functional&lt;/td&gt;
&lt;td&gt;❌ Requires therapy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Query Engine Support&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🏆 Works with all&lt;/td&gt;
&lt;td&gt;🔧 Spark-centric&lt;/td&gt;
&lt;td&gt;⚠️ Limited/growing&lt;/td&gt;
&lt;td&gt;📊 Broad but ancient&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Partition Evolution&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🏆 Advanced magic&lt;/td&gt;
&lt;td&gt;⚠️ Basic&lt;/td&gt;
&lt;td&gt;⚠️ Getting there&lt;/td&gt;
&lt;td&gt;❌ Not happening&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Time Travel&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ Native&lt;/td&gt;
&lt;td&gt;✅ Built-in&lt;/td&gt;
&lt;td&gt;✅ Available&lt;/td&gt;
&lt;td&gt;❌ Time is an illusion&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;cloud-provider-solutions-the-easy-button&quot;&gt;Cloud Provider Solutions: The Easy Button&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS EMR&lt;/strong&gt;: Supports all formats, integrates with Glue&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Azure Synapse&lt;/strong&gt;: Managed Iceberg with optimizations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Google Cloud BigLake&lt;/strong&gt;: Unified analytics across formats&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-bottom-line-why-you-should-care&quot;&gt;The Bottom Line: Why You Should Care&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;For Executives&lt;/strong&gt;: Iceberg reduces operational risk, lowers costs, and speeds up feature delivery.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;For Engineers&lt;/strong&gt;: Iceberg removes the drudgery of data lake management, freeing you to build meaningful systems.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;For Everyone Else&lt;/strong&gt;: Your dashboards, ML models, and reports actually reflect reality.&lt;/p&gt;
&lt;p&gt;The data revolution is here, and it’s being led by technologies like Apache Iceberg.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;em&gt;Apache Iceberg continues evolving rapidly. The technology landscape changes fast, but the need for reliable, scalable, and sane data operations remains constant.&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>Data Infrastructure</category></item><item><title>Understanding the Command Design Pattern: A Must-Have for Event-Driven Architectures</title><link>https://prashantb.me/understanding-the-command-design-pattern-in-go-a-must-have-for-event-driven-architectures/</link><guid isPermaLink="true">https://prashantb.me/understanding-the-command-design-pattern-in-go-a-must-have-for-event-driven-architectures/</guid><description>Encapsulating requests as objects to decouple, queue, log and undo work: the Command pattern in Go, and why it fits event-driven systems.</description><pubDate>Wed, 14 Aug 2024 20:07:39 GMT</pubDate><content:encoded>&lt;p&gt;Imagine you’re building a complex application that must respond to various events, such as user actions, system triggers, or external API calls. As your application grows, the code that handles these events can become unwieldy, leading to tightly coupled, hard-to-maintain systems. This is where the &lt;strong&gt;Command Design Pattern&lt;/strong&gt; comes in handy.&lt;/p&gt;
&lt;p&gt;The Command Design Pattern is a behavioral design pattern that turns a request into a stand-alone object containing all information about the request. This transformation allows you to parameterize methods with different requests, delay or queue a request’s execution, and support undoable operations.&lt;/p&gt;
&lt;p&gt;In this blog post, we’ll explore the Command Design Pattern, focusing on how to implement it. We’ll walk through a story-driven example that demonstrates why this pattern is essential for modern, event-driven architectures.&lt;/p&gt;
&lt;h2 id=&quot;the-problem-unmanageable-event-handling&quot;&gt;&lt;strong&gt;The Problem: Unmanageable Event Handling&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Imagine you’re the lead engineer at a startup developing a sophisticated e-commerce platform. The platform needs to handle various user actions—placing an order, canceling an order, updating profile information, and more. Initially, everything works fine with a few if-else conditions or a simple switch statement. But as the platform grows, you start noticing issues:&lt;/p&gt;
&lt;p&gt;• &lt;strong&gt;Tightly Coupled Code&lt;/strong&gt;: Your event handling logic is scattered across the codebase, making it difficult to manage or modify without introducing bugs.&lt;/p&gt;
&lt;p&gt;• &lt;strong&gt;Lack of Flexibility&lt;/strong&gt;: Adding new actions requires touching multiple parts of the code, leading to longer development cycles.&lt;/p&gt;
&lt;p&gt;• &lt;strong&gt;No Undo Support&lt;/strong&gt;: Users want to undo certain actions, like canceling an order they just placed, but your system wasn’t designed with this in mind.&lt;/p&gt;
&lt;p&gt;Clearly, a better approach is needed—enter the Command Design Pattern.&lt;/p&gt;
&lt;h2 id=&quot;the-solution-command-design-pattern&quot;&gt;&lt;strong&gt;The Solution: Command Design Pattern&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;The Command Design Pattern solves these problems by encapsulating all the details of an operation into a command object. This object includes the operation name, the target of the operation, and any required parameters. By doing so, you can:&lt;/p&gt;
&lt;p&gt;• &lt;strong&gt;Decouple the sender and receiver&lt;/strong&gt;: The object that initiates the action doesn’t need to know anything about the object that performs the action.&lt;/p&gt;
&lt;p&gt;• &lt;strong&gt;Queue, log, or undo commands&lt;/strong&gt;: Since each command is a standalone object, you can easily store it for later execution, log it, or provide an undo functionality.&lt;/p&gt;
&lt;p&gt;• &lt;strong&gt;Add new commands easily&lt;/strong&gt;: Extending your system with new commands doesn’t require changes to existing code, just the addition of new command objects.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Implementing the Command Design Pattern in Go&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let’s implement the Command Design Pattern in Go with an example. We’ll use a story to keep things engaging: Imagine you’re building a task management system where users can add, remove, and mark tasks as completed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1: Define the Command Interface&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The first step is to define a Command interface with a Execute method. This method will be implemented by all concrete command types.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;fmt&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Command interface&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Command&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; interface&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    Execute&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 2: Create Concrete Commands&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Next, we create concrete command types that implement the Command interface. For our task management system, we’ll create three commands: &lt;code&gt;AddTaskCommand&lt;/code&gt;, &lt;code&gt;RemoveTaskCommand&lt;/code&gt;, and &lt;code&gt;CompleteTaskCommand&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Receiver&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; TaskManager&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    tasks []&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskManager&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;AddTask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;task&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    t.tasks &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; append&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(t.tasks, task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    fmt.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Task added:&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskManager&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;RemoveTask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;task&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; i, tsk &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; t.tasks {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; tsk &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; task {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            t.tasks &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; append&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(t.tasks[:i], t.tasks[i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;:]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            fmt.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Task removed:&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;            return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    fmt.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Task not found:&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskManager&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;CompleteTask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;task&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    fmt.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Task completed:&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Concrete Command for adding a task&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; AddTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    taskManager &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskManager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    task        &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;c &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;AddTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Execute&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    c.taskManager.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;AddTask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(c.task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Concrete Command for removing a task&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; RemoveTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    taskManager &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskManager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    task        &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;c &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;RemoveTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Execute&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    c.taskManager.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;RemoveTask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(c.task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Concrete Command for completing a task&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; CompleteTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    taskManager &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskManager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    task        &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;c &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;CompleteTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Execute&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    c.taskManager.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;CompleteTask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(c.task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 3: Implement the Invoker&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The invoker is responsible for executing commands. It doesn’t need to know anything about the commands themselves, just that they implement the Command interface.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Invoker&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; TaskInvoker&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    commandQueue []&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;Command&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskInvoker&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;StoreCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;command&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Command&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    i.commandQueue &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; append&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(i.commandQueue, command)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskInvoker&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;ExecuteCommands&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; _, command &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; i.commandQueue {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        command.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Execute&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    i.commandQueue &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 4: Putting It All Together&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now, let’s see how everything fits together.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    taskManager &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskManager&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;{}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    addTaskCommand &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;AddTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        taskManager: taskManager,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        task:        &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Learn Go&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    removeTaskCommand &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;RemoveTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        taskManager: taskManager,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        task:        &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Learn Python&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    completeTaskCommand &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;CompleteTaskCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        taskManager: taskManager,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        task:        &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Learn Go&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    invoker &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;TaskInvoker&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;{}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    invoker.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;StoreCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(addTaskCommand)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    invoker.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;StoreCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(removeTaskCommand)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    invoker.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;StoreCommand&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(completeTaskCommand)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    invoker.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;ExecuteCommands&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why Use the Command Design Pattern in Modern Applications?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now that we’ve gone through the technical implementation, let’s revisit the story and discuss why the Command Design Pattern is so valuable in modern, event-driven architectures.&lt;/p&gt;
&lt;p&gt;1. &lt;strong&gt;Scalability&lt;/strong&gt;: As your application grows, you’ll inevitably add more features. With the Command Design Pattern, adding new commands doesn’t disrupt existing code, making your system more scalable.&lt;/p&gt;
&lt;p&gt;2. &lt;strong&gt;Maintainability&lt;/strong&gt;: Decoupling the invoker from the receiver makes your code easier to maintain. You can modify or replace commands without affecting other parts of the system.&lt;/p&gt;
&lt;p&gt;3. &lt;strong&gt;Flexibility&lt;/strong&gt;: The pattern provides the flexibility to log, queue, and undo actions. In a real-world e-commerce system, this could mean allowing customers to undo orders or administrators to batch process user actions.&lt;/p&gt;
&lt;p&gt;4. &lt;strong&gt;Testability&lt;/strong&gt;: Commands are easy to test in isolation since they encapsulate all the necessary information for the action they perform.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Command Design Pattern is more than just a design pattern—it’s a powerful tool that can help you build flexible, maintainable, and scalable systems. Whether you’re developing a task management app, an e-commerce platform, or any other event-driven application, this pattern is worth considering.&lt;/p&gt;
&lt;p&gt;By adopting the Command Design Pattern in your Go projects, you’ll be well-equipped to handle the complexities of modern software development. Plus, the benefits of decoupling and flexibility will pay off in the long run as your application grows and evolves.&lt;/p&gt;
&lt;p&gt;So the next time you’re faced with the challenge of managing a myriad of actions in your application, remember the Command Design Pattern and the story of the task management system—it just might be the solution you need.&lt;/p&gt;
</content:encoded><category>Systems &amp; Architecture</category></item><item><title>Async Generators, the superior async await</title><link>https://prashantb.me/async-generators-the-superior-async-await/</link><guid isPermaLink="true">https://prashantb.me/async-generators-the-superior-async-await/</guid><description>Using async generators to consume paginated and streaming sources lazily, without buffering everything in memory.</description><pubDate>Wed, 16 Aug 2023 16:42:07 GMT</pubDate><content:encoded>&lt;p&gt;An async generator is a special way for computers to do things that take time, such as getting information from the internet. Lets take a deeper dive into it from a real world example.&lt;/p&gt;
&lt;p&gt;Imagine you’re at a magical bakery, and you’re trying to bake a variety of delicious cakes. You have a helper named Alice, who is a bit of a baking expert. Now, baking takes time, and some steps might require waiting – like waiting for the cake to bake in the oven. This is similar to how computers sometimes need to wait for things to happen, like fetching data from the internet.&lt;/p&gt;
&lt;p&gt;Now, in the world of programming, JavaScript is like your magical kitchen. Async generators are like a special recipe book that Alice follows to bake cakes, but with a twist. You see, Alice can’t bake all the cakes at once – that would be too overwhelming. Instead, she bakes them one by one, taking breaks when needed.&lt;/p&gt;
&lt;p&gt;Here’s how async generators work in the magical bakery of JavaScript:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Recipe Book (Async Generator Function):&lt;/strong&gt; Just like a recipe book tells Alice how to bake cakes, an async generator function is a set of instructions for JavaScript on how to do tasks that might take time, like fetching data from the internet or reading a big file. This function is special because it can “pause” and “resume” whenever it needs to wait for something to happen.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Baking Cakes (Generating Values):&lt;/strong&gt; Instead of baking cakes, the async generator function produces values over time. These values are like the cakes that Alice bakes. But instead of baking all the cakes at once, the async generator bakes one “value” at a time, and then takes a break before baking the next one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Taking Breaks (Pausing and Resuming):&lt;/strong&gt; Imagine Alice is baking a cake, but then realizes she needs more chocolate chips. She pauses, goes to get the chips, and then resumes baking. Similarly, the async generator can “pause” its execution when it needs to wait for something. It tells JavaScript, “Hey, I’m waiting for something, let me know when it’s ready!” Then, when it’s time, it “resumes” where it left off.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enjoying Cakes (Consuming Values):&lt;/strong&gt; Now, you can be the one enjoying the cakes Alice bakes. In JavaScript, you can “consume” the values the async generator produces. It’s like waiting for Alice to finish baking a cake and then getting to eat it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Many Cakes, One by One (Async Iteration):&lt;/strong&gt; Just as Alice can bake multiple cakes, the async generator can produce multiple values over time. And just like you’d eat the cakes one by one, JavaScript can “consume” these values in the order they’re baked.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So, why are async generators useful? Imagine you’re creating a website that shows cute animal pictures from around the world. You need to fetch these pictures from different websites, which can take time. With async generators, your JavaScript code can fetch and show these pictures one by one, without freezing up the whole website. This way, your website stays responsive and enjoyable, even when dealing with slow tasks like fetching data.&lt;/p&gt;
&lt;p&gt;In simple words, async generators help JavaScript handle tasks that might take time, like baking cakes one by one, so your programs stay smooth and your users keep enjoying their experience. Just like Alice’s magical bakery makes sure you enjoy cakes without waiting too long, async generators in JavaScript keep things running smoothly even when there’s waiting involved.&lt;/p&gt;
&lt;p&gt;Lets dive into some code now -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Imagine we have a function that simulates getting data from the internet&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; fetchDataFromInternet&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;delay&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; Promise&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;resolve&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    setTimeout&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;      resolve&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(data);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }, delay);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Async generator function that fetches data one by one&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; fetchMultipleData&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  yield&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; fetchDataFromInternet&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;2000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;First piece of data&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  yield&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; fetchDataFromInternet&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;3000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Second piece of data&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  yield&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; fetchDataFromInternet&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1500&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Third piece of data&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Using the async generator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; () &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; of&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; fetchMultipleData&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Received:&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, data);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;All data received!&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;fetchDataFromInternet&lt;/code&gt; is a simulated function that returns a promise, pretending to fetch data from the internet. It takes a delay parameter to simulate the time it takes to get the data.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fetchMultipleData&lt;/code&gt; is the async generator function. It uses the &lt;code&gt;yield&lt;/code&gt; keyword to produce values one by one. Each &lt;code&gt;yield&lt;/code&gt; represents fetching data from the internet with a different delay.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;for await...of&lt;/code&gt; loop is used to consume the values produced by the async generator. It waits for each value and logs it.&lt;/li&gt;
&lt;li&gt;When you run the code, you’ll notice that each piece of data takes a different amount of time to be received, but the loop doesn’t wait for all of them to finish before processing the next one. This demonstrates the asynchronous nature of the generator.&lt;/li&gt;
&lt;li&gt;Finally, the “All data received!” message is logged when all the data pieces have been fetched and processed.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Remember, async generators are especially helpful when dealing with tasks that have varying time delays, like fetching data from the internet, reading large files, or processing data in batches. They allow you to handle these tasks efficiently and without freezing up your program.&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Why I moved from Nodejs to Go</title><link>https://prashantb.me/why-i-moved-from-nodejs-to-go/</link><guid isPermaLink="true">https://prashantb.me/why-i-moved-from-nodejs-to-go/</guid><description>Notes from moving backend work from Node.js to Go after eight years of Node in production.</description><pubDate>Sun, 28 May 2023 06:48:34 GMT</pubDate><content:encoded>&lt;p&gt;Over the past 8 years, I have been extensively working with Nodejs on the backend developing various types of I/O intensive web applications. I have developed tens of small scale and large scale services that are handling 1000’s of operations in no time and are surfacing applications across various product domains.&lt;/p&gt;
&lt;p&gt;I was fascinated by the handling of files in nodejs. Allowing to pipe streams and manipulate chunks is extremely powerful. So far, no language has shown the ease and the speed with which these operations are carried out. Sometime back I decided to give GO a go simply because have been hearing really good stuff about it from fellow engineers. The results, to my surprise were drastically different. I knew GO will perform better but as I put the system under load, it became clear, GO simply beats nodejs by a fair margin.&lt;/p&gt;
&lt;p&gt;Let’s try to build a CPU intensive program and compare how both perform. Consider a factorial program. Before moving forward, heres what a factorial is -&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In mathematic the &lt;strong&gt;factorial&lt;/strong&gt; of a non-negative integer denoted by &lt;strong&gt;n!&lt;/strong&gt; is the product of all positive integers less than or equal to &lt;strong&gt;n&lt;/strong&gt;. The factorial of &lt;strong&gt;n&lt;/strong&gt; also equals the product of &lt;strong&gt;n&lt;/strong&gt; with the next smaller factorial:&lt;/p&gt;
&lt;p&gt;n!=n×(n−1)×(n−2)×(n−3)×⋯×3×2×1=n×(n−1)!&lt;/p&gt;
&lt;p&gt;For example, 5! = 5×4! = 5×4×3×2×1 = 120.&lt;br&gt;
The value of 0! is 1.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Heres how it can be written in GO (assuming recursion is understood.)&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;fmt&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; computeFactorial&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; computeFactorial&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(n&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	start &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; time.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Now&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	result &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; computeFactorial&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	elapsed &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; time.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Since&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(start)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	fmt.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Printf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Result: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;%d\n&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, result)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	fmt.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Printf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Time taken: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;%s\n&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, elapsed)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, we calculate the factorial of a number (&lt;em&gt;20&lt;/em&gt;) using a recursive function. Go’s efficient concurrency model and compiled nature allow it to handle CPU-bound tasks like this more efficiently. On a 6 Core Machine, heres the output -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Result: 2432902008176640000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Time taken: 207ns&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;NodeJS Example -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; computeFactorial&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; computeFactorial&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; process.hrtime.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; result&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; computeFactorial&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; elapsed&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; process.hrtime.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; start;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Result:&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, result);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Time taken:&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, elapsed, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;ns&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Nodejs, the same factorial computation is performed using a recursive function. Here is the result on a 6core machine -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Result: 2432902008176640000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Time taken: 17309n ns&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, JavaScript’s single-threaded event loop and interpreted nature can result in slower performance compared to Go for CPU-bound tasks.&lt;/p&gt;
&lt;p&gt;The difference in performance becomes more noticeable as the computational complexity increases or when running multiple parallel computations. Go’s ability to leverage multiple cores efficiently and its compiled nature provide an advantage for CPU-bound tasks, making it a favorable choice in such scenarios.&lt;/p&gt;
&lt;p&gt;It is so evident from the above, for a very simple computation use case, GO is not just easy, but is also pretty optimum. I have compiled some common reasons on why GO is so much better than nodejs -&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Performance:&lt;/strong&gt; Go is known for its high performance and efficiency. It compiles to machine code, which allows it to execute faster than interpreted languages like JavaScript, which is used in Node.js. Go’s lightweight goroutines and built-in concurrency features also make it highly scalable and efficient in handling concurrent tasks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Concurrency and Parallelism:&lt;/strong&gt; Go has excellent support for concurrency and parallelism. Goroutines and channels in Go enable concurrent programming with ease. It allows developers to efficiently handle multiple requests simultaneously, making it well-suited for building scalable and high-performance backend systems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Static Typing:&lt;/strong&gt; Go is a statically typed language, meaning it performs type checking at compile-time. This helps catch errors early in the development process, making it easier to write reliable and maintainable code. In contrast, Node.js uses JavaScript, which is dynamically typed, allowing more flexibility but potentially leading to runtime errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strong Standard Library:&lt;/strong&gt; Go comes with a rich standard library that provides a wide range of functionality out of the box. It includes packages for networking, encryption, HTTP, file handling, and more. This allows developers to rely on the standard library without having to rely heavily on external dependencies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Concurrency Safety:&lt;/strong&gt; Go has built-in features like goroutines and channels, which make it easy to write concurrent code that is less prone to race conditions and other common concurrency issues. Node.js, on the other hand, requires additional effort and the use of external libraries to achieve similar levels of concurrency safety.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Deployment and Execution:&lt;/strong&gt; Go compiles to a single binary that can be easily deployed and executed on various platforms without requiring the installation of any additional runtime dependencies. Node.js applications, on the other hand, require the Node.js runtime environment to be installed on the target server, which adds complexity to deployment.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;More practical examples to follow soon.&lt;/p&gt;
</content:encoded><category>Systems &amp; Architecture</category></item><item><title>Build a Facebook Messenger Bot App</title><link>https://prashantb.me/build-a-facebook-messenger-bot-app/</link><guid isPermaLink="true">https://prashantb.me/build-a-facebook-messenger-bot-app/</guid><description>build a facebook messenger bot from scratch in minutes. Configure answers, questions and other types of replies. This is built using TypeScript, NodeJs, Express</description><pubDate>Mon, 01 Oct 2018 04:35:39 GMT</pubDate><content:encoded>&lt;p&gt;Here is the source code &lt;a href=&quot;https://github.com/prashantban/Facebook-Bot-App&quot; target=&quot;_blank&quot;&gt; {Github Link}&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The goal of this post is to showcase how easy it is to create a Facebbok Messenger Bot which is capable of handling almost all of your customer’s queries or questions etc. Lets dive into the code.&lt;/p&gt;
&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git clone https://github.com/prashantban/Facebook-Bot-App&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cd Facebook-Bot-App&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm install&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm build (To build app)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm run (To run app)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before we begin running this app, we need coupld of things from &lt;code&gt;Facebook Developer&lt;/code&gt; . Here is the list of things needed -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Facebook Page &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Facebook Developer App&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Access Token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;App Secret&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Verify Token&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is the link for &lt;a href=&quot;http://https://developers.facebook.com/apps/&quot;&gt;Facebook Developer Panel&lt;/a&gt;. Now follow the instruction to create a new Facebook Messenger App.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Add a product Messenger&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Select your FB Page and generate the token. This will be your access token.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Then you need to setup webhook endpoint. For this, we will have to write some code and expose a Webhook endpoint on our app.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;webhook-endpoint&quot;&gt;Webhook Endpoint&lt;/h2&gt;
&lt;p&gt;Facebook expects an endpoint in your system where messages will be posted. But prior to posting messages, facebook makes a &lt;code&gt;GET&lt;/code&gt; call to the same api to verify the access token. Remember, above we &lt;code&gt;Verify Token&lt;/code&gt;, put some random string in your &lt;code&gt;.env&lt;/code&gt; file for now.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# Any secret to be kept&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;APP_SECRET=&apos;&amp;lt;Your App Secret&amp;gt;&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ACCESS_TOKEN=&apos;Access Token Copied from Your FB Page&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;VERIFY_TOKEN=&apos;my random string&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We will leave this for now, and get started with our code. This project will be built in &lt;code&gt;TypeScript&lt;/code&gt;. To those who have little to no idea about &lt;code&gt;TypeScript&lt;/code&gt;, can read &lt;a href=&quot;https://www.typescriptlang.org/&quot;&gt;here&lt;/a&gt;. Here are the following things need to be present -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;nodejs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;npm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;typescript&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;express&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Obviously we have done &lt;code&gt;npm install&lt;/code&gt;, so I assume all these things are available. Here is how our folder structure looks (this is the layout of the repository linked above) -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;src/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  server.ts, app.ts              Express server; PORT comes from .env&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  controllers/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    api.ts                       the bot, the code below&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    home.ts, bootbot.d.ts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  modules/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    greet.ts, help.ts, genre.ts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  types/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    payloadTypes.ts, chatTypes.ts, genreResponse.ts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  util/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    logger.ts, genreData.ts, utilFunctions.ts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;.env.example&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;package.json, tsconfig.json&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As simple as it can get, here is the code for our bot.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;use strict&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; express &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;express&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; router&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; express.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Router&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; logger &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../util/logger&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; BootBot &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos;bootbot&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {Payload} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../types/payloadTypes&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {Chat} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../types/chatTypes&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {helpFunc} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../modules/help&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {genreFunc} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../modules/genre&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {greetFunc} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../modules/greet&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; bot&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; BootBot&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    accessToken: process.env.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;ACCESS_TOKEN&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    verifyToken: process.env.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;VERIFY_TOKEN&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    appSecret: process.env.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;APP_SECRET&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * Define Home page route&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * Not using this here though&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;router.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;/&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;_req&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;res&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    res.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;send&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;Thanks for Checking Us Out&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * This will get triggered as and when any one&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * opens the chat window for the first time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;setGreetingText&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Hello, Welcome to XYZ Page. Lets start by knowing your favorite genre.&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * Log the message we receive.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * All the message with type `message`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * will appear in this function&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;on&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;message&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;_chat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Chat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    logger.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;module&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Api Controller&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: payload.message.text, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;details&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: payload});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * Log the Attachment message we receive.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * All the message with type `Attachment message`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * will appear in this function&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;on&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;attachment&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;chat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Chat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    logger.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;module&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Api Controller&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Recieved Attachment&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;details&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: payload});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    chat.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;say&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;We do not support this message type&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * Assign Bot Modules&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(greetFunc);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(helpFunc);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(genreFunc);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * Start the Bot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(process.env.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;BOTPORT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; router;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To explain, FB supports several different types of messages, for example - &lt;code&gt;Normal Message, Greeting Text, Quick Reply, Options etc &lt;/code&gt;, so instead of creating the structure of each of those, we are using a library named &lt;code&gt;bootbot&lt;/code&gt;. This library has a pretty neat interface to define what type of message to be sent. Note, for instantiating an instance of &lt;code&gt;bootbot&lt;/code&gt;, we are passing all the necessary client details to it at the top.&lt;/p&gt;
&lt;p&gt;Let me put up the &lt;code&gt;greetFunc&lt;/code&gt; module as well, to give more context on how a message is programmed.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {Payload} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../types/payloadTypes&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {Chat} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../types/chatTypes&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; logger &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;../util/logger&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; greetFunc&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;bot&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; any&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;hear&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;hello&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;sup&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; /hey( there)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;], (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;chat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Chat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        logger.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;module&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Api Controller&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: payload.message.text, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;details&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: chat});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        chat.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;say&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;Hello, human friend!&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            chat.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;say&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;Please say genre to get the list of genres to search from&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, { typing: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    bot.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;hear&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;/(good)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;bye/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; /see (ya&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;you)/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;adios&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;get lost&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;thankyou&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;], (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;_payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;chat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Chat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        chat.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;say&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;Bye, human!&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All we are doing is defining a set of array with possible values and as soon as a visitor sends any of these values, we respond back with the defined message.&lt;/p&gt;
&lt;p&gt;Assuming, that now its pretty staright forward code, lets come back to the &lt;code&gt;webhook verification&lt;/code&gt; part. First, lets follow the following steps -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;./ngrok http 3000&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note, we are starting our app in port 8080, but are forwarding port 3000 as &lt;code&gt;bootbot&lt;/code&gt; creates a subserver which by default utilizes port 3000.
Now, go back to Facebook Developer Webhook page, select &lt;code&gt;Create Subscription&lt;/code&gt;, enter the &lt;code&gt;ngrok&lt;/code&gt; or &lt;code&gt;localtunnel&lt;/code&gt; or &lt;code&gt;forward&lt;/code&gt; url, paste the &lt;code&gt;secret string defined above&lt;/code&gt; and hit verify. Once facebook is able to hit the endpoint, it will send a &lt;code&gt;challenge&lt;/code&gt; which will be verified against your app secret.&lt;/p&gt;
&lt;p&gt;Thats it, we are pretty much done. You can start the conversation with the bot.&lt;/p&gt;
&lt;p&gt;As part of extension, you can use some sort of Database to store the User and probably the received messages, run some analytics to improve the bot responses. This has a very different challenge, but I leave that to you to determine.&lt;/p&gt;
&lt;p&gt;Here is the source code again &lt;a href=&quot;https://github.com/prashantban/Facebook-Bot-App&quot; target=&quot;_blank&quot;&gt; {Github Link}&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Feel free to contact for any suggestion - dev[at]prashantb[dot]me&lt;/p&gt;
</content:encoded><category>Web Development</category><category>Building Products</category></item><item><title>Authenticating Express React App - Part2</title><link>https://prashantb.me/authenticating-express-react-app-part2/</link><guid isPermaLink="true">https://prashantb.me/authenticating-express-react-app-part2/</guid><description>As we discussed in Part 1 on how to do the client side part of the token based authentication in Express React app, this post will focus on server side of…</description><pubDate>Wed, 19 Apr 2017 19:27:52 GMT</pubDate><content:encoded>&lt;p&gt;As we discussed in &lt;a href=&quot;https://prashantb.me/authenticating-express-react-app-part-1/&quot;&gt;Part 1&lt;/a&gt; on how to do the client side part of the token based authentication in &lt;code&gt;Express&lt;/code&gt; &lt;code&gt;React&lt;/code&gt; app, this post will focus on server side of the authentication. So lets get started -&lt;/p&gt;
&lt;h2 id=&quot;exploring-the-idea&quot;&gt;Exploring the Idea&lt;/h2&gt;
&lt;p&gt;Server side has multiple responsibilities. Below are listed almost all of them -&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Generate a &lt;code&gt;HS256&lt;/code&gt; encrypted token with minimal required User details and a Private Key.&lt;/li&gt;
&lt;li&gt;Have a way to Call Login/Logout/Register routes.&lt;/li&gt;
&lt;li&gt;Use in memory DB ( &lt;code&gt;Redis&lt;/code&gt; in our case ) to persist the token, have a defined &lt;code&gt;TTL&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Call the DB to validate the token with every call that wants access to any private data.&lt;/li&gt;
&lt;li&gt;Invalidate token/user if token is invalid/expired and allow the client side to immediately delete all the stores and logout the user.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here are certain advantages of the above approach -&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Anything in memory is much faster than reading disk. With this, you can reach to a minimum latency.&lt;/li&gt;
&lt;li&gt;Keeping your authenticator separate from main server offers several security advantages.&lt;/li&gt;
&lt;li&gt;Most important, You now have Separation of concerns. Client side is independent and hence is faster than ever, server need not care about session or user any more and the in memory DB fetches result very quickly.&lt;/li&gt;
&lt;li&gt;There are leaps of other advantages, I recommend reading &lt;a href=&quot;http://jonatan.nilsson.is/stateless-tokens-with-jwt/&quot;&gt;Stateless Tokens&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lets get to do some coding then -&lt;/p&gt;
&lt;p&gt;Here is what my package file looks like&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;dependencies&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;body-parser&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^1.17.1&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;express&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^4.15.2&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;flux&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^3.1.2&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;jwt-decode&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^2.2.0&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;jwt-redis-session&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^1.0.5&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;morgan&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^1.8.1&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;react&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^15.5.0&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;react-dom&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^15.5.0&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;react-router&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^3.0.2&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;redis&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^2.7.1&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;request&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^2.81.0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next is our Routes file. This file handles the POST calls to Login and Logout. Its straight forward code, should be easy to understand.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;/** Login Route&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * Create session in redis&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; * &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;@return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;router.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;/user/login&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;req&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;res&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; options&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    url: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;API Server URL&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    body: req.body,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  request.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(options, (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;response&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;body&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;error &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; response.statusCode &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 200&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; response.statusCode &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 304&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;      // this will be attached to the JWT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;      var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; claims &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        user: body.user,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;      // create session &amp;amp; return the token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      req.jwtSession.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(claims, (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;token&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        res.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;          access_token: token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    // Error Occured&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      res.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To give more context, I am using &lt;code&gt;Redis&lt;/code&gt; to store the required user details and generate a token with the &lt;code&gt;Redis Key&lt;/code&gt; which is passed to the client side for persistence. Now, every Api call can be verified by decrypting the token, getting the ID and making a query to redis to check if its valid or not. If it is invalid, send a response in such a way so that the client can immediately log out the user.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/prashantban/Auth&quot;&gt;You can always grab the code from GITHUB&lt;/a&gt;. Share your comments or if you feel a better way, raise an issue in git or you want to collaborate, then raise a pull request.&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Authenticating Express React App - Part 1</title><link>https://prashantb.me/authenticating-express-react-app-part-1/</link><guid isPermaLink="true">https://prashantb.me/authenticating-express-react-app-part-1/</guid><description>React Apps of any nature will have certain sections that are visible to users who have registered in your app and have logged in. Consider an E-Commerce…</description><pubDate>Sun, 09 Apr 2017 19:03:20 GMT</pubDate><content:encoded>&lt;p&gt;&lt;code&gt;React Apps&lt;/code&gt; of any nature will have certain sections that are visible to users who have registered in your app and have logged in. Consider an E-Commerce app, where users can do everything except checkout without being logged in. So how to build such a system with as little pain to the user and as much security to your app as possible. Lets build an &lt;code&gt;Express&lt;/code&gt; app with &lt;code&gt;React &amp;amp; Flux&lt;/code&gt; driving the frontend. We will focus on &lt;a href=&quot;https://www.w3.org/2001/sw/Europe/events/foaf-galway/papers/fp/token_based_authentication/&quot;&gt;&lt;code&gt;Token Based Authentication&lt;/code&gt;&lt;/a&gt; in this article.&lt;/p&gt;
&lt;p&gt;Adding Authentication to any App is not something very complex, but here I’ll be putting up an approach that has worked out for me with very little learning. We will see how to put this up with React’s Flux pattern. The idea is as following -&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;User enters username and password. On success, receives a Token.&lt;/li&gt;
&lt;li&gt;Token is stored in Local Storage.&lt;/li&gt;
&lt;li&gt;State of React Parent Component is populated with necessary user details from Token.&lt;/li&gt;
&lt;li&gt;On hard refresh, the parent component is refreshed, Token is read and again state is populated with necessary re-directions.&lt;/li&gt;
&lt;li&gt;On logout, flush out the state and delete Token.&lt;/li&gt;
&lt;li&gt;Part II of this tutorial will contain the server level actions and how the token could be generated, validated etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;required-package&quot;&gt;Required Package&lt;/h3&gt;
&lt;p&gt;Here is how my &lt;code&gt;package.json&lt;/code&gt; looks like. The major packages are &lt;code&gt;React, React-Dom, Express, Flux, Jwt-Decode, React-Router&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;dependencies&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;express&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^4.14.1&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;flux&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^3.1.2&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;react&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^15.5.3&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;react-dom&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^15.5.3&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;react-router&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^3.0.2&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &quot;jwt-decode&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;^2.2.0&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;},&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;understanding-flux&quot;&gt;Understanding Flux&lt;/h3&gt;
&lt;p&gt;As facebook docs say “Flux is the application architecture that Facebook uses for building client-side web applications. It complements React’s composable view components by utilizing a unidirectional data flow. It’s more of a pattern rather than a formal framework, and you can start using Flux immediately without a lot of new code.”
Nothing more to add from my side on this. The figure at the top of this post shows the Flux flow in our app.&lt;/p&gt;
&lt;p&gt;Lets Get started with Login Container -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// LoginContainer.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Login &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;extends&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; React.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;Component&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  constructor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.state &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      user: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      password: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      state: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;  // Here we handle the Login Event&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  login&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;(event) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    event.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;preventDefault&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    Auth.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;login&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.state.user, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.state.password)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;catch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;err&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;setState&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;prevState&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;props&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;          return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {status: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Error&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;   }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  render&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;h1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;Login &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.state.status&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;h1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;form&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;          &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; htmlFor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;username&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;Username&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;          &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;={&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.state.user&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; placeholder&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;UserName&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; required&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;required&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;          &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; htmlFor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;password&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;Password&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;          &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;password&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;={&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.state.password&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;password&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; placeholder&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Password&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; required&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;required&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;button&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;submit&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; onClick&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;={&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.login.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;bind&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;Submit&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;button&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;form&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next is Our Auth Utility File&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// AuthService.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; AuthService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  login&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;email&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;password&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; options&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      url: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;http://localhost:9000/user/login&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      method: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;POST&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      body: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;stringify&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;email&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: email,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;password&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: password&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; Promise&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;resolve&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;reject&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;      request&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(options, (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;response&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;body&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(response.statusCode &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 200&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  response.statusCode &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 304&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;          body &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; JSON&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;parse&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(body);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;          if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(body.access_granted)  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;            resolve&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;loginUser&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(body.token));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;          else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; reject&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Email/Pass is Invalid&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; reject&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Email/Pass is Invalid&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; AuthService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Login Action&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; loginUser&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;token&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;pathname&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  Dispatcher.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;handleAction&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    type: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;LOGIN_USER&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    data: token,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(pathname) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    browserHistory.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(pathname);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    localStorage.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;setItem&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;token&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, token);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    browserHistory.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Couple of things to notice above. We are having a parameter pathname, this does not get passed when the user tries to login because everytime User Logs in, we want the user to go to homepage. Every other time, we will redirect user to the page where he came from.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Now lets write the Routes&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;We are using &lt;code&gt;react-router&lt;/code&gt; to handle the routes. Here is how my routes.js file look like -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Routes.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Routes&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; () &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;Router&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;Route&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; path&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; component&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;={&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;App&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;      {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;/* Login Route */&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;Route&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; path&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;login&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; component&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;={&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;Login&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;Route&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; component&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;={&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;EnsureLoggedInContainer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;Route&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; path&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;home&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; component&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;={&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;Home&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;Route&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;Route&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;Router&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Routes;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The job of the EnsureLoggedInContainer is to listen for navigation to a nested route and ensure that the user is logged in. If the user is logged in, the component does nothing and simply renders its children (the requested route). If the user is not logged in, EnsureLoggedInConatainer should record the current URL for the purposes of later redirection, and then direct users to the login page.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// EnsureLoggedInContainer.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; EnsureLoggedInContainer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; React&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;Component&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;   constructor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;props&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;      super&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(props)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;      this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.state &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getCurrentState&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    getCurrentState&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;      return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        userLoggedIn: UserStore.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;isLoggedIn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    componentDidMount&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;      if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.state.userLoggedIn) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        browserHistory.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;/user/login&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    render&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;      return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.props.children&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; EnsureLoggedInContainer;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The UserStore, like any other store, has 2 functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;It holds the data it gets from the actions. In our case, that data will be used by all components that need to display the user information.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;It inherits from EventEmmiter. It’ll emit a change event every time its data changes so that Components can be rendered again.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// UserStore.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; UserStoreClass&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; EventEmitter&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;  constructor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    super&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.token &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.dispatchToken &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; AppDispatcher.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;register&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.actionDispatcher.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;bind&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.user &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.isLoggedIn &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  addChangeListener&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;on&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;change&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, callback);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  removeChangeListener&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;removeListener&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;change&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, callback);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  getToken&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.token;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  getUser&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.user;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  isLoggedIn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.isLoggedIn;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  emitChange&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;emit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;change&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;  // Registering the Dispatcher&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;  actionDispatcher&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;payload&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    switch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (payload.action.type) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;      case&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos;LOGIN_USER&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; token&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; action.data;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.token &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; token;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.user &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; jwtDecode&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(token);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.isLoggedIn &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;emitChange&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        break&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; UserStoreClass&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, lets call any API. We will always have access to the user Data in UserStore.
Here is how you can attach the token to the API -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; token&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; UserStore.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getToken&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;fetch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(url, {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  headers: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;    &apos;token&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; : token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;})&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We have implemented Token Based Authentication in React App. In Part II, I will be going through how we can handle the server events, add &lt;code&gt;Redis&lt;/code&gt; to the flow, Generate token and persist it for any future validation. In the mean time &lt;a href=&quot;https://github.com/prashantban/Auth&quot;&gt;grab the code from GITHUB&lt;/a&gt;.&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>JavaScript Call Stack, Event Loop and Callbacks</title><link>https://prashantb.me/javascript-call-stack-event-loop-and-callbacks/</link><guid isPermaLink="true">https://prashantb.me/javascript-call-stack-event-loop-and-callbacks/</guid><description>Straight getting into what the title says, a call stack is simply a stack that the javascript run-time maintains to go through all the calls defined in the…</description><pubDate>Wed, 18 Jan 2017 19:27:50 GMT</pubDate><content:encoded>&lt;p&gt;Straight getting into what the title says, a &lt;code&gt;call stack&lt;/code&gt; is simply a stack that the javascript run-time maintains to go through all the calls defined in the code. The concept is exactly of a stack where the execution starts from the last function entering the stack and goes on till the first function. This is how all the synchronous languages work, but hey, &lt;code&gt;isn&apos;t JavaScript a non-blocking single threaded implementation?&lt;/code&gt; lets learn in detail about this.&lt;/p&gt;
&lt;h2 id=&quot;javascript-call-stack&quot;&gt;JavaScript Call Stack&lt;/h2&gt;
&lt;p&gt;V8, Chromes runtime engine comprises of a Stack and a Heap. Heap is used to allocate memory and stack to do the function calling. Apart from these, a browser consists of &lt;code&gt;Web Api&apos;s, Event Loop, and a Callback Queue.&lt;/code&gt; The figure at the top of this post puts them together; here is a small description -&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Web Api’s - Calls such as Ajax, SetTimeOut, Events etc. These are not part of the V8 engine, instead browser provides support for this.&lt;/li&gt;
&lt;li&gt;CallBack Queue - All the async callback functions or events such as Onclick, or Scroll etc are pushed to this queue. (Detail is provided below)&lt;/li&gt;
&lt;li&gt;Event Loop - This peace has one function to look in the stack and queue and if the stack is empty, push the contents from callback queue to the stack.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lets understand the Stack with the following code&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; square&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; multiply&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(a, b);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; multiply&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; product&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(a, b);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; product&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; b;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;square&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// 100&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is the stack trace ( &lt;code&gt;console.trace()&lt;/code&gt; )&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;product&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;multiply&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;square&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;anonymous&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pretty simple, isn’t it. Everything is synchronous. Now lets consider an even simpler code -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;first&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;setTimeout&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;second&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;third&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;trace&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is the output -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;first&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;third&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;trace&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;second&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What? Even though we are doing setTimeout of 0 seconds, still it was executed at the end.
So, here comes the async nature of Javascript. SetTimeOut doesn’t belong to the runtime environment and is part of Web Api provided by browser. The Stack gets everything in order, but setTimeOut goes to webapi, the timer runs for 0 seconds and instead of putting it back to the stack, it puts it to the &lt;code&gt;callback queue&lt;/code&gt; and the stack carries on executing the next call. As soon as the stack gets empty, &lt;code&gt;Event Loop&lt;/code&gt; comes into the picture and takes the first element in Callback Queue and passes it to the stack and hence it gets executed.&lt;/p&gt;
&lt;p&gt;Note - setTimeout does not guarantee execution in the given time, instead it guarantees the minimum time to execute.&lt;/p&gt;
&lt;p&gt;All the web Api’s work in the same way. Any ajax call made gets resolved by the browser by calling XHR and the callback is passed to the queue, but the program continues execution and as soon as stack is clear, the function is passed to the stack and the execution continues.&lt;/p&gt;
&lt;p&gt;Check out this cool &lt;a href=&quot;https://www.youtube.com/watch?v=QyUFheng6J0&quot;&gt;talk&lt;/a&gt; which explains about the back concepts of javascript.&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Know ES6 Better!</title><link>https://prashantb.me/know-es6-better/</link><guid isPermaLink="true">https://prashantb.me/know-es6-better/</guid><description>I have been working around with ES6 for long time now and I would like to share some interesting things that I found useful in my day to day work life. You…</description><pubDate>Sun, 17 Jul 2016 19:50:18 GMT</pubDate><content:encoded>&lt;p&gt;I have been working around with &lt;code&gt;ES6&lt;/code&gt; for long time now and I would like to share some interesting things that I found useful in my day to day work life. You will find lots of resources describing new features, but are you really willing to accept all the syntax! May be not. So, let me get straight to my take -&lt;/p&gt;
&lt;h2 id=&quot;1-arguments&quot;&gt;1. Arguments&lt;/h2&gt;
&lt;p&gt;Consider a simple case of writing a program to add all the numbers provided as argument. Here were your options prior to ES6 -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; getSum&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; sum &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; arguments&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;; i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        sum &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; arguments&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;[i];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; sum;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Functional JS Approach&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; getSum&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; [].reduce.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;arguments&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;){&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;b;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;ES6 introduces a concept which I remember was taught to me in C++ class and that is variadic arguments. Now we have arguments directly as array, Lets see the code -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; getSum&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; args.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;reduce&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; b);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;2-destructuring&quot;&gt;2. Destructuring&lt;/h2&gt;
&lt;p&gt;This thing has saved lots of time while coding. When you are dealing with objects or arrays with multiple properties or with multiple arguments or even if you are not sure about the arguments ordering, &lt;code&gt;Destructuring&lt;/code&gt; will help you. Lets see what you can do with it -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; obj &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {foo: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, foo2: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; { foo, foo2 } &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; obj;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(foo); &lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Logs 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// With Array&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; arr &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;tony&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;greg&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; [ fname, lname ] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; arr;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(fname);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So basically when you have large name objects and you want to destructure the properties, you can use this simple declaration. I feel in a way its pretty much similar to macros in c++ (highly debatable though). Following is a more deep example -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; calBMI&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;    weight&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;    height&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;    max&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 25&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;    callback&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; bmi &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; weight &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Math.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;pow&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(height, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (bmi &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; max) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;You are OverWeight&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    } &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(bmi);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (callback) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;        callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(bmi);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;callBMI&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    weight,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    height&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;callBMI&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    weight,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    height,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    max: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;30&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;callBMI&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    weight,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    height,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    max: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        Do Something&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this, you do not have to worry about argument ordering or missing arguments. Very very useful concept.&lt;/p&gt;
&lt;h2 id=&quot;3-default-function-parameters&quot;&gt;3. Default Function Parameters&lt;/h2&gt;
&lt;p&gt;This was probably much awaited concept. Previously we had to write if statements to check if the value was passed or not to give it a default value. Now we can do following -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; retFullName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;firstName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;lastName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos;Cozy&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    return firstName &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos; &apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; lastName;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;retFullName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;Tony&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Tony Cozy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;4-map-data-structure&quot;&gt;4. MAP (Data Structure)&lt;/h2&gt;
&lt;p&gt;JavaScript never had an explicit Map data structure but it had associative arrays that did the job very well. The Map object is a simple key/value collection. Any value (both objects and primitive values) may be used as either a key or a value. This data structure actually helps because it can take an &lt;code&gt;iterable&lt;/code&gt; as a parameter which allows to create your own behavior of iteration.&lt;/p&gt;
&lt;p&gt;Note - Internally this is different from conventional maps. Normal iteration will lead to getting elements in insertion order.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; myMap&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Map&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; obj&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;c&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;d&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;myMap.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;tony&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;myMap.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;marks&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;myMap.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(obj, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;some value&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; [key, value] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; myMap) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;  console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(key &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot; = &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; value);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Output&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; tony&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;marks &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;30&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;[object Object] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; some value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;5-set-data-structure&quot;&gt;5. SET (Data Structure)&lt;/h2&gt;
&lt;p&gt;Similar to map, sets are also available in es6. Recall, set data structure keeps only unique values and discards any duplicate values. Lets simply see an example -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;set.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;set.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;set.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Lets check the result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;set.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(a));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;set.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;set.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;set.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(a));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Important to note this&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;Object { &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;Object { &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we see above, sets basically can take any type and will store unique values. However, in case of objects, even though I added two objects with same properties, sets treated them as unique objects because that is the JavaScript treats objects, it is unique for every instance.&lt;/p&gt;
&lt;h2 id=&quot;6-custom-iterators&quot;&gt;6. Custom Iterators&lt;/h2&gt;
&lt;p&gt;Collections like map, set etc or even objects can have custom iterators as said above. Of-course you have the default iterators which simply iterates based on position or insertion order, there are cases when we want to iterate in defined order. You might want to know about Symbol.iterator, here is a link for detailed info - &lt;a href=&quot;https://prashantb.me/iterators-in-javascript-es6/&quot;&gt;link&lt;/a&gt;
Lets create a map, put random values and then define an iterator to display sorted values.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; mp&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Map&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;mp.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;mp.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;mp.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Custom Iterator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;mp[Symbol.iterator] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; _this &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; keys &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; index &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;        next&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (keys &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                keys &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; [];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;                for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; key &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; _this.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                    keys.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(key);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                keys.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; b);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                value: _this.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(keys[index]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                key: keys[index],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                done: index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; keys.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;length&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; it &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; mp[Symbol.iterator]();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; res &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; it.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;res.done) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(res.key &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot; = &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; res.value);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    res &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; it.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Output&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 12&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 20&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As always, please &lt;a href=&quot;mailto:prashantban@gmail.com&quot;&gt;Email&lt;/a&gt; to provide your comments.&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Design Patterns in JavaScript - Part 2</title><link>https://prashantb.me/design-patterns-in-javascript-part-2/</link><guid isPermaLink="true">https://prashantb.me/design-patterns-in-javascript-part-2/</guid><description>Hello folks! A month back I wrote a post explaining how basic fundamental design patterns could be written in JavaScript language. I received few emails of…</description><pubDate>Thu, 14 Jul 2016 17:45:49 GMT</pubDate><content:encoded>&lt;p&gt;Hello folks! A month back I wrote a post explaining how basic fundamental design patterns could be written in JavaScript language. I received few emails of appreciation as well as advice and on one such advice, I am sharing my experience of the current patterns which actually align with your day to day development in JavaScript (this time it is specific to JavaScript). Lets get started -&lt;/p&gt;
&lt;h2 id=&quot;1-revealing-module-pattern&quot;&gt;1. Revealing Module Pattern&lt;/h2&gt;
&lt;p&gt;Anyone who started learning programming would have encountered the term Object Oriented Programming and in it &lt;strong&gt;Encapsulation&lt;/strong&gt;. JavaScript, in particular does not enjoy the liberty of &lt;code&gt;visibility modifiers&lt;/code&gt; like other languages do, but we do want to implement Encapsulation. Now, this can be achieved through this &lt;code&gt;Revealing Module Pattern&lt;/code&gt;. Here is how the core syntax is like -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; myReveal &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    Properties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    Functions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; { Anything you want to be public, return in form of Object  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Usage -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;myReveal.ReturnedObjectProperty&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I will not suggest to not know about closures, but if you are not willing to use closures to hide methods or properties, this method is really very handy. Lets write a simple real world example using &lt;code&gt;WebSQL&lt;/code&gt; to get more clear understanding about this -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; userModule &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; db; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Private Global Variable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; createTable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;transaction&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        transaction.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;executeSql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;create table if not exists diary(id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, published DATE)&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; errorHandler&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;        log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(e);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        try&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            db &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; window.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;openDatabase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            db.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;transaction&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(createTable, errorHandler, callback);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        } &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;catch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (e) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;            errorhandler&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(e);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; storeUser&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;userDetails&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        db.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;transaction&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            t.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;executeSql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;insert into user(userName,published) values(?,?)&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, [userDetails.userName, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Date&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;()],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;                function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;                    callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                }, errorHandler);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }, errorHandler);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; getUser&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        db.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;transaction&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            t.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;executeSql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;select from user where id = ?&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, [id],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;                function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;results&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;                    callback&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(results);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                }, errorHandler);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }, errorHandler);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(msg);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    // Anything returned will become public.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        init: init,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        getUser: getUser,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        storeUser: storeUser&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Usage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;userModule.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(someFunction);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;userModule.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;storeUser&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    userName: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;Prashant&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}, someFunction);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;userModule.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getUser&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, someFunction)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/2d07a676a563691ae1aa1b24eddcb375&quot;&gt;RevealingModule.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;2-asynchronous-module-definition-amd&quot;&gt;2. Asynchronous Module Definition (AMD)&lt;/h2&gt;
&lt;p&gt;This pattern is slowly getting hold of all the internet now, because as the title says &lt;code&gt;Asynchronous&lt;/code&gt;. It actually is an Asynchronous pattern that takes the best out of all the world and yet loads stuff in parallel. This pattern ensures not only encapsulation, but also avoids Global Variables, Namespaces and it works very well within the browser. Lets quickly get into the basic syntax -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Import all modules in a single location&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;libs/otherModuleName&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;otherModuleName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;        // Export your module as an object&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;            myFunction&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                otherModule.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;otherExportedFunction&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is not it, its just a beginning. Anyways, lets talk about the actual usage part of this. I shall be using &lt;code&gt;requireJs&lt;/code&gt;, it is a popular script loader that supports AMD. Cool!, lets build something with this. We shall create a simple script that will load Jquery DatePicker in DOM.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;requirejs&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;jquery&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;bootstrap&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;jq-datepicker&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;        $&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;.datepicker&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;datepicker&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            format: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;dd/mm/yyyy&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            language: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;da&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            keyboardNavigation: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            autoclose: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// HTML Code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;datepicker&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Simple, isn’t it. Lets talk about advantages of this style -&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;I am always sure that the dependencies are loaded before the defined function.&lt;/li&gt;
&lt;li&gt;I can simply extend this function to do all stuff required.&lt;/li&gt;
&lt;li&gt;None of the variables are global, hence no issue of messing up your design in browser.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;AMD is definitely my favorite and long back I wrote a simple script that converts a flat json into a bootstrap table. Here is the &lt;a href=&quot;https://github.com/prashantban/JSON-to-Bootstrap-Table&quot;&gt;link&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That’s it folks for now!&lt;/p&gt;
&lt;p&gt;As always, please &lt;a href=&quot;mailto:prashantban@gmail.com&quot;&gt;Email&lt;/a&gt; to provide your comments.&lt;/p&gt;
</content:encoded><category>Web Development</category><category>Systems &amp; Architecture</category></item><item><title>Design Patterns in JavaScript</title><link>https://prashantb.me/design-patterns-in-javascript/</link><guid isPermaLink="true">https://prashantb.me/design-patterns-in-javascript/</guid><description>Lets look into constructing few of the common design patterns in JavaScript by using Object Oriented Code. We will be discussing one pattern from each of…</description><pubDate>Mon, 25 Apr 2016 22:36:01 GMT</pubDate><content:encoded>&lt;p&gt;Lets look into constructing few of the common design patterns in JavaScript by using Object Oriented Code. We will be discussing one pattern from each of the three common types - &lt;code&gt;Creational - Singleton&lt;/code&gt;, &lt;code&gt;Behavioral - Observer&lt;/code&gt; and &lt;code&gt;Structural - Decorator&lt;/code&gt; pattern. Before getting to code, lets see what is a Design Pattern.&lt;/p&gt;
&lt;h2 id=&quot;what-is-a-design-pattern-&quot;&gt;What is a Design Pattern ?&lt;/h2&gt;
&lt;p&gt;A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code but a description or template for how to solve a problem that can be used in many different situations. [Source : Wikipedia]&lt;/p&gt;
&lt;h2 id=&quot;singleton-pattern&quot;&gt;Singleton Pattern&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Singleton pattern&lt;/code&gt; is a design pattern that restricts the instantiation of a class to one object. We may need to do this to make sure there is only one resource on which any action is taking place. For example, a Logger class - here we will always want every thread/process to write things in a single log file and without any over-writing. Another example would be publishing scores in a game. So how to go about creating a class for this particular requirement. Here is my take -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Singleton&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    // Check if there exists the first object&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Singleton.instance &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;object&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Singleton.instance;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    // Logger function. This could do anything.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(message);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    // We are Sure that below code will only execute if&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    // this is the first time call.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    Singleton.instance &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;    // Simply return the current Instance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Test Cases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; single &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Singleton&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; single2 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Singleton&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; single3 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Singleton&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// If the above 3 Objects point to only one Object,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// We should get true if we check their equality.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(single &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; single2);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Logs true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(single2 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; single3);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Logs true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(single &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; single3);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Logs true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/8ba9adca5b2eaf8cfbc7f1cc1e3ae91d&quot;&gt;Singleton.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The code above looks clean and simple. All we are doing is making sure whichever way we create the object, It will always point to a single instance.&lt;/p&gt;
&lt;h2 id=&quot;observer-pattern&quot;&gt;Observer Pattern&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Observer pattern&lt;/code&gt; is a design pattern in which an object, called the Observer, maintains a list of its dependents, called Subscribers, and notifies them automatically of any state changes, usually by calling one of their methods. This pattern is very useful and is basically the creme logic behind an &lt;mark&gt;MVC Pattern&lt;/mark&gt; where the view represents Subscriber and Model represents Observable. There are tons of other applications and a simple google on it will enlist all. Lets dive into writing the code -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Main Observabale class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Observable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; [];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;Observable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    subscribe&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;Subscriber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;indexOf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(Subscriber) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;            this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(Subscriber);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        } &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; throw&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;Subscriber Already Present.&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;        log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Subscriber &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Subscriber.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot; Registered.&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    unsubscribe&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;UnSubscriber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;indexOf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(UnSubscriber) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;            throw&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;Subscriber &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; UnSubscriber.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot; Not Found.&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        } &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;            for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;; i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;                if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers[i] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; UnSubscriber) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;                    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;splice&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(i, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;                    log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Subscriber &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; UnSubscriber.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot; UnRegistered.&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;                    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    publish&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (message) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;            this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;item&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                item.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(message);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    showSubscribers&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.subscribers.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;item&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;            log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Subscriber - &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (index &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot; : Name is - &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; item.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Subscriber Class. One Could add more options to it and use Inheritance as well&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Subscriber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.name &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;Subscriber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    setName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.name &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    read&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (message) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;            log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.name &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot; recieved the message - &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; message);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Test Cases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Register an Observable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; observer &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Observable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Create Some Subscribers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; subs1 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Subscriber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Tony&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; subs2 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Subscriber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Anthony&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; subs3 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Subscriber&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Martial&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Subscribe The Created Subscribers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;observer.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;subscribe&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(subs1);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;observer.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;subscribe&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(subs2);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;observer.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;subscribe&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(subs3);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Publish a message to all&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;observer.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;publish&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Stock Price gets Cheap&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Show Subscribers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;observer.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;showSubscribers&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Unsubscribe&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;observer.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;unsubscribe&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(subs2);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Another Message Published&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;observer.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;publish&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Stock Price gets Higher&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(message);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/746a872f13beb781155d2c7af699a188&quot;&gt;Observer.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We have a class named &lt;mark&gt;Observable&lt;/mark&gt; which maintains a list of &lt;mark&gt;Subscribers&lt;/mark&gt;. In &lt;mark&gt;Observable Prototype&lt;/mark&gt;, we have simple functions to Subscribe, Unsubscribe, Publish and ShowSubscribers. I do not think more description is required to explain each as the code is pretty simple, shoot me an &lt;a href=&quot;mailto:prashantban@gmail.com&quot;&gt;Email&lt;/a&gt; if you want this to be explained. Here is the output of the above code -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Subscriber Tony Registered.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Subscriber Anthony Registered.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Subscriber Martial Registered.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Tony received the message - Stock Price gets Cheap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Anthony received the message - Stock Price gets Cheap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Martial received the message - Stock Price gets Cheap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Subscriber - 1 : Name is - Tony&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Subscriber - 2 : Name is - Anthony&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Subscriber - 3 : Name is - Martial&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Subscriber Anthony UnRegistered.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Tony received the message - Stock Price gets Higher&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Martial received the message - Stock Price gets Higher&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;decorator-pattern&quot;&gt;Decorator Pattern&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Decorator pattern&lt;/code&gt; is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. This pattern is part of &lt;mark&gt;Structural Design Patterns&lt;/mark&gt; and it is very easy to understand what actually is happening because we deal with extension of classes all the time. Specifically &lt;mark&gt;Decorator&lt;/mark&gt; creates a class that extends a particular feature and adds extra information to it. Examples of this pattern involve GUI Libraries, that require addition of components almost every time. Here is a simple example of Pizza -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Pizza&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;price&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.price &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; price &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.type &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; type &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;plain&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;Pizza&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    getPrice&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.price;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    getType&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.type;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    setPrice&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;price&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.price &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; price;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;    setType&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.type &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; type;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Decorator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; ExtraCheeseOption&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;pizza&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (pizza &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;instanceof&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; Pizza&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; price &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; pizza.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPrice&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        pizza.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;setPrice&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(price);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; price;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    } &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; throw&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &quot;Wrong Argument Passed&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Test Cases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; pizza &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Pizza&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Cheese&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Initial Price of Pizza - &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; pizza.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPrice&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;ExtraCheeseOption&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(pizza);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;After Adding Extra Cheese, New Price of Pizza - &quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; pizza.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPrice&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;());&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/8b3b76fdbaec1d827281e7cdddc9f416&quot;&gt;Decorator.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here, a decorator function is available that gives the option of adding cheese to pizza. Once added, the price changes dynamically. Here is the output -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Initial Price of Pizza - 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;After Adding Extra Cheese, New Price of Pizza - 11&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is it folks. Will be back with more interesting articles. In the mean time, please &lt;a href=&quot;mailto:prashantban@gmail.com&quot;&gt;EMAIL&lt;/a&gt;  to send your feedback.&lt;/p&gt;
</content:encoded><category>Web Development</category><category>Systems &amp; Architecture</category></item><item><title>Working with Java Spark Framework</title><link>https://prashantb.me/working-with-java-spark-framework/</link><guid isPermaLink="true">https://prashantb.me/working-with-java-spark-framework/</guid><description>Creating a Rest based full stack application requires quite a bit of hands on knowledge of Back end services as well as Front end services. In this post, we…</description><pubDate>Tue, 26 Jan 2016 19:40:12 GMT</pubDate><content:encoded>&lt;p&gt;Creating a Rest based full stack application requires quite a bit of hands on knowledge of Back end services as well as Front end services. In this post, we are looking on Java micro web framework named &lt;mark&gt;&lt;a href=&quot;http://sparkjava.com/&quot;&gt;Spark&lt;/a&gt;&lt;/mark&gt;. We will build a rest based system where we can do stuff with User Data.&lt;/p&gt;
&lt;h2 id=&quot;requirements-to-get-along&quot;&gt;Requirements to get along&lt;/h2&gt;
&lt;p&gt;&lt;mark&gt;Spark&lt;/mark&gt; is probably the easiest framework available to build a micro project. It removes the configuration hassles required while working with &lt;mark&gt;Spring or JSP&lt;/mark&gt; etc. Lets get to the business. We would use &lt;mark&gt;Java 8 and Maven&lt;/mark&gt; before starting with the application. To install Java 8 and Maven, you can follow the following steps -&lt;/p&gt;
&lt;h3 id=&quot;install-java-8&quot;&gt;Install Java 8&lt;/h3&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;On Ubuntu&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;$ sudo add-apt-repository ppa:webupd8team/java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;$ sudo apt-get update&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;$ sudo apt-get install oracle-java8-installer &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;$ sudo apt-get install oracle-java8-set-default&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;On Mac, One can directly download JDK 8 from Oracle Website &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Check Java Version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;$ java -version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;java version &quot;1.8.0_72&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;install-maven&quot;&gt;Install Maven&lt;/h3&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;$ sudo apt-get install maven&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It will take a while to install.&lt;/p&gt;
&lt;h2 id=&quot;creating-the-package&quot;&gt;Creating the Package&lt;/h2&gt;
&lt;p&gt;A Maven application requires creating certain configuration files. One such file is &lt;mark&gt;Pom.xml&lt;/mark&gt;. Whether you use &lt;mark&gt;NetBeans&lt;/mark&gt; or &lt;mark&gt;Eclipse&lt;/mark&gt; , Pom.xml is automatically created. We will require to edit this file in order to get started. &lt;mark&gt;Spark&lt;/mark&gt; is required to be included in our package, So add the following dependencies in pom.xml&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;dependencies&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;groupId&amp;gt;com.sparkjava&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;artifactId&amp;gt;spark-core&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;version&amp;gt;2.3&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;groupId&amp;gt;com.sparkjava&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;artifactId&amp;gt;spark-template-freemarker&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;version&amp;gt;2.3&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;dependency&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;groupId&amp;gt;com.fasterxml.jackson.core&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;         &amp;lt;artifactId&amp;gt;jackson-core&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;         &amp;lt;version&amp;gt;2.5.1&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;groupId&amp;gt;com.fasterxml.jackson.core&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          &amp;lt;artifactId&amp;gt;jackson-databind&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          &amp;lt;version&amp;gt;2.5.1&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;/dependencies&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;spark-core - Java Spark Framework&lt;/li&gt;
&lt;li&gt;spark-template-freemarker - Free Marker Template for Front End&lt;/li&gt;
&lt;li&gt;jackson-core - Basic JSON streaming API implementation&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;folder-structure&quot;&gt;Folder Structure&lt;/h2&gt;
&lt;p&gt;The folder structure must look like below (this is the layout of the &lt;a href=&quot;https://github.com/prashantban/Java-Spark-FTL&quot;&gt;app code&lt;/a&gt;) -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;pom.xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;src/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  main/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    java/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      Driver/MainClass.java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      Model/Model.java, UserTable.java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      TemplateEngine/FreeMarkerEngine.java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      User/User.java, Validable.java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    resources/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      TemplateEngine/main.ftl, home.ftl, createForm.ftl,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                     showUser.ftl, updateForm.ftl, removeUser.ftl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      public/css/, public/js/           Bootstrap, jQuery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  test/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    java/CreateUserTest.java, HtmlTest.java, UpdateUserTest.java&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;main-class&quot;&gt;Main class&lt;/h2&gt;
&lt;p&gt;As all the MVC frameworks, Spark also requires writing &lt;mark&gt;Routes&lt;/mark&gt; with get, put, post, delete methods. In our application, we want to first get import stuffs in our Driver Class.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import TemplateEngine.FreeMarkerEngine;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import java.io.IOException;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import static spark.Spark.*;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import spark.ModelAndView;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import com.fasterxml.jackson.core.JsonParseException;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import com.fasterxml.jackson.databind.ObjectMapper;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import com.fasterxml.jackson.databind.SerializationFeature;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import java.io.StringWriter;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import java.util.HashMap;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import java.util.Map;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;public class MainClass {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     *  Entry Point&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     * @param args&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    public static void main(String[] args) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        MainClass s = new MainClass();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        s.init();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we shall write the &lt;mark&gt;init()&lt;/mark&gt; function to define our first route. Here is the code -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;private void init() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        get(&quot;/&quot;, (request, response) -&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           Map&amp;lt;String, Object&amp;gt; viewObjects = new HashMap&amp;lt;String, Object&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           viewObjects.put(&quot;title&quot;, &quot;Welcome to Spark Project&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           viewObjects.put(&quot;templateName&quot;, &quot;home.ftl&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           return new ModelAndView(viewObjects, &quot;main.ftl&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        }, new FreeMarkerEngine());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;front-end&quot;&gt;Front End&lt;/h3&gt;
&lt;p&gt;As described, we now need to write the .ftl file which will contain our Html code. We shall use a Bootstrap starter template to get started.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;html&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;head&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;title&amp;gt;Spark Project&amp;lt;/title&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;link rel=&quot;stylesheet&quot; href=&quot;css/bootstrap.min.css&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;link rel=&quot;stylesheet&quot; href=&quot;css/bootstrap-theme.min.css&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;link rel=&quot;stylesheet&quot; href=&quot;css/starter-template.css&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;/head&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;body&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;div class=&quot;navbar navbar-inverse navbar-fixed-top&quot; role=&quot;navigation&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            &amp;lt;div class=&quot;container&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;div class=&quot;navbar-header&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;button type=&quot;button&quot; class=&quot;navbar-toggle&quot; data-toggle=&quot;collapse&quot; data-target=&quot;.navbar-collapse&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;span class=&quot;sr-only&quot;&amp;gt;Toggle navigation&amp;lt;/span&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;span class=&quot;icon-bar&quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;span class=&quot;icon-bar&quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;span class=&quot;icon-bar&quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;/button&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;div class=&quot;collapse navbar-collapse&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;ul class=&quot;nav navbar-nav&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;li class=&quot;active&quot;&amp;gt;&amp;lt;a href=&quot;https://prashantb.me/&quot;&amp;gt;Home&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;li&amp;gt;&amp;lt;a href=&quot;createUser&quot;&amp;gt;Create User&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;li&amp;gt;&amp;lt;a href=&quot;getAllUsers&quot;&amp;gt;Get All Users&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;li&amp;gt;&amp;lt;a href=&quot;updateUser&quot;&amp;gt;Update User&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        &amp;lt;li&amp;gt;&amp;lt;a href=&quot;removeUser&quot;&amp;gt;Remove User&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;/ul&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;/div&amp;gt;&amp;lt;!--/.nav-collapse --&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;script src=&quot;js/jquery.min.js&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;script src=&quot;js/bootstrap.min.js&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;div class=&quot;container&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            &amp;lt;#include &quot;${templateName}&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;/body&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now create your home file.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;div class=&quot;starter-template&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &amp;lt;h2&amp;gt;${title}&amp;lt;/h2&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;running-the-application&quot;&gt;Running the Application&lt;/h2&gt;
&lt;p&gt;In order to run the application, we need to add certain code to our &lt;mark&gt;POM.XML&lt;/mark&gt; file to let maven know what to do. Here is the code -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;name&amp;gt;SparkProject&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;build&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;plugins&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            &amp;lt;plugin&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;groupId&amp;gt;org.codehaus.mojo&amp;lt;/groupId&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;artifactId&amp;gt;exec-maven-plugin&amp;lt;/artifactId&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;version&amp;gt;1.2.1&amp;lt;/version&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;executions&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;execution&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;phase&amp;gt;test&amp;lt;/phase&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;goals&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;goal&amp;gt;java&amp;lt;/goal&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;/goals&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;configuration&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;mainClass&amp;gt;Driver.MainClass&amp;lt;/mainClass&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;/configuration&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    &amp;lt;/execution&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                &amp;lt;/executions&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            &amp;lt;/plugin&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &amp;lt;/plugins&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &amp;lt;/build&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can run -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mvn clean install&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;navigate to http://localhost:4567/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You should see the app running.&lt;/p&gt;
&lt;h2 id=&quot;rest-framework&quot;&gt;Rest Framework&lt;/h2&gt;
&lt;p&gt;We want to make our application restful, hence we need to deal with JSON for almost everything. I’ll demonstrate on how we can delete a user. Check the below code -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;get(&quot;/removeUser&quot;, (request, response) -&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    Map&amp;lt;String, Object&amp;gt; viewObjects = new HashMap&amp;lt;String, Object&amp;gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    viewObjects.put(&quot;templateName&quot;, &quot;removeUser.ftl&quot;); &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    viewObjects.put(&quot;users&quot;, toJSON(mod.sendUsersId()));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    response.status(200);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    return new ModelAndView(viewObjects, &quot;main.ftl&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        }, new FreeMarkerEngine());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;put(&quot;/removeUser/:id&quot;, (request, response) -&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     String id = request.params(&quot;:id&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     if(mod.removeUser(id)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;         response.status(200);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;         return &quot;User Removed&quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     else {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;         response.status(404);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;         return &quot;No Such User Found&quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, we are using same url but with two different purpose. When you hit &lt;mark&gt;/removeUser/&lt;/mark&gt; , it renders the removeuser template, but when you create a put request on &lt;mark&gt;/removeUser/SomeID&lt;/mark&gt;, that SomeID is checked in the database and if found, it is deleted else a simple message is sent back. See, how easily we can play with the response codes.
&lt;a href=&quot;https://github.com/prashantban/Java-Spark-FTL&quot;&gt;Click here to see the full code&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;test-cases&quot;&gt;Test Cases&lt;/h2&gt;
&lt;p&gt;Java Spark can easily be integrated with JUnit library. We shall write Unit test cases for our application. It’s simple because we do not need to mock anything related to Spark. We just need to mock our Model, which represents the way we access the database. However it has a simple interface and mocking it is straightforward. Let’s look at some examples.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import Model.Model;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import User.User;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import org.junit.Test;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import static org.junit.Assert.*;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import org.easymock.EasyMock;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import static org.easymock.EasyMock.*;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;public class CreateUserTest {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    public CreateUserTest() {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    @Test&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    public void aUserIsNotValid() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        User usr = new User();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setId(&quot;T12&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setFirstName(&quot;Test&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setAge(10);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setGender(&apos;X&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setLastName(&quot;Test&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setPhone(&quot;122&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        assertTrue(!usr.isValid());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    @Test&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    public void aUserIsCorrectlyCreated() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        User usr = new User();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setId(&quot;T12&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setFirstName(&quot;Test&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setAge(10);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setGender(&apos;M&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setLastName(&quot;Test&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        usr.setPhone(&quot;1234567891&quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        assertTrue(usr.isValid());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        Model model = EasyMock.createMock(Model.class);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        expect(model.createUser(&quot;T13&quot;, &quot;Test&quot;,&quot;&quot;,&quot;Test&quot;,20,&apos;M&apos;,&quot;1234567891&quot;,12 &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        )).andReturn(1);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, it is so easy to check each and every unit of our code.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;You can very well follow this &lt;a href=&quot;https://github.com/prashantban/Java-Spark-FTL&quot;&gt;GitHub link&lt;/a&gt; to all the code.
I hope this post gave a handful insight in writing a Full Stack rest based application with Java. Feel free to send comments and corrections as well as suggestions.&lt;/p&gt;
&lt;h2 id=&quot;important-links&quot;&gt;Important Links&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;mark&gt;&lt;a href=&quot;http://sparkjava.com/&quot;&gt;Spark&lt;/a&gt;&lt;/mark&gt;&lt;/li&gt;
&lt;li&gt;&lt;mark&gt;&lt;a href=&quot;https://github.com/prashantban/Java-Spark-FTL&quot;&gt;App Code&lt;/a&gt;&lt;/mark&gt;&lt;/li&gt;
&lt;/ul&gt;
</content:encoded><category>Web Development</category></item><item><title>Create and publish your first Node JS package</title><link>https://prashantb.me/creating-and-publish-your-first-node-js-package/</link><guid isPermaLink="true">https://prashantb.me/creating-and-publish-your-first-node-js-package/</guid><description>Creating a node.js package and further publishing it for other people to use it is pretty simple process. Through this post I shall walk through the process…</description><pubDate>Mon, 25 Jan 2016 06:44:41 GMT</pubDate><content:encoded>&lt;p&gt;Creating a node.js package and further publishing it for other people to use it is pretty simple process. Through this post I shall walk through the process by creating a small library and publishing it.&lt;/p&gt;
&lt;p&gt;Before diving into writing the code and creating the package, make sure you have node.js and npm installed. If not, you can follow the below process -&lt;/p&gt;
&lt;h2 id=&quot;install-nodejs-and-npm&quot;&gt;Install Node.js and npm&lt;/h2&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;Install Node, npm &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Ubuntu &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;sudo apt&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;get install nodejs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;sudo apt&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;get install npm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;sudo ln &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;s &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;usr&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;nodejs &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;usr&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;bin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;Mac users can use Homebrew&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;brew install node&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Assuming node and npm is installed, lets now get into the core business.&lt;/p&gt;
&lt;h2 id=&quot;configure-npm&quot;&gt;configure NPM&lt;/h2&gt;
&lt;p&gt;This configuration step is completely optional but I prefer to do so. This saves a lot of my time. The below code sets your personal detail and every time you create a new package, these details will be automatically set.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;npm set init.author.name &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Prashant Bansal&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;npm set init.author.email &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;prashantban@gmail.com&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;npm set init.author.url &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;http://prashantb.me&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Great, so now we have the personal details set.&lt;/p&gt;
&lt;h2 id=&quot;creating-a-node-module&quot;&gt;Creating a node module&lt;/h2&gt;
&lt;p&gt;A &lt;mark&gt;Node/npm&lt;/mark&gt; module is just an ordinary JavaScript file with the addition that it must follow the &lt;mark&gt;CommonJS&lt;/mark&gt; module spec. Luckily, this is really not as complex as it sounds. Node modules run in their own scope so that they do not conflict with other modules. Node relatedly provides access to some globals to help facilitate module interoperability. The primary 2 items that we are concerned with here are require and exports. You require other modules that you wish to use in your code and your module exports anything that should be exposed publicly. For example:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; require &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; require&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;some_module&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;exports&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(require.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;doSomething&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In our demo, we are going to build a Linkedlist Library.This will be a simple library which will be able to create a Linkedlist datastructure and also be able to define needful functions. If you are preparing for Interviews with Tech companies, you should go through this library and also be able to create your own functions to do certain tasks like reversing a linked list or finding an element or removing it etc. What we need is to first create an empty repository in github.
If you do not want to create , you can very well clone my repository.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git clone git@github.com:prashantban/ll-js.git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cd ll-js&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next up, we shall start our node module. Its easy, just type -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm init&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will initiate your node module and will ask certain questions. These will be part of your &lt;mark&gt;package.json&lt;/mark&gt; file. You can follow my &lt;mark&gt;package.json&lt;/mark&gt; file to know the key and value required.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;name&quot;: &quot;ll-js&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;version&quot;: &quot;0.1.0&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;description&quot;: &quot;JS Library for LinkedList&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;main&quot;: &quot;index.js&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;scripts&quot;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;test&quot;: &quot;./node_modules/.bin/mocha --reporter spec&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;repository&quot;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;type&quot;: &quot;git&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;url&quot;: &quot;https://github.com/prashantban/ll-js.git&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;keywords&quot;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;js&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;linkedlist&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;author&quot;: &quot;Prashant Bansal &amp;lt;prashantban@gmail.com&amp;gt; (http://prashantb.me/)&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;license&quot;: &quot;MIT&quot;,  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &quot;bugs&quot;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;url&quot;: &quot;https://github.com/prashantban/ll-js/issues&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;More or less, your &lt;mark&gt;package.json&lt;/mark&gt; will look similar. Now is the time to move into our main code file - &lt;mark&gt;index.js&lt;/mark&gt; . Here is the initial code we need -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;module.exports = (function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;var Node,LinkedList;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Node = function (item) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    this.item = item;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    this.next = null;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;LinkedList = function () {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    this.head = new Node(&apos;head&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    this.size = 0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;LinkedList.prototype.insert = function(data) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   if(this.find(data) === null) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    	var cur_node = this.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        while (cur_node.next !== null) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            cur_node = cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        }	&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var new_node = new Node(data);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        new_node.next = cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        cur_node.next = new_node;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        this.size += 1;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        return true;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    else {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    	return false;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;LinkedList.prototype.show = function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    var cur_node = this.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    if(!cur_node.next) return &apos;&apos;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    var out = [];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    while (cur_node.next !== null) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        out.push(JSON.stringify(cur_node.next.item));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        cur_node = cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    var res = out.join(&apos; --&amp;gt; &apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    return res;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;return {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    LinkedList : LinkedList&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;})();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/prashantban/ll-js/blob/master/index.js&quot;&gt;Full Library Code&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now is the time to write test cases. Node has a wonderful module named Mocha and Chai. As a beginner, it is probably the most easiest to write test cases in. Lets first install it in our library -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm install mocha --save-dev&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm install chai --save-dev&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similar to &lt;mark&gt;index.js&lt;/mark&gt; file, we also need a file where we could write our test cases. I have created a &lt;mark&gt;test&lt;/mark&gt; folder and inside it I have put another &lt;mark&gt;index.js&lt;/mark&gt; file where we will write all our test cases.&lt;/p&gt;
&lt;p&gt;without explaining the code, since I feel it is self explanatory, here it is -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;var should = require(&apos;chai&apos;).should(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    ds = require(&apos;../index&apos;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    LinkedList = ds.LinkedList;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    describe(&apos;LinkedList&apos; , function(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;		describe(&apos;init&apos; , function(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			it(&apos;should create a Linkedlist Instance&apos; , function(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				var obj = new LinkedList();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				obj.should.be.an(&apos;object&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				obj.size.should.equal(0);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				obj.head.item.should.equal(&apos;head&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				should.not.exist(obj.head.next);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;		});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;		describe(&apos;show&apos;, function(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			var emptylist, fullist;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			before(function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				emptylist = new LinkedList();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				fullist = new LinkedList();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				fullist.insert(1);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				fullist.insert(2);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				fullist.insert(3);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			it(&apos;should create an emptylist&apos;, function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				emptylist.show().should.equal(&apos;&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			it(&apos;should have only one element&apos;, function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				emptylist.insert(1);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				emptylist.show().should.equal(&apos;1&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			it(&apos;should show 3 elements&apos;, function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				fullist.show().should.equal(&apos;1 --&amp;gt; 2 --&amp;gt; 3&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;		});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;	});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/prashantban/ll-js/blob/master/test/index.js&quot;&gt;Full Test Code&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now, to run the test cases, we only need to let our package.json file know about it. to do so, include the following in your &lt;mark&gt;package.json&lt;/mark&gt; file -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&quot;scripts&quot;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;test&quot;: &quot;./node_modules/.bin/mocha --reporter spec&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  },&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and now simply run - &lt;code&gt;npm test&lt;/code&gt;
The results should be similar to as following -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt; ll-js@0.1.2 test /home/bansal/Desktop/Node-Sll/ll-js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt; mocha --reporter spec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  LinkedList&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    init&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should create a Linkedlist Instance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    show&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should create an emptylist&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should have only one element&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should show 3 elements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    remove&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should remove node with data as 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should remove first element&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should remove Last element&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    insertAtHead&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should insert this data at head&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should show the data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    insert&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should insert this data at head&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should show the data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should not allow insertion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    insertAtPosition&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should insert this data at head&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should show the data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should not allow insertion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    Union&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should union the list and change the size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should check for null of new list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    reverse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should reverse the list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      ✓ should return false if empty or only 1 element is present&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  19 passing (23ms)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Great, so now we have all our cases passed. We can be sure of our module now. All you need now is to make sure your git is up to date and has all updates. To do so, write the following lines -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git tag 0.1.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git add .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git commit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;git push origin master --tags&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;publish-to-npm&quot;&gt;Publish to NPM&lt;/h2&gt;
&lt;p&gt;If you are confident that your package has none or very less issues, you could publish your code to NPM for other people to use it. In order to do so, simply write -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm publish&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;once your package is up on npm, one can install it directly by typing -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm install ll-js&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use the version management system of npm to provide updates. Following is the gist of the process -&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Edit your code.&lt;/li&gt;
&lt;li&gt;Update the code in github.&lt;/li&gt;
&lt;li&gt;Update the version in &lt;mark&gt;package.json&lt;/mark&gt;&lt;/li&gt;
&lt;li&gt;publish using &lt;code&gt;npm publish&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lastly, go find your module on the &lt;a href=&quot;http://npmjs.org&quot;&gt;http://npmjs.org&lt;/a&gt; website and share it with friends. Here’s npm’s &lt;a href=&quot;https://www.npmjs.com/package/ll-js&quot;&gt;LL-JS&lt;/a&gt; page.&lt;/p&gt;
&lt;h2 id=&quot;related-links&quot;&gt;Related Links&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://npmjs.org&quot;&gt;NPM JS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/prashantban/ll-js&quot;&gt;LL-JS Github Page&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/ll-js&quot;&gt;LL-JS NPM Page&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://chaijs.com/guide/&quot;&gt;ChaiJS testing Framework&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content:encoded><category>Web Development</category></item><item><title>Understanding Linked List With JavaScript</title><link>https://prashantb.me/linked-list-with-javascript/</link><guid isPermaLink="true">https://prashantb.me/linked-list-with-javascript/</guid><description>As a kid, Learning LinkedList never made sense to me. Statements involving next,null,head etc were just either too difficult or were too mainstream to…</description><pubDate>Sat, 22 Aug 2015 23:55:57 GMT</pubDate><content:encoded>&lt;p&gt;As a kid, Learning LinkedList never made sense to me. Statements involving next,null,head etc were just either too difficult or were too mainstream to understand them at first. But growing up, I came to know about the importance, not because I use LinkedList while programming, but because it is such a basic Data Structure that sometimes becomes the deciding factor whether or not you can get a Developer role in any company.&lt;/p&gt;
&lt;p&gt;Coming back to the topic, In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence. (Source - Wikipedia)
&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Singly-linked-list.svg/408px-Singly-linked-list.svg.png&quot; alt=&quot;A singly linked list: nodes holding a value and a pointer to the next node&quot;&gt;&lt;/p&gt;
&lt;p&gt;I was always shown this diagram whenever LinkedList was discussed and rightly so, but again, it is a bit difficult to understand what is pointing to what and so on. With JavaScript, it is easy to understand the links as plain objects, which is what the figure at the top of this post shows.&lt;/p&gt;
&lt;p&gt;Now this is simple and makes sense to me. I have a Global Object, with first Item being “head” and whose next holds another object whose item is “hahah” and next is an object and this goes on till last element which points to null. Lets begin building it now, if it made sense to you.&lt;/p&gt;
&lt;p&gt;I presume, you have good knowledge of key concepts of Javascript like Objects, Hoisting, Prototyope etc.&lt;/p&gt;
&lt;p&gt;At first we need a Node function which can keep the data and the next link. Now we need a Global LinkedList function which can hold the node links and keep track of the size.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;var Node,LinkedList,obj;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Node = function (item) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     this.item = item;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     this.next = null;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;LinkedList = function () {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    this.head = new Node(&apos;head&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    this.size = 0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, this is it. We have a LinkedList class which has a head element in node and size being zero. Now all we need to do is define prototypes for inserting, removing, finding etc. Here is the code, which should make sense in one reading.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Node,LinkedList,obj,newobj;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;Node&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;item&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.item &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; item;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; () {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.head &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Node&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;head&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.size &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.item &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; data) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;position&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.item &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; data) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(cur_node&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;       	console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;Node Not Found&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;       	return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; i;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;   if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(data) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    	var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (cur_node.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }	&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; new_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Node&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(data);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        new_node.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        cur_node.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; new_node;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.size &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    	return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;show&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (cur_node.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(cur_node.next.item);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPrevious&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.next.item &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; data) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;remove&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; prev_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPrevious&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(data);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (prev_node.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        prev_node.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; prev_node.next.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.size &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;-=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    	return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;union&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;NewList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(NewList &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;instanceof&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt; LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; ===&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Wrong Parameter passed&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; (cur_node.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        cur_node &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; cur_node.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; NewListHead &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; NewList.head;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    cur_node.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; NewListHead.next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.size &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; NewList.size;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    NewListHead.next &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    NewList.size &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;obj &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;obj.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;x&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;obj.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;y&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;obj.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;z&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;newobj &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; LinkedList&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;newobj.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;Insert any object here&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;obj.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;union&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(newobj);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;obj.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;show&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(); &lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Logs all elements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;newobj.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;show&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(); &lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Logs null element&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/626ce1b7f55764a07874&quot;&gt;LinkedList.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Use this and don’t forget to use the union function. Its pretty cool to be able to merge two different LinkedList objects.&lt;/p&gt;
&lt;p&gt;Please email your comments or suggestions here - prashantban[at]gmail[dot]com&lt;/p&gt;
</content:encoded><category>CS Foundations</category></item><item><title>Arbitrary Dimensional Array</title><link>https://prashantb.me/arbitrary-dimensional-array/</link><guid isPermaLink="true">https://prashantb.me/arbitrary-dimensional-array/</guid><description>Have you ever wondered an object greater then 3 Dimensional. Ofcourse, we all know about 2D and 3D Objects but what about 4D or 5D or 6D or 10D. Wikipedia…</description><pubDate>Wed, 19 Aug 2015 02:57:35 GMT</pubDate><content:encoded>&lt;p&gt;Have you ever wondered an object greater then 3 Dimensional. Ofcourse, we all know about 2D and 3D Objects but what about 4D or 5D or 6D or 10D. Wikipedia says 4D is Space and Time and anything beyond is imaginary, but you simply cannot imagine 4D and above object.
&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/5/55/8-cell-simple.gif&quot; width=&quot;250px&quot; alt=&quot;A four-dimensional object, a tesseract, rotating&quot; /&gt;
&lt;em&gt;A 4D object, visualised (Wikimedia Commons)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So lets just build something which is hard to imagine. In computer memory, Everything can be saved in the memory as a flat structure. So if we can visualize pointers, we can visualize Arbitrary Dimensional Array as a flat structure in memory connected with pointers. Lets look at the example in Javascript to understand the concept -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;var a = [[[[]]]]; // Initialize a 4D Array&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;a[0][0][0][0] = 0; // Putting value in one index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;a[0][0][0][1] = 1;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;for(var x of a) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for(var y of x) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        for(var z of y) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            for(var q of z) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                console.log(q);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;// Output //&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As seen in the above example, we had to do 4 loops just to access each element. Also, if you wish to put elements automatically, you need to define those many loops. This is pretty tedious and requires concept like iterator to be able to access each element in some order.&lt;/p&gt;
&lt;p&gt;Most of the Javascript engines are written in C++, so lets just try to build a header file which can initialize Arbitrary Dimensional Array, efficiently save it in memory and also define iterator which can move either in Row-Major order and Column-Major order.&lt;/p&gt;
&lt;p&gt;== Variadic Templates -  ==
Variadic template is a template, which can take an arbitrary number of template arguments of any type. Both the classes &amp;amp; functions can be variadic. Here’s a variadic class template: (Definition from cplusplus.com)&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;template&amp;lt;typename... Arguments&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;class VariadicTemplate;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, basically what we will try to do is instantiate a variadic templated class and we will also write a base case for the same class. As I already said Artbitrary Dimensional Array in the memory can be analyzed as an array of an array and so on.
lets just create the prototype -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;template &amp;lt;typename T, size_t D, size_t... Dims&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;class Array {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;public :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    Array&amp;lt;T,Dims...&amp;gt;* arr;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    size_t Dims_Size, initial;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    static T ValueType;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    class FirstDimensionMajorIterator;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    class LastDimensionMajorIterator;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;// Base Case&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;template &amp;lt;typename T, size_t D&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;class Array&amp;lt;T,D&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    static T ValueType;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    T *arr;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    size_t initial;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    class FirstDimensionMajorIterator;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    class LastDimensionMajorIterator;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I think writing this code is probably the toughest part since its hard to visualize why do we need a class with same name but different template arguments.
From here on, all we need to do is define the following -&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Constructor&lt;/li&gt;
&lt;li&gt;Parameterized Constructor&lt;/li&gt;
&lt;li&gt;Move Constructor&lt;/li&gt;
&lt;li&gt;Assignment Operator&lt;/li&gt;
&lt;li&gt;[] Operator to access a particular element&lt;/li&gt;
&lt;li&gt;Increment and Decrement Operator for Iterators.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once the above concepts are written, the following test case should work fine.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;void initialtest(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ndarray::Array&amp;lt;int, 3, 3, 3&amp;gt; a, b;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int counter = 0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;for(int i=0; i&amp;lt;3; i++){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for(int j=0; j&amp;lt;3; j++){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        for(int k=0; k&amp;lt;3; k++){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            a[i][j][k] = counter;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            b[k][j][i] = counter;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            counter++;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;std::cout&amp;lt;&amp;lt;&quot;Total Elements Added - &quot; &amp;lt;&amp;lt; counter &amp;lt;&amp;lt;std::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;counter=0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;for(auto it = a.fmbegin(); it != a.fmend(); ++it){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    assert(*it == counter);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    counter++;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;std::cout&amp;lt;&amp;lt;&quot;Total Elements Iterated in First Dimension Order - &quot; &amp;lt;&amp;lt; counter &amp;lt;&amp;lt;std::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;counter=0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;for(auto it = b.lmbegin(); it != b.lmend(); ++it){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    assert(*it == counter);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    counter++;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;std::cout&amp;lt;&amp;lt;&quot;Total Elements Iterated in Last Dimension Order - &quot; &amp;lt;&amp;lt; counter&amp;lt;&amp;lt;std::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/prashantban/Arbitrary-Dimensional-Array&quot; target=&quot;_blank&quot;&gt;Click here&lt;/a&gt; to check out the complete source code. Make sure to compile with C++11.&lt;/p&gt;
&lt;p&gt;Feel free to post any comments by Emailing me here - prashantban[at]gmail[dot]com&lt;/p&gt;
</content:encoded><category>CS Foundations</category></item><item><title>Iterators in JavaScript ES6</title><link>https://prashantb.me/iterators-in-javascript-es6/</link><guid isPermaLink="true">https://prashantb.me/iterators-in-javascript-es6/</guid><description>ES6 has come up with a very cool and much needy concept of Iterators. All C++ or Java coders are completely familiar with what are iterators. But, Iterators…</description><pubDate>Mon, 10 Aug 2015 04:03:04 GMT</pubDate><content:encoded>&lt;p&gt;ES6 has come up with a very cool and much needy concept of Iterators. All C++ or Java coders are completely familiar with what are iterators.
But, Iterators in JavaScript is something new and needs a little bit of understanding.&lt;/p&gt;
&lt;p&gt;What are Iterators ? (Definition from Cplusplus.com)
An iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators.&lt;/p&gt;
&lt;p&gt;In JavaScript we do not need operators, since everything in JS is a variable, so all we need is a variable defined as an iterator that shall move.
Lets look into the basic for each loop available in JS&lt;/p&gt;
&lt;pre&gt;
var a = [1,2,3,4,5];
for(var i in a) {
    console.log(&quot;The Position of &quot;+a[i]+&quot; is &quot; + i );
}

//// Result ////
&quot;The Position of 1 is 0&quot;
&quot;The Position of 2 is 1&quot;
&quot;The Position of 3 is 2&quot;
&quot;The Position of 4 is 3&quot;
&quot;The Position of 5 is 4&quot;
&lt;/pre&gt;
&lt;p&gt;This loop is fairly simple and iterates over each element one by one and returns the index. Let us understand a new concept of For Of loop with the same example.&lt;/p&gt;
&lt;pre&gt;
var a = [1,2,3,4,5];
for(var i of a) {
    console.log(&quot;The value is &quot; + i );
}

//// Result ////
&quot;The value is 1&quot;
&quot;The value is 2&quot;
&quot;The value is 3&quot;
&quot;The value is 4&quot;
&quot;The value is 5&quot;
&lt;/pre&gt;
&lt;p&gt;This loop simply gets you the value. Before, I move on, let me clear the difference in these loops. For In loop does not necessarily force the order of the array where as for of loop can be defined to either follow the order or not. All you need to do is define the iterator.
Lets understand the above statements with following example -&lt;/p&gt;
&lt;pre&gt;
var a = {&apos;a&apos;:1,&apos;c&apos;:2,&apos;b&apos;:3}
for(let x in a)
{  console.log(a[x] + &quot;  &quot; + x);  }

//// Prints ////
&quot;1  a&quot;
&quot;2  c&quot;
&quot;3  b&quot;

//// See the order of the keys.
//// Same with For of loop -
for(let x of a)
{ console.log(x); }

// Returns the following error -
TypeError: a[Symbol.iterator] is not a function
&lt;/pre&gt;
&lt;p&gt;By applying the same concept, we received an error saying Symbol.Iterator is not defined. Now this Symbol.Iterator is nothing but a way to define the call of Next function to let the browser know what to do after moving to next element and also, moving to next element is required or not.
The Prototype of Symbol.Iterator is as following -&lt;/p&gt;
&lt;pre&gt;
a[Symbol.Iterator] = function() {
    return {
        next : function() {}
    }
}
&lt;/pre&gt;
&lt;p&gt;So now lets write a function to determine the behavior of our iterator to iterate in ordered manner -&lt;/p&gt;
&lt;pre&gt;
a[Symbol.iterator] = function () {
  var _this = this;
  var keys = null;
  var index = 0;
  
  return {
    next: function () {
      if (keys === null) {
        keys = Object.keys(_this).sort();
      }

      return {
        value: keys[index],
        done: index++ &gt;= keys.length
      };
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;If you are familiar with the Closures in Javascript, this function shouldn’t be difficult to understand. Anyways, at first we define some variables to be used. Then we create an index variable initialized with 0 to start the access of 1st element. Then we return the next function which checks if actually the next function was called or not. If yes, sort the keys and then simply return the value and a boolean done with true or false. When done returns true, the iterator knows to stop.
lets see the result now -&lt;/p&gt;
&lt;pre&gt;
for (var x of a)
{
 console.log(x + &quot;   &quot; + a[x]);
}

/// Result -
&quot;a   1&quot;
&quot;b   3&quot;
&quot;c   2&quot;
&lt;/pre&gt;
&lt;p&gt;There you go. we get the loop in the order we defined. That is the power of this new Iterator.
Moving on, You can define an infinite iterator, without worrying about the break condition. All you need to do is make the done part of next function always return False. Of course, you need to define some logic in your for loop to break out.&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Writing Your First Library in JavaScript</title><link>https://prashantb.me/writing-your-first-library-in-javascript/</link><guid isPermaLink="true">https://prashantb.me/writing-your-first-library-in-javascript/</guid><description>Writing a Library in Javascript can be a bit confusing (It was for me atleast when I started learning). It can be confusing because everything in JS is…</description><pubDate>Mon, 10 Aug 2015 04:01:08 GMT</pubDate><content:encoded>&lt;p&gt;Writing a Library in Javascript can be a bit confusing (It was for me atleast when I started learning). It can be confusing because everything in JS is function, so for every particular requirement you can write a function. Having said that, if you are writing a function which serves a particular need, why not make it generic, so that it can be reused.&lt;/p&gt;
&lt;p&gt;This post is all about creating a very basic library that simply generates an Excel sheet like layout in your browser with the given numbers of Rows and Columns. We are going to do this in pure JavaScript. We will not be using Jquery or any other JavaScript library, since the motive is to understand how to create a library and not to create an Excel sheet.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;http://demo.prashantb.me/Excel/&quot; target=&quot;_blank&quot;&gt;DEMO&lt;/a&gt;   &lt;a href=&quot;https://github.com/prashantban/Excel-Sheet-Using-Native-Javascript&quot; target=&quot;_blank&quot;&gt;DOWNLOAD&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Task&lt;/strong&gt; - Generate Excel Sheet in your Browser using Native JavaScript and HTML, CSS.
&lt;strong&gt;Approach&lt;/strong&gt; - We will stick to Object oriented Paradigm for this task and will try to stick to it as much as possible.
&lt;strong&gt;Insights&lt;/strong&gt; - A normal excel sheet will have following aspects -&lt;/p&gt;
&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Numbering of the Rows and Columns in exact behavior found in Excel.
&lt;li&gt;Rows and Columns with Text Input Feature
&lt;li&gt;Option of Adding or Deleting any Specified Row Or Column.
&lt;li&gt;Ability to Sort the Element.
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;p&gt;First thing first, Microsoft Excel has a very cool way of numbering Rows and Columns. Rows are generally numbered from 1 to some thousands and Columns are numbered from A till ZZZZZZZ… . Generating the row numbers is pretty easy, but generating the Column names is a bit tricky, since it involves Characters and the numbering is based on the 26 Alphabets.
After quite many iterations to my logic, I came up with this solution. (Comment if you can write a better logic)&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;function ColumnNames(n) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for(var i=0; i&amp;lt;n; i++) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var check = i;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var cap = [];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        while(check&amp;gt;=0) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            cap.unshift(String.fromCharCode(65 + (check%26)));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            check = check / 26 - 1;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;       }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    console.log(cap.join(&apos;&apos;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pretty simple, isn’t it. Now lets create a global variable which will be our main point of creating the table. This variable is much like class in c++, which will simply have some members(Row and Column Counts) and some functions.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;var table = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    rowcount        :0,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    columncount     :0,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    addNewRow : function () {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        this.rowcount++;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    addNewColumn : function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        this.columncount++;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    delNewRow : function () {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        this.rowcount--;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    delNewColumn : function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        this.columncount--;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    drawTable : function(a,b) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        if(typeof a===&apos;undefined&apos;) this.rowcount = 10;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        else this.rowcount=a;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        if(typeof b===&apos;undefined&apos;) this.columncount = 15;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        else this.columncount=b;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        draw();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    };  &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This ‘table’ variable is globally available to us since we did not wrap it inside any function. SO if we call table variable, this should say it is an object with property variables being rowcount and columncount. Also, we have functions to increment and decrement the values and a function named drawTable, which is simply to draw the initialized table. If we put up the following syntax in our HTML document, you can simply call the function and do all the necessary work.&lt;/p&gt;
&lt;pre&gt;
table.drawTable()
&lt;/pre&gt;
&lt;p&gt;This function has optional parameters a and b which is if you need a custom table. Now, all we need to do is write a function draw which simply draws the table on the defined HTML ID. Very simple basic Javascript code of getting the Element Id and creating the Table Headers and Input field.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;var draw = function() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    var tableHead = document.createElement(&apos;th&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    tableHead.setAttribute(&apos;id&apos;,&apos;hid-&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    document.getElementById(&apos;heading&apos;).appendChild(tableHead);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    document.getElementById(&apos;hid&apos;).appendChild(document.createTextNode(&quot;Nos&quot;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for (var i = 0; i &amp;lt; table.columncount; i++) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var tableHead = document.createElement(&apos;th&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        tableHead.setAttribute(&apos;id&apos;,&apos;hid-&apos;+i);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        tableHead.setAttribute(&apos;onclick&apos;,&apos;sortData(&apos;+i+&apos;)&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var check = i,cap = [];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        while(check&amp;gt;=0) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            cap.unshift(String.fromCharCode(65 + (check%26)));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            check = (check/26) - 1;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        textnode=document.createTextNode(cap.join(&apos;&apos;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        document.getElementById(&apos;heading&apos;).appendChild(tableHead);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        document.getElementById(&apos;hid-&apos;+i).appendChild(textnode);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for (var i= 0;i&amp;lt;table.rowcount;i++){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    addrowcode(i);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This function determines how many rows to be built and calls add code for each row. Now we need simple individual functions that can add row/column etc.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;var addrowcode = function(i) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    var newRows = document.createElement(&apos;tr&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    newRows.setAttribute(&apos;id&apos;,&apos;id-&apos;+i);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    document.getElementById(&apos;dynamictable&apos;).appendChild(newRows);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    var cell = document.createElement(&apos;td&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    cell.setAttribute(&apos;id&apos;,&apos;htdid-&apos; + i);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    document.getElementById(&apos;id-&apos;+i).appendChild(cell);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    document.getElementById(&apos;htdid-&apos;+i).appendChild(document.createTextNode(i));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for (var j = 0; j &amp;lt; table.columncount; j++) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var cell = document.createElement(&apos;td&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        cell.setAttribute(&apos;id&apos;,&apos;id-&apos;+i+&apos;-&apos;+j);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        document.getElementById(&apos;id-&apos;+i).appendChild(cell);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var inputCell = document.createElement(&apos;input&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        inputCell.setAttribute(&apos;type&apos;,&apos;text&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        inputCell.setAttribute(&apos;onclick&apos;,&apos;this.select()&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        inputCell.setAttribute(&apos;class&apos;,&apos;form-control&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        document.getElementById(&apos;id-&apos;+i+&apos;-&apos;+j).appendChild(inputCell);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;Similarly, one can write Adding of Columns function, Deleting a row/column etc. One important part of this application is to be able to sort the data entered in each column. I’ll show to sort the data in individual columns. One can follow the pattern and write how sorting happens in Datatables or any other Library.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;var sortData = function(j){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    var tsort = [],len;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for(var i =0;i&amp;lt;table.rowcount; i++) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var id = &apos;id-&apos; + i + &apos;-&apos; + j;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var data = document.getElementById(id).getElementsByTagName(&apos;input&apos;)[0].value;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        if(data != &apos;&apos;) tsort.push(data);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    len = tsort.length;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    if(len==0) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        alert(&apos;Empty Column&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        return;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    // Sort function called twice to erase the condition of&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    // either only sorting a number or character.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    tsort = tsort.sort();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    tsort = tsort.sort(function(a,b){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        return a - b;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    for(var i=0;i&amp;lt;table.rowcount;i++) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        var id = &apos;id-&apos; + i + &apos;-&apos; + j;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        if(i&amp;lt;len)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            document.getElementById(id).getElementsByTagName(&apos;input&apos;)[0].value = tsort[i];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;            document.getElementById(id).getElementsByTagName(&apos;input&apos;)[0].value = &apos;&apos;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And that is it. Again, you can Demo the Application and Download it as well. I will appreciate any feedback for this.
&lt;strong&gt;&lt;a href=&quot;http://demo.prashantb.me/Excel/&quot; target=&quot;_blank&quot;&gt;DEMO&lt;/a&gt;  &lt;a href=&quot;https://github.com/prashantban/Excel-Sheet-Using-Native-Javascript&quot; target=&quot;_blank&quot;&gt;DOWNLOAD&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Full Calendar in BackBone.Js</title><link>https://prashantb.me/full-calendar-in-backbone-js/</link><guid isPermaLink="true">https://prashantb.me/full-calendar-in-backbone-js/</guid><description>FullCalendar is a jQuery plugin that provides a full-sized, drag &amp; drop event calendar (Almost similar to Google Calendar) and we are gonna built in…</description><pubDate>Mon, 10 Aug 2015 03:41:21 GMT</pubDate><content:encoded>&lt;p&gt;FullCalendar is a jQuery plugin that provides a full-sized, drag &amp;amp; drop event calendar (Almost similar to Google Calendar) and we are gonna built in BackBone App.&lt;/p&gt;
&lt;p&gt;The concept is very simple, Jquery has a pretty cool plugin named &lt;a href=&quot;http://fullcalendar.io/&quot; target=&quot;_blank&quot;&gt;FullCalendar&lt;/a&gt;. The figure at the top of this post shows how the pieces fit together, and here is the result -&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;http://demo.prashantb.me/FullCalendar&quot; target=&quot;_blank&quot;&gt;DEMO&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To Achieve this, lets first analyze the data requirements. For this post, I am assuming you have slight Idea of how Client Side MVC Framework works.
I shall assume you have a JSON array with the following data -&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A string title&lt;/li&gt;
&lt;li&gt;A start date, encoded in ISO8601 format (for example, ‘2011-08-12T09:55:03Z’)&lt;/li&gt;
&lt;li&gt;An end date, encoded in ISO8601 format&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;
data = [
{&quot;start&quot;: &quot;2015-06-20 15:00:00&quot;, &quot;end&quot;: &quot;2015-06-20 16:00:00&quot;, &quot;title&quot;: &quot;Java&quot;},
{&quot;start&quot;: &quot;2015-06-20 15:00:00&quot;, &quot;end&quot;: &quot;2015-06-20 16:00:00&quot;, &quot;title&quot;: &quot;Java&quot;},
{&quot;start&quot;: &quot;2015-06-20 15:00:00&quot;, &quot;end&quot;: &quot;2015-06-20 16:00:00&quot;, &quot;title&quot;: &quot;Java&quot;}
];
&lt;/pre&gt;
&lt;p&gt;We shall use this JSON array to populate the Backbone Collection and then we will use it to populate our view.
Lets look at our HTML data. You can name it anything, I have named it as index.html&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;html&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;lt;!&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;DOCTYPE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; html&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;html&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;head&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;meta&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; charset&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;Backbone.js App | FullCalendar &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;        &amp;lt;!-- Full Calendar Stylesheet --&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;link&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; href&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.3.1/fullcalendar.min.css&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; rel&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;stylesheet&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;        &amp;lt;!-- The main CSS file --&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;link&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; href&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;assets/css/style.css&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; rel&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;stylesheet&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;head&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;body&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    	&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;main&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;h1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;My Calendar&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;h1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;calendar&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;footer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;h2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; href&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;http://prashantb.me/full-calendar-in-backbone-js/&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;Full Calendar with BackBone.Js&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;h2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;footer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;        &amp;lt;!-- JavaScript Includes --&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; src&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; src&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; src&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.1/backbone-min.js&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; src&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; src&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.3.1/fullcalendar.min.js&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;	&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; src&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;assets/js/script.js&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;body&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#116329;--shiki-dark:#7EE787&quot;&gt;html&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/86c12d8b3b5cd0232372&quot;&gt;calendar.html on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Very simple HTML. Nothing to explain on it. The Calendar division is where we will populate the calendar using backbone.js.
Please note - I am using Underscore.js for some functions which I may not use here, but its a good practice to have it, since you may require it.
Also, FullCalendar has a dependency requirement of moment.min.js. Be sure to include that as well.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;The JavaScript&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Here is the overall idea of our Backbone code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First we will create a service model. It will have properties for the start/end date and time and the title of the event. An object of this class will be created for every service that we offer.&lt;/li&gt;
&lt;li&gt;Then we will create a Backbone collection that will store all the services. This collection will merely work on the type of model and similar to the JSON data provided. Please note - JSON data needs to be consistent with the model.&lt;/li&gt;
&lt;li&gt;After this, we define a view for the services. This view is the main view which is responsible for the data transfer to HTML. Primarily, it iterates through the JSON data and renders the Calendar Script essentials.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is the source code. Pardon me for no Comments. I am pretty lazy at it. Comment if anything is inappropriate or not understandable. I’ll respond quickly.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    data &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; [{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;start&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;2015-06-20 15:00:00&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;end&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;2015-06-20 16:00:00&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;title&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Java&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }, {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;start&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;2015-06-20 15:00:00&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;end&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;2015-06-20 16:00:00&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;title&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Java&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }, {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;start&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;2015-06-20 15:00:00&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;end&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;2015-06-20 16:00:00&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;        &quot;title&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;Java&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    Service &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Backbone.Model.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;extend&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        defaults: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            title: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            start: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            end: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    ServiceCollection &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Backbone.Collection.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;extend&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        model: Service,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    App &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; Backbone.View.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;extend&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;        initialize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;            this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.model &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Service&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;        render&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;            $&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;#calendar&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;fullCalendar&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                header: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                    left: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;prev,next today&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                    center: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;title&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                    right: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;month,basicWeek,basicDay&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                    ignoreTimezone: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                selectable: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                selectHelper: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                editable: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;                ignoreTimezone: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;            });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;            $&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&apos;#calendar&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;fullCalendar&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt;&quot;addEventSource&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.collection.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;toJSON&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; myCollection &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; ServiceCollection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(data);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; App&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;        collection: myCollection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;render&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/d7c2e8510673d2cd6446&quot;&gt;fullcalendar.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That’s it folks. Please leave your comment and suggestions. I will be writing more such stuff.&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Introduction to RxJS</title><link>https://prashantb.me/introduction-to-rxjs/</link><guid isPermaLink="true">https://prashantb.me/introduction-to-rxjs/</guid><description>First thing first - What is RXJS ? &quot;The Reactive Extensions for JavaScript (RxJS) is a set of libraries for composing asynchronous and event-based programs…</description><pubDate>Mon, 10 Aug 2015 03:34:08 GMT</pubDate><content:encoded>&lt;p&gt;First thing first - What is RXJS ?&lt;/p&gt;
&lt;blockquote&gt;&quot;The Reactive Extensions for JavaScript (RxJS) is a set of libraries for composing asynchronous and event-based programs using observable sequences and fluent query operators that many of you already know by Array#extras in JavaScript. Using RxJS, developers represent asynchronous data streams with Observables, query asynchronous data streams using our many operators, and parametrize the concurrency in the asynchronous data streams using Schedulers. Simply put, RxJS = Observables + Operators + Schedulers.&quot;&lt;/blockquote&gt;
&lt;p&gt;So, lets understand this with a peace of code.
We have an array named source and on it we apply higher order functions like Filter (to get the elements which are odd), Map (to edit the result) and finally ForEach loop (where we will iterate through each element and print it to the result.).&lt;/p&gt;
&lt;p&gt;So far so good, here is the output -&lt;/p&gt;
&lt;iframe title=&quot;jsfiddle: the array with filter, map and forEach&quot; width=&quot;100%&quot; height=&quot;200&quot; src=&quot;https://jsfiddle.net/pbansal1/2epy482a/embedded/&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;
&lt;p&gt;Now, lets add the Observable feature of RxJS. This Observable feature is defined to make things asynchronous. So lets try it.&lt;/p&gt;
&lt;iframe title=&quot;jsfiddle: the same steps over an Observable&quot; width=&quot;100%&quot; height=&quot;200&quot; src=&quot;https://jsfiddle.net/pbansal1/mmkyrux4/embedded/&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;
&lt;p&gt;We applied the same higher order function, but observed the same output. Wondering why .? Lets see another code -&lt;/p&gt;
&lt;iframe title=&quot;jsfiddle: interval(500) with take(6)&quot; width=&quot;100%&quot; height=&quot;200&quot; src=&quot;https://jsfiddle.net/pbansal1/eghg78rm/embedded/&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;
&lt;p&gt;Did we just observe the asynchronous behavior. Lets understand the code. We have an interval set of 500 over a take of 6 (0,1,2,3,4,5)
This interval when defined using observable, makes sure that the instance runs asynchronously within the defined interval.&lt;/p&gt;
&lt;p&gt;Example taken from - &lt;a href=&quot;https://egghead.io/technologies/rx&quot; target=&quot;_blank&quot;&gt;Egg Head&lt;/a&gt;
Read more about RJS - &lt;a href=&quot;https://xgrommx.github.io/rx-book/&quot; target=&quot;_blank&quot;&gt;Click Here&lt;/a&gt;&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Passing Objects as Argument to Class - JavaScript</title><link>https://prashantb.me/passing-objects-as-argument-to-class-javascript/</link><guid isPermaLink="true">https://prashantb.me/passing-objects-as-argument-to-class-javascript/</guid><description>Few days back, I struggled with a very small concept in JavaScript that required to pass an object of the same class as an argument to it. In doing so, I…</description><pubDate>Mon, 10 Aug 2015 01:57:45 GMT</pubDate><content:encoded>&lt;p&gt;Few days back, I struggled with a very small concept in JavaScript that required to pass an object of the same class as an argument to it. In doing so, I faced a very genuine issue on how one can pass the object as argument where as the argument is expecting a variable to initiate the respective object. I searched heavily on the Internet and couldn’t find a proper solution. So I thought of writing my own method.&lt;/p&gt;
&lt;p&gt;The task is pretty simple, need to create a class in Javascript which when instantiated can take in variables as well as objects of the same class.
To simplify, let me give you an example.&lt;/p&gt;
&lt;p&gt;Suppose I have a class name defined as Point which simply initializes X and Y axis coordinates. Therefore, the call that I was looking to satisfy was :-&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; p &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Point&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Very Simple. Initializing p with 10 as x and 13 as y.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; q &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Point&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Point&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Point&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)); &lt;/span&gt;&lt;span style=&quot;--shiki-light:#636B74;--shiki-dark:#8B949E&quot;&gt;// Exactly what I am referring to. &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;p.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPoints&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;q.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPoints&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/b9f1412f48b2396c9da5&quot;&gt;point.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The function getPoints simply prints out the value of the x and y.
Lets start building the class.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Point&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.x &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; x;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.y &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; y;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;Point&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPoints&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;Console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.x);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;Console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.y);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/139316364241546e5d8e&quot;&gt;point.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There we are. We have the class built and it simply initializes the object via this.
Now we need to implement the feature that says, if the passed arguments are of type Object, add their x and y points and pass it to the class variables. So getPoints can print those points. Now this is my approach and this may not be the best, but it worked for me.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt; Point&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#953800;--shiki-dark:#FFA657&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.x &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.y &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(x) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos;object&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; typeof&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(y)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos;object&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      {  &lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.x &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; x;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;         this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.y &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; y;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;      } &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(x) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos;object&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;            this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.x &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; x.x;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;            this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.y &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; x.y;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(y) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0A3069;--shiki-dark:#A5D6FF&quot;&gt; &apos;object&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;            this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.x &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; y.x;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;            this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.y &lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; y.y;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;Point&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;prototype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;getPoints&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#CF222E;--shiki-dark:#FF7B72&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.x);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;    console.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C4CD4;--shiki-dark:#D2A8FF&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0550AE;--shiki-dark:#79C0FF&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;.y);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1F2328;--shiki-dark:#E6EDF3&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p class=&quot;code-caption&quot;&gt;&lt;a href=&quot;https://gist.github.com/pbse/c1f61cbd2e20939a9f28&quot;&gt;point.js on GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I simply checked if the type of the arguments passed is object type or not. If it is of type object, handle differently and pass on the value.
So if I put the following in the console -&lt;/p&gt;
&lt;pre&gt;
var p = new Point(new Point(10,13), new Point(12,14));
p.getPoints();
var q = new Point(12,23);
q.getPoints();
&lt;/pre&gt;
&lt;p&gt;The Output is -&lt;/p&gt;
&lt;pre&gt;
22
27
12
23
&lt;/pre&gt;
&lt;p&gt;Any suggestions are most welcome. Again. this may not be the best approach but It worked for me. Do comment.&lt;/p&gt;
</content:encoded><category>Web Development</category></item><item><title>Postfix Calculator Using C &amp; Flex</title><link>https://prashantb.me/postfix-calculator-using-c/</link><guid isPermaLink="true">https://prashantb.me/postfix-calculator-using-c/</guid><description>Flex is an aging UNIX utility that helps you write very fast parsers for almost arbitrary file formats. Formally, they implement Look-Ahead-Left-Right (as…</description><pubDate>Mon, 10 Aug 2015 01:44:06 GMT</pubDate><content:encoded>&lt;p&gt;Flex is an aging UNIX utility that helps you write very fast parsers for almost arbitrary file formats. Formally, they implement Look-Ahead-Left-Right (as opposed to “recursive descent”) parsing of non-ambiguous context-free (as opposed to “natural language”) grammars.&lt;/p&gt;
&lt;p&gt;Why should you learn the Flex pattern syntax when you could just write your own parser? Well, several reasons. First, Flex will generate a parser that is virtually guaranteed to be faster than anything you could write manually in a reasonable amount of time. Second, updating and fixing Flex source files is a lot easier than updating and fixing custom parser code. Third, Flex have mechanisms for error handling and recovery, which is something you definitely don’t want to try to bolt onto a custom parser. Finally, Flex has been around for a long time, so it is far away from bugs than any newer code.&lt;/p&gt;
&lt;p&gt;So, Lets get started with writing our first code in Flex using C Language. We aim to develop a Postfix Calculator. Postfix calculator means that there will be two Operands before an operator and when the parser gets two operands and an operator, it shall evaluate it.&lt;/p&gt;
&lt;p&gt;Flex file has an extension of .l (Lex). Now, a typical Flex file has its own Structure and It Looks something like this -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;%{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  C headers, C code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;%}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Regular definitions e.g.:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;digit   [0-9]   //integers from 0 through 9&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;%%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Token Specifications e.g.:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{digit}+    // Use the previously specified regular   // definition digit.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;%%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;main function&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To compile a Flex file and get the results, run &lt;code&gt;flex&lt;/code&gt; on the &lt;code&gt;.l&lt;/code&gt; file, which writes &lt;code&gt;lex.yy.c&lt;/code&gt;; compile that with a C compiler; then run the result with the expression on standard input -&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;flex postfix.l          # writes lex.yy.c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cc lex.yy.c -o postfix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;./postfix &amp;lt; input.txt&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now lets write code for the Postfix Calculator. I’ll be explaining things in individual sections.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;%{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;#include &amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;#define stack_size 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;static int sp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;static float stack [stack_size];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;static void push (float i) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    if (++sp&amp;lt;stack_size)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        stack[sp]= i;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        {printf (&quot;error: Stack Overflow\n&quot;); exit(1);}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;static float pop (void) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    if (sp&amp;gt;=0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        return stack[sp--];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    else {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        printf (&quot;error: Stack Underflow\n&quot;); exit(1);}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int int_value;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;%}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a simple C Code which is self explanatory. We have a Stack of size 100. This is simply to store the inputs. User can input anything. We will take care of what he inputs and understand the nature through the following code.&lt;/p&gt;
&lt;pre&gt;digit [0-9]
integer {digit}+
real ({digit}+[.]{digit}*)|({digit}*[.]{digit}+)
exp ({integer}|{real})[eE]{integer}
exp2 ({integer}|{real})[eE][+-]{integer}
exp3 [+-]({integer}|{real})[eE][+-]{integer}
integer2 [+-]({integer}|{real})
&lt;/pre&gt;
&lt;p&gt;Essentially, here we simply have defined what are the types of input we want. Digit corresponds to Numbers, then we have real number, exponents etc. Basically, this is just the combination of everything what we can have in a simple postfix calculator. The code that follows to this now is the main LEX code that will parse the input to respective functions and will understand the inputs using the REGEX defined above.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes site-light site-dark&quot; style=&quot;--shiki-light:#1f2328;--shiki-dark:#e6edf3;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0d1117; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;%%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;({integer}|{integer2}|{real}|{exp}|{exp2}|{exp3})       {push (atof(yytext));} // yytext contains all the input and we are pushing the float value.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&quot;+&quot;                      {push (pop() + pop());}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&quot;*&quot;                      {push (pop() * pop());}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&quot;-&quot;                      {float rhs= pop(); push (pop() - rhs);}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&quot;/&quot;                      {float rhs= pop(); push (pop() / rhs);}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;&amp;lt;EOF&amp;gt;&amp;gt;                  {float answer = pop();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;//Small code to simply manipulate the output. Basically handling the float and integer values&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        char str1[20];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        snprintf(str1, 20, &quot;%f&quot;, answer);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; // To know if the answer has trailing zeroes.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        if(strstr(str1,&quot;.0000&quot;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                            memset(str1, 0, strlen(str1));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                            int_value = answer;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                            printf(&quot;%d \n&quot;, int_value);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; // If this statement comes true, then we know the answer should be int. Hence Print Integer.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                         else printf (&quot;%.3f \n&quot;, answer); // Else Print the value with 3 decimal places.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[ \t\n]                   ;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;// This simply bypasses all the tabs and enter key press.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[^-0-9+*/.;eE \t\n]+     {ECHO; fprintf (stderr,&quot;Invalid Character - &quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;printf(&quot;\n&quot;); exit(1);}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;// For every non required character, do not push it to the stack.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;%%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int main (void) {sp= -1; yylex(); return 0;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int yywrap (void) {return 1;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is it. go to your prompt, compile the code and run this. Please do let me know if you feel this code can be made more sophisticated or share if you have a different approach to achieve the same. Good Luck.!!&lt;/p&gt;
</content:encoded><category>CS Foundations</category></item></channel></rss>