<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/rss-styles.xsl" type="text/xsl"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Kamau Washington — Posts</title><description>Notes on enterprise architecture, distributed systems, AWS and the algorithms behind them — from Kamau Washington, Solution Architect, Enterprise Architect and Principal Advisor.</description><link>https://www.kamauwashington.com/</link><language>en</language><lastBuildDate>Sun, 06 Sep 2026 00:00:00 GMT</lastBuildDate><pubDate>Sun, 06 Sep 2026 00:00:00 GMT</pubDate><copyright>© 2026 Kamau Washington</copyright><docs>https://www.rssboard.org/rss-specification</docs><generator>Astro</generator><atom:link href="https://www.kamauwashington.com/rss.xml" rel="self" type="application/rss+xml"/><item><title>Off By Cents</title><link>https://www.kamauwashington.com/2026/09/06/off-by-cents/</link><guid isPermaLink="true">https://www.kamauwashington.com/2026/09/06/off-by-cents/</guid><description>I want to introduce BOE, my new rule engine platform. But the announcement is the easy part. The more honest post is the one that explains why someone who walked away from rule engines over a decade ago spent last year building thirty five of them…</description><pubDate>Sun, 06 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I want to introduce BOE, my new rule engine platform at &lt;a href=&quot;https://boerules.com&quot;&gt;boerules.com&lt;/a&gt;. But the announcement is the easy part. The more honest post is the one that explains why someone who &lt;strong&gt;walked away from rule engines over a decade ago spent last year building thirty five of them.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So here is the long version. It starts fifteen years ago, goes quiet for most of the time in between, and then turns on a single bug I found last year.&lt;/p&gt;
&lt;h2 id=&quot;meshalpha&quot;&gt;MeshAlpha&lt;/h2&gt;
&lt;p&gt;Almost fifteen years ago I wrote MeshAlpha, a Forward Chaining RETE-OO engine in
C#, tested and debugged by hand, before any of us had an AI assistant looking over our
shoulders. Mesh for the discrimination network and a nod to the Von Neumann
machine architecture I was studying at the time, Alpha for the first node type
every RETE tutorial hands you. I read Forgy’s original papers and Robert
Doorenbos’s 1995 CMU thesis on RETE/UL, kept a running study of Drools and the
lesser known engines beside it. All of it traced back to FICO, where I started my career working
with rule engines in my twenties. The
part that ends most rule engine side projects, mine included, is the business
rule management system wrapped around it: an authoring UI, version control for
rules business users can actually touch, testing, simulation and deployment, all
built for people who are not engineers. ILOG, Pega, Blaze Advisor, Sparkling
Logic and above all Drools already had that, and I could not compete with any of
it as one person on a side project. So I put MeshAlpha down and stayed on my
architecture track. That is where things sat for more than a decade.&lt;/p&gt;
&lt;h2 id=&quot;last-year-and-then-off-by-cents&quot;&gt;Last year, and then Off By Cents&lt;/h2&gt;
&lt;p&gt;That changed last year, in 2025, when AI got genuinely good at writing code and
the gap between having an idea and having a working implementation of it stopped
being the obstacle. I was building an integrations scaffold on an AWS native
platform, and part of it was a cost skill: tell me what a given integration
costs to run at different volumes. Ahead of a demo it ran clean five times in a
row, memory cleared between each run, numbers exact every time. On the sixth run
the calculation came back Off By Cents, and took longer than it should have to
get there. &lt;strong&gt;Nobody else in the room noticed.&lt;/strong&gt; An answer that drifts by cents while
deciding what a system costs to run is a real problem, and I had spent enough of my career on rule engines to know exactly where that
kind of drift comes from. It sent me straight back to FICO, back to Forgy, back
to everything I had read and built and set aside years earlier. So I built the
Pricing Engine.&lt;/p&gt;
&lt;h2 id=&quot;why-i-built-the-pricing-engine&quot;&gt;Why I built the Pricing Engine&lt;/h2&gt;
&lt;p&gt;I did not build the Pricing Engine for the company I was working for. I built it for myself, to prove something I had believed for a long time and never had a clean chance to prove: less is more, simple is fast, and fast can still be reliable and deterministic.&lt;/p&gt;
&lt;p&gt;Once the Pricing Engine worked the way I wanted, I kept going. Forward Chaining next, then Backward Chaining, Bayesian, Sequential, Constraint Satisfaction, Utility and Multi Criteria Decision Making. Twenty engines by January 1st, 2026. Today there are more than thirty five, and the list keeps growing.&lt;/p&gt;
&lt;h2 id=&quot;the-gap-i-actually-found&quot;&gt;The gap I actually found&lt;/h2&gt;
&lt;p&gt;Somewhere in that stretch of building I noticed the actual shape of the market, not the shape I had assumed it had back when I put MeshAlpha down. On one end sit the large commercial players. On the other sits a long tail of open source rule engines, each with its own conventions, little to no support, and rarely anything you could call a real BRMS. Most of them hand you a config file, a YAML document, or a JSON schema and call that authoring.&lt;/p&gt;
&lt;div class=&quot;lede&quot;&gt;
&lt;p&gt;This time the BRMS came first. The part that ended MeshAlpha is the part BOE
opens with, and it is built for the agentic era rather than retrofitted to it.&lt;/p&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;#x22;src&amp;#x22;:&amp;#x22;../../assets/posts/boe-console-authoring.jpg&amp;#x22;,&amp;#x22;alt&amp;#x22;:&amp;#x22;The BOE Console authoring a change to the card-fraud-risk ruleset. A plain-language instruction about travel chargebacks on the left has been turned into a new rule in the sequential first-match table on the right, with the new merchant.chargeback_ratio input added to the schema, twelve of fourteen tests passing, and the two failures listed underneath.&amp;#x22;,&amp;#x22;index&amp;#x22;:0}&quot;&gt;&lt;/p&gt;
&lt;figcaption&gt;Authoring in the BOE Console. The change is described in plain language, staged against a draft, and nothing goes live until the tests pass and someone publishes.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;I also noticed something about scale that the industry rarely says out loud. Most production rulesets sit well under 2,500 rules. Most of them are closer to 250 or 500. Past that, a ruleset stops being something a team can reason about, and honestly even 500 rules is pushing it for most teams. Fact volume tells a similar story. In my entire career I have never seen an engine handed millions of facts in a single pass. What I have seen, over and over, are millions of small fact sets, 10 or 20 objects each, triggering an engine individually or in async batches.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;That gave me a number to build toward. If I could run 2,500 rules against
2,500 facts and get an answer back in 250 milliseconds, I would have something
worth shipping.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;micro-engines&quot;&gt;Micro Engines&lt;/h2&gt;
&lt;p&gt;The last realization shaped BOE more than any of the others. Most rule engines on the market, commercial or open source, ship one engine type, or maybe as many as five, and then bend every problem to fit whichever one you licensed. A Scoring problem gets modeled as a Forward Chaining problem because Forward Chaining is what the vendor sells. An Eligibility check runs through the same general purpose evaluator as a pricing calculation, because that evaluator is the only tool in the box.&lt;/p&gt;
&lt;p&gt;I built the opposite of that. Every engine in BOE is its own thing: its own shape, its own runtime, its own input and output schema, its own algorithm tuned for exactly the decision it makes. A Scoring engine does scoring. An Eligibility engine does eligibility. Nothing gets reused just because it happens to already be sitting there.&lt;/p&gt;
&lt;div class=&quot;lede&quot;&gt;
&lt;p&gt;You see it the moment you open the console. The overview is the way into the
BRMS, and every ruleset on it names the engine it runs on.&lt;/p&gt;
&lt;/div&gt;
&lt;figure&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;#x22;src&amp;#x22;:&amp;#x22;../../assets/posts/boe-console-overview.jpg&amp;#x22;,&amp;#x22;alt&amp;#x22;:&amp;#x22;The BOE Console overview, the entry point to the BRMS. Tiles across the top count decisions, latency, evaluation errors and drafts awaiting review. Below them a Rulesets table gives every ruleset its own engine in the ENGINE column: Scoring, Sequential, Risk, Eligibility, State Machine, Pipeline and Monte Carlo. A Needs attention list and an Audit trail run along the bottom.&amp;#x22;,&amp;#x22;index&amp;#x22;:0}&quot;&gt;&lt;/p&gt;
&lt;figcaption&gt;The overview, where the console opens. The ENGINE column is the whole argument: every ruleset runs on the engine its decision calls for, not on one shared evaluator.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;I never needed to compete with ILOG or Pega or Drools on their own terms. Their architecture solves a much bigger problem than most teams actually have, and that scope is exactly what makes them heavy to adopt and slow to change. My problem was smaller and clearer than that from the start: &lt;strong&gt;thirty five specific Micro Engines&lt;/strong&gt;, each one built the way it should have been built if nobody had to compromise for the sake of a shared platform underneath it.&lt;/p&gt;
&lt;div class=&quot;lede&quot;&gt;
&lt;p&gt;That is BOE! You can look through all of it at &lt;a href=&quot;https://boerules.com&quot;&gt;boerules.com&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>BOE</category><category>Rule Engines</category><category>Business Rules</category><category>Charles Forgy</category><category>Drools</category><category>MeshAlpha</category><category>RETE</category><category>RETE-OO</category></item><item><title>SOUNDEX Algorithm in Python3+</title><link>https://www.kamauwashington.com/2023/05/25/soundex-algorithm-in-python3/</link><guid isPermaLink="true">https://www.kamauwashington.com/2023/05/25/soundex-algorithm-in-python3/</guid><description>This post is purely illustrative and educational. It showcases a working version of the Soundex algorithm in Python3+. This code is not to be used in production as is and should be modified or…</description><pubDate>Thu, 25 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;This post is purely illustrative and educational. It showcases a working version of the Soundex algorithm in Python3+. This code is not to be used in production as is and should be modified or rewritten to meet the requirements and standards of a production-level implementation.&lt;/p&gt;
&lt;p&gt;If you would rather skip to the code, or this article is TLDR, no worries! See the code &lt;a href=&quot;https://github.com/kamauwashington/python-soundex-algorithm&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Soundex&quot;&gt;Soundex Algorithm&lt;/a&gt; is a phonetic algorithm that converts an alphanumeric string into a 4 character code by how it is pronounced in English. The 4 character code is comprised of one letter followed by three digits. The first time I came across this algorithm was with a task I was given to perform address and name matching for a skiptracing worflow using Stored Procedures in SQL Server 2000 (eeeeek! 😬 ). It made absolutely no sense to me at the time how it was able to determine how “Geoff” and “Jeff” SOUND alike to a computer. As I made a list of algorithms to familiarize myself with and implement this year, SOUNDEX made it on the list, and here I am. This algorithm can be implemented in any language, I decided to use Python3+ to become more fluent in it.&lt;/p&gt;
&lt;h3 id=&quot;so-how-do-we-get-v532-from-vandeusen&quot;&gt;So how do we get V532 from “VanDeusen”?&lt;/h3&gt;
&lt;p&gt;The algorithm seems to be straight-forward. However, it has a few gotchas that can be observed in many available open-source implementations which will be addressed below. Let’s list the rules of the algorithm (they can be implemented in varying orders) :&lt;/p&gt;
&lt;p&gt;From &lt;a href=&quot;https://en.wikipedia.org/wiki/Soundex&quot;&gt;SOUNDEX Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Remove all non-alpha characters&lt;/li&gt;
&lt;li&gt;If two of the same characters are adjacent in the original alphanumeric string, only retain the first character.&lt;/li&gt;
&lt;li&gt;If two of the same characters are separated by &lt;strong&gt;h, w, y&lt;/strong&gt; only retain the first character.&lt;/li&gt;
&lt;li&gt;If two of the same characters are separated by &lt;strong&gt;a, e, i, o, u&lt;/strong&gt; retain both characters&lt;/li&gt;
&lt;li&gt;Retain the first character of the alphanumeric string and drop all other occurrences of &lt;strong&gt;a, e, i, o, u, y, h&lt;/strong&gt;, &lt;strong&gt;w, y&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Replace consonants with digits as follows (after the first letter):
&lt;ul&gt;
&lt;li&gt;b, f, p, v = 1&lt;/li&gt;
&lt;li&gt;c, g, j, k, q, s, x, z = 2&lt;/li&gt;
&lt;li&gt;d, t = 3&lt;/li&gt;
&lt;li&gt;l = 4&lt;/li&gt;
&lt;li&gt;m, n = 5&lt;/li&gt;
&lt;li&gt;r = 6&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If there are too few characters in the alphanumeric string to assign three numbers, append zeros until there are three numbers. If there are four or more numbers, retain only the first three&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;vowels-and-the-highway-hwy&quot;&gt;Vowels and the Highway (hwy)&lt;/h3&gt;
&lt;p&gt;One of the first steps is to Keep it Short and Simple (KISS Principle). We don’t need a clever algorithm to handle vowels and h, w, y we simply need a code. Uppercase characters will be used to make a lexicon given the rules above.&lt;/p&gt;





























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;A&lt;/th&gt;&lt;th&gt;B&lt;/th&gt;&lt;th&gt;C&lt;/th&gt;&lt;th&gt;D&lt;/th&gt;&lt;th&gt;E&lt;/th&gt;&lt;th&gt;F&lt;/th&gt;&lt;th&gt;G&lt;/th&gt;&lt;th&gt;H&lt;/th&gt;&lt;th&gt;I&lt;/th&gt;&lt;th&gt;J&lt;/th&gt;&lt;th&gt;K&lt;/th&gt;&lt;th&gt;L&lt;/th&gt;&lt;th&gt;M&lt;/th&gt;&lt;th&gt;N&lt;/th&gt;&lt;th&gt;O&lt;/th&gt;&lt;th&gt;P&lt;/th&gt;&lt;th&gt;Q&lt;/th&gt;&lt;th&gt;R&lt;/th&gt;&lt;th&gt;S&lt;/th&gt;&lt;th&gt;T&lt;/th&gt;&lt;th&gt;U&lt;/th&gt;&lt;th&gt;V&lt;/th&gt;&lt;th&gt;W&lt;/th&gt;&lt;th&gt;X&lt;/th&gt;&lt;th&gt;Y&lt;/th&gt;&lt;th&gt;Z&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;em&gt;0’s are vowels, 9’s are h,w,y&lt;/em&gt;&lt;/p&gt;
&lt;h4 id=&quot;implementation-step-1--create-the-lexicon&quot;&gt;Implementation Step #1 – Create the lexicon&lt;/h4&gt;
&lt;p&gt;Moving from A to Z, use the mappings provided by the algorithm to create a string that can later be iterated character by character. 0’s represent vowels, and 9’s represent h, w, y.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;python&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;python&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#5C5A57;font-style:italic&quot;&gt;# codings is a representation of the alphabet according to the SOUNDEX algorithm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;# 0 represents vowels : &apos;A&apos;,&apos;E&apos;,&apos;I&apos;,&apos;O&apos;,&apos;U&apos;, where 9 represents &apos;H&apos;,&apos;W&apos;,&apos;Y&apos;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;# Both 0 and 9 are skipped charcters.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;codings &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;01230129022455012623019292&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h4 id=&quot;implementation-step-2--lexicon-translation&quot;&gt;Implementation Step #2 – Lexicon translation&lt;/h4&gt;
&lt;p&gt;This step is the “coding” step, where the input is transformed into its coded output as per the lexicon. This implementation breaks apart the transformation step from the code generation step for readability and separation of concerns.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;python&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;python&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#5C5A57;font-style:italic&quot;&gt;# use an uppercase representation of the input for easier validation and iterate over each character&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; char &lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;upper&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;())&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # get the ascii character code for the current character&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    charCode &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; ord&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # VALIDATON : if the char is not [A-Z] skip it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; charCode &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 65&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; and&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; charCode &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 90&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        continue&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;color:#5C5A57;font-style:italic&quot;&gt;    # SOUNDEX : Step 1a. Retain the first letter of the input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;finalCode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        finalCode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # SOUNDEX : Step 2. Replace consonants with digits (after the first letter):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    code &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; codings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;charCode &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 65&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    inputCoded&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;code&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;ol&gt;
&lt;li&gt;Get the ASCII representation of the current uppercase character of the iteration&lt;/li&gt;
&lt;li&gt;Given A=65 and Z=90 as ASCII character codes, determine if the character is inclusively between 65 and 90, if not the character skips any further processing (the equivalent of deleting the character in the output of the routine)&lt;/li&gt;
&lt;li&gt;Extract the first character of the input to use as the prefix of the final code&lt;/li&gt;
&lt;li&gt;Use the lexicon to obtain the character value per the specification. When using ASCII it is easy to subtract A from 65 to get “0” which can be used as an index for the lexicon. Append this character to the representation of the input as coded numbers&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The above steps will translate a name into a string of numbers to be parsed in the next step so &lt;strong&gt;VanDeusen&lt;/strong&gt; will be translated into &lt;strong&gt;105300205&lt;/strong&gt;&lt;/p&gt;



























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;V&lt;/th&gt;&lt;th&gt;A&lt;/th&gt;&lt;th&gt;N&lt;/th&gt;&lt;th&gt;D&lt;/th&gt;&lt;th&gt;E&lt;/th&gt;&lt;th&gt;U&lt;/th&gt;&lt;th&gt;S&lt;/th&gt;&lt;th&gt;E&lt;/th&gt;&lt;th&gt;N&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;em&gt;Lexicon translation of VanDeusen&lt;/em&gt;&lt;/p&gt;
&lt;h4 id=&quot;implementation-step-3--soundex-code-creation&quot;&gt;Implementation Step #3 – SOUNDEX Code Creation&lt;/h4&gt;
&lt;p&gt;This step creates the 4 character code from the lexicon translation. While this implementation can be merged with the previous step, this iteration is kept apart from the prior iteration for readability and separation of concerns.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;python&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;python&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;21&lt;/span&gt;&lt;span&gt;22&lt;/span&gt;&lt;span&gt;23&lt;/span&gt;&lt;span&gt;24&lt;/span&gt;&lt;span&gt;25&lt;/span&gt;&lt;span&gt;26&lt;/span&gt;&lt;span&gt;27&lt;/span&gt;&lt;span&gt;28&lt;/span&gt;&lt;span&gt;29&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;31&lt;/span&gt;&lt;span&gt;32&lt;/span&gt;&lt;span&gt;33&lt;/span&gt;&lt;span&gt;34&lt;/span&gt;&lt;span&gt;35&lt;/span&gt;&lt;span&gt;36&lt;/span&gt;&lt;span&gt;37&lt;/span&gt;&lt;span&gt;38&lt;/span&gt;&lt;span&gt;39&lt;/span&gt;&lt;span&gt;40&lt;/span&gt;&lt;span&gt;41&lt;/span&gt;&lt;span&gt;42&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#5C5A57;font-style:italic&quot;&gt;# iterate over the coded input, the index is needed to compare like characters with a 0 or 9 between them&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; index&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; char &lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; enumerate&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;inputCoded&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # prevent further iteration once the finalCode has reached the max length according to the SOUNDEX algorithm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;finalCode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        break&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;color:#5C5A57;font-style:italic&quot;&gt;    # determine if the current character is a skippable (or to be removed) character as per the algorithm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    skipchar &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; char &lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&apos;0&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&apos;9&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # SOUNDEX : Step 3b. Remove all the zero (0 or 9) digits (performed by skipping)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; skipchar &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        continue&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;color:#5C5A57;font-style:italic&quot;&gt;    # store the previous character if available&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; index &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        previousChar &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; inputCoded&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # the first character should be skipped for processing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        continue&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;color:#5C5A57;font-style:italic&quot;&gt;    # SOUNDEX : Step 3a. Replace all adjacent same digits with one digit (performed by skipping)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; char &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; previousChar &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        continue&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;color:#5C5A57;font-style:italic&quot;&gt;    # SOUNDEX : Step 3.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;    #&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;    # If two or more letters with the same number are adjacent in the original word (before step 1),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;    # only retain the first letter; also two letters with the same number separated by &apos;h&apos; , &apos;w&apos; or &apos;y&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;    # are coded as a single number, whereas such letters separated by a vowel are coded twice.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;    # This rule also applies to the first letter.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; index &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; and&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; inputCoded&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; char &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; previousChar &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &apos;0&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;            finalCode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;            continue&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;color:#5C5A57;font-style:italic&quot;&gt;        # characters equaling 9 are skipped if the previous and next value are the same&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        elif&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; previousChar &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &apos;9&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;            continue&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;color:#E8E6E1&quot;&gt;    finalCode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;ol&gt;
&lt;li&gt;If the length of the finalCode is 4 we can stop iterating as that is the maximum number of characters needed for code completion&lt;/li&gt;
&lt;li&gt;If the current character is 0 or 9 it can be skipped or not added to the final code&lt;/li&gt;
&lt;li&gt;If there is a previous character available it should be stored for future iterations&lt;/li&gt;
&lt;li&gt;If this character is the first character it can be skipped as it was already added in Implementation Step #2&lt;/li&gt;
&lt;li&gt;Deduplicate adjacent identical characters&lt;/li&gt;
&lt;li&gt;If two identical characters are separated by a vowel ( 0 ) append the character to the final code&lt;/li&gt;
&lt;li&gt;If two identical characters are separated by h, w, y then skip this character (it is not added to the final code)&lt;/li&gt;
&lt;li&gt;Finally, if this iteration hasn’t skipped, append the character to the final output&lt;/li&gt;
&lt;/ol&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;V&lt;/th&gt;&lt;th&gt;N&lt;/th&gt;&lt;th&gt;D&lt;/th&gt;&lt;th&gt;S&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;V&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;em&gt;Lexicon translation of VanDeusen&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The above steps will convert &lt;strong&gt;105300205&lt;/strong&gt; into the final output of &lt;strong&gt;V532&lt;/strong&gt;. Remember that the first character “&lt;strong&gt;V&lt;/strong&gt;” was extracted in Implementation Step #2&lt;/p&gt;
&lt;h3 id=&quot;thats-it&quot;&gt;Thats it!&lt;/h3&gt;
&lt;p&gt;This was an extremely interesting algorithm that proved to be challenging at times. This implementation may differ from SQL implementations as eluded to by the Wiki article, but this is the &lt;strong&gt;traditional approach&lt;/strong&gt; that meets the conditions specified :&lt;/p&gt;
&lt;p&gt;Using this algorithm, both “Robert” and “Rupert” return the same string “R163” while “Rubin” yields “R150”. “Ashcraft” and “Ashcroft” both yield “A261”. “Tymczak” yields “T522” not “T520” (the chars ‘z’ and ‘k’ in the name are coded as 2 twice since a vowel lies in between them). “Pfister” yields “P236” not “P123” (the first two letters have the same number and are coded once as ‘P’), and “Honeyman” yields “H555”.&lt;/p&gt;
&lt;h4 id=&quot;take-me-to-the-code&quot;&gt;Take me to the &lt;a href=&quot;https://github.com/kamauwashington/python-soundex-algorithm&quot;&gt;Code&lt;/a&gt;!!!&lt;/h4&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>Fun with Algorithms</category><category>algorithms</category><category>phonetic algorithms</category><category>python</category><category>python3</category><category>soundex</category></item><item><title>NATS.io Queue Group REST API w/ Sanic and Python3+</title><link>https://www.kamauwashington.com/2023/05/22/nats-io-queue-groups-rest-api-w-sanic-and-python3/</link><guid isPermaLink="true">https://www.kamauwashington.com/2023/05/22/nats-io-queue-groups-rest-api-w-sanic-and-python3/</guid><description>This post is purely illustrative and educational. It showcases a working version of a Queue Group + Request-Reply pattern implementation in Python3+ with Nats.io and Sanic. This code is not to be used in…</description><pubDate>Mon, 22 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;This post is purely illustrative and educational. It showcases a working version of a &lt;a href=&quot;https://docs.nats.io/nats-concepts/core-nats/queue&quot;&gt;Queue Group&lt;/a&gt; + &lt;a href=&quot;https://docs.nats.io/nats-concepts/core-nats/reqreply&quot;&gt;Request-Reply&lt;/a&gt; pattern implementation in Python3+ with Nats.io and Sanic. This code is not to be used in production as is, and should be modified and or rewritten to meet the requirements and standards of a production-level implementation.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://nats.io/about/&quot;&gt;NATS&lt;/a&gt; is a connective technology much like Kafka or Kinesis though far much simpler to use and adopt. The &lt;a href=&quot;https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern&quot;&gt;Publish-Subscribe&lt;/a&gt; pattern is easy to understand, if one has worked with any message queue, authored/used a file watcher, and has not been exposed to “streaming” technologies; NATS is Open Source, easy to adopt, especially with its built-in asynchronous patterns. However, the goal is not to explain NATS ad infinitum, and add to the pool of NATS explanations across the web or to be preferential.&lt;/p&gt;
&lt;p&gt;The goal is to showcase one of the many built-in patterns in NATS and accrue hands-on time with the technology to better understand it.&lt;/p&gt;
&lt;h3 id=&quot;what-is-a-queue-group-in-3-sentences-or-less&quot;&gt;What is a Queue Group in 3 sentences or less?&lt;/h3&gt;
&lt;p&gt;A Queue Group is essentially a pool of subscribers load-balanced on a NATS subject. Adding two of the same subscriptions to a subject will result in both executing. If these same subscriptions are part of a Queue Group, each message is received by one and only one random subscriber in the subscriber pool.&lt;/p&gt;
&lt;h3 id=&quot;what-is-a-valid-use-case-for-queue-groups&quot;&gt;What is a valid use case for Queue Groups?&lt;/h3&gt;
&lt;p&gt;One of the designs I have favored is having message validation IO completely decoupled from computationally intensive APIs. This is to prevent invalid messages from entering and/or leaving a system, and to offload validation (both light and heavy) to a separate tier that responds immediately vs bundling validation with valuable compute.&lt;/p&gt;
&lt;p&gt;This design can be seen in &lt;a href=&quot;https://docs.aws.amazon.com/apigateway/latest/developerguide/models-mappings.html&quot;&gt;API Gateway, where a JSON Schema&lt;/a&gt; can be defined and executed prior to invoking a Lambda or transmitting to another AWS Service.&lt;/p&gt;
&lt;p&gt;For this implementation, the goal is much simpler, to &lt;strong&gt;gauge the degree of difficulty&lt;/strong&gt; to implement Queue Groups, and &lt;strong&gt;observe them in action&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;lets-get-started&quot;&gt;Let’s get started!&lt;/h3&gt;
&lt;p&gt;There are three parts needed to prove this pattern and its usefulness :&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A running NATS Server. This is extremely easy to install and configure &lt;a href=&quot;https://docs.nats.io/running-a-nats-service/nats_docker&quot;&gt;NATS with docker&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;An API Framework. Python3+ is used for this implementation along with the &lt;a href=&quot;https://sanic.dev/en/&quot;&gt;Sanic Framework&lt;/a&gt; for the API as it is quick to get up and running.&lt;/li&gt;
&lt;li&gt;A Subscriber that will listen to a NATS subject and respond using NATS &lt;a href=&quot;https://docs.nats.io/nats-concepts/core-nats/reqreply&quot;&gt;Request-Reply&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;With these 3 components, implementing an API with Request-Reply to a NATS Subject with Subscribers is extremely straightforward.&lt;/p&gt;
&lt;h3 id=&quot;create-the-subscriber&quot;&gt;Create the Subscriber&lt;/h3&gt;
&lt;p&gt;Subscriber implementation can be seen &lt;a href=&quot;https://github.com/kamauwashington/nats-queue-api-python/blob/main/subscriber.py&quot;&gt;HERE&lt;/a&gt; if TLDR&lt;/p&gt;
&lt;p&gt;The subscriber should be able to identify itself to the requestor, distinguishing itself from other subscribers in the Queue Group on reply. If created correctly, multiple requests to a single NATS subject with multiple queued subscribers should result in a randomly selected subscriber reply on each request.&lt;/p&gt;
&lt;h4 id=&quot;step-1-identify-the-subscriber&quot;&gt;Step #1 Identify the Subscriber&lt;/h4&gt;
&lt;p&gt;A UUID could be used, but what would be the fun in that? Boring! Random human-readable “slugs” or names seem much easier to align which subscriber responded without having to manually scan a few letters of a UUID. For this, a Python library called ‘&lt;a href=&quot;https://pypi.org/project/coolname/&quot;&gt;coolname&lt;/a&gt;‘ was used.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;python&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;python&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#17D0D8&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; coolname &lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; generate_slug&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;# generate a coolname for this subscriber&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;subscriptionName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; generate_slug&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;Using 2 as a parameter to generate_slug(..), generates names like ‘macho-baboon’, or ‘coy-turtle’&lt;/p&gt;
&lt;h4 id=&quot;step-2-create-a-callback-w-an-adjustable-delay&quot;&gt;Step #2 Create a callback w/ an adjustable delay&lt;/h4&gt;
&lt;p&gt;While there is no shortage of algorithms that would slow responses through computationally intensive algorithms, the goal is to prove the load balancing aspect vs cpu performance as this varies from one environment to another.&lt;/p&gt;
&lt;p&gt;To keep it simple, all that is needed is a delay, an async delay to be exact, and use the message sent to the callback to issue a reply back to the NATS reply-to token. An async delay is used as this type of event-based programming lends itself to an async environment like Python’s &lt;a href=&quot;https://docs.python.org/3/library/asyncio.html&quot;&gt;asyncio&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Asynchronous programming was favored as I have become used to it over the years of writing NodeJS APIs. When applicable use the best tool for the job..&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;python&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;python&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#5C5A57;font-style:italic&quot;&gt;# this method executes an async delay for the duration defined in the environment variable : DURATION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; delayedResponse&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;msg &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; NATS&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;msg_class&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; -&gt;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    global&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; subscriptionName&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;color:#5C5A57;font-style:italic&quot;&gt;    # important to use await here, this is a non blocking sleep&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    await&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; asyncio&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;DURATION&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#E8E6E1&quot;&gt;    messageCount &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; msg&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;decode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    replyMessage &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Message %1s recieved by &apos;%2s.&apos;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; %&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;messageCount&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;subscriptionName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;replyMessage&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # using the Request-Reply pattern msg.respond publishes a message to the reply-to (message must be in bytes)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    await&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; msg&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;respond&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;replyMessage&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;encode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note&lt;/strong&gt; : ‘messageCount’ can be thought of as ‘message-id’&lt;/em&gt;, this may be changed after this article is published.&lt;/p&gt;
&lt;h4 id=&quot;step-3-create-the-subscriber&quot;&gt;Step #3 Create the subscriber&lt;/h4&gt;
&lt;p&gt;This was the most interesting implementation hurdle. There are plenty of extremely lengthy Python examples for creating a NATS long-running subscriber. They were ignored in lieu of a simple while loop with an &lt;a href=&quot;https://docs.python.org/3/library/asyncio-task.html#sleeping&quot;&gt;async.sleep&lt;/a&gt; (exactly how I would have done it in NodeJS 😁 )&lt;/p&gt;
&lt;p&gt;With any long-running process like this, it is important to &lt;strong&gt;unsubscribe and close the NATS connection to prevent orphaned processes&lt;/strong&gt;. It is common to close all processes without doing the aforementioned, only to find that messages sent to a subject continue returning responses.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt; – the most essential part of this function is setting ‘&lt;strong&gt;queue&lt;/strong&gt;‘. This is how the load balancing is achieved within the Queue Group.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;python&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;python&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;21&lt;/span&gt;&lt;span&gt;22&lt;/span&gt;&lt;span&gt;23&lt;/span&gt;&lt;span&gt;24&lt;/span&gt;&lt;span&gt;25&lt;/span&gt;&lt;span&gt;26&lt;/span&gt;&lt;span&gt;27&lt;/span&gt;&lt;span&gt;28&lt;/span&gt;&lt;span&gt;29&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;31&lt;/span&gt;&lt;span&gt;32&lt;/span&gt;&lt;span&gt;33&lt;/span&gt;&lt;span&gt;34&lt;/span&gt;&lt;span&gt;35&lt;/span&gt;&lt;span&gt;36&lt;/span&gt;&lt;span&gt;37&lt;/span&gt;&lt;span&gt;38&lt;/span&gt;&lt;span&gt;39&lt;/span&gt;&lt;span&gt;40&lt;/span&gt;&lt;span&gt;41&lt;/span&gt;&lt;span&gt;42&lt;/span&gt;&lt;span&gt;43&lt;/span&gt;&lt;span&gt;44&lt;/span&gt;&lt;span&gt;45&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#5C5A57;font-style:italic&quot;&gt;# this is the main subscriber, note that it is asynchronous as there are multiple awaits within needed by NATS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ()&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; -&gt;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;    # identify subscriber&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    global&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; subscriptionName&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Starting subscriber &apos;%1s&apos;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; %&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;subscriptionName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # provide access to the close connectionGlobal var for use in the keep alive loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    global&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; closeConnection&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;color:#5C5A57;font-style:italic&quot;&gt;    # &lt;/span&gt;&lt;span style=&quot;color:#17D0D8;font-style:italic&quot;&gt;NOTE&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; - this is anycio compatible NATS, not the synchronous NATS functionality&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    nc &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; NATS&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # connect to the server defined in the environment variable : SERVER&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    await&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; nc&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;connect&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;servers&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;SERVER&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Connection to NATS Server &apos;%1s&apos; established by &apos;%2s&apos;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; %&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;SERVER&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;subscriptionName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # create the subscription to the subject defined in the environment variable : NATS_SUBJECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    subscription &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; nc&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;subscribe&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        settings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NATS_SUBJECT&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;        # callback to the delayed response method defined above&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        cb&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;delayedResponse&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;        # IMPORTANT -&gt; specifying queue allows multiple instances of this process to load balance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;        # the queue specified in the environment variable : NATS_QUEUE_NAME.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        queue&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NATS_QUEUE_NAME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # while the global variable closeConnection is False, loop every one second to keep alive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;    # this can be a larger amount if needed, it is set to 1 for quick exiting only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; closeConnection &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        await&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; asyncio&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    ###################### CLEANUP and EXIT ######################&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;color:#5C5A57;font-style:italic&quot;&gt;    # unsubscribe from the above NATS subscription&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    await&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; subscription&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;unsubscribe&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;Unsubscribed &apos;%2s&apos; from &apos;%1s.&apos;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; %&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NATS_SUBJECT&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;subscriptionName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # close the NATS connection, there have been instances where a process was not properly closed w/ orphan connections&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    await&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; nc&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Closed connection to Nats Server &apos;%1s&apos;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; %&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;SERVER&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    # exit this process&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    sys&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;create-the-api&quot;&gt;Create the API&lt;/h3&gt;
&lt;p&gt;Sanic API implementation can be seen &lt;a href=&quot;https://github.com/kamauwashington/nats-queue-api-python/blob/main/api.py&quot;&gt;HERE&lt;/a&gt; if TLDR&lt;/p&gt;
&lt;p&gt;The API is the most simplistic implementation as it simply is a wrapper around NATS Request-Reply mechanics. It is essentially a NATS HTTP proxy. There are &lt;strong&gt;before_server_start&lt;/strong&gt; and &lt;strong&gt;before_server_stop&lt;/strong&gt; for initialization and cleanup respectively, the code can be seen in the link above, as they feel immaterial for the purposes of this article.&lt;/p&gt;
&lt;p&gt;That is all there is to it! &lt;strong&gt;Line 10&lt;/strong&gt;, NATS Request-Reply in action! This makes me 🤦🏽‍♂️ (facepalm) at how simple this is. It is almost too simple.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Keep it Short and Simple (Kiss Principle)&lt;/strong&gt; – I hope that developers take note, implementation should be this simple in 2023+ (should have been this simple in most libraries a decade or more ago).&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;python&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;python&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#5C5A57;font-style:italic&quot;&gt;# basic root endpoint http://&amp;#x3C;SERVER&gt;:&amp;#x3C;PORT&gt; (defaults to http://localhost:8000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; root_request&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;request &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; Request&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;        # increment the message count (to identify messages when multiple subscribers are attached)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;messageCount &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 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;color:#5C5A57;font-style:italic&quot;&gt;        # use NATS Request/Reply pattern to publish to NATS_SUBJECT (it should be load balanced and sticky to the receiving subscriber)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        msg &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; NATS&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;msg_class &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;nc&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;settings&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NATS_SUBJECT&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;messageCount&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;encode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;timeout&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;        # return the subscriber response to the requestor (message returned is in bytes must decode to utf-8)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;decode&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#17D0D8&quot;&gt;    except&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; asyncio&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;TimeoutError&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;        # there should not be timeouts unless a large duration is set&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Timed out waiting for response&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Request timed out sending message %1s.&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; %&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;messageCount&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;lets-run-this-thing&quot;&gt;Lets run this thing!&lt;/h3&gt;
&lt;p&gt;Running this application is straightforward. Follow the steps in the &lt;a href=&quot;https://github.com/kamauwashington/nats-queue-api-python/blob/main/README.md&quot;&gt;README.md&lt;/a&gt; of the repository for prerequisites. Once the environment is ready for a run…&lt;/p&gt;
&lt;h4 id=&quot;step-1-launch-a-subscriber&quot;&gt;Step #1 Launch a Subscriber&lt;/h4&gt;
&lt;p&gt;Ensure a new terminal/console is used for each creation when creating a Subscriber. In each terminal run &lt;strong&gt;python3 subscriber.py&lt;/strong&gt; Creating multiple separate Subscriber processes allows the Queue Group to be formed providing the load-balancing capabilities we set out to prove.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;bash&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-file&quot;&gt;shell&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body no-gutter&quot;&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line sh-cmd&quot;&gt;&lt;span class=&quot;sh-prompt&quot;&gt;my-username@a-computer-name nats-queue-api-python %&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; python3&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; subscriber.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;python3&lt;/span&gt;&lt;span&gt; subscriber.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;Starting&lt;/span&gt;&lt;span&gt; subscriber&lt;/span&gt;&lt;span&gt; &apos;analytic-honeybee&apos;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;Connection&lt;/span&gt;&lt;span&gt; to&lt;/span&gt;&lt;span&gt; NATS&lt;/span&gt;&lt;span&gt; Server&lt;/span&gt;&lt;span&gt; &apos;localhost&apos;&lt;/span&gt;&lt;span&gt; established&lt;/span&gt;&lt;span&gt; by&lt;/span&gt;&lt;span&gt; &apos;analytic-honeybee&apos;.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;Note that the make command is merely a python3 command underneath and that the subscriber name was randomly generated as described in the above Subscriber creation section.&lt;/p&gt;
&lt;p&gt;NOTE – open multiple terminals/consoles to the root of this project and issue the ‘make subscriber’ command to scale up&lt;/p&gt;
&lt;h4 id=&quot;step-2-launch-the-sanic-api&quot;&gt;Step #2 Launch the Sanic API&lt;/h4&gt;
&lt;p&gt;This is a standard Sanic API launch. Ensure that a terminal/console is open to the root of this project and run the command &lt;strong&gt;python3 api.py&lt;/strong&gt;&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;bash&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-file&quot;&gt;shell&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body no-gutter&quot;&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line sh-cmd&quot;&gt;&lt;span class=&quot;sh-prompt&quot;&gt;my-username@a-computer-name nats-queue-api-python %&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; python3&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; api.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;sanic&lt;/span&gt;&lt;span&gt; api:app&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2023-05-21 21:07:06 -0400&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;52986&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;INFO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  ┌──────────────────────────────────────────────────────────────────┐&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │&lt;/span&gt;&lt;span&gt;                          Sanic&lt;/span&gt;&lt;span&gt; v23.3.0&lt;/span&gt;&lt;span&gt;                           │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │&lt;/span&gt;&lt;span&gt;                Goin&apos; Fast @ http://127.0.0.1:8000                │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  ├───────────────────────┬──────────────────────────────────────────┤&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │                       │     mode: production, single worker      │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │     ▄███ █████ ██     │   server: sanic, HTTP/1.1                │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │    ██                 │   python: 3.11.3                         │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │     ▀███████ ███▄     │ platform: macOS-13.3.1-x86_64-i386-64bit │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │                 ██    │ packages: sanic-routing==22.8.0          │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │    ████ ████████▀     │                                          │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │                       │                                          │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  │ Build Fast. Run Fast. │                                          │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;  └───────────────────────┴──────────────────────────────────────────┘&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;[2023-05-21 21:07:06 -0400] [52986] [WARNING] Sanic is running in PRODUCTION mode. Consider using &apos;--debug&apos; or &apos;--dev&apos; while actively developing your application.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;Connection to NATS Server &apos;localhost&apos; established.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;[2023-05-21 21:07:06 -0400] [52991] [INFO] Starting worker [52991]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;It can be seen that ‘make api’ is running the standard Sanic app start command underneath.&lt;/p&gt;
&lt;h4 id=&quot;step-3-load-test&quot;&gt;Step #3 Load Test!&lt;/h4&gt;
&lt;p&gt;Using &lt;a href=&quot;https://www.artillery.io/&quot;&gt;Artillery&lt;/a&gt; it is extremely easy to run load tests and get a report showing what effect general load or sustained load over time has on the response time.&lt;/p&gt;
&lt;p&gt;Open a new terminal/console to the root of this project and run &lt;strong&gt;artillery quick —count 20 -n 20 http://&amp;#x3C;HOST&gt;:&amp;#x3C;HOST_PORT&gt;/ —output report.json &amp;#x26;&amp;#x26; artillery report report.json&lt;/strong&gt;&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;bash&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-file&quot;&gt;shell&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body no-gutter&quot;&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line sh-cmd&quot;&gt;&lt;span class=&quot;sh-prompt&quot;&gt;my-username@a-computer-name nats-queue-api-python %&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; artillery&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; quick&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; --count&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; http://127.0.0.1:8000/&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; --output&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; report.json&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; artillery&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; report&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; report.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;artillery&lt;/span&gt;&lt;span&gt; quick&lt;/span&gt;&lt;span&gt; --count&lt;/span&gt;&lt;span&gt; 40&lt;/span&gt;&lt;span&gt; -n&lt;/span&gt;&lt;span&gt; 20&lt;/span&gt;&lt;span&gt; http://127.0.0.1:8000/&lt;/span&gt;&lt;span&gt; --output&lt;/span&gt;&lt;span&gt; report.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;Log&lt;/span&gt;&lt;span&gt; file:&lt;/span&gt;&lt;span&gt; report.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;artillery&lt;/span&gt;&lt;span&gt; report&lt;/span&gt;&lt;span&gt; report.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line sh-out&quot;&gt;&lt;span&gt;Report&lt;/span&gt;&lt;span&gt; generated:&lt;/span&gt;&lt;span&gt; report.json.html&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;1 Subscriber in Queue Group&lt;/strong&gt; ~2 seconds median&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;#x22;src&amp;#x22;:&amp;#x22;../../assets/posts/nats-image-3.png&amp;#x22;,&amp;#x22;alt&amp;#x22;:&amp;#x22;Bar chart of http.response_time with one subscriber in the queue group: minimum around 100 ms, but median, p95, p99 and maximum all clustered at roughly 2,000 ms.&amp;#x22;,&amp;#x22;index&amp;#x22;:0}&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Median of ~2000ms&lt;/strong&gt; (2 seconds) with 40 users sending 20 requests each&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3 Subscribers in Queue Group&lt;/strong&gt; ~500 milliseconds median&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;#x22;src&amp;#x22;:&amp;#x22;../../assets/posts/nats-image-1.png&amp;#x22;,&amp;#x22;alt&amp;#x22;:&amp;#x22;Bar chart of http.response_time with three subscribers in the queue group: minimum around 100 ms, median around 500 ms, p95 around 1,520 ms, p99 around 1,720 ms and maximum around 1,815 ms.&amp;#x22;,&amp;#x22;index&amp;#x22;:0}&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Median of ~500ms&lt;/strong&gt; (1/2 second) with 40 users sending 20 requests each&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;As can be seen, the Queue Group pattern opens many possibilities for handling targeted scalability. This was a feature I didn’t expect would perform this well and be implemented with such ease. All in all, this will definitely be a pattern I will use with NATS, and possibly with other connective technologies if supported. Woot!&lt;/p&gt;
&lt;h4 id=&quot;take-me-to-the-code&quot;&gt;Take me to the &lt;a href=&quot;https://github.com/kamauwashington/nats-queue-api-python/tree/main&quot;&gt;Code&lt;/a&gt;!!!&lt;/h4&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>Nats.io</category><category>nats</category><category>Publish Subscribe</category><category>python</category><category>Queue Groups</category><category>Request-Reply</category><category>REST</category><category>Sanic</category></item><item><title>Creation of ION-Query for MeshAlpha.io (Pt. 1)</title><link>https://www.kamauwashington.com/2023/01/18/creation-of-ion-query-for-meshalpha-io/</link><guid isPermaLink="true">https://www.kamauwashington.com/2023/01/18/creation-of-ion-query-for-meshalpha-io/</guid><description>This post chronicles the journey of creating an Open Source TypeScript Rule Engine from conception to release. While the code will be open source (MIT License), it is asked that this or other articles…</description><pubDate>Wed, 18 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;This post chronicles the journey of creating an Open Source TypeScript Rule Engine from conception to release. While the code will be open source (MIT License), it is asked that this or other articles are referenced, and attributed to Kamau Washington if a strategy or code block is used outside of meshalpha.io repositories.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; : MeshAlpha.io is a work in progress, updates will be found on this site pre-release&lt;/p&gt;
&lt;p&gt;Earlier in my career I was introduced to the world of Rule Engines, and immediately found a subject that was far more intriguing and challenging than standard Full-Stack SDLC. Blaze Advisor, BizTalk BRE, ILOG JRules were areas of study, as well as the works of Charles L. Forgy the designer of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Rete_algorithm&quot;&gt;RETE&lt;/a&gt; algorithm. It took the better part of a year to understand RETE-OO (object oriented RETE), Conflict Resolution, Agendas, Working Memory, Alpha and Beta nodes, discrimination networks, forward chaining, backward chaining, and more. The following year, I completed my first RETE based Rule Engine in C# which I named “MeshAlpha”.&lt;/p&gt;
&lt;p&gt;While the Engine was fast, extendable, well documented, and passed nearly every Rule Engine test I could get my hands on, there were a few main issues that stood out :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An &lt;strong&gt;extremely simple DSL&lt;/strong&gt; (Domain Specific Language) to represent and author rules outside of code, treating Rules as external configuration vs compiled syntax.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;extremely simple UI&lt;/strong&gt; that allows Rules to be authored without knowledge of the underlying engine, or with limited to exposure to Rule Engines.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;extremely simple code base&lt;/strong&gt;, following patterns and practices that would allow authors besides myself to contribute to the codebase with minimal ramp-up.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;starting-over-dsl-first&quot;&gt;Starting over, DSL first&lt;/h2&gt;
&lt;p&gt;There are many Rule Engines complete with their own DSL, often requiring a significant amount of onboarding for the language itself let alone the Engine ecosystem. This should be avoided at all costs this time around.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PROBLEM STATEMENT&lt;/strong&gt;&lt;br&gt;
Build a &lt;em&gt;text-based&lt;/em&gt; DSL for Querying Facts (Objects, Classes, Models, Interfaces, Types…) using only JSON and or YAML to empower business users, and developers alike. The DSL should be easily readable, pattern based, and allow for hinting in IDEs as well as by schema.&lt;/p&gt;
&lt;h3 id=&quot;start-small-with-a-reusable-pattern&quot;&gt;Start small, with a reusable pattern&lt;/h3&gt;
&lt;p&gt;This DSL should be comprised of repeatable patterns that when parsed and executed alter their behavior based on their location in a query. It should also be familiar to read and write. A pattern that stuck out was &lt;a href=&quot;https://rosettacode.org/wiki/S-expressions&quot;&gt;S-Expressions&lt;/a&gt; or Symbolic Expressions : a tree structured notation of data.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;text&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;text&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;// this is an example of an S-Expression (pseudo code)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(multiply 3 (add 2 5))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;This may look very simple, and appear as if functionality would be lost, however this is an extremely simple, and readable notation to express the following :&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;“multiply three by the sum of two and five”&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This can easily by translated into something familiar like JSON&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;text&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;text&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;// this is an example of an S-Expression in JSON&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;multiply&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        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;            &quot;add&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                2,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                5&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;/div&gt;&lt;/figure&gt;
&lt;p&gt;This then can be easily translated into executable JavaScript&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; multiply&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(...&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; args&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;reduce&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;total&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; total &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(...&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; args&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;reduce&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;total&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; total &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;multiply&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)))&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // = 21&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;immediately-the-possibilities-can-be-realized&quot;&gt;Immediately, the possibilities can be realized!&lt;/h3&gt;
&lt;p&gt;After running permutations of functions, selectors, constants, etc. the S-Expression seems to be the most robust and simple method of implementing a DSL for MeshAlpha.io’s ION (the Rule Engine). A simple repeatable method signature provides &gt; 80% of needed capabilities to query fact sets.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;// this is a simple signature (pseudo code) that provides powerful functionality&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; pseudoOperation&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // operation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;    BaseTypes&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; PropertyQuery&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    ...(&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;BaseTypes&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; PropertyQuery&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)[]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;Given the above, I was armed with enough to create the first pass of the DSL, with VS-Code hinting by means of well defined Types and Type Guards in TypeScript. Below is a working POC of the DSL in ION-Query…&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;21&lt;/span&gt;&lt;span&gt;22&lt;/span&gt;&lt;span&gt;23&lt;/span&gt;&lt;span&gt;24&lt;/span&gt;&lt;span&gt;25&lt;/span&gt;&lt;span&gt;26&lt;/span&gt;&lt;span&gt;27&lt;/span&gt;&lt;span&gt;28&lt;/span&gt;&lt;span&gt;29&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;31&lt;/span&gt;&lt;span&gt;32&lt;/span&gt;&lt;span&gt;33&lt;/span&gt;&lt;span&gt;34&lt;/span&gt;&lt;span&gt;35&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;// rule-fact-query.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    person &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        type &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Person&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        where &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            // NOTE* &quot;and&quot; is optional at root&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;and&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            // firstName is &quot;John&quot;, &quot;Jane&quot;, or &quot;Jim&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;            [&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;eq&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; $self &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;firstName&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;},...[&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Jane&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Jim&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            /*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            * lastName matches (Regex) strings ending with variations of &quot;smith&quot; ,&quot;Smith&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            * &quot;smithson&quot;, &quot;Smithson&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;            [&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;mt&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; $self &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;lastName&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;[sS]mith(?:son)$&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            // age is greater or equal to 21&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;            [&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;gte&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; $self &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;age&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;21&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    address &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        type &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Address&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        where &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            // join on personId between address and person&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;            [&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;eq&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; $person &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;personId&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;},{&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; $self &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;personId&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;            // state is California&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;            [&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;eq&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; $self &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;state&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;CA&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;            [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;                &quot;or&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;                // city is in &quot;Los Angeles&quot;, &quot;Alameda&quot;, or &quot;Culver City&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;                [&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;in&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; $self &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;city&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;},[&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Los Angeles&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Alameda&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;Culver City&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;                // addressLine1 starts with 10 or 40&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;                [&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;sw&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; $self &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;addressLine1&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;40&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;            ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h2 id=&quot;to-be-continued&quot;&gt;To be continued…&lt;/h2&gt;
&lt;p&gt;This was a very challenging exercise that will play a huge part in the creation of the ION-Query library. While this initial step is to prove the theory that a simple readable DSL could be written in JSON, TypeScript, and JavaScript (YAML will be added later), it opens the door to the next steps of supporting provided and custom functions, as well as prototyping the design for the actions supported when a query produces a result from a Fact Set.&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>Rule Engines</category><category>Abstract Syntax Tree</category><category>AST</category><category>Business Rules</category><category>MeshAlpha.io</category><category>RETE</category><category>RETE-OO</category><category>S-Expressions</category></item><item><title>ISO 6346 – Shipping Container Codes</title><link>https://www.kamauwashington.com/2022/09/15/iso-6346-shipping-container-codes/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/15/iso-6346-shipping-container-codes/</guid><description>I often spend time researching and implementing algorithms in multiple languages as a way to keep my programming skillset sharp, learn a new language and its syntactical sugar, and to have the algorithms at…</description><pubDate>Thu, 15 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;blockquote&gt;
&lt;p&gt;This post is purely illustrative and showcases a few implementations that achieve the same goal. Feel free to use any of them in your implementation.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I often spend time researching and implementing algorithms in multiple languages as a way to keep my programming skillset sharp, learn a new language and its syntactical sugar, and to have the algorithms at my disposal when and if I need them.&lt;/p&gt;
&lt;p&gt;This article references&lt;br&gt;
&lt;a href=&quot;https://www.containercontainer.com/iso6346/&quot;&gt;Container Container ISO 6346&lt;/a&gt;&lt;br&gt;
&lt;a href=&quot;https://en.wikipedia.org/wiki/ISO_6346&quot;&gt;Wikipedia ISO 6346&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;ISO 6346 is an international standard that describes the identification of a shipping container. The standard is maintained by the BIC (International Container Bureau) and covers the serial number, owner, country code, and size of any given shipping container.&lt;/p&gt;
&lt;p&gt;An example of a compliant ISO 6346 Code is &lt;strong&gt;CSQU3054383&lt;/strong&gt;&lt;/p&gt;























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Owner Code&lt;/th&gt;&lt;th&gt;Category Identifier&lt;/th&gt;&lt;th&gt;Serial Number&lt;/th&gt;&lt;th&gt;Check Digit&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;CSQ&lt;/td&gt;&lt;td&gt;U&lt;/td&gt;&lt;td&gt;305438&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;[A-Z]{3}&lt;/td&gt;&lt;td&gt;[UJRZ]&lt;/td&gt;&lt;td&gt;\d{6}&lt;/td&gt;&lt;td&gt;\d&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;em&gt;A container owned by &lt;a href=&quot;https://en.wikipedia.org/wiki/Hapag-Lloyd&quot;&gt;Hapag-Lloyd AG&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It is relatively straightforward to validate via Regex as shown above. However, the fun algorithm portion that validates a shipping container code is as follows.&lt;/p&gt;
&lt;h3 id=&quot;calculation-1&quot;&gt;Calculation 1&lt;/h3&gt;
&lt;p&gt;An equivalent numerical value is assigned to each letter of the alphabet, beginning with 10 for the letter A (&lt;strong&gt;11 and multiples thereof are omitted&lt;/strong&gt;) for Owner Code and Category Identifier.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;A&lt;/th&gt;&lt;th&gt;B&lt;/th&gt;&lt;th&gt;C&lt;/th&gt;&lt;th&gt;D&lt;/th&gt;&lt;th&gt;E&lt;/th&gt;&lt;th&gt;F&lt;/th&gt;&lt;th&gt;G&lt;/th&gt;&lt;th&gt;H&lt;/th&gt;&lt;th&gt;I&lt;/th&gt;&lt;th&gt;J&lt;/th&gt;&lt;th&gt;K&lt;/th&gt;&lt;th&gt;L&lt;/th&gt;&lt;th&gt;M&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;14&lt;/td&gt;&lt;td&gt;15&lt;/td&gt;&lt;td&gt;16&lt;/td&gt;&lt;td&gt;17&lt;/td&gt;&lt;td&gt;18&lt;/td&gt;&lt;td&gt;19&lt;/td&gt;&lt;td&gt;20&lt;/td&gt;&lt;td&gt;21&lt;/td&gt;&lt;td&gt;23&lt;/td&gt;&lt;td&gt;24&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;N&lt;/th&gt;&lt;th&gt;O&lt;/th&gt;&lt;th&gt;P&lt;/th&gt;&lt;th&gt;Q&lt;/th&gt;&lt;th&gt;R&lt;/th&gt;&lt;th&gt;S&lt;/th&gt;&lt;th&gt;T&lt;/th&gt;&lt;th&gt;U&lt;/th&gt;&lt;th&gt;V&lt;/th&gt;&lt;th&gt;W&lt;/th&gt;&lt;th&gt;X&lt;/th&gt;&lt;th&gt;Y&lt;/th&gt;&lt;th&gt;Z&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;25&lt;/td&gt;&lt;td&gt;26&lt;/td&gt;&lt;td&gt;27&lt;/td&gt;&lt;td&gt;28&lt;/td&gt;&lt;td&gt;29&lt;/td&gt;&lt;td&gt;30&lt;/td&gt;&lt;td&gt;31&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;34&lt;/td&gt;&lt;td&gt;35&lt;/td&gt;&lt;td&gt;36&lt;/td&gt;&lt;td&gt;37&lt;/td&gt;&lt;td&gt;38&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;calculation-2&quot;&gt;Calculation 2&lt;/h3&gt;
&lt;p&gt;Each of the numbers calculated in step 1 is multiplied by 2&lt;sup&gt;position&lt;/sup&gt;, where position is the exponent to base 2. Position starts at 0, from left to right.&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;C&lt;/th&gt;&lt;th&gt;S&lt;/th&gt;&lt;th&gt;Q&lt;/th&gt;&lt;th&gt;U&lt;/th&gt;&lt;th&gt;3&lt;/th&gt;&lt;th&gt;0&lt;/th&gt;&lt;th&gt;5&lt;/th&gt;&lt;th&gt;4&lt;/th&gt;&lt;th&gt;3&lt;/th&gt;&lt;th&gt;8&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;30&lt;/td&gt;&lt;td&gt;28&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;16&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;64&lt;/td&gt;&lt;td&gt;128&lt;/td&gt;&lt;td&gt;256&lt;/td&gt;&lt;td&gt;512&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;60&lt;/td&gt;&lt;td&gt;112&lt;/td&gt;&lt;td&gt;256&lt;/td&gt;&lt;td&gt;48&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;320&lt;/td&gt;&lt;td&gt;512&lt;/td&gt;&lt;td&gt;768&lt;/td&gt;&lt;td&gt;4096&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;calculation-3&quot;&gt;Calculation 3&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Sum up all results of Step above = &lt;strong&gt;6185&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Divide them by 11 = &lt;strong&gt;562.272..&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Round the result down towards zero i.e. make the result a whole number (integer) = &lt;strong&gt;562&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Multiply the integer value by 11 = &lt;strong&gt;6182&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Subtract the result of &lt;em&gt;(4)&lt;/em&gt; from the result of &lt;em&gt;(1)&lt;/em&gt;: This is the check digit = &lt;strong&gt;3&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h2 id=&quot;calculation-1-implementations&quot;&gt;Calculation 1 Implementations&lt;/h2&gt;
&lt;p&gt;While there are multiple calculations as outlined above, the Calculation 1 implementation is the most interesting. The goal for me was to retrieve the number mapped to an A-Z character &lt;strong&gt;without the use of if statements&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;implementation-1-dictionary&quot;&gt;Implementation 1 (Dictionary)&lt;/h3&gt;
&lt;p&gt;This seems simple enough, create a dictionary/object that represents these values. This will be the most common implementation seen on the web.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; alphaReductionDictionary&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;character&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; }&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;        &quot;A&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;B&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 12&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;C&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 13&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;D&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 14&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;E&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;F&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 16&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;G&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 17&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;H&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 18&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;I&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 19&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;J&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;K&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 21&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;L&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 23&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;M&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 24&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;        &quot;N&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 25&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;O&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 26&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;P&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 27&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Q&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 28&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;R&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 29&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;S&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;T&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 31&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;U&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 32&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;V&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 34&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;W&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 35&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;X&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 36&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Y&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 37&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Z&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 38&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;implementation-2-ascii-character-code-w-math&quot;&gt;Implementation 2 (ASCII Character Code w/ Math)&lt;/h3&gt;
&lt;p&gt;However, I wanted to see if this can be solved via ASCII char code and math alone, as this seems to be a common theme in alphanumeric codes: &lt;em&gt;omit multiples of n&lt;/em&gt;&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; charCodeReduced &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; shippingContainerChar&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;charCodeAt&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 55&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // this reduces ASCII A to 10, as per algorithm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; charCodeRemainder &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; charCodeReduced &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 11&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // identifies occurrences of multiples of 11, and the position of items that arent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; weight &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; Math&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;trunc&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;charCodeReduced &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; charCodeRemainder&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // generates a number that indicates any addition to charCode needed to skip multiples of 11&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; Math&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;trunc&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;charCodeReduced &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; weight&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 11&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; charCodeReduced&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // generates an integer as per the ISO 6346 specification&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;implementation-3-most-elegant&quot;&gt;Implementation 3 (Most Elegant)&lt;/h3&gt;
&lt;p&gt;While I have two solid implementations, I realized that position played a part in the calculation, and with A starting at 10, 0-9 fit nicely in front of it. I thought I was original in this concept however, it seems that this is a valid way of solving this problem as well. It took some digging, but it was found on &lt;a href=&quot;https://en.bitcoinwiki.org/wiki/ISO_6346#Code_Sample_.28Javascript.29&quot;&gt;bitcoinwiki&lt;/a&gt;.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; characterIndexMap&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;0123456789A?BCDEFGHIJK?LMNOPQRSTU?VWXYZ&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;characterIndexMap&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;indexOf&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;shippingContainerChar&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;));&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;With these implementations in place, creating a function that completes the algorithm is easy. Note, the full implementation allows the implementations above to be injected into the calculation process.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;21&lt;/span&gt;&lt;span&gt;22&lt;/span&gt;&lt;span&gt;23&lt;/span&gt;&lt;span&gt;24&lt;/span&gt;&lt;span&gt;25&lt;/span&gt;&lt;span&gt;26&lt;/span&gt;&lt;span&gt;27&lt;/span&gt;&lt;span&gt;28&lt;/span&gt;&lt;span&gt;29&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;31&lt;/span&gt;&lt;span&gt;32&lt;/span&gt;&lt;span&gt;33&lt;/span&gt;&lt;span&gt;34&lt;/span&gt;&lt;span&gt;35&lt;/span&gt;&lt;span&gt;36&lt;/span&gt;&lt;span&gt;37&lt;/span&gt;&lt;span&gt;38&lt;/span&gt;&lt;span&gt;39&lt;/span&gt;&lt;span&gt;40&lt;/span&gt;&lt;span&gt;41&lt;/span&gt;&lt;span&gt;42&lt;/span&gt;&lt;span&gt;43&lt;/span&gt;&lt;span&gt;44&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; isISO6346Compliant&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;containerNumber&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; getCharValue&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; boolean&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;	// fail fast if any parameter is null or undefined&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;containerNumber &lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;\s&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;containerNumber&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;getCharValue&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; return&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;	// validate for ISO 6346 compliant container number&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;	const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; isoPattern&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; RegExp&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;A-Z&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]{3}[&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;UJRZ&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;\d&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;{6}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;\d&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;	// fail fast if the containerNumber does not meet ISO 6346 standards&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isoPattern&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;containerNumber&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; return&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    // get check digit (last character of containerNumber)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; checkDigit&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;containerNumber&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;containerNumber&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;length &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    /*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;     * Calculation Step 3a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;     *&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;     * calculate sum of container number chars multiplied by exponent to base 2,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;     * the last character (check digit) is omitted from calculation as per spec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;     */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; calcSumExpB2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; containerNumber&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;length &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;++)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; extractedValue&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;        const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; contNumChar&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; containerNumber&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;        /*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;         * the first 4 characters are alphabetical, they need to be converted with&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;         * multiples of 11 omitted&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;         */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        extractedValue &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;i &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)?&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; getCharValue&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;contNumChar&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;contNumChar&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;        // multiply the extracted value by exponent to base 2 using the position of the char&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;        calcSumExpB2 &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; extractedValue &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; Math&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;pow&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    // Calculation Step 3b: Division by 11&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; stepCalc &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; calcSumExpB2 &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 11&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    // Calculation Step 3c: Erase decimal digits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    stepCalc &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; Math&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;trunc&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;stepCalc&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    // Calculation Step 3d: Multiply by 11&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    stepCalc &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;*=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 11&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;    // Calculation Step 3e: a) - d) === CheckDigit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; calcSumExpB2 &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; stepCalc &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; checkDigit&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h2 id=&quot;there-it-is&quot;&gt;There it is!&lt;/h2&gt;
&lt;p&gt;This exercise was a lot of fun and showcased various ways of implementing an algorithm. The full source code for this exercise can be found &lt;a href=&quot;https://github.com/kamauwashington/ISO-6346&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>Fun with Algorithms</category><category>javascript</category><category>nodejs</category><category>typescript</category></item><item><title>AWS CDK Static Web + API Multiple Branches in One Account</title><link>https://www.kamauwashington.com/2022/09/06/aws-cdk-static-web-api-multiple-branches-in-one-account/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/06/aws-cdk-static-web-api-multiple-branches-in-one-account/</guid><description>With one development team working on a single project in multiple branches, it may be advantageous to deploy each branch in an isolated manner that grants other developers and product owners access to a…</description><pubDate>Tue, 06 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;blockquote&gt;
&lt;p&gt;This post is purely illustrative and is to be considered a deployment option that may suit an organization’s CICD workflow and feedback loop for Website and API resource deployment.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;With one development team working on a single project in multiple branches, it may be advantageous to deploy each branch in an isolated manner that grants other developers and product owners access to a work in progress remotely.&lt;/p&gt;
&lt;p&gt;A use case for this is a client-rendered SPA or Static Website with an API being deployed in AWS, three developers working on three separate new features in one Micro-frontend. The organization feedback loop standard is to publish to the development environment for visual review after completing code review.&lt;/p&gt;
&lt;p&gt;The development environment would be written over multiple times with each commit, leaving reviewers to gamble as to which feature they are experiencing.&lt;/p&gt;
&lt;h3 id=&quot;50000ft-view-of-namespace-deployment&quot;&gt;50,000ft view of namespace deployment&lt;/h3&gt;
&lt;p&gt;Technically namespace deployment is already performed in the manner of subdomains, resource tagging and prefixing, and other methods. If building a Stack on each commit, and tearing it down after a few hours is a desired or current workflow, this can be achieved via namespaced deployment as shown in the “dev” site below (for which I used a random phrase generator).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;dev.example.com
&lt;ul&gt;
&lt;li&gt;infinity-mouse.example.com&lt;/li&gt;
&lt;li&gt;highfalutin-cord.example.com&lt;/li&gt;
&lt;li&gt;makeshift-foot.example.com&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;qa.example.com&lt;/li&gt;
&lt;li&gt;stage.example.com&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.example.com&quot;&gt;http://www.example.com&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;so-how-do-we-do-that-in-aws&quot;&gt;So how do we do that in AWS?&lt;/h3&gt;
&lt;p&gt;We are going to need a few things first, but it can be done using the &lt;a href=&quot;https://cdkworkshop.com/&quot;&gt;AWS CDK&lt;/a&gt;. These items should be complete or available before attempting to deploy.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;AWS CDK installed&lt;/li&gt;
&lt;li&gt;AWS credentials (best defined via profile)&lt;/li&gt;
&lt;li&gt;A domain name that points to AWS nameservers&lt;/li&gt;
&lt;li&gt;A CNAME for the wildcard domain (*.example.com – created by default)&lt;/li&gt;
&lt;li&gt;A wildcard SSL certificate issued for the domain through ACM in the us-east-1 region as this is &lt;a href=&quot;https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cnames-and-https-requirements.html&quot;&gt;required by CloudFront&lt;/a&gt; for HTTPS (we want to test on HTTPS)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;create-constants-from-environment-variables&quot;&gt;Create constants from Environment Variables&lt;/h3&gt;
&lt;p&gt;These environment variables will help with the naming of the resources while authoring the stack, these should be required.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DOMAIN_NAME&lt;/li&gt;
&lt;li&gt;APPLICATION_NAME&lt;/li&gt;
&lt;li&gt;NAMESPACE&lt;/li&gt;
&lt;li&gt;CERTIFICATE_ARN&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;add-tags-to-all-resources-in-the-stack&quot;&gt;Add tags to all resources in the Stack&lt;/h3&gt;
&lt;p&gt;Stack-created resources may have unique names that do not include the Namespace as a helper construct may have generated them. Applying tags to all resources makes them easier to find when searching, and can be found all at once using the &lt;a href=&quot;https://docs.aws.amazon.com/ARG/latest/userguide/tag-editor.html&quot;&gt;AWS Tag Editor&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To add tags to all resources in one go, it can be done when initializing the stack (in the bin dir).&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; app &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; cdk&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;App&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; AwsCdkNamespaceDeploymentWebStack&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; `&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;cdk&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;Tags&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&apos;NAMESPACE&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;cdk&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;Tags&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&apos;APPLICATION_NAME&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; APPLICATION_NAME&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;gotcha-1--plan-to-use-hostedzonesfromlookup-env-needs-to-be-set-at-the-stack-level&quot;&gt;Gotcha #1 – Plan to use “HostedZones.fromLookup()“? env needs to be set at the stack level!!!&lt;/h3&gt;
&lt;p&gt;This seems like an anti-pattern and doesn’t follow the convention of most constructs however, it was easy enough to remedy using process.env vs hard coding values. These environment variables get set regardless of how the AWS account and Region are loaded&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; AwsCdkNamespaceDeploymentWebStack&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; CDK&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Stack&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;  constructor&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;scope&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Construct&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; props&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;?:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; CDK&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;StackProps&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;		super&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;scope&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;			env&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;				account&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; process&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;env&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;CDK_DEFAULT_ACCOUNT&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;				region&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; process&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;env&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;CDK_DEFAULT_REGION&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;			},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;		});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;gotcha-2-s3-bucket-naming-conventions&quot;&gt;Gotcha #2 S3 Bucket naming conventions&lt;/h3&gt;
&lt;p&gt;Since S3 bucket names are unique, they have a ton of &lt;a href=&quot;https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html&quot;&gt;restrictions on the bucket name&lt;/a&gt;. This should be ok, as the &lt;strong&gt;bucket name needs to match the domain name to be resolved by Route53&lt;/strong&gt; (not so much by CloudFront). If the final domain is infinity-mouse.example.com, then the bucket name should be infinity-mouse.example.com.&lt;/p&gt;
&lt;p&gt;The bucket name must also be lowercased.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; websiteBucket&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Bucket&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Bucket&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; `&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; S3 Bucket`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;			websiteIndexDocument&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &apos;index.html&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;			bucketName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; domainWithNamespace&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;toLowerCase&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;			removalPolicy&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; CDK&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;RemovalPolicy&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;DESTROY&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;			autoDeleteObjects&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;		});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;deploying-static-assets&quot;&gt;Deploying Static Assets&lt;/h3&gt;
&lt;p&gt;For client-side rendering SPAs and Static Websites, the assets will either be compiled into or worked on in a directory in the project. CDK makes this deployment easy through the BucketDeployment construct.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; bucketDeployment&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; BucketDeployment&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; BucketDeployment&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;			this&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;			`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; S3 Bucket Deployment`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;			{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;				sources&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;Source&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;asset&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&apos;./dist&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;				destinationBucket&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; websiteBucket&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;			}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;		);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;an-origin-access-identity-oai-must-be-created-to-grant-s3-bucket-read-access-to-cloudfront&quot;&gt;An Origin Access Identity (OAI) must be created to grant S3 bucket read access to CloudFront.&lt;/h3&gt;
&lt;p&gt;The OAI allows the S3 bucket to remain private while allowing exposure through the controls of CloudFront.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; originAccessIdentity&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; OriginAccessIdentity&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; OriginAccessIdentity&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;			this&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;			`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; CloudFront OAI`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;		);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;		// grant read rights to the S3 Bucket for the OAI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;		websiteBucket&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;grantRead&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;originAccessIdentity&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;apigateway-stagename-needs-to-be-set&quot;&gt;ApiGateway stageName needs to be set&lt;/h3&gt;
&lt;p&gt;In the ApiGateway documentation, it is stated that ApiGateway creates a “prod” stageName by default when creating a RestAPI. While this may be the case, errors were being thrown until this was set manually. This will need further review, however, this stage name could be anything as the environments are isolated from one another.&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; api &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; RestApi&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; `&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; Rest Api`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;			description&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; `API for &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; environment`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;			deployOptions&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;				stageName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &apos;prod&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;			},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;      restApiName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; NAMESPACE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;cloudfront-target-and-aaaa-record&quot;&gt;CloudFront Target and Aaaa record&lt;/h3&gt;
&lt;p&gt;For the solution to work properly the CloudFront distribution needs to be the next in command once a request reaches the hosted zone. The logic is saying, on the arrival of a request to this hosted zone (the root domain), send the request to an AWS service, in this case, CloudFront to handle routing, serving content, etc.&lt;/p&gt;
&lt;p&gt;The use of Aaaa record is to use the IPv6 address of CloudFormation, there are benefits to IPv6 all of which can easily be found in IPv4 vs IPv6 comparison articles.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;there-it-is&quot;&gt;There it is!&lt;/h2&gt;
&lt;p&gt;This exercise was fun, and a bit challenging to say the least. Hopefully, it proves to be a good starting point for someone looking for this style of deployment.&lt;/p&gt;
&lt;p&gt;The repository associated with this article will more than likely be refactored to clean it up a bit, possibly a neater file structure, the use of constructs, and or child stacks.&lt;/p&gt;
&lt;h3 id=&quot;show-me-the-code&quot;&gt;SHOW ME THE CODE!!!&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/kamauwashington/aws-cdk-namespaced-web-api&quot;&gt;https://github.com/kamauwashington/aws-cdk-namespaced-web-api&lt;/a&gt;&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>AWS</category><category>AWS CDK</category><category>CloudFront</category><category>javascript</category><category>Route53</category><category>SPA</category><category>Static Website</category><category>typescript</category><category>Website</category><category>Wildcard Certificate</category></item><item><title>Using AWS CDK with dotenv -r and TypeScript</title><link>https://www.kamauwashington.com/2022/09/06/using-aws-cdk-with-dotenv-r-and-typescript/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/06/using-aws-cdk-with-dotenv-r-and-typescript/</guid><description>Note, I prefer the node --require option of loading .env variables over importing or requiring in application code.</description><pubDate>Tue, 06 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Note, I prefer the node —require option of loading .env variables over importing or requiring in application code.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;tldr&quot;&gt;TLDR;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;install &lt;a href=&quot;https://www.npmjs.com/package/dotenv&quot;&gt;dotenv&lt;/a&gt; as a development dependency&lt;/li&gt;
&lt;li&gt;in the &lt;em&gt;cdk.json&lt;/em&gt; in the root of the project directory add the following in bold :
&lt;ul&gt;
&lt;li&gt;{ “app”: “npx ts-node &lt;strong&gt;-r dotenv/config&lt;/strong&gt; —prefer-ts-exts bin/&amp;#x3C;stack-name&gt;.ts” }&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;p&gt;When developing via AWS CDK it is easy to set environment variables on resources as they are being defined to be stored in AWS. However, in some repositories there is a need to set environment variables for the CDK Stack to use.&lt;/p&gt;
&lt;p&gt;This can be done from the command line, or by setting the environment variables for the user, terminal, or machine. However these options do not provide the convenience of &lt;a href=&quot;https://www.npmjs.com/package/dotenv&quot;&gt;dotenv&lt;/a&gt; especially when many environment variables are needed during local development (See my notes on dotenv for local only &lt;a href=&quot;https://www.kamauwashington.com/2022/09/05/preload-dotenv-dont-require-it/&quot;&gt;here&lt;/a&gt;)&lt;/p&gt;
&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;confirm that &lt;strong&gt;ts-node&lt;/strong&gt; can be found in the package.json of the AWS CDK project&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;it should be present as it is a part of &lt;em&gt;cdk init&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;if &lt;em&gt;ts-node&lt;/em&gt; is not present run &lt;strong&gt;npm install -D ts-node&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;npx&lt;/em&gt; can also be used&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;run &lt;strong&gt;npm install -D dotenv&lt;/strong&gt; for the current AWS CDK TypeScript project&lt;/li&gt;
&lt;li&gt;create a &lt;strong&gt;.env&lt;/strong&gt; file in the project root and add the necessary environment variables&lt;/li&gt;
&lt;li&gt;ensure the .env is added to the &lt;strong&gt;.gitignore&lt;/strong&gt; file as this should not be kept in the repository&lt;/li&gt;
&lt;li&gt;open the &lt;strong&gt;cdk.json&lt;/strong&gt; file&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;find the &lt;strong&gt;app&lt;/strong&gt; keyword&lt;/li&gt;
&lt;li&gt;add &lt;strong&gt;-r dotenv/config&lt;/strong&gt; right after &lt;strong&gt;npx ts-node&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;the end result should look like this :
&lt;ul&gt;
&lt;li&gt;“app”: “npx ts-node -r dotenv/config —prefer-ts-exts bin/&amp;#x3C;stack-name&gt;.ts”&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once this is done, the &lt;strong&gt;cdk&lt;/strong&gt; commands will utilize the environment variables from the .env file. This is a very convenient way to load environment variables using a nodejs standard that most developers know, without having to install additional libraries, clis, or custom scripts.&lt;/p&gt;
&lt;h3 id=&quot;before&quot;&gt;Before&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;json&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;json&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;21&lt;/span&gt;&lt;span&gt;22&lt;/span&gt;&lt;span&gt;23&lt;/span&gt;&lt;span&gt;24&lt;/span&gt;&lt;span&gt;25&lt;/span&gt;&lt;span&gt;26&lt;/span&gt;&lt;span&gt;27&lt;/span&gt;&lt;span&gt;28&lt;/span&gt;&lt;span&gt;29&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;31&lt;/span&gt;&lt;span&gt;32&lt;/span&gt;&lt;span&gt;33&lt;/span&gt;&lt;span&gt;34&lt;/span&gt;&lt;span&gt;35&lt;/span&gt;&lt;span&gt;36&lt;/span&gt;&lt;span&gt;37&lt;/span&gt;&lt;span&gt;38&lt;/span&gt;&lt;span&gt;39&lt;/span&gt;&lt;span&gt;40&lt;/span&gt;&lt;span&gt;41&lt;/span&gt;&lt;span&gt;42&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;npx ts-node --prefer-ts-exts bin/my-stack.ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;watch&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;**&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;exclude&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;README.md&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;cdk*.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;**/*.d.ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;**/*.js&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;tsconfig.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;package*.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;yarn.lock&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;node_modules&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;test&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:stackRelativeExports&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-rds:lowercaseDbIdentifier&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-lambda:recognizeVersionProps&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-lambda:recognizeLayerVersion&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-ec2:uniqueImdsv2TemplateName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:checkSecretUsage&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-iam:minimizePolicies&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-ecs:arnFormatIncludesClusterName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:validateSnapshotRemovalPolicy&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-s3:createDefaultLoggingPolicy&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-apigateway:disableCloudWatchRole&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:enablePartitionLiterals&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:target-partitions&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;aws&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;aws-cn&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;after&quot;&gt;After&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;json&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;json&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;21&lt;/span&gt;&lt;span&gt;22&lt;/span&gt;&lt;span&gt;23&lt;/span&gt;&lt;span&gt;24&lt;/span&gt;&lt;span&gt;25&lt;/span&gt;&lt;span&gt;26&lt;/span&gt;&lt;span&gt;27&lt;/span&gt;&lt;span&gt;28&lt;/span&gt;&lt;span&gt;29&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;31&lt;/span&gt;&lt;span&gt;32&lt;/span&gt;&lt;span&gt;33&lt;/span&gt;&lt;span&gt;34&lt;/span&gt;&lt;span&gt;35&lt;/span&gt;&lt;span&gt;36&lt;/span&gt;&lt;span&gt;37&lt;/span&gt;&lt;span&gt;38&lt;/span&gt;&lt;span&gt;39&lt;/span&gt;&lt;span&gt;40&lt;/span&gt;&lt;span&gt;41&lt;/span&gt;&lt;span&gt;42&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;npx ts-node -r dotenv/config --prefer-ts-exts bin/my-stack.ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;watch&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;**&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;exclude&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;README.md&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;cdk*.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;**/*.d.ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;**/*.js&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;tsconfig.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;package*.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;yarn.lock&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;node_modules&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;test&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:stackRelativeExports&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-rds:lowercaseDbIdentifier&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-lambda:recognizeVersionProps&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-lambda:recognizeLayerVersion&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-ec2:uniqueImdsv2TemplateName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:checkSecretUsage&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-iam:minimizePolicies&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-ecs:arnFormatIncludesClusterName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:validateSnapshotRemovalPolicy&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-s3:createDefaultLoggingPolicy&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/aws-apigateway:disableCloudWatchRole&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:enablePartitionLiterals&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@aws-cdk/core:target-partitions&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;aws&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;      &quot;aws-cn&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>AWS</category><category>-r</category><category>--require</category><category>AWS CDK</category><category>dotenv</category><category>nodejs</category><category>typescript</category></item><item><title>Avoid using ts-node when performance testing</title><link>https://www.kamauwashington.com/2022/09/05/avoid-using-ts-node-when-performance-testing/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/avoid-using-ts-node-when-performance-testing/</guid><description>Often times when performance testing, or comparing resource utilization (ex NodeJS vs Java), I have seen developers load testing, performance testing, and even one-off testing, with ts-node. ts-node is an awesome piece of kit…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;Often times when performance testing, or comparing resource utilization (ex NodeJS vs Java), I have seen developers load testing, performance testing, and even one-off testing, with ts-node. ts-node is an awesome piece of kit, but it is for rapid prototyping, watching, and development only, it is not a production runtime.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When performance testing, even one offs, use &lt;strong&gt;tsc my-nodejs-app.ts &amp;#x26;&amp;#x26; node my-node-app&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;simple-test-sample&quot;&gt;Simple test sample&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; numberArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;numberArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; heapUsed &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; Math&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;process&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;memoryUsage&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;heapUsed &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; cpuTime &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; Math&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;process&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;cpuUsage&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;user &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`Approximately &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;heapUsed&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;MB and &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;cpuTime&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;ms of CPU time was used by this process.`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;using-ts-node&quot;&gt;Using ts-node&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;text&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;text&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Approximately 61MB and 1466ms of CPU time was used by this process.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;using-tsc-indexts--node-index&quot;&gt;Using tsc index.ts &amp;#x26;&amp;#x26; node index&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;text&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;text&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Approximately 2MB and 25ms of CPU time was used by this process.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;heapUsed&lt;/em&gt; is the actual memory used during the execution of a Node process&lt;/li&gt;
&lt;li&gt;memoryUsage provides rss, heapTotal, heapUsed, external, arrayBuffers metrics as well&lt;/li&gt;
&lt;li&gt;the command &lt;em&gt;NODE_ENV=production tsc index.ts &amp;#x26;&amp;#x26; node index&lt;/em&gt; will ensure that your one off test mimics your production environment build and runtime&lt;/li&gt;
&lt;li&gt;this is an illustration, not an &lt;em&gt;accurate&lt;/em&gt; representation of resource usage
&lt;ul&gt;
&lt;li&gt;an APM + a performance test with sustained load over time will provide the most accurate information&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A great article that discusses NodeJS profiling &lt;a href=&quot;https://nodejs.org/en/docs/guides/simple-profiling&quot;&gt;here&lt;/a&gt;…&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>empirical data</category><category>javascript</category><category>nodejs</category><category>performance</category><category>performance testing</category><category>ts-node</category><category>typescript</category></item><item><title>AWS OpenSearch Search Request Logging</title><link>https://www.kamauwashington.com/2022/09/05/aws-opensearch-search-request-logging/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/aws-opensearch-search-request-logging/</guid><description>This one took some time to figure out, and the AWS documentation Monitoring audit logs in Amazon OpenSearch Service did not point directly to how to log inbound REST search requests to OpenSearch to…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;blockquote&gt;
&lt;p&gt;There may be rules and regulations for logging in your production environment, please consult with your web engineering, or infrastructure equivalent prior to logging in this fashion to prevent unwanted charges and or side-effects.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This one took some time to figure out, and the AWS documentation &lt;a href=&quot;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/audit-logs.html&quot;&gt;Monitoring audit logs in Amazon OpenSearch Service&lt;/a&gt; did not point directly to how to log inbound REST search requests to OpenSearch to CloudWatch. There wasn’t much on the web either in terms of discussions, or questions on the matter, so I hope this helps someone.&lt;/p&gt;
&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Enable &lt;strong&gt;Audit logs&lt;/strong&gt; on [your-domain] in the AWS Console, or via REST, or via CLI&lt;/li&gt;
&lt;li&gt;Create a CloudWatch log group, or choose an existing one.&lt;/li&gt;
&lt;li&gt;Choose an access policy that contains the appropriate permissions, or create a policy&lt;/li&gt;
&lt;li&gt;Add &lt;strong&gt;“logs:CreateLogStream”&lt;/strong&gt; to the $.Statement.Action of the policy if not present&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;If using an existing policy, consult with the owner of the policy to determine if this action is already present and or if a new policy should be created for you&lt;/li&gt;
&lt;/ul&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;In OpenSearch Dashboard go to the &lt;strong&gt;Security Plugin&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Configure &lt;strong&gt;General Settings&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Enable &lt;strong&gt;REST Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Disable &lt;strong&gt;FAILED_LOGIN, MISSING_PRIVILEGES, GRANTED_PRIVILEGES&lt;/strong&gt; &lt;em&gt;unless of-course these are needed&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Disable &lt;strong&gt;Transport layer&lt;/strong&gt; &lt;em&gt;unless of-course this is needed&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Enable &lt;strong&gt;Request Bod&lt;/strong&gt;y&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;It may be a good idea to ignore requests sent to &lt;strong&gt;_dashboard&lt;/strong&gt; or any other paths that should be excluded from the logs to prevent overflow&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;{$.audit_rest_request_path = &lt;em&gt;_search}&lt;/em&gt;&lt;/em&gt; is an easy way to find the incoming the requests, though adding a custom header may be a better option to ensure that only valid searches are being pulled in the resultset&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;sample-request-log-record&quot;&gt;Sample request log record&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;json&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;json&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;21&lt;/span&gt;&lt;span&gt;22&lt;/span&gt;&lt;span&gt;23&lt;/span&gt;&lt;span&gt;24&lt;/span&gt;&lt;span&gt;25&lt;/span&gt;&lt;span&gt;26&lt;/span&gt;&lt;span&gt;27&lt;/span&gt;&lt;span&gt;28&lt;/span&gt;&lt;span&gt;29&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;31&lt;/span&gt;&lt;span&gt;32&lt;/span&gt;&lt;span&gt;33&lt;/span&gt;&lt;span&gt;34&lt;/span&gt;&lt;span&gt;35&lt;/span&gt;&lt;span&gt;36&lt;/span&gt;&lt;span&gt;37&lt;/span&gt;&lt;span&gt;38&lt;/span&gt;&lt;span&gt;39&lt;/span&gt;&lt;span&gt;40&lt;/span&gt;&lt;span&gt;41&lt;/span&gt;&lt;span&gt;42&lt;/span&gt;&lt;span&gt;43&lt;/span&gt;&lt;span&gt;44&lt;/span&gt;&lt;span&gt;45&lt;/span&gt;&lt;span&gt;46&lt;/span&gt;&lt;span&gt;47&lt;/span&gt;&lt;span&gt;48&lt;/span&gt;&lt;span&gt;49&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_cluster_name&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;111111111111:test-cluster&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_rest_request_params&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;test&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_node_name&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;33b23f3f2e8e9607bc355b9c17cf21af&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_request_initiating_user&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;prototype-user&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_rest_request_method&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;GET&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_category&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;AUTHENTICATED&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_request_origin&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;REST&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_request_body&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;YOUR SEARCH FILTER WOULD BE FOUND HERE AS TEXT vs JSON OBJECT&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_node_id&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;4AIFR8dTTFuEkk9ce5Yd2w&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_request_layer&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;REST&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_rest_request_path&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;/test/_search&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;@timestamp&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;2022-04-14T02:24:05.539+00:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_request_effective_user_is_admin&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_format_version&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_request_remote_address&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;1.1.1.1&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_rest_request_headers&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Accept&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;*/*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Connection&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;close&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;User-Agent&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;PostmanRuntime/7.28.4&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Host&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;search-chdxDWWhtdv3d2jxenfkdjq.us-east-1.es.amazonaws.com&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Accept-Encoding&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;gzip, deflate, br&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Cache-Control&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;no-cache&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Postman-Token&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;4d2342ca-0d38-44e0-8da6-2d8fd85f65d9&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Content-Length&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;1721&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Content-Type&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;            &quot;application/json&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;    &quot;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;audit_request_effective_user&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;random-user&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>AWS</category><category>Cloudwatch</category><category>Logging</category><category>OpenSearch</category></item><item><title>Developer Ethos</title><link>https://www.kamauwashington.com/2022/09/05/developer-ethos/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/developer-ethos/</guid><description>During code review, triage, and early stages of sprint development we are so focused on the deliverables that we often forget that our commits, communication, teamwork, and ownership are more than simple roles and…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;During code review, triage, and early stages of sprint development we are so focused on the deliverables that we often forget that our commits, communication, teamwork, and ownership are more than simple roles and responsibilities… they are all part of our development culture.&lt;/p&gt;
&lt;hr&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;I will ABC (Always Be Collaborative).&lt;/strong&gt; Do not build in isolation. Collaborate with other developers working on an application, the end user, and the testers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will seek business requirement clarity.&lt;/strong&gt; Don’t imagine and build. If there is a lack of clarity – discuss, record then build towards the exact business requirement for acceptance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will document my code, for myself and my peers.&lt;/strong&gt; Self-documenting code is a myth. The best developers clearly and thoroughly explain each step of functionality, inputs, outputs, side effects, parameters, and often the reason why the functionality was introduced.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will remain focused.&lt;/strong&gt; Working on various applications in parallel is a surefire way to become overwhelmed, and potentially introduce one size fits all solutions that don’t scale. Tackle development serially in small, achievable blocks of time and code. Break larger tasks into smaller more manageable tasks whenever possible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will avoid shortcuts.&lt;/strong&gt; Shortcuts have proven over time to create defect pile-ups. While tasks may take longer and require more code, files, thought, and collaboration – doing it immediately with tenacity will spare you and your peers from unneeded and unwanted cycle waste.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will support the code that I author.&lt;/strong&gt; As a project grows by feature, fix, or refactor, a developer should take pride in the ownership of the code introduced, and support changes if a peer has difficulty achieving a desired result or if the code introduced requires enhancement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will author code with performance in mind.&lt;/strong&gt; All of the lazy loading, dynamic invocation, and tree-shaking in the world will not convert a slow application into a fast one on their own. Understand the common performance concerns of the architecture, framework, and language and code with these in mind.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will code defensively.&lt;/strong&gt; Remember Murphy’s Law : “Anything that can go wrong will go wrong”. Programming languages attempt to protect developers from mistakes, and ease complex implementations – but this is happy path. The opposite and or edge cases will happen.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will unit test my code.&lt;/strong&gt; Unit testing builds confidence for refactoring, maintenance, bug fixing, feature introduction, and can do all this locally before deployment. When issues are found, write unit tests for those cases as well. Remember, the tests are your safety net.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will refactor my code.&lt;/strong&gt; Refactoring is one of the best practices that all developers must have. Code in most cases can be improved after the first iteration. Upon completion of a deliverable, take the time to analyze what improvements can be made in the next iteration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will be an expert on the fundamentals.&lt;/strong&gt; Knowing the latest and greatest is fashionable, and adds more tooling to your problem-solving arsenal. However, knowledge of the fundamentals often negates the use of a new approach or framework – for something much simpler.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will be honest. I don’t know everything.&lt;/strong&gt; My code is not perfect. I need to learn more. I just don’t get “this”. I think we could collaborate better as a team. There were some issues with your last merge request. I feel that I am not performing at the same level as my peers. I’m awesome! It is healthy to be honest with yourself, your peers, and your leadership. It is just as healthy to accept honesty.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will raise awareness to problematic areas in a codebase.&lt;/strong&gt; Defects and bugs found during sanity testing and or in higher environments are often created by anti-patterns, lack of documentation, happy path programming, indecipherable lengthy implementation, etc. Notify the original/last commit author, and team, mark the offending areas with a //TODO:, and create a backlog item.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I will write clean, readable code.&lt;/strong&gt; Code is written for humans to read and understand. It is compiled for computers to understand. Taking shortcuts in the code to save a few characters or new lines is an anti-pattern that makes it more difficult for the next developer to read and maintain. “the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. …Therefore, making it easy to read makes it easier to write.” – Robert C. Martin&lt;/li&gt;
&lt;/ul&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>Practice and Principles</category></item><item><title>Double vs Triple Equals</title><link>https://www.kamauwashington.com/2022/09/05/double-vs-triple-equals/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/double-vs-triple-equals/</guid><description>A very common point of confusion in JS/TS programming is what double equals and triple equals actually do. While this is base JS functionality, the method by how it works is quite simple (maybe)…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;A very common point of confusion in JS/TS programming is what double equals and triple equals actually do. While this is base JS functionality, the method by how it works is quite simple (maybe) : &lt;em&gt;Type Coercion&lt;/em&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Because JavaScript is weakly typed, values can be easily converted from one type to another. &lt;strong&gt;Type Coercion&lt;/strong&gt; is the implicit conversion from one type to another. There are only three types of conversion in Javascript: to string, to boolean, to number, allowing for “sameness” or “equality checking”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Double Equals&lt;/strong&gt; checks for sameness and performs &lt;em&gt;Type Coercion&lt;/em&gt; before the equality operation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Triple Equals&lt;/strong&gt; checks for strict &lt;em&gt;type and value&lt;/em&gt; equality without performing &lt;em&gt;Type Coercion&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;js-sameness-double-equals-in-action&quot;&gt;JS Sameness (Double Equals) in action&lt;/h3&gt;
&lt;p&gt;it is easy to see the trouble one can get into&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;javascript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;\n\t\r\n&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;00000005&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;js-strict-equality-triple-equals-in-action&quot;&gt;JS Strict Equality (Triple Equals) in action&lt;/h3&gt;
&lt;p&gt;desired equality outcomes&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;javascript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;\n\t\r\n&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ===&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&quot;00000005&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ===&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ===&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // false&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;for-fun-a-brain-teaser&quot;&gt;For fun, a brain teaser!&lt;/h3&gt;
&lt;p&gt;WTNaN???&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;javascript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;!NaN&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;!NaN&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ===&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // true!?!?!?&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt; use ==, != when comparing a value or property with &lt;em&gt;null&lt;/em&gt; or &lt;em&gt;undefined&lt;/em&gt; (if allowed in linter), to ensure expected truthy/falsey with possibly null/undefined values&lt;/li&gt;
&lt;li&gt;Purists will suggest that double equals should never be used. However, this should be a configurable lint rule, deciding to use or not to use double equals by a team or enterprise.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Type Coercion&lt;/em&gt; is invoked by many of the other operators (beware) &amp;#x3C;,&amp;#x3C;=,&gt;,&gt;=,~,!,+,-,*
&lt;ul&gt;
&lt;li&gt;use typeof(x) === typeof(y) if you need to be certain beforehand&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If using defined types, in 99% of cases TS transpilation should fail and force correction (thus, use more TS and use it appropriately)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A great article that discusses &lt;em&gt;Type Coercion&lt;/em&gt; [here](&lt;a href=&quot;https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/#:~:text=Type%20coercion%20is%20the%20process,Symbol%20(added%20in%20ES6)%E2%80%A6&quot;&gt;https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/#:~:text=Type%20coercion%20is%20the%20process,Symbol%20(added%20in%20ES6)…&lt;/a&gt;&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>javascript</category><category>nodejs</category><category>type coercion</category><category>typescript</category></item><item><title>Enum Flags … are … awesome!</title><link>https://www.kamauwashington.com/2022/09/05/enum-flags-are-awesome/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/enum-flags-are-awesome/</guid><description>An often overlooked feature of most if not all languages is the concept of Enum Flags. In a nutshell, Enum Flags are an efficient way of storing and representing a collection of boolean values…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;An often overlooked feature of most if not all languages is the concept of Enum Flags. In a nutshell, Enum Flags are an efficient way of storing and representing a collection of boolean values with one value 😎. This applies to all SQL languages, C#, Java, Python, Rust, GoLang, the list goes on, and illustrated here in TypeScript.&lt;/p&gt;
&lt;p&gt;Is it possible to store if a field is required, read-only, and visible in the number 13?&lt;/p&gt;
&lt;h3 id=&quot;before&quot;&gt;Before&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;21&lt;/span&gt;&lt;span&gt;22&lt;/span&gt;&lt;span&gt;23&lt;/span&gt;&lt;span&gt;24&lt;/span&gt;&lt;span&gt;25&lt;/span&gt;&lt;span&gt;26&lt;/span&gt;&lt;span&gt;27&lt;/span&gt;&lt;span&gt;28&lt;/span&gt;&lt;span&gt;29&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;31&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;interface&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; IFieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isReadOnly &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; boolean&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isMasked &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; boolean&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isRequired &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; boolean&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isVisible &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; boolean&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isHeaderVisible &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; boolean&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;// fieldA should be readOnly, required, and visible&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; fieldARules &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; IFieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isReadOnly &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isMasked &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isRequired &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isVisible &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isHeaderVisible &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`the representation in JSON would be : &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;stringify&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;fieldARules&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;/*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;// the JSON representation will be -&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;  &quot;isReadOnly&quot;: true,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;  &quot;isMasked&quot;: false,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;  &quot;isRequired&quot;: true,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;  &quot;isVisible&quot;: true,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;  &quot;isHeaderVisible&quot;: false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`Is fieldA readOnly? &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;fieldARules&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isReadOnly&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; //true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`Is fieldA required? &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;fieldARules&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isRequired&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; //true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`Is fieldA visible? &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;fieldARules&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isVisible&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; //true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;after&quot;&gt;After&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;11&lt;/span&gt;&lt;span&gt;12&lt;/span&gt;&lt;span&gt;13&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;15&lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;17&lt;/span&gt;&lt;span&gt;18&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;enum&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    None &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isReadOnly &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // 1 &amp;#x3C;&amp;#x3C; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isMasked &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // 1 &amp;#x3C;&amp;#x3C; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isRequired &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // 1 &amp;#x3C;&amp;#x3C; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isVisible &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // 1 &amp;#x3C;&amp;#x3C; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    isHeaderVisible &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 16&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // 1 &amp;#x3C;&amp;#x3C; 4 try it! console.log(1&amp;#x3C;&amp;#x3C;4) prints 16&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#5C5A57;font-style:italic&quot;&gt;// fieldA should be readOnly, required, and visible&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; fieldARulesEnum &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isReadOnly &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isRequired &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isVisible&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`the representation in JSON would be : &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;fieldARulesEnum&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;/*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;// the JSON representation will be -&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;   13&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`Is fieldA readOnly? &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;fieldARulesEnum&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isReadOnly&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isReadOnly&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; //true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`Is fieldA required? &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;fieldARulesEnum&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isRequired&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isRequired&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; //true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`Is fieldA visible? &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;fieldARulesEnum&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isVisible&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRule&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;isVisible&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; //true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;bonus&quot;&gt;Bonus&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;sql&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;sql&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#17D0D8&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;    COUNT&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; FieldRules&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;WHERE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    fieldName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &apos;fieldA&apos;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; AND&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    fieldRule &amp;#x26; &lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; &amp;#x3C;&gt;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; -- testing for isRequired&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt; only use when necessary, and where applicable. While this functionality is cool, determine if a use case warrants it.&lt;/li&gt;
&lt;li&gt;Bitwise operations can be used to determine exclusivity, inclusivity, remove items, add items, if all flags are set, etc.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;INSIGHT&lt;/strong&gt; : Using the example above imagine a form with 40 fields and the JSON that would be needed if using booleans (200 fields), vs 40 with Enum Flags.
&lt;ul&gt;
&lt;li&gt;This is a very useful exercise when thinking about data transmission, slow connections, compression/decompression&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;It is not uncommon to see a set of enum flags defined across an enterprise or vertical and used between different languages and datastores supporting bitwise operations (which is most if not all)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Visit the &lt;a href=&quot;https://basarat.gitbook.io/typescript/type-system/enums&quot;&gt;TypeScript Deep Dive for Enums&lt;/a&gt; for more in-depth information!&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>enum flags</category><category>enums</category><category>javascript</category><category>nodejs</category><category>typescript</category></item><item><title>JS Array Functions Instead of Lodash</title><link>https://www.kamauwashington.com/2022/09/05/js-array-functions-instead-of-lodash/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/js-array-functions-instead-of-lodash/</guid><description>Lodash is an amazing library and is often used to minimize the hassle of working with arrays, numbers, objects, strings, etc. However, it is often seen that one of its primary uses is array…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;Lodash is an amazing library and is often used to minimize the hassle of working with arrays, numbers, objects, strings, etc. However, it is often seen that one of its primary uses is array manipulation, and accessing elements within.&lt;/p&gt;
&lt;p&gt;TypeScript (and ES6) have the majority this functionality built in!&lt;/p&gt;
&lt;p&gt;The example below illustrates the find method in Lodash vs TS/JS :&lt;/p&gt;
&lt;h3 id=&quot;before&quot;&gt;Before&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; _ &lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &apos;lodash&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; foundResult &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; _&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;workingArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;item &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; item &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;foundResult&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // prints 9&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;after&quot;&gt;After&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; foundResult &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; undefined&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;item &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; item &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;foundResult&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // prints 9&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;but-wait-i-may-need-an-index-as-well-as-the-individual-item&quot;&gt;But wait! I may need an index as well as the individual item!&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; foundResult &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; undefined&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;item &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; index &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; index &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; item &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;foundResult&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // prints 9&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Lodash comes at a cost in file size and performance (in many cases compared to native), consider existing functionality before importing Lodash&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt; While inline anonymous functions work in most cases for simple logic, opt for a separate &lt;em&gt;named&lt;/em&gt; function (imported from a separate file if warranted) to house complex manipulation/accessor logic promoting reuse and independent testability.&lt;/li&gt;
&lt;li&gt;The above is a simple example using &lt;em&gt;find&lt;/em&gt;, there are more methods available :
&lt;ul&gt;
&lt;li&gt;every, some, includes, filter, map, reduce, and more!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Visit the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods&quot;&gt;Array Prototype Documentation&lt;/a&gt; for more in-depth information!&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>arrays</category><category>javascript</category><category>lodash</category><category>nodejs</category><category>typescript</category></item><item><title>Literal Types / Type Guards</title><link>https://www.kamauwashington.com/2022/09/05/literal-types-type-guards/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/literal-types-type-guards/</guid><description>When using TypeScript, we often (and hopefully as a manner of practice, always) declare the type of a variable or property. TypeScript allows the possibility of a variable or property to be of one…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;When using TypeScript, we often (and hopefully as a manner of practice, always) declare the type of a variable or property. TypeScript allows the possibility of a variable or property to be of one or more types. This means that a variable or property can be either a number, string, or custom type by declaring it like so :&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; myVariable &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; ICustomType&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;The interesting part is that this can be done with primitives, mixed primitives, and types for variables, properties, and functions!&lt;/p&gt;
&lt;h3 id=&quot;example-1&quot;&gt;Example 1&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; TennisScore&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;love&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 40&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;all&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;deuce&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;ad-in&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;ad-out&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; score &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; TennisScore&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // allowed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;score &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;love&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // allowed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;score &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt;// Type &apos;1&apos; is not assignable to type &apos;TennisScore&apos;.(2322)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;example-2&quot;&gt;Example 2&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; CardinalDirection&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;North&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;South&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;East&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;West&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#17D0D8&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; whichWay&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; CardinalDirection&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;N&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // Type &apos;&quot;N&quot;&apos; is not assignable to type &apos;CardinalDirection&apos;.(2322)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h2 id=&quot;example-3&quot;&gt;Example 3&lt;/h2&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; CardinalDirection&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;North&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;South&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;East&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;West&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; CardinalDirectionShorthand&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;N&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;S&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;E&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;W&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#17D0D8&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; whichWay&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; CardinalDirection&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; CardinalDirectionShorthand&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;N&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // allowed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt; this feature is &lt;em&gt;NOT&lt;/em&gt; the same as runtime validation. This is often called “Narrowing” in scripting languages, which takes a loosely defined value, and &lt;em&gt;narrows&lt;/em&gt; the possibilities of types and or primitives to defined constraints.&lt;/li&gt;
&lt;li&gt;Literal types can be used to return one or more types, or pass one or more types of values to and from a function.&lt;/li&gt;
&lt;li&gt;There is awesome intellisense support, notifying individuals working in a codebase what values or types are possible.&lt;/li&gt;
&lt;li&gt;if..then logic will only allow comparison with values or types defined (with intellisense guidance as well). The exception is the else statement.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Visit the &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types&quot;&gt;TypeScript Documentation on Literal Types&lt;/a&gt; for more in-depth information!&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>javascript</category><category>nodejs</category><category>typescript</category></item><item><title>Preload dotenv, don’t require it</title><link>https://www.kamauwashington.com/2022/09/05/preload-dotenv-dont-require-it/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/preload-dotenv-dont-require-it/</guid><description>Oh my dotenv! If you work with NodeJS at some point you will use and implement dotenv. It loads environment variables from a .env file (configurable name btw), into process.env, following the 12 Factor…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;Oh my dotenv! If you work with NodeJS at some point you will use and implement dotenv. It loads environment variables from a .env file (configurable name btw), into process.env, following the 12 Factor App methodology of separating environment from code. Pure awesomeness, and makes development configuration loading a breeze mimicking your higher environments.&lt;/p&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;An important note. &lt;strong&gt;Do not use .env in production!&lt;/strong&gt; Your environments be them containers, RHEL, Cloud, CentOS, Windows Server are responsible for and capable of providing environment variables without &lt;em&gt;dotenv&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;p&gt;Given the above, one should also avoid bundling dotenv as a dependency, exclude configuration in the codebase, and &lt;strong&gt;consider dotenv a developer workstation tool only&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;avoid&quot;&gt;Avoid&lt;/h3&gt;
&lt;p&gt;dotenv documentation states “&lt;em&gt;As early as possible in your application, require and configure dotenv.&lt;/em&gt;“. To avoid loading in higher environments, most perform the following check :&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;javascript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; 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;color:#17D0D8&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;process&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;env&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;node_env &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &apos;development&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; require&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;&apos;dotenv&apos;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; //this should not be in your code&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;better&quot;&gt;Better&lt;/h3&gt;
&lt;p&gt;Preload dotenv in your start script, by installing dotenv as a development dependency npm install —save-dev dotenv and using Node’s built in -r flag to preload the configuration module from dotenv, you no longer have to _require(‘dotenv’).config() in your code!&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;bash&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-file&quot;&gt;shell&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body no-gutter&quot;&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line sh-cmd&quot;&gt;&lt;span class=&quot;sh-prompt&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; node&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; -r&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; dotenv/config&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; your_script.js&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;but-i-use-ts-node&quot;&gt;But I use ts-node!!!&lt;/h3&gt;
&lt;p&gt;Node’s -r flag applies to ts-node as well!&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;bash&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-file&quot;&gt;shell&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body no-gutter&quot;&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;$ts-node -r dotenv/config your_script.ts&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;It is still possible to use the &lt;em&gt;dotenv_config_path&lt;/em&gt; argument using the preload method, for custom file name and path&lt;/li&gt;
&lt;li&gt;this should be placed in your npm scripts section in &lt;em&gt;start&lt;/em&gt; or even better &lt;em&gt;start-dev&lt;/em&gt;
&lt;ul&gt;
&lt;li&gt;important note, avoid using &lt;em&gt;npm start&lt;/em&gt; in production (though Heroku and similar services do for simplicity and workflow), best practice is to use the NodeJS binary instead of the package manager like so &lt;em&gt;node my-app.js&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>12-factor</category><category>dotenv</category><category>javascript</category><category>nodejs</category><category>ts-node</category><category>typescript</category></item><item><title>Template Literals</title><link>https://www.kamauwashington.com/2022/09/05/template-literals/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/template-literals/</guid><description>In many TS code-reviews one may find string concatenation (with the addition operator) used to assemble a message, query, multi-line comments, or other manners of literals. While this gets the job done, it is…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;In many TS code-reviews one may find string concatenation (with the addition operator) used to assemble a message, query, multi-line comments, or other manners of literals. While this gets the job done, it is often prone to error, difficult to read, and in many cases tedious to refactor and or implement.&lt;/p&gt;
&lt;p&gt;TypeScript (and ES6) added Template Literals, and the TS/JS world couldn’t be happier!&lt;/p&gt;
&lt;h3 id=&quot;before&quot;&gt;Before&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; firstName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Jane&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; lastName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;  string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Smith&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; message &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;A person with the first name of &quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; firstName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot; and the last name of &quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; lastName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot; says hello!&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // A person with the first name of Jane and the last name of Smith says hello!&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;after&quot;&gt;After&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; firstName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Jane&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; lastName &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;  string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; &quot;Smith&quot;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; message &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; `A person with the first name of &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;firstName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; and the last name of &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;lastName&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; says hello!`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // A person with the first name of Jane and the last name of Smith says hello!&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;this “magic” happens when backticks are used to encapsulate a string vs ticks or quotes&lt;/li&gt;
&lt;li&gt;you can use full expressions inside of the &lt;code&gt;${expresssion|variable-name}&lt;/code&gt; directive&lt;/li&gt;
&lt;li&gt;use the backtick to create multi-line strings as well without using newline and the addition operator&lt;/li&gt;
&lt;li&gt;TS Template Literals are &lt;em&gt;POWERFUL&lt;/em&gt; much more than is shown above!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Visit the &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html&quot;&gt;TypeScript Documentation for Template Literals&lt;/a&gt; for more in-depth information!&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>interpolation</category><category>javascript</category><category>nodejs</category><category>templating</category><category>typescript</category></item><item><title>The awesome Readonly&lt;T&gt; Utility Type</title><link>https://www.kamauwashington.com/2022/09/05/the-awesome-readonlyt-utility-type/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/the-awesome-readonlyt-utility-type/</guid><description>Lets all be honest, side effects are terrible. They are especially terrible when working with a runtime that doesn’t implicitly discern between mutable and immutable pre transpilation or compilation (like JavaScript). I have often…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;Lets all be honest, side effects are terrible. They are especially terrible when working with a runtime that doesn’t implicitly discern between mutable and immutable pre transpilation or compilation (like JavaScript). I have often found functions during code review where an Array or Object is manipulated in the subroutine returning the input Array or Object. Ideally, functions should remain pure.&lt;/p&gt;
&lt;p&gt;A pure function is a function where the return value is only determined by its input values, without observable side effects…&lt;/p&gt;
&lt;p&gt;So how do we enforce immutability pre-transpilation? Is that even possible in TypeScript?&lt;/p&gt;
&lt;h3 id=&quot;before&quot;&gt;Before&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; myNumArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#17D0D8&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; removeFirstItem&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;inArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    inArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;shift&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; inArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;removeFirstItem&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;myNumArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;myNumArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // prints [1, 2, 3, 4, 5]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;after-will-not-compile-enforcing-pure-function-practice&quot;&gt;After (will not compile enforcing pure function practice)&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; myNumArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#17D0D8&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; removeFirstItem&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;inArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Readonly&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Readonly&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    inArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;shift&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;();&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // Property &apos;shift&apos; does not exist on type &apos;readonly number[]&apos;.(2339)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; inArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;removeFirstItem&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;myNumArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;myNumArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;after-pt-2&quot;&gt;After pt. 2&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;7&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; myNumArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&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;color:#17D0D8&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; removeFirstItem&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;inArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Readonly&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Readonly&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; numArrayOut &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; inArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;concat&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;();&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; // returns new mutable array;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;    numArrayOut&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;shift&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; numArrayOut&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#5C5A57;font-style:italic&quot;&gt; //implicit cast to Readonly&amp;#x3C;T&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;removeFirstItem&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;myNumArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;myNumArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; prints &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; untouched&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt; The above code WILL RUN (in the “After” block), but if using TypeScript it will not compile&lt;/li&gt;
&lt;li&gt;The use of Readonly as a return value of a pure function is a design choice not a mandate. It is however good practice as it requires any chained operations or next use to explicitly create a new mutable instance to work with, further enforcing pure functions principles.&lt;/li&gt;
&lt;li&gt;This is very useful in function type definitions as it explicitly requires that the implementation be pure.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Visit the &lt;a href=&quot;https://basarat.gitbook.io/typescript/type-system/readonly&quot;&gt;TypeScript Deep Dive for Readonly&lt;/a&gt; for more in-depth information.&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>nodejs</category><category>pure functions</category><category>typescript</category><category>utility types</category></item><item><title>Use for..of in lieu of traditional incremental for loops when appropriate</title><link>https://www.kamauwashington.com/2022/09/05/use-for-of-in-lieu-of-traditional-incremental-for-loops-when-appropriate/</link><guid isPermaLink="true">https://www.kamauwashington.com/2022/09/05/use-for-of-in-lieu-of-traditional-incremental-for-loops-when-appropriate/</guid><description>For loops are for loops, sure. There is absolutely nothing wrong with the traditional incremental iterator approach. However, this recommendation is about readability, maintenance, decreasing code smells, as well as best practice when using…</description><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;hr&gt;
&lt;p&gt;For loops are for loops, sure. There is absolutely nothing wrong with the traditional incremental iterator approach. However, this recommendation is about readability, maintenance, decreasing code smells, as well as best practice when using loops in TypeScript (and in this case ES6+ as well). Often during code review, one will find references to an item via its parent array and index numerous times (think myArray[i].firstName and guess how lastName is accessed? 😞). It is a best practice in most if not all languages to create a variable out of this value accessed by index once.&lt;/p&gt;
&lt;p&gt;The example below illustrates for..of in TS and ES6+&lt;/p&gt;
&lt;h3 id=&quot;before&quot;&gt;Before&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;workingArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;++)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;  console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;workingArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;after&quot;&gt;After&lt;/h3&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; item &lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;  console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;item&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;but-wait-i-may-need-an-index-as-well-as-the-individual-item&quot;&gt;But wait! I may need an index as well as the individual item!&lt;/h3&gt;
&lt;p&gt;it is so awesome that someone thought about this beforehand!&lt;/p&gt;
&lt;figure class=&quot;code&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;code-head&quot;&gt;&lt;div class=&quot;code-id&quot;&gt;&lt;span class=&quot;code-lang&quot;&gt;typescript&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;code-body&quot;&gt;&lt;div class=&quot;code-gutter&quot; aria-hidden=&quot;true&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code kamau-ink&quot; style=&quot;background-color:#131313;color:#E8E6E1; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt; Array&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;number&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FF826F&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;item&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#17D0D8&quot;&gt; of&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt; workingArray&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;entries&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;())&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;  console&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#EACC56&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;item&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt; has an index of &lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E8E6E1&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#8BD28D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B9B6B0&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;for..of&lt;/em&gt; and &lt;em&gt;for..in&lt;/em&gt; are &lt;strong&gt;not&lt;/strong&gt; the same. for..of is for items in an array, &lt;em&gt;for..in&lt;/em&gt; is for properties of an object. It is syntactically confusing when said aloud admittedly 😕&lt;/li&gt;
&lt;li&gt;utilize &lt;em&gt;const&lt;/em&gt; and &lt;em&gt;let&lt;/em&gt; keywords where appropriate, to explicitly define mutability pre-transpilation, and prevent simple coding mistakes.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;for..of&lt;/em&gt; gets transpiled to the incremental iterator style when targeting &amp;#x3C; ES6 for backward compatibility&lt;/li&gt;
&lt;li&gt;Why use &lt;em&gt;for..of&lt;/em&gt; over &lt;em&gt;Array.prototype.forEach&lt;/em&gt;? Each has its own use case. However, at the time of authoring, it is not possible to stop or break a forEach without throwing an exception which is not always desirable.
&lt;ul&gt;
&lt;li&gt;It is important to note that using await in a loop, only works in sequence with &lt;em&gt;for..of&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;INSIGHT&lt;/strong&gt;: A reverse iterator is missing IMHO, as it is not always desirable to reverse an array then loop. A reverse iterator would be the equivalent of traditional for loops with i–;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Visit the &lt;a href=&quot;https://basarat.gitbook.io/typescript/future-javascript/for...of&quot;&gt;TypeScript Deep Dive on for..of&lt;/a&gt; for more in-depth information!&lt;/p&gt;</content:encoded><dc:creator>Kamau Washington</dc:creator><category>TypeScript Bits</category><category>javascript</category><category>nodejs</category><category>typescript</category></item></channel></rss>