{"path": "/posts/even-more-ideas-with-types-and-javascript-equality-ordering-and-enumeration/", "title": "even more ideas with types and javascript (equality, ordering and enumeration) · Bruno Dias", "html": "<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <meta name=\"description\" content=\"even more ideas with types and javascript (equality, ordering and enumeration)\">\n  <title>even more ideas with types and javascript (equality, ordering and enumeration) · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/even-more-ideas-with-types-and-javascript-equality-ordering-and-enumeration/\" data-post-metadata>\n  <meta name=\"author\" content=\"Bruno Dias\" data-post-metadata>\n  <meta property=\"og:type\" content=\"article\" data-post-metadata>\n  <meta property=\"og:title\" content=\"even more ideas with types and javascript (equality, ordering and enumeration)\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/even-more-ideas-with-types-and-javascript-equality-ordering-and-enumeration/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2023-03-19T15:35:28Z\" data-post-metadata>\n  <meta property=\"og:image\" content=\"https://diasbruno.github.io/images/ideas-with-types.png\" data-post-metadata>\n  <link rel=\"icon\" href=\"/assets/site/signature.svg\" type=\"image/svg+xml\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"diasbruno\" href=\"/atom.xml\">\n  <link rel=\"alternate\" type=\"application/json\" href=\"/posts/even-more-ideas-with-types-and-javascript-equality-ordering-and-enumeration/content.json\">\n  <link rel=\"stylesheet\" href=\"/assets/site/tokens.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/site.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/footer.css\">\n\n</head>\n<body class=\"light\">\n<div id=\"site-page\">\n<a class=\"skip-link\" href=\"#content\">Skip to content</a>\n<header class=\"site-header\">\n  <a class=\"brand\" href=\"/\" data-site-navigation aria-label=\"Bruno Dias, home\">\n    <img src=\"/assets/site/signature.svg\" width=\"24\" height=\"34\" alt=\"\">\n  </a>\n  <nav aria-label=\"Main navigation\">\n    <ul>\n<li><a class=\"nav-vrn\" href=\"/vrn/\" data-site-navigation><span class=\"nav-label\">vrn</span> <span class=\"nav-badge\">research</span></a></li>\n<li><a class=\"nav-posts\" href=\"/posts/\" data-site-navigation aria-current=\"location\">Posts</a></li>\n<li><a class=\"nav-projects\" href=\"/projects/\" data-site-navigation>Projects</a></li>\n<li><a class=\"nav-about\" href=\"/about/\" data-site-navigation>About</a></li>\n</ul>\n  </nav>\n</header>\n\n<main class=\"section\" id=\"content\" tabindex=\"-1\">\n  <article class=\"post-detail\">\n    <header class=\"section-header\">\n      <p class=\"post-category\">Post</p>\n      <h1>even more ideas with types and javascript (equality, ordering and enumeration)</h1>\n\n      <p><time datetime=\"2023-03-19T15:35:28Z\">2023-03-19</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<p>more interesing stuff with types\nand javascript. let's see <code>equality</code>, <code>ordering</code> and <code>enumeration</code>.</p>\n<p>every application has some types that\ndoesn't have much behaviors. they mostly just\nrepresent something. for example: currencies.</p>\n<p>the accepted currencies are: <code>USD</code>, <code>BRL</code> and <code>JPY</code>.</p>\n<p>we could use an <code>enum</code> to make our lives easier.</p>\n<pre><code class=\"language-js\">enum Currency {\n  USD = 'USD',\n  BRL = 'BRL',\n  JPY = 'JPY'\n}\n\n{\n  &quot;currency&quot;: Currency.JPY,\n  &quot;amount&quot;: 10\n}\n</code></pre>\n<p>this is better than using raw strings all over the place\nto avoid typos and forgetting if a currency is valid.</p>\n<p>these is no much we can do with them, they only help\nif we are doing equality.</p>\n<pre><code class=\"language-js\">object.currency === Currency.JPY\n</code></pre>\n<p>...and nothing more.</p>\n<p>let's say we want to put them in order by currency.\nwe would need to do it by hand and it's terrible.</p>\n<h4>equality</h4>\n<p>this is the easy one.</p>\n<pre><code class=\"language-js\">const USD = new (function USD() {});\nconst BRL = new (function BRL() {});\nconst JPY = new (function JPY() {});\n</code></pre>\n<p>this will allow us to attach behaviors to the objects.</p>\n<pre><code class=\"language-js\">USD === USD;\n// true\n\nUSD === BRL;\n// false\n\n// if we want to follow fantasy land specification.\n\nfunction currencyEq(b) {\n  return this === b;\n};\n\nUSD.equals = currencyEq;\nBRL.equals = currencyEq;\nJPY.equals = currencyEq;\n\nUSD.equals(JPY);\n// false\n\nUSD.equals(USD);\n// true\n</code></pre>\n<p>Still didn't change much, but we now have the hability\nto improve your simple types.</p>\n<h4>enumeration</h4>\n<p>we don't get really enumeration from our <code>enum</code> types,\nbut now the can implement ourself!</p>\n<pre><code class=\"language-js\">USD.toEnum = () =&gt; 0;\nBRL.toEnum = () =&gt; 1;\nJPY.toEnum = () =&gt; 2;\n\nconst Currency = {\n  fromEnum(x) {\n    switch(x) {\n    case 0: return USD;\n    case 1: return BRL;\n    case 2: return JPY;\n    default: { throw new Error(`no current for ${x}.`); }\n    }\n  }\n};\n\n// nice properties!\nCurrency.fromEnum(currency.toEnum()) === currency;\n// true\n\n// now can do weird stuff...\n\nfunction cycle(n, fn) {\n  let x = 0;\n  return () =&gt; fn(x++ % n);\n}\n\nconst currenciesCycle = cycle(3, Currency.fromEnum);\n\ncurrenciesCycle();\n// USD\ncurrenciesCycle();\n// BRL\ncurrenciesCycle();\n// JPY\ncurrenciesCycle();\n// USD\ncurrenciesCycle();\n// BRL\ncurrenciesCycle();\n// JPY\n...\n</code></pre>\n<h4>ordering</h4>\n<p>ordering is also an interesting operation that people\ndon't think about.</p>\n<pre><code class=\"language-js\">// in this example, we are going to\n// use the inverse of enum.\n// so, the order will be: USD, BRL, JPY\nfunction currencyOrd(b) {\n  return this.toEnum() &gt; b.toEnum();\n}\n\nUSD.lte = currencyOrd;\nBRL.lte = currencyOrd;\nJPY.lte = currencyOrd;\n\nconst Currency = {\n  // ...\n  sortAsc(a, b) {\n    return -b.lte(a);\n  },\n  sortDesc(a, b) {\n    return -a.lte(b);\n  }\n};\n\n// sorting descendent\n[JPY, USD, BRL, JPY, BRL].sort(Currency.sortDesc);\n// [JPY, JPY, BRL, BRL, USD]\n\n// sorting ascendent\n[JPY, USD, BRL, JPY, BRL].sort(Currency.sortAsc);\n// [USD, BRL, BRL, JPY, JPY]\n</code></pre>\n<p>the complete example can be found at <a href=\"https://github.com/diasbruno/equality-enum-order-with-javascript\">diasbruno/equality-enum-order-with-javascript at github</a>.</p>\n\n      </section>\n      <aside class=\"post-links\" aria-labelledby=\"post-links-heading\">\n        <h2 id=\"post-links-heading\">Links</h2>\n        <ul>\n<li><a href=\"/posts/\" data-site-navigation>All posts</a></li>\n<li><a href=\"/posts/some-interesting-ideas-with-types-and-javascript/\" data-site-navigation>some interesting ideas with types and javascript</a></li>\n        </ul>\n      </aside>\n    </div>\n  </article>\n</main>\n\n<footer class=\"site-footer\">\n  <div class=\"site-footer-inner\">\n    <a class=\"site-footer-home\" href=\"/\" data-site-navigation>Bruno Dias</a>\n    <ul aria-label=\"Footer links\">\n      <li><a href=\"https://github.com/diasbruno\">GitHub</a></li>\n      <li><a href=\"/atom.xml\">Atom feed</a></li>\n    </ul>\n  </div>\n</footer>\n\n</div>\n<script type=\"module\" src=\"/assets/site/navigation.js\"></script>\n</body>\n</html>\n"}
