<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Be a Better Developer</title>
    <description>Practical notes on development</description>
    <link>http://www.bebetterdeveloper.com/</link>
    <atom:link href="http://www.bebetterdeveloper.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Thu, 24 Mar 2016 11:45:33 -0700</pubDate>
    <lastBuildDate>Thu, 24 Mar 2016 11:45:33 -0700</lastBuildDate>
    <generator>Jekyll v3.0.3</generator>
    
      <item>
        <title>Creating multilingual website w/ React &amp; Redux</title>
        <description>&lt;p&gt;Here is another post in series about React. This time I’m exploring Redux. I assume you know what Redux is already (otherwise, read &lt;a href=&quot;redux.js.org&quot;&gt;redux.js.org&lt;/a&gt; to learn about it). If you haven’t read my blog post &lt;a href=&quot;http://www.bebetterdeveloper.com/coding/getting-started-react-flux.html&quot;&gt;Creating multilingual website w/ React &amp;amp; Flux&lt;/a&gt; - I’d recommend you go through it, because I will be doing exactly the same thing here but using Redux instead of Flux.
&lt;img src=&quot;/img/post_img/rre-1.png&quot; alt=&quot;React and Redux logo&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;my-target&quot;&gt;My target&lt;/h2&gt;

&lt;p&gt;I want to create a simple multilingual website. I need to have:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;source structure and process suitable for mid-sized project&lt;/li&gt;
  &lt;li&gt;fast update of UI on language change (desirably without page refresh)&lt;/li&gt;
  &lt;li&gt;easy testable solution&lt;/li&gt;
  &lt;li&gt;explore Redux&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ll use ES6 where it is suitable, I automate dev process using Gulp.&lt;/p&gt;

&lt;h2 id=&quot;result&quot;&gt;Result&lt;/h2&gt;

&lt;p&gt;The main purpose of this project is to get my hands on Redux, thus the output is not that important, but you can check the result &lt;a href=&quot;http://Lenabarinova.github.io/react-redux-example/&quot;&gt;here&lt;/a&gt;. This is how it looks.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/out.gif&quot; alt=&quot;Switch language&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Two menus: left and right - left scrolls to different anchors in html, the right menu switches the language.&lt;/p&gt;

&lt;p&gt;Working code in &lt;a href=&quot;https://github.com/LenaBarinova/react-redux-example&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;clone--install&quot;&gt;Clone &amp;amp; install&lt;/h2&gt;

&lt;p&gt;Just download the code and run from within &lt;em&gt;react-redux-example&lt;/em&gt; directory&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are using Visual Studio Code - you can build it using &lt;code&gt;⇧⌘B&lt;/code&gt; (on Mac), otherwise use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$gulp build
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you don not have Gulp installed on your machine, you should install it globally before moving forward:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install gulp -g
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To view a result on your local machine, just open &lt;em&gt;index.html&lt;/em&gt; from &lt;em&gt;react-redux-example/dist&lt;/em&gt; folder in a browser.&lt;/p&gt;

&lt;h2 id=&quot;code-walk-through&quot;&gt;Code walk-through&lt;/h2&gt;

&lt;p&gt;Let me guide you through this example step by step. Here is a list of actions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-redux.html#step-1-setting-development-environment&quot;&gt;Setting development environment&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-redux.html#step-2-supportive-functions&quot;&gt;Supportive functions&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-redux.html#step-3-ui-components&quot;&gt;UI components&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-redux.html#step-4-starting-point&quot;&gt;Starting point&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-redux.html#step-5-redux&quot;&gt;Redux&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;step-1-setting-development-environment&quot;&gt;Step 1. Setting development environment&lt;/h2&gt;

&lt;p&gt;I use &lt;a href=&quot;https://code.visualstudio.com/&quot;&gt;Visual Studio Code&lt;/a&gt; as my IDE. For development tasks such as run tests, build, deploy - I use &lt;a href=&quot;http://gulpjs.com/&quot;&gt;gulp&lt;/a&gt;, you can check my tasks configuration in &lt;em&gt;react-redux-example/gulpfile.js&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;step-2-supportive-functions&quot;&gt;Step 2. Supportive functions&lt;/h2&gt;

&lt;p&gt;On my website I want to show the content in three languages. I have created &lt;em&gt;data/content.json&lt;/em&gt; file to hold the text in structured form so I could manipulate it easily later. This is my simplified ‘database’.&lt;/p&gt;

&lt;p&gt;To retrieve data from my ‘database’ (I mean from my json) I have created a fake API function in &lt;em&gt;api.js&lt;/em&gt;. Later on I’ll just call my API to get data. It is as simple as this:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/9b0c04120dd196f5f23d.js&quot;&gt;&lt;/script&gt;
This is the place you should put a call to your real API or database to retrieve your actual data.&lt;/p&gt;

&lt;h2 id=&quot;step-3-ui-components&quot;&gt;Step 3. UI components&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/rre-3.png&quot; alt=&quot;Switch language&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;My UI consist of very few simple components: it’s a couple of containers to show text and a menu to navigate through that text as well as change the language of the text being shown.&lt;/p&gt;

&lt;p&gt;For that I have created 4 React components: &lt;strong&gt;Menu, About, Home&lt;/strong&gt; and &lt;strong&gt;Page&lt;/strong&gt;. In the picture above, you can see my components tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Home&lt;/strong&gt; and &lt;strong&gt;About&lt;/strong&gt; components are only for information display purpose, they do not have any logic inside, they just get data through &lt;code&gt;props&lt;/code&gt; and simply show it.&lt;/p&gt;

&lt;p&gt;This is how &lt;em&gt;home.jsx&lt;/em&gt; looks like:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/a91a9a9d2f23963f3a7d.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And here I have my &lt;em&gt;about.jsx&lt;/em&gt;:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/217c8c918a449ee4c500.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Menu&lt;/strong&gt; component also gets content through &lt;code&gt;props&lt;/code&gt;, but in addition to that, it gets the function that should be called on language switch.
&lt;script src=&quot;https://gist.github.com/LenaBarinova/3b086bd93c3628000fa3.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;To bring these components together I have a wrapper component &lt;strong&gt;Page&lt;/strong&gt;. It’s job is to create &lt;strong&gt;Home, Menu and About&lt;/strong&gt; and pass correct data to each of these components. &lt;strong&gt;Page&lt;/strong&gt; itself gets all the content it shares to child components, through &lt;code&gt;props&lt;/code&gt; as well. I’ll show code for the &lt;strong&gt;Page&lt;/strong&gt; in &lt;a href=&quot;/coding/getting-started-react-redux.html#step-5-redux&quot;&gt;Step 5. Redux&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;step-4-bringing-it-all-together&quot;&gt;Step 4. Bringing it all together&lt;/h2&gt;

&lt;p&gt;So I have a very simple almost empty &lt;em&gt;index.html&lt;/em&gt; with a placeholder &lt;code&gt;div&lt;/code&gt; &lt;em&gt;page&lt;/em&gt; defined, which later will hold my react component.
&lt;script src=&quot;https://gist.github.com/LenaBarinova/451160ffc734da7e4819.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Finally, I have a &lt;em&gt;app.jsx&lt;/em&gt; file. The content of which I’m gonna explain in the next step (&lt;a href=&quot;/coding/getting-started-react-redux.html#step-5-redux&quot;&gt;Step 5. Redux&lt;/a&gt;), since it involves Redux. Basically in this file I render my &lt;strong&gt;Page&lt;/strong&gt; react component into my &lt;em&gt;page&lt;/em&gt; &lt;code&gt;div&lt;/code&gt; and pass data to it.&lt;/p&gt;

&lt;h2 id=&quot;step-5-redux&quot;&gt;Step 5. Redux&lt;/h2&gt;

&lt;p&gt;After all preparations are done, we can move forward and start with Redux.&lt;/p&gt;

&lt;p&gt;For Redux in this example I use two libraries: Redux and React-Redux. If you haven’t done it already - go ahead and install them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install redux react-redux --save
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is what I want to achieve: when new language is selected in the &lt;strong&gt;Menu&lt;/strong&gt; component, action is invoked and the state is changed (to hold a content in different language) and the new content from a state is being passed to &lt;strong&gt;Page&lt;/strong&gt; and from there down to all children.&lt;/p&gt;

&lt;p&gt;Here is how I would do that using Flux.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/rre-4.png&quot; alt=&quot;Redux flow&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The flow looks similar when using Redux with some differences: Flux Store’s role is covered by Reducer, there is no Dispatcher and no support for multiple stores in Redux.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/rre-2.png&quot; alt=&quot;Redux flow&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Let’s map this diagram items with my code.&lt;/p&gt;

&lt;p&gt;In my &lt;em&gt;actions.js&lt;/em&gt; file I have one action, named &lt;em&gt;switchLanguage&lt;/em&gt;, it holds &lt;em&gt;action_type&lt;/em&gt; and a property &lt;em&gt;language&lt;/em&gt; (to pass the value of user’s selected language for the content to be shown):
&lt;script src=&quot;https://gist.github.com/LenaBarinova/63e98ba22af870ee6875.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And &lt;em&gt;action_type&lt;/em&gt; is a simple constant defined in &lt;em&gt;action_types.js&lt;/em&gt; file
&lt;script src=&quot;https://gist.github.com/LenaBarinova/8409357b2c008959d082.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Then, in &lt;em&gt;reducer.js&lt;/em&gt; file I have defined initial state named &lt;em&gt;initialState&lt;/em&gt; for my app - it holds a text in default language (English) retrieved from my database. And  I have a function named &lt;em&gt;reducer&lt;/em&gt;, that returns me a new state according to the given action. So if &lt;em&gt;reducer&lt;/em&gt; is called with &lt;em&gt;switchLanguage&lt;/em&gt; action as a parameter, it will return me the content of the page in a language provided as a property in action.
&lt;script src=&quot;https://gist.github.com/LenaBarinova/b67e7022ab514d333324.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Now we have an initial state of the app, the description of state’s mutation (action) and a function to handle this mutation (reducer). Let’s move on to the presentation part.&lt;/p&gt;

&lt;p&gt;As I mentioned before, I have &lt;em&gt;app.jsx&lt;/em&gt; as an entry point of my app. If I were not using Redux, I would simply render my &lt;strong&gt;Page&lt;/strong&gt; component into my &lt;code&gt;page&lt;/code&gt; div like this:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/181d67b6f61064e92865.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Since I’m using Redux, I need to do a bit more here. First, I need to create complete state of my app (usually called store). For this I’m using Redux helper function &lt;em&gt;createStore&lt;/em&gt;: it takes my &lt;em&gt;reducer&lt;/em&gt; and returns me state (read &lt;a href=&quot;http://redux.js.org/docs/api/createStore.html&quot;&gt;Redux documentation&lt;/a&gt; for more about this function and Redux API). Second, I need to pass my &lt;em&gt;store&lt;/em&gt; to my &lt;strong&gt;Page&lt;/strong&gt; component. Here I’m using &lt;em&gt;Provider&lt;/em&gt; from React-Redux module (for more on this topic read &lt;a href=&quot;https://github.com/reactjs/react-redux/blob/master/docs/api.md&quot;&gt;React-Redux documentation&lt;/a&gt;). &lt;em&gt;Provider&lt;/em&gt; takes your app’s store and your components and passes store to those of components, that are ‘connected to store’ using &lt;em&gt;connect()&lt;/em&gt; function (from React-Redux lib).
Here is the code:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/88aeb733147022fc6592.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And the final part of this puzzle - &lt;strong&gt;Page&lt;/strong&gt; component, which is ‘connected to store’ and gets the store from &lt;em&gt;Provider&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here is it’s code:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/LenaBarinova/5a526c660ea536011c7a.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;As you probably have noticed, in my &lt;em&gt;app.jsx&lt;/em&gt; when creating &lt;strong&gt;Page&lt;/strong&gt; component I do not pass any &lt;code&gt;props&lt;/code&gt; to it, only to &lt;em&gt;Provider&lt;/em&gt;, but inside the &lt;strong&gt;Page&lt;/strong&gt; I refer to &lt;code&gt;props&lt;/code&gt;. This is enabled by React-Redux: from one side, in &lt;strong&gt;Page&lt;/strong&gt; I have connected &lt;strong&gt;Page&lt;/strong&gt; to store using connect() function, and in &lt;em&gt;app.jsx&lt;/em&gt; I use &lt;em&gt;Provider&lt;/em&gt;, which actually pass store to my &lt;strong&gt;Page&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The trickiest part for me was the usage of &lt;em&gt;connect()&lt;/em&gt;, it does both: makes your component subscribed to store updates and handles dispatching for provided function.&lt;/p&gt;

&lt;h2 id=&quot;literature&quot;&gt;Literature&lt;/h2&gt;

&lt;p&gt;While exploring Redux and making my first Redux app work - I’ve read many blog posts, watched video tutorials and browsed many github repositories. Here is the list of resources, that I’ve found most useful:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://egghead.io/series/getting-started-with-redux&quot;&gt;Video training on Redux by the author&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.sitepoint.com/how-to-build-a-todo-app-using-react-redux-and-immutable-js/&quot;&gt;How to build a Todo app using React, Redux and Immutable.js&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://redux.js.org/&quot;&gt;Redux documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/reactjs/react-redux/tree/master/docs&quot;&gt;React-Redux documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.jchapron.com/2015/08/14/getting-started-with-redux/&quot;&gt;Getting started with Redux&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://blog.krawaller.se/posts/a-react-redux-example-app/&quot;&gt;A React-Redux example app&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My earlier posts in React series:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.bebetterdeveloper.com/coding/getting-started-react-flux.html&quot;&gt;Creating multilingual website w/ React &amp;amp; Flux&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.bebetterdeveloper.com/coding/getting-started-react-mocha.html&quot;&gt;Getting started w/ React &amp;amp; Mocha&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.bebetterdeveloper.com/coding/shallow-rendering-react-mocha.html&quot;&gt;Testing React components using Shallow Rendering&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.bebetterdeveloper.com/coding/es6-react-babel.html&quot;&gt;ES2015 + React using Gulp&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Fri, 26 Feb 2016 00:00:00 -0800</pubDate>
        <link>http://www.bebetterdeveloper.com/coding/getting-started-react-redux.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/coding/getting-started-react-redux.html</guid>
        
        <category>JavaScript</category>
        
        <category>React</category>
        
        <category>Gulp</category>
        
        <category>ES2015</category>
        
        <category>ES6</category>
        
        <category>Redux</category>
        
        <category>multilingual</category>
        
        
        <category>Coding</category>
        
      </item>
    
      <item>
        <title>Serverless system architecture using AWS, React and Node.js</title>
        <description>&lt;p&gt;This post is about my most recent freetime-killer “DIY project” - &lt;a href=&quot;http://price-tracker-website.s3-website-us-west-2.amazonaws.com/&quot;&gt;Price tracker&lt;/a&gt;, which has a beautiful serverless architecture that I want to share with all of your.&lt;/p&gt;

&lt;p&gt;The story behind this app: I want to buy some items (such as skiing goggles, or new luggage set, or shoes), I want to get them for the best price and these items are not urgent for me so I can wait for a price drop. But I don’t want to hunt for sales and constantly check online stores for price decrease. I’ve got better things to do :)&lt;/p&gt;

&lt;p&gt;So I’ve developed a system, which tracks price changes for my desirable products in online stores (which I trust to buy from) and notifies me over the email when price drops.&lt;/p&gt;

&lt;h2 id=&quot;architecture&quot;&gt;Architecture&lt;/h2&gt;

&lt;p&gt;There are three major parts of the system:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/architecture/serverless-system-architecture-using-aws.html#website&quot;&gt;Website&lt;/a&gt; - to view the products and recent prices in different shops&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/architecture/serverless-system-architecture-using-aws.html#watcher&quot;&gt;Watcher&lt;/a&gt; - a job scheduled to scrape online shops and track price changes every couple of hours&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/architecture/serverless-system-architecture-using-aws.html#notifier&quot;&gt;Notifier&lt;/a&gt; - a job to watch changes to the price and notify me over the email when the price drops&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In addition to these parts - there is a database to hold all my products, shops and price changes.&lt;/p&gt;

&lt;h2 id=&quot;technologies&quot;&gt;Technologies&lt;/h2&gt;

&lt;p&gt;In autumn 2015 I won a $100 AWS credit as a HackersRank prize in an algorithms coding contest and I decided to make a good use of it. So the platform of a choice to build this system is AWS. In addition to that, lately I am focused on everything JavaScript, thus the language of a choice is JavaScript: both for a frontend - React.js and backend - Node.js.&lt;/p&gt;

&lt;p&gt;Here is the architecture:
&lt;img src=&quot;/img/post_img/pt-architecture.png&quot; alt=&quot;Architecture&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;implementation&quot;&gt;Implementation&lt;/h2&gt;

&lt;p&gt;The sources are in Github at &lt;a href=&quot;https://github.com/LenaBarinova/PriceTracker&quot;&gt;LenaBarinova/PriceTracker&lt;/a&gt;. 
Now shortly about how I implemented each of my three major system components.&lt;/p&gt;

&lt;h4 id=&quot;website&quot;&gt;Website&lt;/h4&gt;

&lt;p&gt;My Website - is a quite simple Single Page Application done in React using Flux pattern to handle UI changes. Website gets information about the products via a REST API implemented through AWS API Gateway, which calls AWS Lamdba function (written in Node.js) to actually get data from database. I use DynamoDB as a database, storing all of my products as JSON objects. UI is built using Bootstrap and some more advanced css styles to create flipping cards effects for products.&lt;/p&gt;

&lt;p&gt;Here’s how it looks:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/pt-ui.gif&quot; alt=&quot;Price Tracker UI&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Tests for UI are written in Mocha. Website is hosted in Amazon S3. For deployment I use Gulp and many Gulp plugins to transpose, convert from ES6, concatenate and minify my JavaScript files, run tests, bundle all together and upload to S3 bucket.&lt;/p&gt;

&lt;h3 id=&quot;watcher&quot;&gt;Watcher&lt;/h3&gt;

&lt;p&gt;Watcher is AWS Lambda function implemented in Node.js. It queries a database, gets all the products and runs scrapers for each product to retrieve a new price from online stores. Scraper function is specific for each store, it loads shop’s html and gets/parses price fields using jQuery selectors. If there is change in price - product is updated with a new price and persisted to DynamoDB.&lt;/p&gt;

&lt;p&gt;Watcher is scheduled to run every 4 hours using AWS CloudWatch Events.&lt;/p&gt;

&lt;h3 id=&quot;notifier&quot;&gt;Notifier&lt;/h3&gt;

&lt;p&gt;One more AWS Lambda function which listens to events in DynamoDB and if there is a price decrease - it sends email notification using AWS SNS service.&lt;/p&gt;

&lt;p&gt;All AWS Lambda functions are written in JavaScript and tested using Mocha. Deployment is as easy as running “gulp deploy” task - it runs tests, gets all dependant node modules, zips all files together and uploads my JavaScript code to AWS as Lamdba functions.&lt;/p&gt;

&lt;h2 id=&quot;pros-and-cons&quot;&gt;Pros and Cons&lt;/h2&gt;

&lt;p&gt;Though it’s nice not to have a server and run everything on different services, and Lambda is a kind of magic, there are some drawbacks to it. In my case it’s perfect for Watcher and Notifier, but not that good for a data retrieval from DynamoDB. The problem is that in order to run Lambda function there is a lot of work done in the background (container creation, environment configuration, etc.) before actually running my function, that takes some time. When the task is a background job - it’s totally fine, but with UI - it gives me undesirable lag before loading my products for the first time. I don’t worry about it now, because I’m the only user of this system and visit this site once a week or so, but if someday there is more users than me, I’ll definitely look into it and refactor this part of the system.&lt;/p&gt;

&lt;p&gt;Besides this small issue, everything else works as a charm. And with deployment automation - it is a real pleasure to work on this “project”.&lt;/p&gt;

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

&lt;p&gt;&lt;em&gt;P.S.: If you want to hear more about any part of this system - just leave a comment. And if you by any chance would like to try PriceTracker yourself - let me know, I’d be happy to set up it for you.&lt;/em&gt;&lt;/p&gt;
</description>
        <pubDate>Fri, 22 Jan 2016 00:00:00 -0800</pubDate>
        <link>http://www.bebetterdeveloper.com/coding/architecture/serverless-system-architecture-using-aws.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/coding/architecture/serverless-system-architecture-using-aws.html</guid>
        
        <category>JavaScript</category>
        
        <category>Node.js</category>
        
        <category>React</category>
        
        <category>Flux</category>
        
        <category>ES2015</category>
        
        <category>AWS</category>
        
        <category>Lambda</category>
        
        <category>DynamoDB</category>
        
        <category>API Gateway</category>
        
        <category>SNS</category>
        
        <category>S3</category>
        
        
        <category>Coding</category>
        
        <category>Architecture</category>
        
      </item>
    
      <item>
        <title>Testing React components using Shallow Rendering</title>
        <description>&lt;p&gt;This is a continuation of &lt;a href=&quot;http://www.bebetterdeveloper.com/coding/getting-started-react-mocha.html&quot;&gt;Getting started w/ React &amp;amp; Mocha&lt;/a&gt; blog post. Here I’ll shortly describe one more alternative how to test a React component.
&lt;img src=&quot;/img/post_img/react-logo.png&quot; alt=&quot;React logo&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;set-up&quot;&gt;Set-up&lt;/h2&gt;

&lt;p&gt;Code for this example can be found on &lt;a href=&quot;https://github.com/LenaBarinova/react-mocha-example/tree/shallow-rendering&quot;&gt;GitHub&lt;/a&gt;. After downloading the code, run&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;followed by&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm test
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or this gulp command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$gulp build
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are using Visual Studio Code - you can build it using &lt;code&gt;⇧⌘B&lt;/code&gt; (on Mac).&lt;/p&gt;

&lt;h2 id=&quot;testing&quot;&gt;Testing&lt;/h2&gt;

&lt;p&gt;So I have this react component, named &lt;em&gt;VeryFirstDiv&lt;/em&gt;:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/04649597e425254ad5ae.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;In my previous post to test it I was creating fake DOM using JSDOM library and then rendering my testable component into this fake DOM (Read more about it &lt;a href=&quot;http://www.bebetterdeveloper.com/coding/getting-started-react-mocha.html#step-3-adding-first-mocha-test&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;This time I’ll only use &lt;a href=&quot;https://www.npmjs.com/package/react-addons-test-utils&quot;&gt;React TestUtils add-on&lt;/a&gt; and rather new feature of it - &lt;a href=&quot;https://facebook.github.io/react/docs/test-utils.html#shallow-rendering&quot;&gt;Shallow Rendering&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Shallow rendering is an experimental feature that lets you render a component “one level deep” and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In my test I create an instance of Shallow Renderer:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let renderer = ReactTestUtils.createRenderer();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, render my &lt;em&gt;VeryFirstDiv&lt;/em&gt; component using this &lt;em&gt;renderer&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;renderer.render(&amp;lt;VeryFirstDiv /&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and get the output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;myDiv = renderer.getRenderOutput();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At this point you can check anything you fancy. I’ll for example want to test whether it is of a type ‘div’ and does it have a class named “veryFirstDiv”
Here’s my test:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/ee83bdfc9330b14ee6d2.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;For a comparison, I’ve created same test as in using DOM version:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/44b3a2a997865777a0b2.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Testing using Shallow Rendering is faster. You can even notice it on this small example: my test using fake DOM runs for &lt;em&gt;~50ms&lt;/em&gt;, while using Shallow Rendering test takes only &lt;em&gt;10ms&lt;/em&gt;. I believe the gain is even more significant on big projects. And as a pleasant side effect it doesn’t require additional libraries.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;
</description>
        <pubDate>Tue, 24 Nov 2015 00:00:00 -0800</pubDate>
        <link>http://www.bebetterdeveloper.com/coding/shallow-rendering-react-mocha.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/coding/shallow-rendering-react-mocha.html</guid>
        
        <category>JavaScript</category>
        
        <category>Mocha</category>
        
        <category>Gulp</category>
        
        <category>ES2015</category>
        
        <category>React</category>
        
        <category>Shallow rendering</category>
        
        
        <category>Coding</category>
        
      </item>
    
      <item>
        <title>Creating multilingual website w/ React &amp; Flux</title>
        <description>&lt;p&gt;My post on &lt;a href=&quot;http://www.bebetterdeveloper.com/coding/getting-started-react-mocha.html&quot;&gt;Getting started w/ React &amp;amp; Mocha&lt;/a&gt; just made it up to the Top 1 of all my blog posts, reaching 1000 uniqueue views, so I decided to continue these series.
&lt;img src=&quot;/img/post_img/rfe-1.png&quot; alt=&quot;React and Flux logo&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;my-target&quot;&gt;My target&lt;/h2&gt;

&lt;p&gt;This time I want to create a simple multilingual website. I need to have:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;source structure and process suitable for mid-sized project&lt;/li&gt;
  &lt;li&gt;fast update of UI on language change (desirably without page refresh)&lt;/li&gt;
  &lt;li&gt;easy testable solution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ll use ES6 where it is suitable, I automate dev process using Gulp.&lt;/p&gt;

&lt;h2 id=&quot;result&quot;&gt;Result&lt;/h2&gt;

&lt;p&gt;This is how the final result looks like:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/out.gif&quot; alt=&quot;Switch language&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Working code in &lt;a href=&quot;https://github.com/LenaBarinova/react-flux-example&quot;&gt;GitHub&lt;/a&gt;.
To check the website, follow this &lt;a href=&quot;http://Lenabarinova.github.io/react-flux-example/&quot;&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;tldr&quot;&gt;tl;dr&lt;/h2&gt;

&lt;p&gt;Just download the code and run&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are using Visual Studio Code - you can build it using &lt;code&gt;⇧⌘B&lt;/code&gt; (on Mac), otherwise use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$gulp build
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To run a server locally I use &lt;a href=&quot;https://www.npmjs.com/package/http-server&quot;&gt;http-server&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$http-server -a localhost -p 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For those, who are in a hurry to get started with Flux you can skip all preparation steps and go straight to &lt;a href=&quot;/coding/getting-started-react-flux.html#step-4-flux&quot;&gt;Using Flux&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;my-actions&quot;&gt;My actions&lt;/h2&gt;

&lt;p&gt;To create this multilingual website I followed these steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-flux.html#step-1-simple-html&quot;&gt;Created simple HTML&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-flux.html#step-2-react-components&quot;&gt;Created React components with hardcoded content&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-flux.html#step-3-content-from-json&quot;&gt;Moved content to JSON&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-flux.html#step-4-flux&quot;&gt;Used Flux&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/coding/getting-started-react-flux.html#step-5-refactoring-and-tests&quot;&gt;Made some refactoring and add tests&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;step-1-simple-html&quot;&gt;Step 1. Simple HTML&lt;/h2&gt;

&lt;p&gt;So I started by creating my website as a simple HTML, using Bootstrap, to have the basic static page structure, which I could split to the React components later. That’s how my &lt;em&gt;index.html&lt;/em&gt; look like:&lt;/p&gt;
&lt;style type=&quot;text/css&quot;&gt;
  .gist {width:640px !important;}
  .gist-file
  .gist-data {max-height: 600px; max-width: 640px;}
&lt;/style&gt;

&lt;script src=&quot;https://gist.github.com/LenaBarinova/82dbb2570f81395bc2f6.js&quot;&gt;&lt;/script&gt;

&lt;h2 id=&quot;step-2-react-components&quot;&gt;Step 2. React components&lt;/h2&gt;

&lt;p&gt;First I installed React and ReactDOM.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install react react-dom --save
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then I prepared everything that is needed for jsx and ES2015 transformations:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install babel gulp browserify vinyl-source-stream babelify babel-preset-es2015 babel-preset-react --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For Babel to transform React files and ES2015 - created &lt;em&gt;.babelrc&lt;/em&gt; file with these settings:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/1303d2f6a01a35ff01e9.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Created gulp file with a build task. Here it is:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/7f6e5e6945a89a6f17a6.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Now moving to React. In my page I identify three components:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Menu&lt;/li&gt;
  &lt;li&gt;Home section&lt;/li&gt;
  &lt;li&gt;About section&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I created these three components, adding additional one to wrap these three. In this step I added everything in one file &lt;em&gt;app.jsx&lt;/em&gt; (will refactor it in further steps). So my &lt;em&gt;app.jsx&lt;/em&gt; file looks like this:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/7244d61616f3427a8673.js&quot;&gt;&lt;/script&gt;
Note: when using html in React don’t forget to rename class to className.&lt;/p&gt;

&lt;p&gt;Added a placeholder for my react components instead of all the html tags in my &lt;em&gt;index.html&lt;/em&gt; and added reference to react and react-dom libraries as well as to my app.js file, which is being produced for me after running&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$gulp build
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or &lt;code&gt;⇧⌘B&lt;/code&gt; (in VSCode on Mac).&lt;/p&gt;

&lt;p&gt;So my &lt;em&gt;index.html&lt;/em&gt; now is this:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/e0a7a419f161aa7d8362.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;browse files in GitHub at &lt;a href=&quot;https://github.com/LenaBarinova/react-flux-example/tree/b4f1d2557efaa593f4dd4558fae75ec3db884da5&quot;&gt;this point&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;step-3-content-from-json&quot;&gt;Step 3. Content from JSON&lt;/h2&gt;

&lt;p&gt;What I decided to do now is to move all the content text to JSON file and load the text I have created helper API. The main idea behind this is that later this loading from JSON could be replaced by calling real API to retrieve needed data.&lt;/p&gt;

&lt;p&gt;This is how my data &lt;em&gt;content.json&lt;/em&gt; file looks like:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/8e0a8f5e3a09b2794ec5.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;This is my helper API, which load JSON into array and then returns it filtered by language.
&lt;script src=&quot;https://gist.github.com/LenaBarinova/aa9ed4e4afcaaa8b80ff.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;So now I update my React components to use text from my API. I decided to call API method from my wrapper component and then pass down needed parts to child components via &lt;em&gt;props&lt;/em&gt;. This is my app.jsx:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/279de096b29d412f5b70.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;In addition I appended &lt;em&gt;gulpfile.js&lt;/em&gt; with new task to delete previously generated &lt;em&gt;app.js&lt;/em&gt; file. This is how gulpfile.js looks like now:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/be4c3e8dbe599fe06996.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;For this I needed to install del library:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install del --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;step-4-flux&quot;&gt;Step 4. Flux&lt;/h2&gt;

&lt;p&gt;So finally, all preparation tasks are done and we can move forward and add Flux.
First let’s start with installation.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install flux --save
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I follow the flow that is being recommended by Flux:
&lt;img src=&quot;https://facebook.github.io/flux/img/flux-simple-f8-diagram-with-client-action-1300w.png&quot; alt=&quot;Flux flow&quot; class=&quot;right&quot; /&gt;
Picture is taken from &lt;a href=&quot;https://facebook.github.io/flux/docs/overview.html#content&quot;&gt;Flux manual&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let’s map this diagram items with my code. So starting from left to right, I’ve got two actions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;initApp - I call this in my app entry point to load content using default language ‘en’ (I say it’s an action on the left, very first box in the diagram)&lt;/li&gt;
  &lt;li&gt;switchLanguage - I dispatch this action on click in language menu (I say it’s an action above the Store on the diagram)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My &lt;em&gt;actions.js&lt;/em&gt; file:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/cded3bd3a79e1466029b.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Then I have a dispatcher:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/8c7a5114b7f5341077b8.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;In my store I hold the content and register some callbacks with dispatcher to be invoked on specific actions. My &lt;em&gt;store.js&lt;/em&gt; file:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/f1c17fa8bad12f7bc5f7.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And last, but not least - view. So here I have two components that are related to this flow:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Menu component - invokes SWITCH_LANGUAGE action on menu item click and passes selected language (this covers View -&amp;gt; Action -&amp;gt; Dispatcher -&amp;gt; Store loop on the diagram)&lt;/li&gt;
  &lt;li&gt;Page component - listens to the store changes and when change is happening - gets updated content (this covers Upper Action -&amp;gt; Dispatcher -&amp;gt; Store -&amp;gt; View loop on the diagram).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is my updated Menu component:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/51022402974b0f06b70e.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And here it is my Page component:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/5cac4ff430e13622c42f.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-4-refactoring-and-tests&quot;&gt;Step 4. Refactoring and Tests&lt;/h2&gt;

&lt;p&gt;I won’t be giving any detailed instruction here, but what I did - is refactored my code:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Changed source file structure&lt;/li&gt;
  &lt;li&gt;Split react components to different files&lt;/li&gt;
  &lt;li&gt;Added minification step to the gulp task&lt;/li&gt;
  &lt;li&gt;Now working on tests, and I think, that they deserve a separate blog post.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The last version of this example may be found on &lt;a href=&quot;https://github.com/LenaBarinova/react-flux-example&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I just love how it works you can experience this smooth language change &lt;a href=&quot;http://lenabarinova.github.io/react-flux-example/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Fri, 20 Nov 2015 00:00:00 -0800</pubDate>
        <link>http://www.bebetterdeveloper.com/coding/getting-started-react-flux.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/coding/getting-started-react-flux.html</guid>
        
        <category>JavaScript</category>
        
        <category>React</category>
        
        <category>Gulp</category>
        
        <category>ES2015</category>
        
        <category>Flux</category>
        
        <category>multilingual</category>
        
        
        <category>Coding</category>
        
      </item>
    
      <item>
        <title>Hello world, Python</title>
        <description>&lt;p&gt;&lt;img src=&quot;/img/post_img/python-logo.png&quot; alt=&quot;Python&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Today I started exploring Python. For those who know me - Yes, Python! Bear with me - I’ve got my solid reasons for that (will share it one day).&lt;/p&gt;

&lt;p&gt;In this short blog post I just want to share how I setup my IDE - &lt;a href=&quot;https://code.visualstudio.com/&quot;&gt;Visual Studio Code&lt;/a&gt; on MacOS to handle my Python adventure.&lt;/p&gt;

&lt;p&gt;First, I’ve installed &lt;a href=&quot;https://www.python.org/downloads/&quot;&gt;Python&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then, created &lt;code&gt;app.py&lt;/code&gt; file with “hello world” code in it and setup VSCode task to run this script, as so:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;From a Command pallete (&lt;code&gt;⇧⌘P&lt;/code&gt;) hit “Configure Task Runner”.&lt;/li&gt;
  &lt;li&gt;In a &lt;code&gt;task.json&lt;/code&gt; file added this:&lt;/li&gt;
&lt;/ol&gt;

&lt;script src=&quot;https://gist.github.com/LenaBarinova/1afd44a8470a511bc5ad.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;You should change python command path (if not default as mine) as well as &lt;code&gt;args&lt;/code&gt; according to your setup, I just simply have my whole script in &lt;code&gt;app.py&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Finally, as this is the only task in my VSCode tasks list - it runs on &lt;code&gt;⇧⌘B&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Simple as that!&lt;/p&gt;
</description>
        <pubDate>Thu, 01 Oct 2015 00:00:00 -0700</pubDate>
        <link>http://www.bebetterdeveloper.com/coding/hello-world-python.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/coding/hello-world-python.html</guid>
        
        <category>Python</category>
        
        <category>VSCode</category>
        
        <category>Visual Studio Code</category>
        
        <category>IDE</category>
        
        <category>Mac OS</category>
        
        <category>dev environment</category>
        
        
        <category>Coding</category>
        
      </item>
    
      <item>
        <title>Sharing a blog post hosted on GitHub Pages</title>
        <description>&lt;p&gt;I am hosting all of my personal blogs on &lt;a href=&quot;https://pages.github.com/&quot;&gt;GitHub Pages&lt;/a&gt; now (here are some reasons why: &lt;a href=&quot;http://bebetterleader.com/coding/why-did-i-go-static.html&quot;&gt;‘Static HTML is the new black’&lt;/a&gt;). With it comes a challenge when I try to share new posts on social media.&lt;/p&gt;

&lt;p&gt;I want to have a nice share dialog with description and image of my blog post. Like so:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/fbog-share-dialog.png&quot; alt=&quot;Share dialog&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In order to archive this, I had to add some code to my Jekyll templates and implement some workarounds with urls and domain configuration. That’s how I did it.&lt;/p&gt;

&lt;p&gt;First, I added meta tag generation to a head part of my post html:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/c63a40854bcd0569a9a6.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;I get all the open graph meta tags being generated as FB and other social media systems expect it to:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/LenaBarinova/1ef9ae8a8322e3397051.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;But once I try to share my blog post or scrape url with &lt;a href=&quot;https://developers.facebook.com/tools/debug/og/object/&quot;&gt;Open Graph Object Debugger&lt;/a&gt; I get &lt;code&gt;Error parsing input URL, no data was cached, or no data was scraped.&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/fbog-error.png&quot; alt=&quot;Error parsing input URL, no data was cached, or no data was scraped.&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;After spending some time searching for the reasons why this is happening, I found that, Github sometimes return a 302 (Redirect status code) instead of a 200 (OK) in order to avoid DDoS attacks. Same issue is described in this &lt;a href=&quot;http://www.rovrov.com/blog/2014/11/11/github-pages-302-redirect/&quot;&gt;blog post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To fix it, in addition to GitHub’s IPs I’ve added &lt;code&gt;www&lt;/code&gt; subdomain through &lt;code&gt;CNAME&lt;/code&gt; pointing to my GitHub address &lt;code&gt;lenabarinova.github.io&lt;/code&gt; in my domain configuration.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/fbog-domain-config.png&quot; alt=&quot;Domain configuration&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now I use url with &lt;code&gt;www&lt;/code&gt; prefix when I want to share my blog post on social media and it works just fine - I get desirable share dialog:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/fbog-fixed.png&quot; alt=&quot;Scraper works&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;With this workaround I can share my posts using nice summary and image in a share dialog.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;update-10172015&quot;&gt;Update 10/17/2015&lt;/h4&gt;

&lt;p&gt;By the way in my &lt;code&gt;gh-pages&lt;/code&gt; branch on a GitHub I have &lt;code&gt;CNAME&lt;/code&gt; configured using subdomain + domain, like this: &lt;code&gt;www.bebetterdeveloper.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/fblog-cname.png&quot; alt=&quot;CNAME&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 17 Aug 2015 00:00:00 -0700</pubDate>
        <link>http://www.bebetterdeveloper.com/deployment/sharing-a-blog-post-hosted-on-github-pages.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/deployment/sharing-a-blog-post-hosted-on-github-pages.html</guid>
        
        <category>gh-pages</category>
        
        <category>Jekyll</category>
        
        <category>open graph meta tags</category>
        
        <category>Error parsing input URL</category>
        
        <category>fb debugger</category>
        
        <category>social sharing</category>
        
        
        <category>Deployment</category>
        
      </item>
    
      <item>
        <title>Getting started w/ React &amp; Mocha</title>
        <description>&lt;p&gt;&lt;em&gt;11/23/2015: I updated this blog post as well as the code to support the newest version of React, ReactDOM, ReactTestUtils, Mocha, JSDOM, Babel. The changed are really minor, but may be frustraiting for some. For those, who are interested in how should this code be written using older version of these libs (React 0.13.3, Mocha 2.0.1, JSDOM 3.1.2, Mocha-JSDOM 1.0.0, Babel 5.1.13) please clone this &lt;a href=&quot;https://github.com/LenaBarinova/react-mocha-example/tree/9b71d468a1b7af5b6366be71a3e0dee9b45e3f37&quot;&gt;commit from GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It took me half day to setup [almost] empty environment for React app development with testing done using Mocha.
I decided to share my findings here so it would remain.
&lt;img src=&quot;/img/post_img/vfd-react-mocha.png&quot; alt=&quot;React and Mocha logo&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;my-target&quot;&gt;My target&lt;/h2&gt;
&lt;p&gt;I wanted to create an environment for further React development. The things that I needed to have:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;source structure, that would be suitable for medium-sized project&lt;/li&gt;
  &lt;li&gt;build scripts, that would do all needed transformations, run tests, show the output of my app&lt;/li&gt;
  &lt;li&gt;Mocha as a test framework of choice&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For build scripts I prefer to use gulp.&lt;/p&gt;

&lt;h2 id=&quot;tldr&quot;&gt;tl;dr&lt;/h2&gt;
&lt;p&gt;You can find working code &lt;a href=&quot;https://github.com/LenaBarinova/react-mocha-example&quot;&gt;here&lt;/a&gt;. Just download it and run&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;followed by&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm test
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are using Visual Studio Code - you can build it using &lt;code&gt;⇧⌘B&lt;/code&gt; (on Mac).&lt;/p&gt;

&lt;h2 id=&quot;how-i-did-it&quot;&gt;How I did it&lt;/h2&gt;
&lt;p&gt;I started with these three stages:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Prepare the environment&lt;/li&gt;
  &lt;li&gt;Create first React component&lt;/li&gt;
  &lt;li&gt;Add Mocha tests&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;step-1-prepare-the-environment&quot;&gt;Step 1. Prepare the environment&lt;/h2&gt;
&lt;p&gt;First install &lt;a href=&quot;https://nodejs.org/&quot;&gt;Node.js&lt;/a&gt; (if you don’t have it yet).
In your project directory (&lt;em&gt;react-mocha-example&lt;/em&gt; in my case) through Terminal do:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and follow the instruction. Then run this command to install libraries to setup the working environment:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install react react-dom babel babel-preset-es2015 babel-preset-react gulp gulp-load-plugins browserify babelify babel-core vinyl-source-stream gulp-open --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now your &lt;em&gt;package.json&lt;/em&gt; should look like this:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/31ce415a9beac855c75e.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Add &lt;em&gt;.babelrc&lt;/em&gt; file to your project root with these settings:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/23f8af665b0a8b15c855.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Create &lt;em&gt;gulpfile.js&lt;/em&gt; to configure build steps:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/143d0b90f977e60184fa.js&quot;&gt;&lt;/script&gt;
I’m using &lt;a href=&quot;https://www.npmjs.com/package/gulp-load-plugins&quot;&gt;gulp-load-plugins&lt;/a&gt; for easy and fast use of different gulp plugins. It enables me to access all the plugins I have listed in package.json file without ‘require’ each of them separately.&lt;/p&gt;

&lt;p&gt;So now everything is ready to start really writing a code. Create &lt;em&gt;index.html&lt;/em&gt; file:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/65ee857afca288768cb6.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Run build command: either using &lt;code&gt;⇧⌘B&lt;/code&gt; (from Visual Studio Code on Mac) or command line:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$gulp build
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And you should get Chrome opened with very first version of our app.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/vfd-1.png&quot; alt=&quot;First step result&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-2-create-first-react-component&quot;&gt;Step 2. Create first React component&lt;/h2&gt;
&lt;p&gt;Create a &lt;em&gt;components&lt;/em&gt; directory and &lt;em&gt;component.jsx&lt;/em&gt; file in it:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/8a04f9f62934b4773b5d.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Add this componenet to DOM (I did it in &lt;em&gt;app.jsx&lt;/em&gt; file in my project root directroy &lt;em&gt;react-mocha-example&lt;/em&gt;)
&lt;script src=&quot;https://gist.github.com/LenaBarinova/2d3096930e0f739ab7e5.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Run build task and we can see our React component in a browser.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/vfd-2.png&quot; alt=&quot;Second step result&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-3-adding-first-mocha-test&quot;&gt;Step 3. Adding first Mocha test&lt;/h2&gt;
&lt;p&gt;This was the trickiest step for me. I found a couple of good articles on the topic. These are my favourite:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/jesstelford/react-isomorphic-boilerplate&quot;&gt;Isomorphic Server &amp;amp; Browser Side Rendering with React (in 3 parts)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.hammerlab.org/2015/02/14/testing-react-web-apps-with-mocha/&quot;&gt;Testing React Web Apps with Mocha (in 2 parts)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.asbjornenge.com/wwc/testing_react_components.html&quot;&gt;Testing React Components&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ok, let’s begin.
First, we need to install &lt;a href=&quot;http://mochajs.org/&quot;&gt;Mocha&lt;/a&gt;. To do it, run npm install command with –save-dev, this way your &lt;em&gt;package.json&lt;/em&gt; file will be kept updated with all the dependances you use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install mocha --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Create test directory and a first &lt;em&gt;empty-test.js&lt;/em&gt;, which just asserts true all the time.
&lt;script src=&quot;https://gist.github.com/LenaBarinova/a5a34db9c6a9cf8ccb38.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;You can try whether it works just running Mocha from command line from you project root folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$mocha
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should get this output:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/vfd-3.png&quot; alt=&quot;Third step result&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now let’s add test running step in our gulp configuration.
For this I used &lt;a href=&quot;https://www.npmjs.com/package/gulp-mocha&quot;&gt;gulp-mocha&lt;/a&gt; - a gulp plugin wrapper around Mocha.&lt;/p&gt;

&lt;p&gt;Now add a new ‘test’ step in &lt;em&gt;gulpfile.js&lt;/em&gt; to run mocha:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/7d65aec1091f2ded3063.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Before moving on to testing our React component, we need to take care of a couple of things.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt; - DOM mocking. You can find very good explanation about how to mock DOM in this &lt;a href=&quot;http://www.asbjornenge.com/wwc/testing_react_components.html&quot;&gt;blog post&lt;/a&gt;.
To follow this example we need to install &lt;a href=&quot;https://github.com/tmpvar/jsdom&quot;&gt;jsdom&lt;/a&gt; and &lt;a href=&quot;https://github.com/rstacruz/mocha-jsdom&quot;&gt;mocha-jsdom&lt;/a&gt;.
I’ve created &lt;em&gt;dom-mock.js&lt;/em&gt; file (it’s a bit different from what is given in the article, I adopted it to be compatible with the newest JSDOM version).
&lt;script src=&quot;https://gist.github.com/LenaBarinova/fab84f93dae04ca4123a.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second&lt;/strong&gt; - install &lt;a href=&quot;https://www.npmjs.com/package/react-addons-test-utils&quot;&gt;React TestUtils add-on&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install react-addons-test-utils --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And finally - the test! I’ve created &lt;em&gt;component-test.js&lt;/em&gt; file in test directory:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/c4fd4c4cdad19b28fe0f.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Run build. Hurray! The test is passing:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/post_img/vfd-4.png&quot; alt=&quot;Fourth step result&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can also configure your &lt;em&gt;package.json&lt;/em&gt; - to run tests from command line using npm. For this add test section in your &lt;em&gt;package.json&lt;/em&gt; like this:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/085b9e9c727f166df806.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Now you can run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm test
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That’s it! You can download the latest code for this example from &lt;a href=&quot;https://github.com/LenaBarinova/react-mocha-example&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;
</description>
        <pubDate>Thu, 16 Jul 2015 00:00:00 -0700</pubDate>
        <link>http://www.bebetterdeveloper.com/coding/getting-started-react-mocha.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/coding/getting-started-react-mocha.html</guid>
        
        <category>JavaScript</category>
        
        <category>Mocha</category>
        
        <category>Gulp</category>
        
        <category>ES2015</category>
        
        <category>React</category>
        
        
        <category>Coding</category>
        
      </item>
    
      <item>
        <title>ES2015 + React using Gulp</title>
        <description>&lt;p&gt;&lt;em&gt;1/4/2016: I have updated this blog post by adding steps to install needed libraries and added command list for easy and fast start.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Recently I started exploring new features of ECMAScript 2015 while using React. So here is how I set-up my dev environment with &lt;a href=&quot;http://gulpjs.com/&quot;&gt;gulp&lt;/a&gt;.
I have 2 simple steps  (running one after other in this sequence):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Transform XLS files: I precompile React JSX template files into plain JavaScript using gulp-react:
  &lt;script src=&quot;https://gist.github.com/LenaBarinova/b7b58c270e3e560056b3.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Transform ES2015 files: I take JavaScript files, produced after first transformation and turn them into ES5 files using gulp-babel:
  &lt;script src=&quot;https://gist.github.com/LenaBarinova/7245b2c53e06eb8772ab.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before running these tasks be sure you have all necessary packages installed on your machine:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$npm install gulp gulp-react gulp-babel gulp-open
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here it all gathered and chained together:
&lt;script src=&quot;https://gist.github.com/LenaBarinova/95b2d933b6f75d6cc495.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;In addition to two transformation tasks I like to open output in browser, for this I use gulp-open.&lt;/p&gt;

&lt;p&gt;I use &lt;a href=&quot;https://code.visualstudio.com/&quot;&gt;Visual Studio Code&lt;/a&gt; as my IDE, so I have set this gulp task named “build” as a build task in VS Code settings.
&lt;img src=&quot;/img/post_img/vs-code-settings.png&quot; alt=&quot;Blog infrastructure&quot; class=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And now once I change code and want to check it - I just press ⇧⌘B (on Mac) and the result is in front of me.&lt;/p&gt;

&lt;p&gt;To try it working on your machine:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone git@github.com:LenaBarinova/react-es6-gulp-example.git
cd react-es6-gulp-example
npm install gulp gulp-react gulp-babel gulp-open
gulp build
&lt;/code&gt;&lt;/pre&gt;
</description>
        <pubDate>Tue, 14 Jul 2015 00:00:00 -0700</pubDate>
        <link>http://www.bebetterdeveloper.com/coding/es6-react-babel.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/coding/es6-react-babel.html</guid>
        
        <category>JavaScript</category>
        
        <category>Gulp</category>
        
        <category>ES2015</category>
        
        <category>React</category>
        
        <category>dev environment</category>
        
        
        <category>Coding</category>
        
      </item>
    
      <item>
        <title>Sorting: Heap Sort</title>
        <description>&lt;p&gt;I have already &lt;a href=&quot;http://www.bebetterdeveloper.com/data-structure-max-priority-queue/&quot;&gt;demonstrated&lt;/a&gt; one of the binary heap usage scenarios – priority queue. Today I want to show another binary heap usage example – heap sort. The algorithm consist of two steps:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;on the given set of numbers ensure max-heap invariant (&lt;em&gt;the value of each node is not bigger than value of its parent with biggest element at the root&lt;/em&gt;)&lt;/li&gt;
  &lt;li&gt;while heap is not empty remove first (max) item from the max-heap&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first part is almost identical to the way we &lt;a href=&quot;https://github.com/sergejusb/algorithms/blob/master/data-structures/binaryHeap.js#L20&quot;&gt;ensured&lt;/a&gt; binary heap invariant during delete operation. In order to make second step space efficient, we need do following things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;swap first (max) item with the last one in the max-heap&lt;/li&gt;
  &lt;li&gt;decrease size of the max-heap by 1&lt;/li&gt;
  &lt;li&gt;ensure max-heap invariant starting from the first item&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s sort items [3 4 1 3 5 1 2]. As always I’ll use colors to specify &lt;span style=&quot;color: #99cc00;&quot;&gt;parent&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;child&lt;/span&gt; or items to be &lt;span style=&quot;color: #ff0000;&quot;&gt;swapped&lt;/span&gt;:&lt;/p&gt;

&lt;p&gt;Step #1&lt;/p&gt;

&lt;p&gt;The binary tree constructed from the given items does not conform max-heap invariant:&lt;/p&gt;

&lt;pre&gt;     3
   4   1
  3 5 1 2&lt;/pre&gt;

&lt;p&gt;To ensure max-heap invariant for the given set we need to take each node in the tree (except leafs) and recursively ensure it is bigger than any of the child nodes. As a reminder, our binary heap stores items starting from index 1, so we need to temporarily add null item at the beginning: [null 3 4 1 3 5 1 2]&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 3 4 &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; &lt;/td&gt;
      &lt;td&gt;3 5 &lt;span style=&quot;color: #0000ff;&quot;&gt;1 2&lt;/span&gt;]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 3 4 &lt;span style=&quot;color: #99cc00;&quot;&gt;2&lt;/span&gt; &lt;/td&gt;
      &lt;td&gt;3 5 &lt;span style=&quot;color: #0000ff;&quot;&gt;1 1&lt;/span&gt;]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 3 &lt;span style=&quot;color: #99cc00;&quot;&gt;4&lt;/span&gt; 2&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;3 5&lt;/span&gt; 1 1]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 3 &lt;span style=&quot;color: #99cc00;&quot;&gt;5&lt;/span&gt; 2&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;3 4&lt;/span&gt; 1 1]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;5 2&lt;/span&gt; &lt;/td&gt;
      &lt;td&gt;3 4 1 1]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;3 2&lt;/span&gt; &lt;/td&gt;
      &lt;td&gt;3 4 1 1]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 5 &lt;span style=&quot;color: #99cc00;&quot;&gt;3&lt;/span&gt; 2&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;3 4&lt;/span&gt; 1 1]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 5 &lt;span style=&quot;color: #99cc00;&quot;&gt;4&lt;/span&gt; 2&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;3 3&lt;/span&gt; 1 1]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;At this point we have binary max-heap:&lt;/p&gt;

&lt;pre&gt;     5
   4   2
  3 3 1 1&lt;/pre&gt;
&lt;p&gt;Step #2&lt;/p&gt;

&lt;p&gt;Swap first (max) item with the last one and ensure max-heap invariant:&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;5&lt;/span&gt; 4 2 3 3 1 &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt; 4 2 3 3 1&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;5&lt;/span&gt;]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;4 2&lt;/span&gt; 3 3 1&lt;/td&gt;
      &lt;td&gt;5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;1 2&lt;/span&gt; 3 3 1&lt;/td&gt;
      &lt;td&gt;5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 4 &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; 2 &lt;span style=&quot;color: #0000ff;&quot;&gt;3 3&lt;/span&gt; 1&lt;/td&gt;
      &lt;td&gt;5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 4 &lt;span style=&quot;color: #99cc00;&quot;&gt;3&lt;/span&gt; 2 &lt;span style=&quot;color: #0000ff;&quot;&gt;3 1&lt;/span&gt; 1&lt;/td&gt;
      &lt;td&gt;5]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;4&lt;/span&gt; 3 2 3 1 &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt; 3 2 3 1&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;4&lt;/span&gt; 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;3 2&lt;/span&gt; 3 1&lt;/td&gt;
      &lt;td&gt;4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;1 2&lt;/span&gt; 3 1&lt;/td&gt;
      &lt;td&gt;4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 3 &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; 2 &lt;span style=&quot;color: #0000ff;&quot;&gt;3 1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 3 &lt;span style=&quot;color: #99cc00;&quot;&gt;3&lt;/span&gt; 2 &lt;span style=&quot;color: #0000ff;&quot;&gt;1 1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;4 5]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;3&lt;/span&gt; 3 2 1 &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt; 3 2 1&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;3&lt;/span&gt; 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;3 2&lt;/span&gt; 1&lt;/td&gt;
      &lt;td&gt;3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;1 2&lt;/span&gt; 1&lt;/td&gt;
      &lt;td&gt;3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null 3 &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; 2 &lt;span style=&quot;color: #0000ff;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;3&lt;/span&gt; 1 2 &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt; 1 2&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;3&lt;/span&gt; 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;1 2&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;1 1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;2&lt;/span&gt; 1 &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt; 1&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;2&lt;/span&gt; 3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #99cc00;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;2 3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;2 3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt; 2 3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;[null &lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;1 2 3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;[null&lt;/td&gt;
      &lt;td&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;1&lt;/span&gt; 1 2 3 3 4 5]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Finally we remove leading null item and we have sorted items! Implementation (&lt;a href=&quot;https://github.com/sergejusb/algorithms/blob/master/sorting/sort_tests.js&quot;&gt;tests&lt;/a&gt;) of the heap sort is heavily based on the binary heap implementation:&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;http://gist-it.appspot.com/https://github.com/sergejusb/algorithms/blob/master/sorting/heapSort.js?
footer=minimal&quot;&gt;&lt;/script&gt;

&lt;p&gt;&lt;strong&gt;Complexity&lt;/strong&gt;: O(n·logn)&lt;/p&gt;
</description>
        <pubDate>Mon, 24 Feb 2014 00:00:26 -0800</pubDate>
        <link>http://www.bebetterdeveloper.com/algorithms/sorting/sorting-heap-sort.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/algorithms/sorting/sorting-heap-sort.html</guid>
        
        <category>Binary Heap</category>
        
        <category>Heap Sort</category>
        
        
        <category>Algorithms</category>
        
        <category>Sorting</category>
        
      </item>
    
      <item>
        <title>Data Structure: Max Priority Queue</title>
        <description>&lt;p&gt;Today I will implement another important abstract data type – &lt;a href=&quot;http://en.wikipedia.org/wiki/Priority_queue&quot;&gt;priority queue&lt;/a&gt;. Priority queues are used:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;in heap sort&lt;/li&gt;
  &lt;li&gt;to track top N elements in a very long sequence&lt;/li&gt;
  &lt;li&gt;to merge K ordered sequences and produce single ordered sequence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Usually priority queues have following functions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;insert() – to add new item with a priority&lt;/li&gt;
  &lt;li&gt;deleteMin() or deleteMax() – to remove an item with min/max priority&lt;/li&gt;
  &lt;li&gt;findMin() or findMax() – to get an item with min/max priority&lt;/li&gt;
  &lt;li&gt;length() – to get the number of items in the priority queue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a foundation I’ll use in the &lt;a href=&quot;http://www.bebetterdeveloper.com/data-structure-binary-heap/&quot;&gt;last post&lt;/a&gt; described efficient data structure – binary heap (in fact, max-heap). Overall, priority queue can be seen as a generalization of &lt;a href=&quot;http://www.bebetterdeveloper.com/data-structure-stack-array/&quot;&gt;stack&lt;/a&gt; and &lt;a href=&quot;http://www.bebetterdeveloper.com/data-structure-queue/&quot;&gt;queue&lt;/a&gt; data structures. Stack can be implemented as a max priority queue where priority of each inserted element is monotonically increasing and queue – where priority of each inserted element is monotonically decreasing.&lt;/p&gt;

&lt;p&gt;The implementation of max priority queue (&lt;a href=&quot;https://github.com/sergejusb/algorithms/blob/master/data-structures/maxPriorityQueue_tests.js&quot;&gt;tests&lt;/a&gt;) is almost identical to the max-heap implementation:&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;http://gist-it.appspot.com/https://github.com/sergejusb/algorithms/blob/master/data-structures/maxPriorityQueue.js?
footer=minimal&quot;&gt;&lt;/script&gt;

&lt;p&gt;&lt;strong&gt;Complexity&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;insert - O(logn)&lt;/li&gt;
  &lt;li&gt;deleteMin/deleteMax - O(logn)&lt;/li&gt;
  &lt;li&gt;findMin/findMax - O(1)&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sat, 15 Feb 2014 00:00:00 -0800</pubDate>
        <link>http://www.bebetterdeveloper.com/data%20structure/data-structure-max-priority-queue.html</link>
        <guid isPermaLink="true">http://www.bebetterdeveloper.com/data%20structure/data-structure-max-priority-queue.html</guid>
        
        <category>Binary Heap</category>
        
        <category>Priority Queue</category>
        
        
        <category>Data Structure</category>
        
      </item>
    
  </channel>
</rss>
